【发布时间】:2019-08-31 13:24:50
【问题描述】:
我尝试使用分类变量进行多元线性回归。 使用了一种热编码器技术解决了这个问题,但得到了这个错误。
我尝试使用 pd.to_datetime() 函数将日期字符串转换为时间戳,但随后它也给出了类似的错误 - TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 'Timestamp'
所以我删除了这个东西,然后用其他方法解决了实际指定的错误..
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
dfle = df #df is the dataset containing column names ----- 'Section', 'BRAND', 'RSP', 'Monthstartdate', and 'Sales'*(to be predicted)* ---------
dfle.Section = le.fit_transform(dfle.Section) #Categorical values (2 in number )
dfle.BRAND = le.fit_transform(dfle.BRAND) #Categorical values (390 in number)
X = dfle[['Section', 'BRAND', 'RSP', 'Monthstartdate']].values
y = dfle.Sales
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(categorical_features = [0])
X = ohe.fit_transform(X).toarray()
预期的结果是数组本来可以正确拟合但出现此错误。
错误 -
----> X = ohe.fit_transform(X).toarray()
ValueError:无法将字符串转换为浮点数:'01/06/2016'
(在这种情况下,'01/06/2016' 是字符串,而不是时间戳,如果它可以是时间戳并解决回归问题,那就太棒了)
【问题讨论】:
-
Sklearn 不正式支持时间数据,尽管它可能偶尔会起作用。也许只是将时间戳转换为 unix 时间
-
我通常使用“自第一个月以来的月份数”或“经过的月份数”,因此该列将变为 0,1,2,3,4,5... 等。
-
但是,我认为年份也是一个重要因素。我们怎么能忽略它呢? @詹姆斯菲利普斯
-
你能同时使用“一年中的月份”和“年”吗?
标签: python scikit-learn linear-regression one-hot-encoding