Python Forum
ValueError: Input contains infinity or a value too large for dtype('float64') - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: ValueError: Input contains infinity or a value too large for dtype('float64') (/thread-17268.html)



ValueError: Input contains infinity or a value too large for dtype('float64') - Rabah_r - Apr-04-2019

Hello python community, i need help.
I'm working on machine learning. However, i have problem at the cleaning step.
i use this code:
# Importing the libraries 
import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 

# Importing the dataset 
dataset = pd.read_csv('Rural3.csv') 

#cleaning missing data 
from sklearn.preprocessing import Imputer 
imputer= Imputer(missing_values='NaN', strategy='mean' , axis = 0) 
imputer.fit(dataset)
At this level, a notification appear: Columns (14,15) have mixed types. Specify dtype option on import or set low_memory=False.

I continue executing the following lines :
X = dataset.iloc[:, :-1].values 
y = dataset.iloc[:, 75].values
 
# Splitting the dataset into the Training set and Test set 
from sklearn.model_selection import train_test_split 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
 
# Feature Scaling 
from sklearn.preprocessing import StandardScaler 
sc = StandardScaler() 
X_train = sc.fit_transform(X_train) 
X_test = sc.transform(X_test)
At this level, the error appears: ValueError: Input contains infinity or a value too large for dtype('float64').

What should i do please ?! i didn't know what to do ?!


RE: ValueError: Input contains infinity or a value too large for dtype('float64') - scidam - Apr-06-2019

Pandas is very flexible. So, you don't need to use Imputer, just do this work with Pandas:

dataset.fillna(dataset.mean(), inplace=True)