data
Reading data for machine learning: CSV tables, batching samples, MNIST and images, and synthetic data.
import data
This package readies data for training a model. LoadCsv builds a table that turns text columns into category numbers by itself, and split_xy divides it into a feature matrix and a label.
The Make... functions build synthetic data from a fixed seed, for trying algorithms out without needing a file. All of it works with the matrix and tensor packages.
Types
Table
struct Table| Name | Type | Description |
|---|---|---|
names | Vector | The column names. |
kinds | Vector | The kind of each column; one of the COL_ constants. |
cats | Vector | The levels of each categorical column. |
raw | Vector | The original text of the cells. |
cells | matrix.Matrix | The numeric value of the cells; a categorical column holds the level number. |
import data
import matrix
func main:
text := "age,city,score,ok\n30,tehran,1.5,1\n25,shiraz,2.5,0\n41,tehran,3.0,1\n"
mut t := data.LoadCsv(text, true)
println t.rows(), t.cols(), t.names, t.col_index("score"), t.col_index("x")
println t.kinds[0] == data.COL_INT, t.kinds[1] == data.COL_CAT, t.kinds[2] == data.COL_FLOAT, data.COL_STR
println t.cat_levels("city")
mut m := t.to_matrix()
println m.rows, m.cols, m.at(2, 1)
mut x := matrix.Matrix {}
mut y := matrix.Matrix {}
println t.split_xy("ok", x, y), x.cols, y.at(1, 0)
mut good := true
_ := data.LoadCsvFile("missing.csv", true, good)
println good
matrix.Free(m)
matrix.Free(x)
matrix.Free(y)
t.free()
endrows
func rows(): intThe example for this item is shown with the ones above: data-Table.salam
cols
func cols(): intThe example for this item is shown with the ones above: data-Table.salam
col_index
func col_index(name: str): intThe example for this item is shown with the ones above: data-Table.salam
cat_levels
func cat_levels(name: str): Vector<str>The example for this item is shown with the ones above: data-Table.salam
to_matrix
func to_matrix(): matrix.MatrixThe example for this item is shown with the ones above: data-Table.salam
split_xy
func split_xy(label: str, x &: matrix.Matrix, y &: matrix.Matrix): booly and the rest into x.The example for this item is shown with the ones above: data-Table.salam
free
func free()The example for this item is shown with the ones above: data-Table.salam
BatchIter
struct BatchIter| Name | Type | Description |
|---|---|---|
x | matrix.Matrix | The features. |
y | matrix.Matrix | The labels. |
batch | int = 32 | The batch size. |
shuffle | bool = true | Shuffle the order. |
seed | i64 = 0 | The shuffle seed. |
order | Vector | The order of the samples in this epoch. |
pos | int = 0 | The current position. |
import data
import matrix
import tensor
func main:
mut y := matrix.Matrix {}
x := data.MakeBlobs(10, 2, 3, 0.2, 1, y)
mut set := data.MatrixDataset { x = x, y = y }
println set.len()
mut it := set.batches(4, false, 0)
println it.batches_per_epoch()
mut batch_x := matrix.Matrix {}
mut batch_y := matrix.Matrix {}
until it.next(batch_x, batch_y):
println batch_x.rows, batch_x.cols, batch_y.rows
end
it.reset(1)
mut t := tensor.Tensor {}
mut labels := Vector {} as Vector<int>
println it.next_tensor(t, labels), labels.len()
println data.YLabels(y).len(), data.XTensor(x).shape
mut good := true
_ := data.ParseIdx(null, 0, good)
println good
_ := data.LoadIdx("missing.idx", good)
println good, data.LoadMnist("a", "b", batch_x, batch_y)
mut classes := Vector {} as Vector<str>
println data.LoadImageFolder("missing-folder", 8, 8, true, t, labels, classes)
it.free()
set.free()
endreset
func reset(epoch: i64)The example for this item is shown with the ones above: data-Batch.salam
next
func next(x_out &: matrix.Matrix, y_out &: matrix.Matrix): boolx_out and y_out; the last batch may be smaller. False when no batch is left.The example for this item is shown with the ones above: data-Batch.salam
next_tensor
func next_tensor(x_out &: tensor.Tensor, labels_out &: Vector<int>): boolThe example for this item is shown with the ones above: data-Batch.salam
batches_per_epoch
func batches_per_epoch(): intThe example for this item is shown with the ones above: data-Batch.salam
free
func free()The example for this item is shown with the ones above: data-Batch.salam
MatrixDataset
struct MatrixDataset| Name | Type | Description |
|---|---|---|
x | matrix.Matrix | The feature matrix, one row per sample. |
y | matrix.Matrix | The labels, one row per sample. |
The example for this item is shown with the ones above: data-Batch.salam
len
func len(): intThe example for this item is shown with the ones above: data-Batch.salam
batches
func batches(batch: int, shuffle: bool, seed: i64): BatchItershuffle is true the order is shuffled from seed.The example for this item is shown with the ones above: data-Batch.salam
free
func free()The example for this item is shown with the ones above: data-Batch.salam
Constants and variables
Column kinds COL_…
| Name | Value |
|---|---|
COL_FLOAT | 0 |
COL_INT | 1 |
COL_CAT | 2 |
COL_STR | 3 |
The example for this item is shown with the ones above: data-Table.salam
Functions
MakeBlobs
func MakeBlobs(n: int, centers: int, p: int, spread: f64, seed: i64, y &: matrix.Matrix): matrix.Matriximport data
import matrix
func count_ones(y: matrix.Matrix): int:
mut n := 0
repeat y.rows in i:
if y.at(i, 0) == 1.0:
n += 1
end
end
ret n
end
func main:
mut y := matrix.Matrix {}
blobs := data.MakeBlobs(90, 3, 2, 0.5, 42, y)
println blobs.rows, blobs.cols, y.rows
moons := data.MakeMoons(100, 0.1, 7, y)
println moons.rows, moons.cols, count_ones(y)
circles := data.MakeCircles(60, 0.05, 0.5, 7, y)
println circles.rows, count_ones(y)
mut coef := matrix.Matrix {}
reg := data.MakeRegression(50, 3, 0.1, 1, y, coef)
println reg.rows, reg.cols, coef.rows
cls := data.MakeClassification(40, 4, 2, 1.0, 3, y)
println cls.rows, cls.cols, count_ones(y) > 0
endMakeMoons
func MakeMoons(n: int, noise: f64, seed: i64, y &: matrix.Matrix): matrix.MatrixThe example for this item is shown with the ones above: data-Synth.salam
MakeCircles
func MakeCircles(n: int, noise: f64, factor: f64, seed: i64, y &: matrix.Matrix): matrix.Matrixfactor is the radius ratio of the inner circle.The example for this item is shown with the ones above: data-Synth.salam
MakeRegression
func MakeRegression(n: int, p: int, noise: f64, seed: i64, y &: matrix.Matrix, true_coef &: matrix.Matrix): matrix.Matrixtrue_coef.The example for this item is shown with the ones above: data-Synth.salam
MakeClassification
func MakeClassification(n: int, p: int, classes: int, sep: f64, seed: i64, y &: matrix.Matrix): matrix.Matrixsep sets how far apart the classes are.The example for this item is shown with the ones above: data-Synth.salam
LoadImageFolder
func LoadImageFolder(root: str, width: int, height: int, gray: bool, x &: tensor.Tensor, labels &: Vector<int>, classes &: Vector<str>): boolgray is true.The example for this item is shown with the ones above: data-Batch.salam
ParseIdx
func ParseIdx(p: void*, n: int, ok &: bool): matrix.MatrixThe example for this item is shown with the ones above: data-Batch.salam
LoadIdx
func LoadIdx(path: str, ok &: bool): matrix.MatrixThe example for this item is shown with the ones above: data-Batch.salam
LoadMnist
func LoadMnist(images_path: str, labels_path: str, x &: matrix.Matrix, y &: matrix.Matrix): boolThe example for this item is shown with the ones above: data-Batch.salam
LoadCsv
func LoadCsv(text: str, header: bool): TableThe example for this item is shown with the ones above: data-Table.salam
LoadCsvFile
func LoadCsvFile(path: str, header: bool, ok &: bool): TableThe example for this item is shown with the ones above: data-Table.salam
XTensor
func XTensor(x &: matrix.Matrix): tensor.TensorThe example for this item is shown with the ones above: data-Batch.salam
YLabels
func YLabels(y &: matrix.Matrix): Vector<int>The example for this item is shown with the ones above: data-Batch.salam