那里
我正在学习Coursera的IBM数据科学课程,并试图创建一些片段进行实践。我创建了以下code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
# Import and format the dataframes
ibov = pd.read_csv('https://raw.githubusercontent.com/thiagobodruk/datasets/master/ibov.csv')
ifix = pd.read_csv('https://raw.githubusercontent.com/thiagobodruk/datasets/master/ifix.csv')
ibov['DATA'] = pd.to_datetime(ibov['DATA'], format='%d/%m/%Y')
ifix['DATA'] = pd.to_datetime(ifix['DATA'], format='%d/%m/%Y')
ifix = ifix.sort_values(by='DATA', ascending=False)
ibov = ibov.sort_values(by='DATA', ascending=False)
ibov = ibov[['DATA','FECHAMENTO']]
ibov.rename(columns={'FECHAMENTO':'IBOV'}, inplace=True)
ifix = ifix[['DATA','FECHAMENTO']]
ifix.rename(columns={'FECHAMENTO':'IFIX'}, inplace=True)
# Merge datasets
df_idx = ibov.merge( ifix, how='left', on='DATA')
df_idx.set_index('DATA', inplace=True)
df_idx.head()
# Split training and testing samples
x_train, x_test, y_train, y_test = train_test_split(df_idx['IBOV'], df_idx['IFIX'], test_size=0.2)
# Convert the samples to Numpy arrays
regr = linear_model.LinearRegression()
x_train = np.array([x_train])
y_train = np.array([y_train])
x_test = np.array([x_test])
y_test = np.array([y_test])
# Plot the result
regr.fit(x_train, y_train)
y_pred = regr.predict(y_train)
plt.scatter(x_train, y_train)
plt.plot(x_test, y_pred, color='blue', linewidth=3) # This line produces no result
我在train_test_split()
方法返回的输出值方面遇到了一些问题。所以我把它们转换成Numpy数组,然后我的代码工作了。我可以正常绘制散点图,但我不能绘制预测线
在myIBM Data Cloud Notebook上运行此代码会产生以下警告:
/opt/conda/envs/Python36/lib/python3.6/site-packages/matplotlib/axes/_base.py:380: MatplotlibDeprecationWarning: cycling among columns of inputs with non-matching shapes is deprecated. cbook.warn_deprecated("2.2", "cycling among columns of inputs "
我在谷歌和StackOverflow上搜索过,但我不知道哪里错了
我会感谢你的帮助。提前谢谢
代码中有几个问题,比如
y_pred = regr.predict(y_train)
和划线方式以下代码段应为您提供正确的方向:
要对已拟合模型的测试子集执行相同操作:
如果您有任何问题,请随时提问
相关问题 更多 >
编程相关推荐