1

For my machine learning code, I have some unknown values with '?' in my csv file. So, I am trying to replace them with 'Nan' but it throws some error. The following code is for the replacement of '?' that I have used. Can anyone please solve this? Thanks in advance !

 import numpy
    import pandas as pd
    import matplotlib as plot
    import numpy as np
    df = pd.read_csv('cdk.csv')
    x=df.iloc[:,0:24].values
    y=df.iloc[:,24].values
    from sklearn.preprocessing import Imputer
    imputer = Imputer(missing_values='NaN', strategy='most_frequent', axis =0,copy=False)
    imputer = imputer.fit(x[:,0:5])
    imputer.fit_transform(x[:,0:5])

    imputer = Imputer(missing_values='normal', strategy='mode', axis =0,copy=False)
    imputer = imputer.fit(x[:,5:7])
    imputer.fit_transform(x[:,5:7])

This is what error it throws,

Traceback (most recent call last):
  File "kidney.py", line 10, in <module>
    imputer = imputer.fit(x[:,0:5])
  File "C:\Users\YAASHI\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\preprocessing\imputation.py", line 155, in fit
    force_all_finite=False)
  File "C:\Users\YAASHI\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: could not convert string to float: '?'

Link for the csv file

2
  • This is a common ValueError. It has nothing to do with machine learning, so please do not tag as such Commented Aug 26, 2018 at 2:37
  • You say you are trying to replace ? with NaN, but you have showed no code which mentions ?. Where is your code to replace the ?s? Commented Aug 26, 2018 at 2:38

1 Answer 1

2

If you want to replace all ? strings with NaN, do this:

df.replace('?', np.nan, inplace=True)

Or better yet, load them as NaN as you read the CSV:

df = pd.read_csv('cdk.csv', na_values=['?'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.