【问题标题】:"ValueError: could not convert string to float: " machine learning fit classifier“ValueError:无法将字符串转换为浮点数:”机器学习适合分类器
【发布时间】:2019-10-14 09:11:38
【问题描述】:

当我尝试拟合分类器时出现错误:

ValueError:无法将字符串转换为浮点数:'4/1/2010'

# Load the Pandas libraries with alias 'pd'
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from math import sqrt
from ml_metrics import rmse

# Read data from file 'filename.csv'
# (in the same directory that your python process is based)
# Control delimiters, rows, column names with read_csv (see later)
data = pd.read_csv("NASDAQ.csv")
data.dropna(inplace=True)

#df.drop_duplicates(inplace=True)
nInstances, nAttributes = data.shape
if data.shape[0]:
    train = data[:1762]
    test = data[1762:]
x_train= train.values[:,0:nAttributes-1]
y_train= train.values[:,nAttributes-1]


# classifiers Linear Regression, Logistic Regression, kNN, SVM και MLP
clf = LinearRegression().fit(x_train, y_train)

能否请您检查一下并帮我找出问题所在?

【问题讨论】:

  • 你能打印你得到错误的堆栈跟踪吗?通常,它包含程序中导致问题的行。
  • 错误告诉你问题。您的数据包含字符串日期值,如 '4/1/2010',但 sklearn 只接受数字输入
  • @G.Anderson 如何将其转换为数字?我试过这个不成功?你有什么想法吗?
  • @GreenCloakGuy 文件“C:/Users/spyros/Downloads/Dialexi 5/Ασκηση.py”,第 26 行,在 clf = LinearRegression().fit(x_train, y_train) ValueError:无法将字符串转换为浮点数:'4/1/2010'
  • This question and answer 有一些很好的提示和技巧,如果您想保留日期作为功能。您也可以尝试完全删除日期列,除非您认为它会帮助您的模型而不会引入数据泄漏

标签: python machine-learning


【解决方案1】:

对于日期列,您可以按照this answer的建议在读取csv时使用parse_dates=['column name']

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 2021-11-16
    • 2019-08-31
    • 2016-03-20
    • 1970-01-01
    • 2018-10-06
    • 2019-03-23
    • 2018-06-13
    • 2013-05-30
    相关资源
    最近更新 更多