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 |
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 |
ndarray
|
in file order, and |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
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 |
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 |
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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 |
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 |
required |
Returns:
| Type | Description |
|---|---|
LinearModel
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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()
Load the bundled housing dataset, ready to fit.
Returns:
| Type | Description |
|---|---|
ndarray
|
A tuple |
ndarray
|
|