Линейная регрессия: от dtype ('<M8 [ns]') до dtype ('float64') - PullRequest
1 голос
/ 16 июня 2019

Я получаю следующую ошибку 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)

Видите ли вы, что я делаю здесь неправильно? Здесь ссылка на мои данные.

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()
...