2017-12-16 33 views
0
나는 다음과 같은 변수가

:회귀 사용하여 파이썬

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
from sklearn.model_selection import train_test_split 


np.random.seed(0) 
n = 15 
x = np.linspace(0,10,n) + np.random.randn(n)/5 
y = np.sin(x)+x/6 + np.random.randn(n)/10 


X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0) 


def part1_scatter(): 
    %matplotlib notebook 
    plt.figure() 
    plt.scatter(X_train, y_train, label='training data') 
    plt.scatter(X_test, y_test, label='test data') 
    plt.legend(loc=4); 

그리고 다음과 같은 질문 :

는도 1, 3, 6에 대한 훈련 데이터 X_train에 다항식 선형 회귀 모델에 맞는 기능을 쓰기를, (sklearn.preprocessing의 PolynomialFeatures를 사용하여 다항식 피쳐를 작성한 다음 선형 회귀 모델을 적용합니다.) 각 모델에 대해 x = 0 ~ 10 간격 (예 : np.linspace (0,10,100))에 대해 100 개의 예측 된 값을 찾습니다. 이것을 numpy 배열에 저장하십시오. 이 배열의 첫 번째 행은 차수 1, 두 번째 행 차수 3, 세 번째 행 차수 6 및 네 번째 행 차수 9에서 훈련 된 모델의 출력과 일치해야합니다.

이것은 내 코드이지만 't는 운동 : 어디에 문제가

line 58  for i 
in degree: ^IndentationError: unexpected 
indent 

당신이 말해 줄 수 :

def answer_one(): 
    from sklearn.linear_model import LinearRegression 
    from sklearn.preprocessing import PolynomialFeatures 

    np.random.seed(0) 
n = 15 
x = np.linspace(0,10,n) + np.random.randn(n)/5 
y = np.sin(x)+x/6 + np.random.randn(n)/10 

X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0) 
results = [] 
pred_data = np.linspace(0,10,100) 
degree = [1,3,6,9] 
y_train1 = y_train.reshape(-1,1) 

for i in degree: 
    poly = PolynomialFeatures(degree=i) 
    pred_poly1 = poly.fit_transform(pred_data[:,np.newaxis]) 
    X_F1_poly = poly.fit_transform(X_train[:,np.newaxis]) 
    linreg = LinearRegression().fit(X_F1_poly, y_train1) 
    pred = linreg.predict(pred_poly1) 
    results.append(pred) 

    dataArray = np.array(results).reshape(4, 100) 

    return dataArray 

나는이 오류가? 하지 또한, for에서

답변

0

return 문은 이루어집니다 for 이후에 수행되어야한다, 그래서 들여 쓰기를해야한다.

+0

"for"아래의 "return"을 시도했지만 작동하지 않습니다. –

+0

for 다음과 같이 반환하면 다음과 같은 오류 메시지가 나타납니다 : i 회선에 대해정도의^IndentationError : 예기치 않은 들여 쓰기 –

0

당신은 identing와 중지

n = 15 

당신 라인의 시작에서 . 그래서 그 부분은 기능으로 인식되지 않습니다. 이것은 n = 15부터 모든 줄에 4 칸을 넣음으로써 해결할 수 있습니다.