训练和测试模型 PollyPlot
Training and Testing Model PollyPlot
我正在尝试使用 Matplotlib/Seaborn 绘制多项式图。我是数据科学的新手,因此我在使用这段代码时遇到了问题:
def PollyPlot(xtrain, xtest, y_train, y_test, lr,poly_transform):
width = 12
height = 10
plt.figure(figsize=(width, height))
#training data
#testing data
# lr: linear regression object
#poly_transform: polynomial transformation object
xmax=max([xtrain.values.max(), xtest.values.max()])
xmin=min([xtrain.values.min(), xtest.values.min()])
x=np.arange(xmin, xmax, 0.1)
plt.plot(xtrain, y_train, 'ro', label='Training Data')
plt.plot(xtest, y_test, 'go', label='Test Data')
plt.plot(x, lr.predict(poly_transform.fit_transform(x.reshape(-1, 1))), label='Predicted Function')
plt.ylim([-10000, 60000])
plt.ylabel('Price')
plt.legend()
这是绘制多项式函数的函数。但是当我调用函数时:
PollyPlot(x_train[['horsepower']], x_test[['horsepower']], y_train, y_test, poly, pr)
我收到以下错误:
InvalidIndexError: (slice(None, None, None), None)
如有任何帮助,我们将不胜感激。
我做了 2 处更改:
我已经从函数中删除了 .values。计算最大值和最小值,因为我会将 xtrain 和 xtest 转换为 numpy:
def PollyPlot(xtrain, xtest, y_train, y_test, lr,poly_transform):
宽度 = 12
高度 = 10
plt.figure(图形大小=(宽度, 高度))
#training data
#testing data
# lr: linear regression object
#poly_transform: polynomial transformation object
xmax=max([xtrain.max(), xtest.max()])
xmin=min([xtrain.min(), xtest.min()])
x=np.arange(xmin, xmax, 0.1)
plt.plot(xtrain, y_train, 'ro', label='Training Data')
plt.plot(xtest, y_test, 'go', label='Test Data')
plt.plot(x, lr.predict(poly_transform.fit_transform(x.reshape(-1, 1))), label='Predicted Function')
plt.ylim([-10000, 60000])
plt.ylabel('Price')
plt.legend()
为了执行,我已经将 x_train 和 x_test 转换为 numpy:
PollyPlot(x_train[['horsepower']].to_numpy(), x_test[['horsepower']].to_numpy (), y_train.to_numpy(), y_test.to_numpy(), 聚,pr)
我正在尝试使用 Matplotlib/Seaborn 绘制多项式图。我是数据科学的新手,因此我在使用这段代码时遇到了问题:
def PollyPlot(xtrain, xtest, y_train, y_test, lr,poly_transform):
width = 12
height = 10
plt.figure(figsize=(width, height))
#training data
#testing data
# lr: linear regression object
#poly_transform: polynomial transformation object
xmax=max([xtrain.values.max(), xtest.values.max()])
xmin=min([xtrain.values.min(), xtest.values.min()])
x=np.arange(xmin, xmax, 0.1)
plt.plot(xtrain, y_train, 'ro', label='Training Data')
plt.plot(xtest, y_test, 'go', label='Test Data')
plt.plot(x, lr.predict(poly_transform.fit_transform(x.reshape(-1, 1))), label='Predicted Function')
plt.ylim([-10000, 60000])
plt.ylabel('Price')
plt.legend()
这是绘制多项式函数的函数。但是当我调用函数时:
PollyPlot(x_train[['horsepower']], x_test[['horsepower']], y_train, y_test, poly, pr)
我收到以下错误:
InvalidIndexError: (slice(None, None, None), None)
如有任何帮助,我们将不胜感激。
我做了 2 处更改:
我已经从函数中删除了 .values。计算最大值和最小值,因为我会将 xtrain 和 xtest 转换为 numpy:
def PollyPlot(xtrain, xtest, y_train, y_test, lr,poly_transform): 宽度 = 12 高度 = 10 plt.figure(图形大小=(宽度, 高度))
#training data #testing data # lr: linear regression object #poly_transform: polynomial transformation object xmax=max([xtrain.max(), xtest.max()]) xmin=min([xtrain.min(), xtest.min()]) x=np.arange(xmin, xmax, 0.1) plt.plot(xtrain, y_train, 'ro', label='Training Data') plt.plot(xtest, y_test, 'go', label='Test Data') plt.plot(x, lr.predict(poly_transform.fit_transform(x.reshape(-1, 1))), label='Predicted Function') plt.ylim([-10000, 60000]) plt.ylabel('Price') plt.legend()
为了执行,我已经将 x_train 和 x_test 转换为 numpy:
PollyPlot(x_train[['horsepower']].to_numpy(), x_test[['horsepower']].to_numpy (), y_train.to_numpy(), y_test.to_numpy(), 聚,pr)