Skip to content

Commit 55a97ec

Browse files
committed
Cleaned up repo
1 parent 7c8f0b2 commit 55a97ec

36 files changed

+1094
-736
lines changed

.eslintrc.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["@typescript-eslint"],
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:@typescript-eslint/eslint-recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
210
"env": {
311
"browser": true,
412
"es6": true,
513
"mocha": true
614
},
7-
"extends": "eslint:recommended",
815
"globals": {
916
"Atomics": "readonly",
1017
"SharedArrayBuffer": "readonly"

.prettierrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"trailingComma": "es5",
2+
"trailingComma": "none",
33
"tabWidth": 2,
44
"semi": false,
55
"singleQuote": true,

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
"simple-statistics": "^7.7.0"
1717
},
1818
"scripts": {
19-
"test": "nyc mocha --require ts-node/register 'test/**/*.test.ts'",
20-
"test:clean": "yarn build:clean && yarn test",
19+
"test": "nyc mocha --require ts-node/register '**/*.test.ts'",
20+
"test:clean": "yarn build:clean && yarn test && yarn lint && yarn prettier",
2121
"dev": "nodemon",
2222
"build": "tsc",
2323
"build:clean": "rimraf ./build && tsc",
24-
"lint": "eslint ./src",
24+
"prettier": "prettier --check './src'",
25+
"lint": "eslint ./src/**/*.ts",
2526
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
2627
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov",
2728
"patch": "npm version patch"
@@ -58,12 +59,15 @@
5859
"@types/mocha": "^8.2.2",
5960
"@types/node": "^16.9.6",
6061
"@types/table": "^6.3.2",
62+
"@typescript-eslint/eslint-plugin": "^5.1.0",
63+
"@typescript-eslint/parser": "^5.1.0",
6164
"chai": "^4.2.0",
6265
"coveralls": "^3.1.0",
63-
"eslint": "^7.1.0",
66+
"eslint": "^8.0.1",
6467
"mocha": "^7.2.0",
6568
"nodemon": "^2.0.7",
6669
"nyc": "^15.1.0",
70+
"prettier": "^2.4.1",
6771
"rimraf": "^3.0.2",
6872
"ts-node": "^10.0.0",
6973
"typescript": "^4.4.2",

src/estimators/elastic.net.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ export class ElasticNet extends SGD {
3434
modelCompileArgs: {
3535
optimizer: train.adam(0.1),
3636
loss: losses.meanSquaredError,
37-
metrics: ['mse'],
37+
metrics: ['mse']
3838
},
3939
modelFitArgs: {
4040
batchSize: 32,
4141
epochs: 1000,
4242
verbose: 0,
43-
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 50 })],
43+
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 50 })]
4444
},
4545
denseLayerArgs: {
4646
units: 1,
4747
kernelRegularizer: regularizers.l1l2({
4848
l1: params.alpha * params.l1Ratio,
49-
l2: 0.5 * params.alpha * (1 - params.l1Ratio),
49+
l2: 0.5 * params.alpha * (1 - params.l1Ratio)
5050
}),
51-
useBias: Boolean(params.fitIntercept),
52-
},
51+
useBias: Boolean(params.fitIntercept)
52+
}
5353
})
5454
}
5555
}

src/estimators/lasso.regression.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ export class LassoRegression extends SGD {
3333
modelCompileArgs: {
3434
optimizer: train.adam(0.1),
3535
loss: losses.meanSquaredError,
36-
metrics: ['mse'],
36+
metrics: ['mse']
3737
},
3838
modelFitArgs: {
3939
batchSize: 32,
4040
epochs: 1000,
4141
verbose: 0,
42-
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 50 })],
42+
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 50 })]
4343
},
4444
denseLayerArgs: {
4545
units: 1,
4646
kernelRegularizer: regularizers.l1({ l1: params.alpha }),
47-
useBias: Boolean(params.fitIntercept),
48-
},
47+
useBias: Boolean(params.fitIntercept)
48+
}
4949
})
5050
}
5151
}

test/estimators/linear.model.test.ts renamed to src/estimators/linear.model.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as tf from '@tensorflow/tfjs-core'
33
import { assert } from 'chai'
44
import { LinearRegression } from '../../dist'
55
import 'mocha'
6-
import { tensorEqual } from '../utils.test'
7-
import { tensor, Tensor1D, tensor2d, Tensor2D } from '@tensorflow/tfjs-core'
6+
import { tensorEqual } from '../utils'
7+
import { Tensor1D, Tensor2D } from '@tensorflow/tfjs-core'
88

9-
function roughlyEqual(a: number, b: number, tol: number = 0.1) {
9+
function roughlyEqual(a: number, b: number, tol = 0.1) {
1010
return Math.abs(a - b) < tol
1111
}
1212

@@ -25,7 +25,7 @@ describe('LinearRegression', function () {
2525
[[1], [2]],
2626
[
2727
[2, 1],
28-
[4, 2],
28+
[4, 2]
2929
]
3030
)
3131

src/estimators/linear.model.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ export class LinearRegression extends SGD {
4747
modelCompileArgs: {
4848
optimizer: train.adam(0.1),
4949
loss: losses.meanSquaredError,
50-
metrics: ['mse'],
50+
metrics: ['mse']
5151
},
5252
modelFitArgs: {
5353
batchSize: 32,
5454
epochs: 1000,
5555
verbose: 0,
56-
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 30 })],
56+
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 30 })]
5757
},
5858
denseLayerArgs: {
5959
units: 1,
60-
useBias: Boolean(params.fitIntercept),
61-
},
60+
useBias: Boolean(params.fitIntercept)
61+
}
6262
})
6363
}
6464
}

src/estimators/logistic.regression.ts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// /**
2+
// * @license
3+
// * Copyright 2021, JsData. All rights reserved.
4+
// *
5+
// * This source code is licensed under the MIT license found in the
6+
// * LICENSE file in the root directory of this source tree.
7+
8+
// * Unless required by applicable law or agreed to in writing, software
9+
// * distributed under the License is distributed on an "AS IS" BASIS,
10+
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// * See the License for the specific language governing permissions and
12+
// * limitations under the License.
13+
// * ==========================================================================
14+
// */
15+
16+
// import '@tensorflow/tfjs-node'
17+
// import { losses, train } from '@tensorflow/tfjs-core'
18+
// import { callbacks } from '@tensorflow/tfjs-layers'
19+
// import { SGD } from './sgd.linear'
20+
// import {
21+
// initializers,
22+
// regularizers,
23+
// Tensor1D,
24+
// Tensor2D,
25+
// } from '@tensorflow/tfjs-node'
26+
// import { tensor1dConv, tensor2dConv } from 'utils'
27+
28+
// // First pass at a LinearRegression implementation using gradient descent
29+
// // Trying to mimic the API of scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html
30+
31+
// export interface LogisticRegressionParams {
32+
// penalty: 'l1' | 'l2' | 'none'
33+
// C: number
34+
// fitIntercept: boolean
35+
// }
36+
37+
// export class LogisticRegression extends SGD {
38+
// constructor(params: LogisticRegressionParams) {
39+
// // Assume Binary classification
40+
// // If we call fit, and it isn't binary then update args
41+
// super({
42+
// modelCompileArgs: {
43+
// optimizer: train.adam(0.1),
44+
// loss: losses.sigmoidCrossEntropy,
45+
// metrics: ['accuracy'],
46+
// },
47+
// modelFitArgs: {
48+
// batchSize: 32,
49+
// epochs: 1000,
50+
// verbose: 0,
51+
// callbacks: [
52+
// callbacks.earlyStopping({ monitor: 'loss', patience: 50 }),
53+
// ],
54+
// },
55+
// denseLayerArgs: {
56+
// units: 1,
57+
// useBias: Boolean(params.fitIntercept),
58+
// kernelInitializer: initializers.zeros(),
59+
// biasInitializer: initializers.zeros(),
60+
// kernelRegularizer:
61+
// params.penalty === 'l2'
62+
// ? regularizers.l2({ l2: params.C })
63+
// : params.penalty === 'l1'
64+
// ? regularizers.l1({ l1: params.C })
65+
// : undefined,
66+
// },
67+
// })
68+
// }
69+
70+
// // Placeholder for eventual code that will determine if we need to
71+
// // do one-hot-encoding
72+
// async fit(
73+
// X: Tensor2D | number[][],
74+
// y: Tensor1D | number[]
75+
// ): Promise<LogisticRegression> {
76+
// let XTwoD = tensor2dConv(X)
77+
// let yOneD = tensor1dConv(y)
78+
// if (this.model.layers.length === 0) {
79+
// this.initializeModel(XTwoD.shape[1])
80+
// }
81+
// await this.model.fit(XTwoD, yOneD, { ...this.modelFitArgs })
82+
// return this
83+
// }
84+
// }

src/estimators/ridge.regression.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ export class RidgeRegression extends SGD {
3434
modelCompileArgs: {
3535
optimizer: train.adam(0.1),
3636
loss: losses.meanSquaredError,
37-
metrics: ['mse'],
37+
metrics: ['mse']
3838
},
3939
modelFitArgs: {
4040
batchSize: 32,
4141
epochs: 1000,
4242
verbose: 0,
43-
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 50 })],
43+
callbacks: [callbacks.earlyStopping({ monitor: 'mse', patience: 50 })]
4444
},
4545
denseLayerArgs: {
4646
units: 1,
4747
kernelRegularizer: regularizers.l2({ l2: params.alpha }),
48-
useBias: Boolean(params.fitIntercept),
49-
},
48+
useBias: Boolean(params.fitIntercept)
49+
}
5050
})
5151
}
5252
}

src/estimators/sgd.linear.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ import {
1919
Tensor1D,
2020
Tensor2D,
2121
tensor1d,
22-
tensor2d,
22+
tensor2d
2323
} from '@tensorflow/tfjs-core'
2424
import {
2525
layers,
2626
sequential,
2727
Sequential,
2828
ModelFitArgs,
29-
ModelCompileArgs,
29+
ModelCompileArgs
3030
} from '@tensorflow/tfjs-layers'
3131
import { DenseLayerArgs } from '@tensorflow/tfjs-layers/dist/layers/core'
3232
import {
3333
convertToNumericTensor1D_2D,
34-
convertToNumericTensor2D,
34+
convertToNumericTensor2D
3535
} from '../utils'
3636
import { Scikit2D, ScikitVecOrMatrix } from '../types'
37+
import { PredictorMixin } from '../mixins'
3738
/**
3839
* SGD is a thin Wrapper around Tensorflow's model api with a single dense layer.
3940
* With this base class and different error functions / regularizers we can
@@ -71,12 +72,12 @@ export interface SGDParams {
7172
modelFitArgs: ModelFitArgs
7273

7374
/**
74-
* The arguments for a single dense layer in tensorflow. This also defaults to
75+
* The arguments for a single dense layer in tensorflow. This also defaults to
7576
* different settings based on the regressor / classifier. An example dense layer
7677
* might look like.
7778
* const model = sequential()
7879
model.add(
79-
layers.dense({ inputShape: [100],
80+
layers.dense({ inputShape: [100],
8081
units: 1,
8182
useBias: true,
8283
})
@@ -85,13 +86,14 @@ export interface SGDParams {
8586
denseLayerArgs: DenseLayerArgs
8687
}
8788

88-
export class SGD {
89+
export class SGD extends PredictorMixin {
8990
model: Sequential
9091
modelFitArgs: ModelFitArgs
9192
modelCompileArgs: ModelCompileArgs
9293
denseLayerArgs: DenseLayerArgs
9394

9495
constructor(params: SGDParams) {
96+
super()
9597
this.model = sequential()
9698
this.modelFitArgs = params.modelFitArgs
9799
this.modelCompileArgs = params.modelCompileArgs
@@ -187,7 +189,7 @@ export class SGD {
187189
let myIntercept = tensor1d([params.intercept_], 'float32')
188190
this.initializeModel(myCoef.shape, myIntercept.shape, [
189191
myCoef,
190-
myIntercept,
192+
myIntercept
191193
])
192194
return this
193195
}
@@ -203,7 +205,7 @@ export class SGD {
203205
*
204206
* lr = new LinearRegression()
205207
* lr.getParams()
206-
* // =>
208+
* // =>
207209
{
208210
modelCompileArgs: {
209211
optimizer: train.adam(0.1),
@@ -227,7 +229,7 @@ export class SGD {
227229
return {
228230
modelFitArgs: this.modelFitArgs,
229231
modelCompileArgs: this.modelCompileArgs,
230-
denseLayerArgs: this.denseLayerArgs,
232+
denseLayerArgs: this.denseLayerArgs
231233
}
232234
}
233235

@@ -303,7 +305,7 @@ export class SGD {
303305
* await lr.fit(X, [1,2,3]);
304306
* lr.coef_
305307
* // => tensor1d([[ 1.2, 3.3, 1.1, 0.2 ]])
306-
*
308+
*
307309
* await lr.fit(X, [ [1,2], [3,4], [5,6] ]);
308310
* lr.coef_
309311
* // => tensor2d([ [1.2, 3.3], [3.4, 5.6], [4.5, 6.7] ])

test/impute/simple.imputer.test.ts renamed to src/impute/simple.imputer.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assert } from 'chai'
22
import { SimpleImputer } from '../../dist'
3-
import { Series, DataFrame } from 'danfojs-node'
43

54
describe('SimpleImputer', function () {
65
it('Imputes with "constant" strategy. In this strategy, we give the fill value', function () {

0 commit comments

Comments
 (0)