연습 - Scikit Learn을 사용하여 선형 회귀 수행

완료됨

연구 커뮤니티에서 널리 사용되는 또 다른 인기 있는 Python 라이브러리는 데이터에서 정보를 추출하는 데 도움이 되는 기계 학습 모델을 빌드하는 데 탁월한 scikit-learn입니다. 이 연습에서는 scikit-learn(이미 2단원에서 가져온)을 사용하여 NASA 기후 데이터의 추세선을 컴퓨팅합니다.

  1. Notebook 하단의 빈 셀에 커서를 놓습니다. 셀 형식을 Markdown 으로 변경하고 텍스트로 "scikit-learn을 사용하여 선형 회귀 수행"을 입력합니다.

  2. 코드 셀을 추가하고 다음 코드에 붙여넣습니다.

    # Pick the Linear Regression model and instantiate it
    model = LinearRegression(fit_intercept=True)
    
    # Fit/build the model
    model.fit(yearsBase[:, np.newaxis], meanBase)
    mean_predicted = model.predict(yearsBase[:, np.newaxis])
    
    # Generate a plot like the one in the previous exercise
    plt.scatter(yearsBase, meanBase)
    plt.plot(yearsBase, mean_predicted)
    plt.title('scatter plot of mean temp difference vs year')
    plt.xlabel('years', fontsize=12)
    plt.ylabel('mean temp difference', fontsize=12)
    plt.show()
    
    print(' y = {0} * x + {1}'.format(model.coef_[0], model.intercept_))
    
  3. 이제 셀을 실행하여 회귀선이 포함된 산점도를 표시합니다.

    sckikit-learn에서 계산한 회귀선이 있는 산점도입니다.

    sckikit-learn에서 계산한 회귀선이 있는 산점도

출력은 이전 연습의 출력과 거의 동일합니다. scikit-learn이 자동으로 처리하는 작업이 더 많다는 차이점이 있습니다. 특히, scikit-learn의 LinearRegression 함수가 선 함수를 알아서 코딩하므로 NumPy에서 할 때처럼 직접 코딩할 필요가 없습니다. scikit-learn은 정교한 기계 학습 모델을 빌드할 때 유용하게 사용되는 다양한 유형의 회귀를 지원합니다.