2017-01-24 5 views
0

저는이 말을 처음 접했지만 아무도 저에게 무엇이 잘못되었는지 말해 줄 수 있습니까? 나는 실제로 Excel에서 가지고있는 데이터를 기반으로 예측 분석 (선형 회귀 그래프)을하려고합니다. 그러나 내 그래프는 플롯되지 않았고 또한이 오류에 직면했습니다.NameError : name 'x_train'이 정의되지 않았습니다.

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) 
+0

, 여기에'x_train = np.array (x_train) .reshape (LEN (x_train), -1)'는'아직 x_train' 과제 '에 선언되지 않은 x_train'를 사용하려고하고 있습니다. 허용되지 않습니다 – Andersson

+0

x_train의 선언이 누락되었습니다 : 'x_train = np.array (x_train) .reshape (len (x_train), -1)' –

+0

6 번에서 7 번 사이의 행을 놓쳤습니다. df를 x_train과 x_test로 나눕니다. 'x_train, x_test = ... '와 같은 것 –

답변

4

변수를 정의하기 전에 변수 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 | 
# +------------------------------------------+ 
+0

ASCII 형식을 좋아합니다. +1. – rayryeng

관련 문제