Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scikit-learn.impute isn't being imported from Imputer via Spyder using the code from Machine Learning A-Z tutorial

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])
like image 626
Vygandas Razhas Avatar asked Dec 18 '19 01:12

Vygandas Razhas


2 Answers

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

like image 113
parsa Avatar answered Oct 03 '22 02:10

parsa


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

like image 24
seralouk Avatar answered Oct 03 '22 04:10

seralouk