2
$\begingroup$

Wanna apply a specific scaler, say StandardScaler, on a specific feature, keeping other features intact.

the dataset format is something like: [ [1, 0.2, 1000], [2, 0.1, 2400], [3, 0.9, 7620] ]

I need to transform only one column, the third in this example.

I don't want to use pandas.

$\endgroup$

2 Answers 2

1
$\begingroup$

Just pass one column to the scaler, and change the data inlace, something like:

x[i,:] = scaler.transform(x[i,:])

Once the scaler is fitted.

$\endgroup$
2
  • $\begingroup$ Thanks, It works only if x is numpy.array, not list. Btw, no problem, wrapping x in numpy.array(). $\endgroup$
    – Hosein
    Dec 30, 2018 at 3:57
  • $\begingroup$ If it's just a list, then x[i] should work ;) $\endgroup$ Dec 30, 2018 at 8:08
4
$\begingroup$

As mentioned, the easiest way is to apply the StandardScaler to only the subset of features that need to be scaled, and then concatenate the result with the remaining features.

Alternatively, scikit-learn also offers (a still experimental, i.e. subject to change) ColumnTransformer API. It works similar to a pipeline:

from sklearn.compose import ColumnTransformer 
from sklearn.preprocessing import  StandarScaler 
column_trans = ColumnTransformer(
    [('scaler', StandardScaler(),2],
    remainder='passthrough') 
column_trans.fit_transform(X)

The third entry of the tuple (here : 2) can be a single or a list of column indices (or column names when working with a pandas DataFrame).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.