danfo.StandardScaler
Standardize features by removing the mean and scaling to unit variance.
class danfo.StandScaler
danfo.js provides the StandardScaler class for the standardization of DataFrame and Series. The standard score of a sample x
is calculated as:
z = (x - u) / s
where u
is the mean of the training samples or zero if with_mean=False
, and s
is the standard deviation of the training samples or one if with_std=False
.
The API is similar to sklearn's StandardScaler, and provides a fit and transform method.
Examples
Standardize Series Object
const dfd = require("danfojs-node")
let scaler = new dfd.StandardScaler()
let sf = new dfd.Series([100,1000,2000, 3000])
sf.print()
scaler.fit(sf)
let sf_enc = scaler.transform(sf)
sf_enc.print()
βββββ€βββββββ
β 0 β 100 β
βββββΌβββββββ’
β 1 β 1000 β
βββββΌβββββββ’
β 2 β 2000 β
βββββΌβββββββ’
β 3 β 3000 β
βββββ§βββββββ
βββββ€ββββββββββββββββββββββ
β 0 β -1.3135592937469482 β
βββββΌββββββββββββββββββββββ’
β 1 β -0.4839428961277008 β
βββββΌββββββββββββββββββββββ’
β 2 β 0.4378530979156494 β
βββββΌββββββββββββββββββββββ’
β 3 β 1.3596490621566772 β
βββββ§ββββββββββββββββββββββ
Standardize DataFrame Object
const dfd = require("danfojs-node")
let data = [[100, 1000, 2000, 3000],
[20, 30, 89, 12],
[1, 1, 1, 0]]
let df = new dfd.DataFrame(data, { columns: ['a', 'b', 'c', 'd'] })
df.print()
let scaler = new dfd.StandardScaler()
scaler.fit(df)
let df_enc = scaler.transform(df)
df_enc.print()
ββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ
β β a β b β c β d β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 0 β 100 β 1000 β 2000 β 3000 β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 1 β 20 β 30 β 89 β 12 β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 2 β 1 β 1 β 1 β 0 β
ββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ
ββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ
β β a β b β c β d β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 0 β 1.3909024000167β¦ β 1.4137537479400β¦ β 1.4131401777267β¦ β 1.4142049551010β¦ β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 1 β -0.473994612693β¦ β -0.675643563270β¦ β -0.658863127231β¦ β -0.702851355075β¦ β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 2 β -0.916907668113β¦ β -0.738110065460β¦ β -0.754277229309β¦ β -0.711353600025β¦ β
ββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ
See also MinMaxScaler
Last updated