디지털미디어랩 머신러닝 여름캠프

3주차(3) : Linear Regression 실습

참고 자료

Scikit-Learn 패키지

Scikit-Learn 패키지는 머신 러닝 교육 및 실무를 위한 파이썬 패키지로 다음과 같은 구성 요소들을 갖추고 있다.

벤치마크용 샘플 데이터 세트

  • 데이터 전처리(preprocessing) 기능
  • Supervised learning
  • Unsupervised learning
  • 모형 평가 및 선택

자세한 내용은 다음 웹사이트를 참조한다.

Scikit-Learn에서 제공하는 머신러닝 모형

  • scikit-learn 패키지의 장점은 다양한 머신 러닝 모형 즉, 알고리즘을 하나의 패키지에서 모두 제공하고 있다는 점이다.
  • 다음은 scikit-learn 패키지에서 제공하는 머신 러닝 모형의 목록이다.
  • 이 목록은 대표적인 것들만을 나열한 것이며 지속적으로 모형들이 추가되고 있다.

Supervised learning

http://scikit-learn.org/stable/supervised_learning.html

  • Generalized Linear Models
    • Ordinary Least Squares
    • Ridge/Lasso/Elastic Net Regression
    • Logistic regression
    • Polynomial regression
    • Perceptron
  • Linear and Quadratic Discriminant Analysis
  • Support Vector Machines
  • Stochastic Gradient Descent
  • Nearest Neighbor Algorithms
  • Gaussian Processes
  • Naive Bayes
  • Decision Trees
  • Ensemble methods
    • Random Forests
    • AdaBoost

Unsupervised learning

http://scikit-learn.org/stable/unsupervised_learning.html

  • Gaussian mixture models
  • Manifold learning
  • Clustering
    • K-means
    • DBSCAN
  • Biclustering
  • Decomposing
    • Principal component analysis (PCA)
    • Factor Analysis
    • Independent component analysis (ICA)
    • Latent Dirichlet Allocation (LDA)
  • Covariance estimation
  • Novelty and Outlier Detection
  • Density Estimation

데이터 불러오기

  • 먼저 pandas로 데이터를 불러 온다.
In [1]:
import pandas as pd

data = pd.read_excel('week4.xlsx')
data
Out[1]:
hours scores
0 2 30
1 3 50
2 9 80
3 10 90

입력변수(X), 출력변수(y) 설정

In [ ]:
X = data.drop('scores', axis=1)
y = data['scores']

Scikit-Learn의 LinearRegression 활용

In [ ]:
from sklearn import linear_model

H = linear_model.LinearRegression()
H.fit(X, y)

test = [[i] for i in range(1, 11)]
#test = [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]

print(H.predict(test))

sklearn으로 생성한 LinearRegression 모델의 Hypothesis

  • H.coef_ : W값
  • H.intercept_ : b값
  • H.residues_는 잔차의 제곱합(Cost)
In [ ]:
print(H.coef_)
print(H.intercept_)
print(H.residues_)
  • 따라서, $$H(x) = 6.6x + 22.9$$ $$Cost(W, b) = Cost(6.6, 22.9) = 97.0$$