@@ -8,7 +8,7 @@ const utils = new Utils();
88export class MinMaxScaler {
99 /**
1010 * Fit minmax scaler on data, to obtain their min and max value
11- * @param {data } data [DataRame | Series | Array]
11+ * @param {data } data [DataFrame | Series | Array]
1212 * @returns Array
1313 */
1414 fit ( data ) {
@@ -77,15 +77,58 @@ export class MinMaxScaler {
7777 . arraySync ( ) ;
7878 return new DataFrame ( output_data ) ;
7979 } else {
80- throw Error ( "Value Error: Data type not supoorted" ) ;
80+ throw Error ( "Value Error: Data type not supported" ) ;
81+ }
82+ }
83+
84+ /**
85+ * Restore a transformed array to their original values,
86+ * using the min and max generated from the fitting on data
87+ * @param {Series|Array|DataFrame } data
88+ * @returns Series|DataFrame
89+ */
90+ inverse_transform ( data ) {
91+ if ( data instanceof Series ) {
92+ if ( data . dtypes . includes ( "string" ) ) {
93+ throw Error ( "Dtype Error: Cannot perform operation on string dtypes" ) ;
94+ }
95+ let tensor_data = tensor ( data . values ) ;
96+ let output_data = tensor_data
97+ . mul ( this . max . sub ( this . min ) )
98+ . add ( this . min )
99+ . arraySync ( ) ;
100+ return new Series ( output_data ) ;
101+ } else if ( Array . isArray ( data ) ) {
102+ let tensor_data = tensor ( data ) ;
103+ let output_data = tensor_data
104+ . mul ( this . max . sub ( this . min ) )
105+ . add ( this . min )
106+ . arraySync ( ) ;
107+ if ( utils . __is_1D_array ( data ) ) {
108+ return new Series ( output_data ) ;
109+ } else {
110+ return new DataFrame ( output_data ) ;
111+ }
112+ } else if ( data instanceof DataFrame ) {
113+ if ( data . dtypes . includes ( "string" ) ) {
114+ throw Error ( "Dtype Error: Cannot perform operation on string dtypes" ) ;
115+ }
116+ let tensor_data = tensor ( data . values ) ;
117+ let output_data = tensor_data
118+ . mul ( this . max . sub ( this . min ) )
119+ . add ( this . min )
120+ . arraySync ( ) ;
121+ return new DataFrame ( output_data ) ;
122+ } else {
123+ throw Error ( "Value Error: Data type not supported" ) ;
81124 }
82125 }
83126}
84127
85128export class StandardScaler {
86129 /**
87130 *
88- * @param {data } data [DataRame | Series | Array]
131+ * @param {data } data [DataFame | Series | Array]
89132 * @returns Array
90133 */
91134 fit ( data ) {
@@ -140,7 +183,41 @@ export class StandardScaler {
140183 let output_data = tensor_data . sub ( this . mean ) . div ( this . std ) . arraySync ( ) ;
141184 return new DataFrame ( output_data ) ;
142185 } else {
143- throw Error ( "Value Error: Data type not supoorted" ) ;
186+ throw Error ( "Value Error: Data type not supported" ) ;
187+ }
188+ }
189+
190+ /**
191+ * Restore a transformed array to their original values,
192+ * using the mean and std generated from the fitting on data
193+ * @param {Series|Array|DataFrame } data
194+ * @returns Series|DataFrame
195+ */
196+ inverse_transform ( data ) {
197+ if ( data instanceof Series ) {
198+ if ( data . dtypes . includes ( "string" ) ) {
199+ throw Error ( "Dtype Error: Cannot perform operation on string dtypes" ) ;
200+ }
201+ let tensor_data = tensor ( data . values ) ;
202+ let output_data = tensor_data . mul ( this . std ) . add ( this . mean ) . arraySync ( ) ;
203+ return new Series ( output_data ) ;
204+ } else if ( Array . isArray ( data ) ) {
205+ let tensor_data = tensor ( data ) ;
206+ let output_data = tensor_data . mul ( this . std ) . add ( this . mean ) . arraySync ( ) ;
207+ if ( utils . __is_1D_array ( data ) ) {
208+ return new Series ( output_data ) ;
209+ } else {
210+ return new DataFrame ( output_data ) ;
211+ }
212+ } else if ( data instanceof DataFrame ) {
213+ if ( data . dtypes . includes ( "string" ) ) {
214+ throw Error ( "Dtype Error: Cannot perform operation on string dtypes" ) ;
215+ }
216+ let tensor_data = tensor ( data . values ) ;
217+ let output_data = tensor_data . mul ( this . std ) . add ( this . mean ) . arraySync ( ) ;
218+ return new DataFrame ( output_data ) ;
219+ } else {
220+ throw Error ( "Value Error: Data type not supported" ) ;
144221 }
145222 }
146223}
@@ -237,7 +314,7 @@ export class StandardScaler {
237314
238315// /**
239316// * Fit robust scalar on data to obtain the first quantile and third quantile
240- // * @param {data } data [DataRame | Series | Array]
317+ // * @param {data } data [DataFrame | Series | Array]
241318// * @returns Array
242319// */
243320// fit(data){
0 commit comments