我是个新手,但是谁能告诉我这是怎么回事?实际上,我正在尝试基于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:\MongoDB\MongoData.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)
发布于 2017-01-24 19:09:44
在定义变量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 |
# +------------------------------------------+
https://stackoverflow.com/questions/41826434
复制相似问题