首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NameError:未定义名称“x_train”

NameError:未定义名称“x_train”
EN

Stack Overflow用户
提问于 2017-01-24 18:59:16
回答 1查看 14.9K关注 0票数 0

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

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-01-24 19:09:44

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

代码语言:javascript
运行
复制
  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 |
#  +------------------------------------------+
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41826434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档