Vikrant
Vikrant

Reputation: 13

Type error while using scikit-learns SimpleImputer

This code is for data preprocessing that I am learning in an online course of ML.

import numpy as np
import matplotlib.pyplot as plt  #pyplot is a sublibrary of matplotlib
import pandas as pd

dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:,:-1]
Y = dataset.iloc[:,-1]


from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan,strategy = 'mean',verbose = 0) 
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])

But it is giving this Type error: unhashable type: 'slice' . Please help me with this.

The image for the same is also attached.

Upvotes: 0

Views: 328

Answers (2)

glemaitre
glemaitre

Reputation: 1003

I would also advise to make use of sklearn.pipeline.Pipeline and sklearn.compose .ColumnTransformer make these preprocessing transformation if your final goal is to predict: https://scikit-learn.org/stable/auto_examples/compose/plot_column_transformer_mixed_types.html#sphx-glr-auto-examples-compose-plot-column-transformer-mixed-types-py

Upvotes: 0

Rajith Thennakoon
Rajith Thennakoon

Reputation: 4130

X is a dataframe and you can't access like X[:,1:3].you should use iloc. Try this

imputer = imputer.fit(X.iloc[:,1:3])
X.iloc[:,1:3] = imputer.transform(X.iloc[:,1:3])

Upvotes: 1

Related Questions