我正在尝试将所有缺失数据(以’?’表示)转换为NaN
,并使用sklearn
中的填补工具将其平均为一个平均值。为了在我的问题上可以重现,我已经在下面包含了我的代码:我使用PyCharm作为IDE,操作系统为Mac OS X,使用的是Anaconda上的Python 2.7.12
这是我的代码:
import matplotlib.pyplot as pltimport numpy as npimport pandas as pddf = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/communities/communities.data', header=None, sep=',\s', na_values=["?"])df.tail()from sklearn.preprocessing import Imputerimr = Imputer(missing_values='NaN', strategy='mean', axis=0)imr= imr.fit(df)
这是我的错误信息
/Users/zdong/anaconda/bin/python/Users/zdong/PycharmProjects/ml/crim_workingfile.py
/Users/zdong/PycharmProjects/ml/crim_workingfile.py:4: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning databases/communities/communities.data', header=None, sep=',\s', na_values=["?"])
Traceback (most recent call last): File "/Users/zdong/PycharmProjects/535_final/535_workingfile.py", line 8,in <module>imr= imr.fit(df) File "/Users/zdong/anaconda/lib/python2.7/site-packages/sklearn/preprocessing/imputation.py",line 156, in fitforce_all_finite=False) File "/Users/zdong/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.py"line 382, in check_arrayarray = np.array(array, dtype=dtype, order=order, copy=copy)ValueError: invalid literal for float(): 6,?,?,Ontariocity,10,0.2,0.78,0.14,0.46,0.24,0.77,0.5,0.62,0.4,0.17,0.21,1,0.4,0.73,0.22,0.25,0.26,0.47,0.29,0.36,0.24,0.28,0.32,0.22,0.27,0.25,0.29,0.16,0.35,0.5,0.55,0.16,0.47,0.58,0.53,0.2,0.6,0.24
请帮助这个沮丧的新手QAQ…
回答:
好的,我认为这里已经足够给出一个实际的答案了。查看你的数据,前5列看起来是关于城市的信息(名称,其他值>=1),其余的看起来是你对最后一行fit
感兴趣的数据。
你的问题在于fit
尝试将所有数据转换为浮点数,显然在城市名称上失败了。传入fit
的数据应该是除了前5列之外的所有数据(如果第5列是偏置项的话,可能需要4列)。无论如何,尝试如下代码:
df = pd.read_csv('communities.data', header=None, na_values=["?"], usecols=range(5, 128))
并根据你需要的列数来更改5的值。