Data and Science

Python is the language most data work and most science is done in. In Circuitry a table, a chart or a worked answer appears beside the code that made it, in a single file or in a notebook of cells.

Who this is for

A university student, a researcher, an analyst, or a school or college student doing statistics or a science project. You should already know some Python: variables, lists and calling functions.

What you need

  • Circuitry in a web browser, in the desktop app, or on an iPad or a phone.
  • Nothing to install for the first program. numpy, pandas, matplotlib and Pillow load by themselves when a file imports them.
  • A connection the first time a file uses a library, while it is fetched. See where each library runs below.

Your first program

Make a Python file ( → Code → Python) and type or paste this:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    "day": ["Mon", "Tue", "Wed", "Thu", "Fri"],
    "cups": [12, 19, 7, 15, 22],
    "cookies": [5, 8, 11, 9, 14],
})
df["total"] = df["cups"] + df["cookies"]

display(df)
print("Best day:", df.loc[df["total"].idxmax(), "day"])

df.plot.bar(x="day", y=["cups", "cookies"], rot=0)
plt.title("Sold this week")
plt.show()

Press Run in the footer. The first run takes longer than later ones, while pandas and matplotlib load.

What you should see

The preview opens beside the code. At the top is the table, with the total column the program added. Below it is a bar chart with two bars for each day. Under the preview is the printed line, Best day: Fri.

  • display(df) shows a DataFrame as a table. It works without an import, as it does in a notebook.
  • plt.show() shows the chart. Tables and charts appear one below the other, in the order the program shows them.
  • print() goes under the preview.

Three things to try

  1. Your own numbers. Change the lists, or add a "tea" column, and include it in the total and in the chart's y.
  2. A summary. Add display(df.describe()) after display(df) for the count, mean, smallest and largest of each column.
  3. A line chart. Change df.plot.bar( to df.plot.line(.

Exact answers: sympy

sympy does algebra and calculus in symbols and gives the expression back, not a rounded number. This is a plain Python file with no preview, so the answers print in the output panel under the code.

import sympy as sp

x = sp.symbols("x")
print(sp.factor(x**2 - 5*x + 6))
print(sp.diff(x**2 * sp.sin(x), x))
print(sp.integrate(1 / x, x))
print(sp.solve(x**2 - 2, x))
(x - 3)*(x - 2)
x**2*cos(x) + 2*x*sin(x)
log(x)
[-sqrt(2), sqrt(2)]

The lesson is Symbolic Maths with SymPy.

Measurements into answers: scipy and scikit-learn

scipy fits curves, solves equations and runs statistical tests. scikit-learn trains models. Both of these find the straight line through six measurements:

import numpy as np
from scipy.optimize import curve_fit
from sklearn.linear_model import LinearRegression

hours = np.array([1, 2, 3, 4, 5, 6])
marks = np.array([52, 58, 61, 70, 74, 81])

def line(x, a, b):
    return a * x + b

(a, b), _ = curve_fit(line, hours, marks)
print(f"scipy: marks = {a:.2f} * hours + {b:.2f}")

model = LinearRegression().fit(hours.reshape(-1, 1), marks)
print("scikit-learn, 8 hours:", round(model.predict([[8]])[0], 1))
scipy: marks = 5.77 * hours + 45.80
scikit-learn, 8 hours: 92.0

The lesson is SciPy: turning measurements into answers.

Where each library runs

Circuitry picks where Python runs for each file, from what the file imports. You do not choose.

LibraryIn a web browserIn the iPhone and iPad appIn the desktop app
numpy, matplotlib, PillowLoads on first importBuilt into the appNo install needed
pandasLoads on first importBuilt into the appNo install needed
sympyLoads on first importFetched the first time it is usedNeeds a pip line if it is not installed
scipy, scikit-learnLoads on first importNot built into the app: the file runs on the Python in the page insteadNeeds a pip line if it is not installed
  • In a web browser, a Python that runs inside the page does the work. It fetches each library the first time a file imports it, which needs a connection and takes a moment.
  • On iPhone and iPad, a file whose libraries are all built into the app runs on the app's own Python. A file that imports a library the app does not have runs on the Python in the page, as it would in a browser. An app from before pandas was built in does the same for pandas.
  • In the desktop app, Circuit runs the file. To add a library there, put a pip line at the top of the file: !pip install scipy scikit-learn. See Installing Python Packages.

A notebook instead of a file: CodeBook

A code file runs from the top every time. A CodeBook is a notebook: a document of cells that you run one at a time, where a variable made in one Python cell is still there in the next. It suits work you build up in steps and want to explain as you go.

  • Make one from the new-document menu: CodeBook.
  • A cell's last line is shown: a DataFrame as a table, a figure as a picture, a sympy expression as a formula.
  • from IPython.display import display, HTML, Markdown, Math works, and so does display() without the import.

Bringing Jupyter notebooks with you: File → Import… turns a .ipynb file into a CodeBook. Outputs are not imported; run the cells to make them again. See Coming from Jupyter and Import from Other Tools.

Where to go next