Skip to content
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

Kelbow #93

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipcook/datacook",
"version": "0.1.2-beta.12",
"version": "0.1.3-beta.2",
"description": "A JavaScript library for feature engineering on datasets",
"main": "dist/index.js",
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/model/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export abstract class BaseEstimator {
}
}

export class BaseClustering extends BaseEstimator {
export abstract class BaseClustering extends BaseEstimator {
public nClusters: number;
constructor() {
super();
this.estimatorType = 'clustering';
Expand Down
3 changes: 3 additions & 0 deletions src/model/clustering/kmeans/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ export class KMeans extends BaseClustering {
* @returns Stringfied model parameters
*/
public async toJson(): Promise<string> {
if (!this.centroids) {
throw new TypeError('Please fit model at first');
}
const modelParams = {
name: 'KMeans',
nInit: this.nInit,
Expand Down
62 changes: 62 additions & 0 deletions src/model/clustering/kmeans/k-elbow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { dispose, RecursiveArray, Tensor } from '@tensorflow/tfjs-core';
import { KMeans } from '.';
import { checkArray } from '../../../utils/validation';

// export class kElbow
export interface InertiaItem {
inertia: number;
k: number;
model: KMeans;
}
export interface KElbowPrams {
k?: [ number, number ],
verbose?: boolean
}

/**
* K-elbow implements the “elbow” method to help users to select the optimal number of clusters
* by fitting the model with a range of values for K
* @param kmeans original kmeans model
* @param xData input data
* @param params paramters
* Option in parameters
* -----
* - `k`: [ start<number>, end<number> ], start for minimum number of clusters, end for maximum number of clusters,
* **default=[ 2, 10 ]**
* - `verbose`: boolean, verbosity, **default=false**
* @returns Array of inertia for each of clusters
*/
export const kElbow = async (estimitor: KMeans, xData: Tensor | RecursiveArray<number>, params: KElbowPrams = {}): Promise<InertiaItem[]> => {
const xTensor = checkArray(xData);
const { k = [ 2, 10 ], verbose = false } = params;
const [ start, end ] = k;
const elbowsData = [];
const {
tol,
nInit,
maxIterTimes,
init
} = estimitor;
const modelVerbosity = estimitor.verbose;
for (let i = start; i < end; i++) {
const curEstimator = new KMeans({ tol, nInit, maxIterTimes, init, verbose: modelVerbosity });
curEstimator.nClusters = i;
if (verbose) {
console.log('Start fitting for nCluster=', i);
}
await curEstimator.fit(xTensor);
const inertia = curEstimator.inertiaDense(xTensor, curEstimator.centroids);
if (verbose) {
console.log(`nCluster=${i} fitted, inertia=${inertia}`);
}
elbowsData.push({
inertia,
k: i,
model: curEstimator
});
}
if (!(xData instanceof Tensor)) {
dispose(xTensor);
}
return elbowsData;
};
Empty file.
1 change: 1 addition & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * as NaiveBayes from './naive-bayes';
export { LogisticRegression, LinearRegression } from './linear-model';
export { PCA } from './pca';
export { KMeans } from './clustering/kmeans';
export { kElbow } from './clustering/kmeans/k-elbow';
export { LinearRegressionAnalysis } from './stat/linear-regression-analysis';
export { stepwiseLinearRegression } from './stat/stepwise';
export { NonLinearRegression, NonLinearRegressionTrainParams } from './non-linear-model/NonLinearRegression';
Expand Down
37 changes: 34 additions & 3 deletions src/preprocess/standard-scaler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { divNoNan, RecursiveArray, sqrt, Tensor, sub, Rank, mul, add, tidy } from '@tensorflow/tfjs-core';
import { divNoNan, RecursiveArray, sqrt, Tensor, sub, Rank, mul, add, tidy, tensor } from '@tensorflow/tfjs-core';
import { getVariance, getMean } from '../stat';
import { checkArray } from '../utils/validation';
import { TransformerMixin } from './base';
Expand Down Expand Up @@ -56,7 +56,7 @@ export class StandardScaler extends TransformerMixin {
const xTensor = checkArray(X, 'float32', 2);
this.checkAndSetNFeatures(xTensor, true);
this.mean = getMean(xTensor);
this.standardVariance = sqrt(getVariance(xTensor));
this.standardVariance = tidy(() => sqrt(getVariance(xTensor)));
this.nFeatures = xTensor.shape[1];
this.nSamplesSeen = xTensor.shape[0];
}
Expand All @@ -72,7 +72,7 @@ export class StandardScaler extends TransformerMixin {
const xCentered = this.withMean ? sub(xTensor, this.mean) : xTensor;
return this.withStd ? divNoNan(xCentered, this.standardVariance) : xCentered;
});
}
}/* */
/**
* recover scaled transformed data to original scale
* @param X input data
Expand All @@ -86,4 +86,35 @@ export class StandardScaler extends TransformerMixin {
return this.withMean ? add(xScaleReturn, this.mean) : xScaleReturn;
});
}

/**
* Dump model parameters to json string.
* @returns Stringfied model parameters
*/
public async toJson(): Promise<string> {
const modelParams = {
name: 'StandardScaler',
withMean: this.withMean,
withStd: this.withStd,
mean: await this.mean.array(),
standardVariance: await this.standardVariance.array()
};
return JSON.stringify(modelParams);
}

/**
* Load model json string.
* @returns Stringfied model parameters
*/
public async fromJson(modelJson: string): Promise<void> {
const params = JSON.parse(modelJson);
if (params.name !== 'StandardScaler') {
throw new TypeError(`${params.name} is not StandardScaler`);
}
this.mean = params.mean ? tensor(params.mean) : null;
this.standardVariance = params.standardVariance ? tensor(params.standardVariance) : null;
this.withMean = params.withMean;
this.withStd = params.withStd;
if (this.mean) this.nFeature = this.mean.shape[0];
}
}
16 changes: 16 additions & 0 deletions test/node/model/kelbow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { KMeans } from '../../../src/model';
import { kElbow } from '../../../src/model/clustering/kmeans/k-elbow';
import * as tf from '@tensorflow/tfjs-core';

const clust1 = tf.add(tf.mul(tf.randomNormal([ 2000, 2 ]), tf.tensor([ 2, 2 ])), tf.tensor([ 5, 5 ]));
const clust2 = tf.add(tf.mul(tf.randomNormal([ 2000, 2 ]), tf.tensor([ 2, 2 ])), tf.tensor([ 10, 0 ]));
const clust3 = tf.add(tf.mul(tf.randomNormal([ 2000, 2 ]), tf.tensor([ 2, 2 ])), tf.tensor([ -10, 0 ]));
const clusData = tf.concat([ clust1, clust2, clust3 ]);

describe('KElbow', () => {
it('calculate elbow data', async () => {
const kmeans = new KMeans();
const elbowData = await kElbow(kmeans, clusData, { verbose: true });
console.log(elbowData);
});
});
12 changes: 12 additions & 0 deletions test/node/preprocess/standard-scaler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ describe('Standard Scaler', () => {
assert.isTrue(tensorEqual(means, tf.zeros([ 5 ]), 1e-3));
assert.isTrue(tensorEqual(variance, tf.ones([ 5 ]), 1e-3));
});
it('save and load model', async () => {
const scaler = new StandardScaler();
await scaler.fit(cases);
const modelJson = await scaler.toJson();
const scaler2 = new StandardScaler();
await scaler2.fromJson(modelJson);
const transformed = await scaler2.transform(cases);
const means = tf.mean(transformed, 0);
const variance = getVariance(transformed);
assert.isTrue(tensorEqual(means, tf.zeros([ 5 ]), 1e-3));
assert.isTrue(tensorEqual(variance, tf.ones([ 5 ]), 1e-3));
});
});