假设我有一个.txt文件,其中包含
2,3,4,?,5
我想替换丢失的值'?'通过所有其他数据的平均值,任何好主意? 如果字符串列表,我想替换'如何?'最频繁的字符串,即' a'
'a','b','c','?','a','a'
我尝试了一些方法,但他们不会工作。我首先使用
import numpy as np
from sklearn.preprocessing import Imputer
row = np.genfromtxt('a.txt',missing_values='?',dtype=float,delimiter=',',usemask=True)
# this will give: row = [2 3 4 -- 5]. I checked it will use filling_values=-1 to replace missing data
# but if I add 'filling_values=np.nan' in it, it will cause error,'cannot convert float into int'
imp = Imputer(missing_values=-1, strategy='mean')
imp.fit_transform(row)
# this will give: array([2., 3., 4.,5.], which did not replace missing_value by mean value.
如果我可以替换'?'和np.nan一起,我想我能成功。
答案 0 :(得分:1)
我无法重现您描述的错误,'无法将float转换为int'。
试试这个:
>>> row = np.genfromtxt('a.txt',missing_values='?',dtype=float,delimiter=',')
>>> np.mean(row[~np.isnan(row)])
3.5
>>> mean = np.mean(row[~np.isnan(row)])
>>> row[np.isnan(row)] = mean
>>> row
array([ 2. , 3. , 4. , 3.5, 5. ])
修改强>
如果您希望使用字符串,这是使用普通列表的解决方案。
>>> row = ['a','b','c','?','c','b','?','?','b']
>>> from collections import Counter
>>> letter_counts = Counter(letter for letter in row if letter != '?')
>>> letter_counts.most_common(1)
[('b', 3)]
>>> most_common_letter = letter_counts.most_common(1)[0][0]
>>> [letter if letter != '?' else most_common_letter
... for letter in row]
['a', 'b', 'c', 'b', 'c', 'b', 'b', 'b', 'b']