TypeError: float() argument must be a string or a number, not 'SimpleImputer'
TypeError: float() argument must be a string or a number, not 'SimpleImputer'
all_data = pd.concat([train_data, test_data])
average = all_data.Age.median()
print('Average Age: {0}'.format(average))
train_data.fillna(value= {'Age' : average, inplace : True})
test_data.fillna(value= {'Age' : average, inplace : True})
错误:
TypeError Traceback (most recent call
last) <ipython-input-42-e465cf5beb42> in <module>()
1 all_data = pd.concat([train_data, test_data])
----> 2 average = all_data.Age.median().values()
3 print('Average Age: {0}'.format(average))
4 train_data.fillna(value= {'Age' : average, inplace : True})
5 test_data.fillna(value= {'Age' : average, inplace : True})
3 frames /usr/local/lib/python3.7/dist-packages/pandas/core/nanops.py
in nanmedian(values, axis, skipna, mask)
611 if not is_float_dtype(values.dtype):
612 try:
--> 613 values = values.astype("f8")
614 except ValueError as err:
615 # e.g. "could not convert string to float: 'a'"
TypeError: float() argument must be a string or a number, not
'SimpleImputer'
你好,我被这个案例困住了,即使使用 scikit-learn 也显示相同。请帮助我并提前致谢!!
这应该可以解决问题:
median = all_data['Age'].median()
train_data['Age'] = train_data['Age'].fillna(value=median)
test_data['Age'] = test_data['Age'].fillna(value=median)
all_data = pd.concat([train_data, test_data])
average = all_data.Age.median()
print('Average Age: {0}'.format(average))
train_data.fillna(value= {'Age' : average, inplace : True})
test_data.fillna(value= {'Age' : average, inplace : True})
错误:
TypeError Traceback (most recent call
last) <ipython-input-42-e465cf5beb42> in <module>()
1 all_data = pd.concat([train_data, test_data])
----> 2 average = all_data.Age.median().values()
3 print('Average Age: {0}'.format(average))
4 train_data.fillna(value= {'Age' : average, inplace : True})
5 test_data.fillna(value= {'Age' : average, inplace : True})
3 frames /usr/local/lib/python3.7/dist-packages/pandas/core/nanops.py
in nanmedian(values, axis, skipna, mask)
611 if not is_float_dtype(values.dtype):
612 try:
--> 613 values = values.astype("f8")
614 except ValueError as err:
615 # e.g. "could not convert string to float: 'a'"
TypeError: float() argument must be a string or a number, not
'SimpleImputer'
你好,我被这个案例困住了,即使使用 scikit-learn 也显示相同。请帮助我并提前致谢!!
这应该可以解决问题:
median = all_data['Age'].median()
train_data['Age'] = train_data['Age'].fillna(value=median)
test_data['Age'] = test_data['Age'].fillna(value=median)