【问题标题】:I am trying to convert the blank values of my csv file to the mean of the columns but it is giving "could not convert string to float: '-' " error我正在尝试将我的 csv 文件的空白值转换为列的平均值,但它给出“无法将字符串转换为浮点数:'-'”错误
【发布时间】:2020-05-24 22:29:14
【问题描述】:
import pandas as pd 
import numpy as np
from sklearn.preprocessing import Imputer 
data = pd.read_csv("austin_weather.csv")
data = data.drop(['Events', 'Date'], axis = 1) 
X = data.iloc[:, :-1].values
Y = data.iloc[:, 18].values
data = data.replace('T', 0.0)
imputer = Imputer(missing_values="-", strategy="mean", axis = 0)
imputer.fit(X[:])

imputer 函数无法将“-”空白值转换为相应列的平均值

【问题讨论】:

    标签: pandas numpy machine-learning scikit-learn python-3.6


    【解决方案1】:

    对于折旧类sklearn.preprocessing.Imputer 参数missing_values 是字符串NaN 或数字。

    所以你可以先用np.nan替换所有-值:X.replace('-', np.nan),然后调用 imputer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多