Skip to content

Different API for serialization #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ function getTypeName(val, bigObj) {
}

function generateProperties(jsonClass, bigObj) {
// console.log(jsonClass.children)
let interface = getInterfaceForClass(jsonClass, bigObj)
let allConstructorArgs = []
if (interface && interface.children) {
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@tensorflow/tfjs": "^3.16.0",
"@tensorflow/tfjs-node": "^3.16.0",
"base64-arraybuffer": "^1.0.2",
"lodash": "^4.17.21",
"mathjs": "^10.0.0",
"simple-statistics": "^7.7.0"
Expand Down
17 changes: 8 additions & 9 deletions src/cluster/KMeans.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { KMeans } from './KMeans'

import { fromJSON, KMeans } from '../index'
// Next steps: Improve on kmeans cluster testing
describe('KMeans', () => {
const X = [
Expand Down Expand Up @@ -38,7 +37,7 @@ describe('KMeans', () => {
)
})

it('should save kmeans model', () => {
it('should save kmeans model', async () => {
const expectedResult = {
name: 'KMeans',
nClusters: 2,
Expand All @@ -48,7 +47,7 @@ describe('KMeans', () => {
randomState: 0,
nInit: 10,
clusterCenters: {
type: 'Tensor',
name: 'Tensor',
value: [
[2.5, 1],
[2.5, 4]
Expand All @@ -57,20 +56,20 @@ describe('KMeans', () => {
}
const kmean = new KMeans({ nClusters: 2, randomState: 0 })
kmean.fit(X)
const ksave = kmean.toJson() as string
const ksave = await kmean.toObject()

expect(expectedResult).toEqual(JSON.parse(ksave))
expect(expectedResult).toEqual(ksave)
})

it('should load serialized kmeans model', () => {
it('should load serialized kmeans model', async () => {
const centroids = [
[2.5, 1],
[2.5, 4]
]
const kmean = new KMeans({ nClusters: 2, randomState: 0 })
kmean.fit(X)
const ksave = kmean.toJson() as string
const ksaveModel = new KMeans().fromJson(ksave)
const ksave = await kmean.toJSON()
const ksaveModel = await fromJSON(ksave)
expect(centroids).toEqual(ksaveModel.clusterCenters.arraySync())
})

Expand Down
2 changes: 1 addition & 1 deletion src/cluster/KMeans.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Scikit2D } from '../types'
import { convertToNumericTensor2D, sampleWithoutReplacement } from '../utils'
import Serialize from '../serialize'
import { Serialize } from '../simpleSerializer'
import { tf } from '../shared/globals'

/*
Expand Down
31 changes: 28 additions & 3 deletions src/compose/ColumnTransformer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ColumnTransformer } from './ColumnTransformer'
import { MinMaxScaler } from '../preprocessing/MinMaxScaler'
import { SimpleImputer } from '../impute/SimpleImputer'
import {
fromJSON,
SimpleImputer,
MinMaxScaler,
ColumnTransformer
} from '../index'
import * as dfd from 'danfojs-node'

describe('ColumnTransformer', function () {
Expand Down Expand Up @@ -30,4 +33,26 @@ describe('ColumnTransformer', function () {

expect(result.arraySync()).toEqual(expected)
})
it('ColumnTransformer serialize/deserialize test', async function () {
const X = [
[2, 2], // [1, .5]
[2, 3], // [1, .75]
[0, NaN], // [0, 1]
[2, 0] // [.5, 0]
]
let newDf = new dfd.DataFrame(X)

const transformer = new ColumnTransformer({
transformers: [
['minmax', new MinMaxScaler(), [0]],
['simpleImpute', new SimpleImputer({ strategy: 'median' }), [1]]
]
})

transformer.fitTransform(newDf)
let obj = await transformer.toJSON()
let myResult = await fromJSON(obj)

expect(myResult.transformers.length).toEqual(2)
})
})
8 changes: 5 additions & 3 deletions src/compose/ColumnTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DataFrameInterface, Scikit1D, Scikit2D, Transformer } from '../types'
import { isDataFrameInterface, isScikitLike2D } from '../typesUtils'
import { DataFrameInterface, Scikit1D, Transformer } from '../types'
import { isDataFrameInterface } from '../typesUtils'
import { Serialize } from '../simpleSerializer'
import { tf } from '../shared/globals'
/*
Next steps:
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface ColumnTransformerParams {
]
* ```
*/
export class ColumnTransformer {
export class ColumnTransformer extends Serialize {
transformers: TransformerTriple
remainder: Transformer | 'drop' | 'passthrough'

Expand All @@ -75,6 +76,7 @@ export class ColumnTransformer {
transformers = [],
remainder = 'drop'
}: ColumnTransformerParams = {}) {
super()
this.transformers = transformers
this.remainder = remainder
}
Expand Down
15 changes: 7 additions & 8 deletions src/dummy/DummyClassifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DummyClassifier } from './DummyClassifier'

import { DummyClassifier, fromJSON } from '../index'
describe('DummyClassifier', function () {
it('Use DummyClassifier on simple example (mostFrequent)', function () {
const clf = new DummyClassifier()
Expand Down Expand Up @@ -51,7 +50,7 @@ describe('DummyClassifier', function () {

expect(scaler.classes).toEqual([1, 2, 3])
})
it('should serialize DummyClassifier', function () {
it('should serialize DummyClassifier', async function () {
const clf = new DummyClassifier()

const X = [
Expand All @@ -70,10 +69,10 @@ describe('DummyClassifier', function () {
}

clf.fit(X, y)
const clfSave = clf.toJson() as string
expect(expectedResult).toEqual(JSON.parse(clfSave))
const clfSave = await clf.toObject()
expect(expectedResult).toEqual(clfSave)
})
it('should load DummyClassifier', function () {
it('should load DummyClassifier', async function () {
const clf = new DummyClassifier()

const X = [
Expand All @@ -85,8 +84,8 @@ describe('DummyClassifier', function () {
const y = [10, 20, 20, 30]

clf.fit(X, y)
const clfSave = clf.toJson() as string
const newClf = new DummyClassifier().fromJson(clfSave)
const clfSave = await clf.toJSON()
const newClf = await fromJSON(clfSave)
expect(clf).toEqual(newClf)
})
})
15 changes: 8 additions & 7 deletions src/dummy/DummyRegressor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DummyRegressor } from './DummyRegressor'
import { DummyRegressor, fromJSON } from '../index'

describe('DummyRegressor', function () {
it('Use DummyRegressor on simple example (mean)', function () {
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('DummyRegressor', function () {
reg.fit(X, y)
expect(reg.predict(predictX).arraySync()).toEqual([10, 10, 10])
})
it('Should save DummyRegressor', function () {
it('Should save DummyRegressor', async function () {
const reg = new DummyRegressor({ strategy: 'constant', constant: 10 })

const X = [
Expand All @@ -68,15 +68,16 @@ describe('DummyRegressor', function () {
name: 'DummyRegressor',
EstimatorType: 'regressor',
strategy: 'constant',
constant: 10
constant: 10,
quantile: undefined
}

reg.fit(X, y)

expect(saveResult).toEqual(JSON.parse(reg.toJson() as string))
expect(saveResult).toEqual(await reg.toObject())
})

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

const X = [
Expand All @@ -92,8 +93,8 @@ describe('DummyRegressor', function () {
]

reg.fit(X, y)
const saveReg = reg.toJson() as string
const newReg = new DummyRegressor().fromJson(saveReg)
const saveReg = await reg.toJSON()
const newReg = await fromJSON(saveReg)

expect(newReg.predict(predictX).arraySync()).toEqual([10, 10, 10])
})
Expand Down
15 changes: 9 additions & 6 deletions src/ensemble/VotingClassifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { makeVotingClassifier, VotingClassifier } from './VotingClassifier'
import { DummyClassifier } from '../dummy/DummyClassifier'

import { LogisticRegression } from '../linear_model/LogisticRegression'
import {
makeVotingClassifier,
VotingClassifier,
DummyClassifier,
LogisticRegression,
fromJSON
} from '../index'

describe('VotingClassifier', function () {
it('Use VotingClassifier on simple example (voting = hard)', async function () {
Expand Down Expand Up @@ -118,8 +121,8 @@ describe('VotingClassifier', function () {

await voter.fit(X, y)

const savedModel = (await voter.toJson()) as string
const newModel = new VotingClassifier({}).fromJson(savedModel)
const savedModel = await voter.toJSON()
const newModel = await fromJSON(savedModel)

expect(newModel.predict(X).arraySync()).toEqual([1, 1, 1, 1, 1])
}, 30000)
Expand Down
10 changes: 0 additions & 10 deletions src/ensemble/VotingClassifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Scikit1D, Scikit2D } from '../types'
import { tf } from '../shared/globals'
import { ClassifierMixin } from '../mixins'
import { LabelEncoder } from '../preprocessing/LabelEncoder'
import { fromJson, toJson } from './serializeEnsemble'

/*
Next steps:
Expand Down Expand Up @@ -154,15 +153,6 @@ export class VotingClassifier extends ClassifierMixin {
): Promise<Array<tf.Tensor1D> | Array<tf.Tensor2D>> {
return (await this.fit(X, y)).transform(X)
}

public fromJson(model: string) {
return fromJson(this, model)
}

public async toJson(): Promise<string> {
const classJson = JSON.parse(super.toJson() as string)
return toJson(this, classJson)
}
}

export function makeVotingClassifier(...args: any[]) {
Expand Down
14 changes: 9 additions & 5 deletions src/ensemble/VotingRegressor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { makeVotingRegressor, VotingRegressor } from './VotingRegressor'
import { DummyRegressor } from '../dummy/DummyRegressor'
import { LinearRegression } from '../linear_model/LinearRegression'
import {
makeVotingRegressor,
VotingRegressor,
fromJSON,
DummyRegressor,
LinearRegression
} from '../index'

describe('VotingRegressor', function () {
it('Use VotingRegressor on simple example ', async function () {
Expand Down Expand Up @@ -51,8 +55,8 @@ describe('VotingRegressor', function () {

await voter.fit(X, y)

const savedModel = (await voter.toJson()) as string
const newModel = new VotingRegressor({}).fromJson(savedModel)
const savedModel = await voter.toJSON()
const newModel = await fromJSON(savedModel)
expect(newModel.score(X, y)).toEqual(voter.score(X, y))
}, 30000)
})
10 changes: 0 additions & 10 deletions src/ensemble/VotingRegressor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Scikit1D, Scikit2D } from '../types'
import { tf } from '../shared/globals'
import { RegressorMixin } from '../mixins'
import { fromJson, toJson } from './serializeEnsemble'
/*
Next steps:
0. Write validation code to check Estimator inputs
Expand Down Expand Up @@ -95,15 +94,6 @@ export class VotingRegressor extends RegressorMixin {
public async fitTransform(X: Scikit2D, y: Scikit1D) {
return (await this.fit(X, y)).transform(X)
}

public fromJson(model: string) {
return fromJson(this, model) as this
}

public async toJson(): Promise<string> {
const classJson = JSON.parse(super.toJson() as string)
return toJson(this, classJson)
}
}

/**
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy