Home>
replace NaN with the median but this is numpy array

rep_features_train = imputer.transform (features_train)

change into Data Frame

rep_features_train_df = pd.DataFrame (rep_features_train,
columns = features_train.columns)
display (rep_features_train_df.head ())

When I execute the above, I get the following error.


ImportError Traceback (most recent call last)
in
1 # fill median values ​​to NaN with Imputer
---->2 from sklearn.preprocessing import Imputer
3 imputer = Imputer (strategy ='median')
4 imputer.fit (features_scaling [numerical])
Five

ImportError: cannot import name'Imputer' from'sklearn.preprocessing' (/Users/opt/anaconda3/lib/python3.8/site-packages/sklearn/preprocessing/init.py)

Can you tell me what's wrong and what to do?

  • Answer # 1

    About sklearn.preprocessing.Imputer
    I think the above article will be helpful.

    It seems that Imputer is not supported when the version of scikit-learn which is the environment of the above article is 0.22.2.

  • Answer # 2

    I think that you are referring to some material, but the version of sklearn of the environment assumed in that material is probably old.

    I found that 0.19.2 has Imputer in sklearn.preprocessing, but later versions don't.

    The module versions must match for the same source to work. The same thing may happen with other modules, so it's safe to get information from the document/author who is visa.

    If the module etc. cannot be replaced from the current environment, the function with the same function has moved to another place, so there is no choice but to correct it by referring to the API document.

  • Answer # 3

    ImportError: cannot import name'Imputer' from'sklearn.preprocessingIs very important, so if you can decipher this content yourself, you will be able to solve the problem smoothly.


    This time, I will write a sample of the decoding result for reference.

    hereImputerThe one namedsklearn.preprocessingIt is written that it cannot be imported because it is not included in.

    sklearn.preprocessingI don't know because I haven't usedImputerIt seems that you need to check if the name is correct and if scikit_learn? Is installed correctly.