Skip to content

API reference

Generated from the docstrings in the source code.

tinyml: load a CSV, split it, fit a linear model, and score it.

LinearModel

A fitted linear model: prediction = X @ coef + intercept.

Create one with fit_linear rather than directly.

__init__(coef, intercept)

Store fitted parameters.

Parameters:

Name Type Description Default
coef ndarray

One coefficient per feature column.

required
intercept float

The constant term.

required

predict(X)

Predict a target value for every row of X.

Parameters:

Name Type Description Default
X ndarray

2-D feature matrix with the same columns, in the same order, as the data the model was fitted on. For a single sample, pass one row: [[...]].

required

Returns:

Type Description
ndarray

A 1-D array with one prediction per row.

Raises:

Type Description
ValueError

If X is not 2-D, has a different number of columns than the model was fitted on, or is empty.

load_csv(path, target)

Load a numeric CSV file into features and a target.

Parameters:

Name Type Description Default
path str | Path

Path to a CSV file with a header row. Every column must be numeric.

required
target str

Name of the column to predict.

required

Returns:

Type Description
ndarray

A tuple (X, y): X is a 2-D array of every column except target,

ndarray

in file order, and y is a 1-D array of the target column.

Raises:

Type Description
KeyError

If target is not a column in the file.

train_test_split(X, y, test_size=0.2, seed=0)

Shuffle rows and split them into a training set and a test set.

Parameters:

Name Type Description Default
X ndarray

Feature matrix, one row per sample.

required
y ndarray

Target values, one per row of X, 1-D or a single column.

required
test_size float

Fraction of rows to hold out for testing, between 0 and 1. The test set always gets at least one row.

0.2
seed int

Random seed, so the same inputs always give the same split.

0

Returns:

Type Description
ndarray

A tuple (X_train, X_test, y_train, y_test), with X parts 2-D and

ndarray

y parts 1-D.

Raises:

Type Description
ValueError

If X is not 2-D, if y is not 1-D or a single column, if their sample counts differ or they are empty, if test_size is not strictly between 0 and 1, or if the split would leave no rows for training.

evaluate(y, y_pred)

Score predictions with root mean squared error (RMSE).

Lower is better; 0 means every prediction is exact. The result is in the same units as y.

Parameters:

Name Type Description Default
y ndarray

True target values, 1-D or a single column.

required
y_pred ndarray

Predicted values, one per entry of y, 1-D or a single column.

required

Returns:

Type Description
float

The RMSE as a float.

Raises:

Type Description
ValueError

If either input has any other shape, if they hold different numbers of values, or if they are empty.

fit_linear(X, y)

Fit ordinary least squares with an intercept.

Parameters:

Name Type Description Default
X ndarray

2-D feature matrix, one row per sample.

required
y ndarray

Target values, one per row of X, 1-D or a single column.

required

Returns:

Type Description
LinearModel

A LinearModel with .coef, .intercept and .predict().

Raises:

Type Description
ValueError

If X is not 2-D, if y is not 1-D or a single column, or if their sample counts differ or they are empty.

Sample data

Sample data that ships with tinyml, so examples run anywhere with no download.

housing_path()

Locate the bundled housing CSV.

The file has a header row and 40 houses with four numeric columns: size_sqft, bedrooms, age_years and price (sale price in dollars).

Returns:

Type Description
Path

The path to housing.csv inside the installed tinyml package.

housing()

Load the bundled housing dataset, ready to fit.

Returns:

Type Description
ndarray

A tuple (X, y): X has 40 rows and the columns size_sqft,

ndarray

bedrooms and age_years; y holds the 40 sale prices.