My code isn't working that I copied word for word from the Machine Learning A-Z™: Hands-On Python & R In Data Science tutorial course. I am using Python 3.7, I have installed the scikit-learn package in my environment. It isn't working, I have tried looking for a package that has sklearn although it doesn't seem to find anything. It is giving me this error.
I am running my environment through Anaconda.
ImportError: cannot import name 'Imputer' from 'sklearn.preprocessing' (C:\Users\vygan\.conda\envs\env\lib\site-packages\sklearn\preprocessing\__init__.py)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Data.csv')
X = pd.DataFrame(dataset.iloc[:, :-1].values)
y = pd.DataFrame(dataset.iloc[:, 3].values)
# Taking care of missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])
it moved permanently from preprocessing to impute library, u can call it like:
from sklearn.impute import SimpleImputer
it's quite the same. if it doesn't work, you should uninstall it with pip and then install it again it may not installed properly for the first time
it doesn't have axis anymore but you could easily handle it with pandas dataframe header like this:
si=SimpleImputer()
si.fit([dataset["headername"]])
there is a strategy parameter that let you choose between "mean", "most_frequent","median" and "constant"
but there is another imputer that I like more:
from sklearn.impute import KNNImputer
which will impute missing values with an average of k nearest neighbors
A more complete answer:
Imputer
(https://sklearn.org/modules/generated/sklearn.preprocessing.Imputer.html`)
can be found only in versions 0.19.1 and below.
SimpleImputer
appeared at the latest versions and this is what you need.
Try to install the latest version:
pip install -U scikit-learn # or using conda
And then use:
from sklearn.impute import SimpleImputer
Source: https://github.com/mindsdb/lightwood/issues/75
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With