线性回归:从dtype('<M8 [ns]')到dtype('float64')

时间:2019-06-16 07:51:12

标签: python machine-learning linear-regression

我收到以下TypeError: TypeError: Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

原因似乎与该部分有关: X = event_data.index.values.reshape(-1,1)

您在这里看到我做错了吗? Here指向我的数据的链接。

from sklearn import linear_model

def load_event_data():
    df = pd.read_csv('sample-data.csv', usecols=['created', 'total_gross'])
    df['created'] = pd.to_datetime(df.created)
    return df.set_index('created').resample('D').sum().fillna(0)

event_data = load_event_data()
event_data['total_gross_accumulated'] = event_data['total_gross'].cumsum()
print(event_data.index.dtype)
event_data.head()

# Explore data
X = event_data.index
y = event_data['total_gross_accumulated']

plt.xticks(rotation=90)
plt.plot(X, y)
plt.show()

# Create and Fit a Linear Regression Model
regr = linear_model.LinearRegression()

# Reshape X
X = event_data.index.values.reshape(-1,1)
regr.fit(X, y)
y_predict = regr.predict(X)

# Show data and prediction
plt.plot(X, y)
plt.plot(X, y_predict)
plt.show()

0 个答案:

没有答案