gpt4 book ai didi

python - 名称错误 : name 'x_train' is not defined

转载 作者:行者123 更新时间:2023-11-30 09:25:43 28 4
gpt4 key购买 nike

我是新手,但谁能告诉我出了什么问题?我实际上正在尝试根据 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)

最佳答案

在定义变量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 |
# +------------------------------------------+

关于python - 名称错误 : name 'x_train' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826434/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com