Syntax Error in machine learning code

machine_learning

#1

I tried to run the following code in python but got syntax error… Could anyone help to resolve this issue?

-- coding: utf-8 --

“”"
Spyder Editor
This is a temporary script file.
“”"
#Importing liabraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Importing data sets
dataset = pd.read_csv(‘Data.csv’)
X=dataset.iloc[:,:-1].values
y=dataset.iloc[:,3].values
#Taking care of missing data
from sklearn.preprocesing import Imputer
imputer = Imputer.(missing_values = ‘np.nan’, strategy = ‘mean’, axis = 0)
imputer = imputer.fit([X:,1:3])
X[:,1] = imputer.transform(X[:,1])
Error Message is :
File “”, line 18
imputer = Imputer.(missing_values = ‘np.nan’, strategy = ‘mean’, axis = 0)
^
SyntaxError: invalid syntax


#2

You should get rid of the . between Imputer and (. It should be like

imputer = Imputer(missing_values = ‘np.nan’, strategy = ‘mean’, axis = 0)

Hope this helps


#3

>>> import numpy as np
>>>from sklearn.impute import SimpleImputer
>>> imp = SimpleImputer(missing_values=np.nan, strategy=‘mean’)

>>> imp.fit([[1, 2], [np.nan, 3], [7, 6]]) SimpleImputer(copy=True, fill_value=None, missing_values=nan, strategy=‘mean’, verbose=0)
>>> X = [[np.nan, 2], [6, np.nan], [7, 6]]
>>> print(imp.transform(X))