名称错误: 名称"x_train"未定义



我是新手,但谁能告诉我出了什么问题?我实际上正在尝试根据我在 excel 中的数据进行预测分析(线性回归图)。但是,我的图表没有绘制出来,我也遇到了这个错误。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy
from sklearn import linear_model
df = pd.read_csv("C:MongoDBMongoData.csv") 
x_train = np.array(x_train).reshape(len(x_train), -1)
x_train.shape
y_train= [1,2,3,4,5]
x_test = x_test.reshape(-1, 1)
x_test.shape
linear = linear_model.LinearRegression()
linear.fit(x_train, y_train)
linear.score(x_train, y_train)
print('Coefficient: n', linear.coef_)
print('Intercept: n', linear.intercept_)
predicted= linear.predict(x_test)

定义变量之前,x_train使用变量两次。您需要先定义它,然后再使用它。

  x_train = np.array(x_train).reshape(len(x_train), -1)
# ^^^^^^^            ^^^^^^^              ^^^^^^^
#    |                  |                    |
#    |    +------------------------------------------------+
#    |    | You use x_train twice before it's ever defined |
#    |    +------------------------------------------------+
#  +------------------------------------------+
#  | Your first definition of x_train is here |
#  +------------------------------------------+

相关内容

  • 没有找到相关文章

最新更新