Python - 线性回归 TypeError:无效类型提升 [英] Python - linear regression TypeError: invalid type promotion

查看:53
本文介绍了Python - 线性回归 TypeError:无效类型提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行线性回归,但我认为数据类型有问题.我已经逐行测试,一切正常,直到我到达最后一行,在那里我遇到了问题 TypeError: invalid Type Promotion.根据我的研究,我认为这是由于日期格式造成的.
这是我的代码:

i am trying to run linear regression and i am having issues with data type i think. I have tested line by line and everything works until i reach last line where i get the issue TypeError: invalid Type promotion. Based on my research i think it is due to date format.
Here is my code:

import pandas as pd
import numpy as np  
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split 
from sklearn.linear_model import LinearRegression
data=pd.read_excel('C:\\Users\\Proximo\\PycharmProjects\Counts\\venv\\Counts.xlsx')  
data['DATE'] = pd.to_datetime(data['DATE'])
data.plot(x = 'DATE', y = 'COUNT', style = 'o')
plt.title('Corona Spread Over the Time')
plt.xlabel('Date')
plt.ylabel('Count')
plt.show()

X=data['DATE'].values.reshape(-1,1)
y=data['COUNT'].values.reshape(-1,1)
X_train,X_test,Y_train,Y_test=train_test_split(X,y,test_size=.2,random_state=0)
regressor = LinearRegression()  
regressor.fit(X_train,Y_train)
y_pre = regressor.predict(X_test)

当我运行它时,这是我得到的完整错误:

When i run it this is the full error i get:

    ---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-21-c9e943251026> in <module>
----> 1 y_pre = regressor.predict(X_test)
      2 

c:\users\slavi\pycharmprojects\coronavirus\venv\lib\site-packages\sklearn\linear_model\_base.py in predict(self, X)
    223             Returns predicted values.
    224         """
--> 225         return self._decision_function(X)
    226 
    227     _preprocess_data = staticmethod(_preprocess_data)

c:\users\slavi\pycharmprojects\coronavirus\venv\lib\site-packages\sklearn\linear_model\_base.py in _decision_function(self, X)
    207         X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
    208         return safe_sparse_dot(X, self.coef_.T,
--> 209                                dense_output=True) + self.intercept_
    210 
    211     def predict(self, X):

c:\users\Proximo\pycharmprojects\Count\venv\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output)
    149             ret = np.dot(a, b)
    150     else:
--> 151         ret = a @ b
    152 
    153     if (sparse.issparse(a) and sparse.issparse(b)

TypeError: invalid type promotion

我的日期格式如下:

array([['2020-01-20T00:00:00.000000000'],
   ['2020-01-21T00:00:00.000000000'],
   ['2020-01-22T00:00:00.000000000'],
   ['2020-01-23T00:00:00.000000000'],
   ['2020-01-24T00:00:00.000000000'],
   ['2020-01-25T00:00:00.000000000'],
   ['2020-01-26T00:00:00.000000000'],
   ['2020-01-27T00:00:00.000000000'],
   ['2020-01-28T00:00:00.000000000'],
   ['2020-01-29T00:00:00.000000000'],
   ['2020-01-30T00:00:00.000000000'],
   ['2020-01-31T00:00:00.000000000'],
   ['2020-02-01T00:00:00.000000000'],
   ['2020-02-02T00:00:00.000000000']], dtype='datetime64[ns]')

关于如何解决这个问题有什么建议吗?

Any suggestion on how to resolve this issue?

推荐答案

我认为线性回归不适用于日期类型数据.您需要将其转换为数值数据.例如

I think linear regression not work for date type data.You need to convert it to numerical data. for example

import numpy as np
import pandas as pd
import datetime as dt

X_test = pd.DataFrame(np.array([
   ['2020-01-24T00:00:00.000000000'],
   ['2020-01-25T00:00:00.000000000'],
   ['2020-01-26T00:00:00.000000000'],
   ['2020-01-27T00:00:00.000000000'],
   ['2020-01-28T00:00:00.000000000'],
   ['2020-01-29T00:00:00.000000000'],
   ['2020-01-30T00:00:00.000000000'],
   ['2020-01-31T00:00:00.000000000'],
   ['2020-02-01T00:00:00.000000000'],
   ['2020-02-02T00:00:00.000000000']], dtype='datetime64[ns]'))

X_test.columns = ["Date"]
X_test['Date'] = pd.to_datetime(X_test['Date'])
X_test['Date']=X_test['Date'].map(dt.datetime.toordinal)

试试这个方法.这应该可行.

Try this approach.this should work.

注意 - 最好将训练集日期转换为数字并在该数据上进行训练.

Note - it is better to covert training set dates to numeric and train on that data.

这篇关于Python - 线性回归 TypeError:无效类型提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆