feat: not complete serialization · javascriptdata/scikit.js@7fbea07 · GitHub
Skip to content

Commit 7fbea07

Browse files
committed
feat: not complete serialization
1 parent fc7c97b commit 7fbea07

22 files changed

Lines changed: 489 additions & 140 deletions

‎src/cluster/KMeans.test.ts‎

Lines changed: 8 additions & 8 deletions

‎src/cluster/KMeans.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Scikit2D } from '../types'
22
import { convertToNumericTensor2D, sampleWithoutReplacement } from '../utils'
3-
import Serialize from '../serialize'
3+
import { Serialize } from '../simpleSerializer'
44
import { tf } from '../shared/globals'
55

66
/*

‎src/compose/ColumnTransformer.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { DataFrameInterface, Scikit1D, Scikit2D, Transformer } from '../types'
2-
import { isDataFrameInterface, isScikitLike2D } from '../typesUtils'
1+
import { DataFrameInterface, Scikit1D, Transformer } from '../types'
2+
import { isDataFrameInterface } from '../typesUtils'
3+
import { Serialize } from '../simpleSerializer'
34
import { tf } from '../shared/globals'
45
/*
56
Next steps:
@@ -64,7 +65,7 @@ export interface ColumnTransformerParams {
6465
]
6566
* ```
6667
*/
67-
export class ColumnTransformer {
68+
export class ColumnTransformer extends Serialize {
6869
transformers: TransformerTriple
6970
remainder: Transformer | 'drop' | 'passthrough'
7071

‎src/dummy/DummyClassifier.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DummyClassifier } from './DummyClassifier'
2-
2+
import { fromObject } from '../simpleSerializer'
33
describe('DummyClassifier', function () {
44
it('Use DummyClassifier on simple example (mostFrequent)', function () {
55
const clf = new DummyClassifier()
@@ -51,7 +51,7 @@ describe('DummyClassifier', function () {
5151

5252
expect(scaler.classes).toEqual([1, 2, 3])
5353
})
54-
it('should serialize DummyClassifier', function () {
54+
it('should serialize DummyClassifier', async function () {
5555
const clf = new DummyClassifier()
5656

5757
const X = [
@@ -70,10 +70,10 @@ describe('DummyClassifier', function () {
7070
}
7171

7272
clf.fit(X, y)
73-
const clfSave = clf.toJson() as string
74-
expect(expectedResult).toEqual(JSON.parse(clfSave))
73+
const clfSave = await clf.toObject()
74+
expect(expectedResult).toEqual(clfSave)
7575
})
76-
it('should load DummyClassifier', function () {
76+
it('should load DummyClassifier', async function () {
7777
const clf = new DummyClassifier()
7878

7979
const X = [
@@ -85,8 +85,8 @@ describe('DummyClassifier', function () {
8585
const y = [10, 20, 20, 30]
8686

8787
clf.fit(X, y)
88-
const clfSave = clf.toJson() as string
89-
const newClf = new DummyClassifier().fromJson(clfSave)
88+
const clfSave = await clf.toObject()
89+
const newClf = await fromObject(clfSave)
9090
expect(clf).toEqual(newClf)
9191
})
9292
})

‎src/dummy/DummyRegressor.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DummyRegressor } from './DummyRegressor'
2-
2+
import { toObject, fromObject } from '../simpleSerializer'
33
describe('DummyRegressor', function () {
44
it('Use DummyRegressor on simple example (mean)', function () {
55
const reg = new DummyRegressor()
@@ -55,7 +55,7 @@ describe('DummyRegressor', function () {
5555
reg.fit(X, y)
5656
expect(reg.predict(predictX).arraySync()).toEqual([10, 10, 10])
5757
})
58-
it('Should save DummyRegressor', function () {
58+
it('Should save DummyRegressor', async function () {
5959
const reg = new DummyRegressor({ strategy: 'constant', constant: 10 })
6060

6161
const X = [
@@ -73,10 +73,10 @@ describe('DummyRegressor', function () {
7373

7474
reg.fit(X, y)
7575

76-
expect(saveResult).toEqual(JSON.parse(reg.toJson() as string))
76+
expect(saveResult).toEqual(await toObject(reg))
7777
})
7878

79-
it('Should load serialized DummyRegressor', function () {
79+
it('Should load serialized DummyRegressor', async function () {
8080
const reg = new DummyRegressor({ strategy: 'constant', constant: 10 })
8181

8282
const X = [
@@ -92,8 +92,8 @@ describe('DummyRegressor', function () {
9292
]
9393

9494
reg.fit(X, y)
95-
const saveReg = reg.toJson() as string
96-
const newReg = new DummyRegressor().fromJson(saveReg)
95+
const saveReg = await toObject(reg)
96+
const newReg = await fromObject(saveReg)
9797

9898
expect(newReg.predict(predictX).arraySync()).toEqual([10, 10, 10])
9999
})

‎src/impute/SimpleImputer.test.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { tf } from '../shared/globals'
22
import { SimpleImputer } from './SimpleImputer'
3-
3+
import { toObject, fromObject } from '../simpleSerializer'
44
describe('SimpleImputer', function () {
55
it('Imputes with "constant" strategy 2D one column. In this strategy, we give the fill value', function () {
66
const imputer = new SimpleImputer({ strategy: 'constant', fillValue: 3 })
@@ -119,7 +119,7 @@ describe('SimpleImputer', function () {
119119
expect(returned.arraySync()).toEqual(expected)
120120
expect(imputer.transform([[NaN, NaN]]).arraySync()).toEqual([[4, 3]])
121121
})
122-
it('Should serialized Imputer', function () {
122+
it('Should serialized Imputer', async function () {
123123
const imputer = new SimpleImputer({ strategy: 'mostFrequent' })
124124

125125
const data = [
@@ -129,21 +129,21 @@ describe('SimpleImputer', function () {
129129
[4, 2],
130130
[6, NaN]
131131
]
132-
133132
const expected = {
134133
name: 'SimpleImputer',
135-
missingValues: null,
134+
missingValues: NaN,
135+
fillValue: undefined,
136136
strategy: 'mostFrequent',
137137
statistics: {
138-
type: 'Tensor',
138+
name: 'Tensor',
139139
value: [4, 3]
140140
}
141141
}
142142

143-
const returned = imputer.fitTransform(data)
144-
expect(JSON.parse(imputer.toJson() as string)).toEqual(expected)
143+
imputer.fitTransform(data)
144+
expect(await toObject(imputer)).toEqual(expected)
145145
})
146-
it('Should load serialized Imputer', function () {
146+
it('Should load serialized Imputer', async function () {
147147
const imputer = new SimpleImputer({ strategy: 'mostFrequent' })
148148

149149
const data = [
@@ -162,8 +162,9 @@ describe('SimpleImputer', function () {
162162
[6, 3]
163163
]
164164

165-
const returned = imputer.fitTransform(data)
166-
const newImputer = new SimpleImputer().fromJson(imputer.toJson() as string)
165+
imputer.fitTransform(data)
166+
const thing = await toObject(imputer)
167+
const newImputer = await fromObject(thing)
167168
const newReturned = newImputer.transform(data)
168169
expect(newReturned.arraySync()).toEqual(expected)
169170
expect(newImputer.transform([[NaN, NaN]]).arraySync()).toEqual([[4, 3]])

‎src/index.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Serialize from './serialize'
2+
13
/**
24
* @license
35
* Copyright 2021, JsData. All rights reserved.
@@ -83,3 +85,9 @@ export {
8385
DecisionTreeRegressor,
8486
DecisionTreeRegressorParams
8587
} from './tree/DecisionTree'
88+
89+
export { fromObject, Serialize } from './simpleSerializer'
90+
91+
export { ClassificationCriterion, RegressionCriterion } from './tree/Criterion'
92+
export { Splitter } from './tree/Splitter'
93+
export { DecisionTreeBase, DecisionTree } from './tree/DecisionTree'

‎src/linear_model/LinearRegression.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LinearRegression } from './LinearRegression'
22
import { tensorEqual } from '../utils'
33
import { tf } from '../shared/globals'
4-
4+
import { toObject, fromObject } from '../simpleSerializer'
55
function roughlyEqual(a: number, b: number, tol = 0.1) {
66
return Math.abs(a - b) < tol
77
}
@@ -148,8 +148,10 @@ describe('LinearRegression', function () {
148148
const lr = new LinearRegression({ fitIntercept: false })
149149
await lr.fit(mediumX, yPlusJitter)
150150

151-
const serialized = await lr.toJson()
152-
const newModel = new LinearRegression({}).fromJson(serialized)
151+
const serialized = await lr.toObject()
152+
console.log({ serialized })
153+
const newModel = await fromObject(serialized)
154+
console.log(newModel)
153155

154156
expect(tensorEqual(newModel.coef, tf.tensor1d([2.5, 1]), 0.1)).toBe(true)
155157
expect(roughlyEqual(newModel.intercept as number, 0)).toBe(true)

‎src/linear_model/LogisticRegression.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LogisticRegression } from './LogisticRegression'
22
import { tf } from '../shared/globals'
3-
3+
import { fromObject } from '../simpleSerializer'
44
describe('LogisticRegression', function () {
55
it('Works on arrays (small example)', async function () {
66
const lr = new LogisticRegression()
@@ -133,8 +133,8 @@ describe('LogisticRegression', function () {
133133
let logreg = new LogisticRegression({ penalty: 'l2' })
134134
await logreg.fit(X, y)
135135

136-
const serializeModel = await logreg.toJson()
137-
const newModel = logreg.fromJson(serializeModel)
136+
const serializeModel = await logreg.toObject()
137+
const newModel = await fromObject(serializeModel)
138138
const newModelResult = newModel.predict(Xtest)
139139

140140
expect(newModelResult.arraySync()).toEqual([0, 0, 0, 0, 0, 0, 2, 2, 2])

‎src/linear_model/SgdClassifier.ts‎

Lines changed: 9 additions & 0 deletions

0 commit comments

Comments
 (0)