본문 바로가기

scikit-learn Machine Learning in Python 본문

Python/machine learning

scikit-learn Machine Learning in Python

Data to Knowledge 2020. 8. 11. 17:10

이번에는 scikit-learn Machine Learning in Python을 이용하여 머신러닝을 구현하는 방법에 대하여 간단히 알아보겠습니다. 

  • Simple and efficient tools for predictive data analysis
  • Accessible to everybody, and reusable in various contexts
  • Built on NumPy, SciPy, and matplotlib
  • Open source, commercially usable - BSD license
파이썬에서 머신러닝을 구현하기 위해서는 scikit-learn 라이브러리(https://scikit-learn.org/stable/) 사용하면 편리합니다. 여기서는 머신러닝의 주요 기법인 로지스틱 회귀, 선형SVM, 비선형 SVM, Decision Tree, Random Forest, k-NN 등이 가능합니다.

from sklearn.datasets import make_classification

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.svm import LinearSVC

from sklearn.svm import SVC

from sklearn.tree import DecisionTreeClassifier

from sklearn.ensemble import RandomForestClassifier

# 데이터 X, 라벨 y 생성(samples=3000, features=2, random_state=55)

X, y = make_classification(n_samples=3000, n_features=2, n_redundant=0, random_state=0)

# train 데이터, test데이터를 분할(테스트 크기=0.3,random_state=55)

train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.3, random_state=55)

# 모델 구축

model_list = { '로지스틱 회귀':LogisticRegression(),

              '선형 SVM':LinearSVC(),

              '비선형 SVM':SVC(),

              '결정 트리':DecisionTreeClassifier(),

              '랜덤 포레스트':RandomForestClassifier()}

# for 문으로 모델을 학습시키고, 정확도 출력

for model_name, model in model_list.items():

    # 모델학습

    model.fit(train_X, train_y)

    print(model_name)

# 정확도 출력

    print('정확도:' + str(model.score(test_X,test_y)))

    print()

[결과]

로지스틱 회귀

정확도: 0.9111111111111111

선형 SVM

정확도: 0.9111111111111111

비선형 SVM

정확도: 0.9822222222222222

결정 트리

정확도: 0.9755555555555555

랜덤 포레스트

정확도:0.9844444444444445

 

'Python > machine learning' 카테고리의 다른 글

Convolutional Neural Network  (0) 2020.08.11
독립성분분석(ICA)  (0) 2020.01.28
Comments