小编Soh*_*ate的帖子

如何将 dtype='datetime64[ns]' 转换为浮点数?

我正在练习线性回归,在这里我将日期作为输入 x 传递并期望输出 y(float)

x = df[('Date')].values
x = x.reshape(-1, 1)
y= df[('MeanTemp')].values #MeanTemp column has float values
y = y.reshape(-1, 1)
Run Code Online (Sandbox Code Playgroud)

当我打印 x 时,输出是:

array([['1942-07-01T00:00:00.000000000'],
       ['1942-07-02T00:00:00.000000000'],
       ['1942-07-03T00:00:00.000000000'],
       ['1942-07-04T00:00:00.000000000'],
       ['1942-07-05T00:00:00.000000000'],
       ['1942-07-06T00:00:00.000000000'],
       ['1942-07-07T00:00:00.000000000'],
       ['1942-07-08T00:00:00.000000000'],
       ['1942-07-09T00:00:00.000000000'],
       ['1942-07-10T00:00:00.000000000']], dtype='datetime64[ns]')
Run Code Online (Sandbox Code Playgroud)

现在,当我使用线性回归时

linlin = LinearRegression()
linlin.fit(x, y)
Run Code Online (Sandbox Code Playgroud)

它没有给出任何错误,但是当我写的时候

linlin.predict(x)


TypeError: The DTypes <class 'numpy.dtype[float64]'> and <class 'numpy.dtype[datetime64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is `object`.
Run Code Online (Sandbox Code Playgroud)

弹出上面的TypeError。如何将此数据类型转换为浮点型以便预测函数正常工作?

python typeerror pandas scikit-learn

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

pandas ×1

python ×1

scikit-learn ×1

typeerror ×1