dataset modules

괴도소녀·2021년 7월 12일
0

Machine Learning

목록 보기
5/10

scikit learn에서 기본적으로 제공해주는 데이터셋들이 존재한다.
loading datasets

그 중 대표적으로 사용하는 데이터셋중 하나인 wine데이터를 가져와 보겠다.

from sklearn.datasets import load_wine

data = load_wine()
type(data)
---------------------------------------------
sklearn.utils.Bunch

data를 뜯어 보면 dictionary와 유사하게 생긴 것을 알 수 있으며, 데이터의 타입은 Bunch라는 것을 알 수 있다.

  • keys() : data들의 key값들을 알아내는 메소드
data.keys()
-------------------------------------------------------
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])
  • shape : 데이터의 n_samples(row. 데이터 갯수), n_features(columns. 데이터 특성)의 모양을 알아보기 위해 shape을 쓰면 된다.
data.data.shape
--------------------
(178, 13)
  • ndim : 데이터의 차원 확인
data.data.ndim
----------------------------
2
  • target : 데이터의 target 확인
data.target
----------------------------------------------------------------
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2])
  • target의 shape 확인
data.target.shape
-----------------------------------
(178,)
  • feature_names : 특성들의 이름이 저장되있다.
data.feature_names
------------------------------------
['alcohol',
 'malic_acid',
 'ash',
 'alcalinity_of_ash',
 'magnesium',
 'total_phenols',
 'flavanoids',
 'nonflavanoid_phenols',
 'proanthocyanins',
 'color_intensity',
 'hue',
 'od280/od315_of_diluted_wines',
 'proline']
  • target_names : 분류하고자 하는 대상
data.target_names
-------------------------------------------------------
array(['class_0', 'class_1', 'class_2'], dtype='<U7')
  • DESCR : describe의 약자로 데이터에 대한 설명
print(data.DESCR)
---------------------------------
너무 길다. 그래서 첨부를 포기한다.

우선 데이터를 X, y로 나눠주자.

X = data.data
y = data.target

그리고 분류 모델을 사용한다.

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier()

모델을 학습시키자.

model.fit(X, y)

마지막으로 예측을 해보자.

y_pred = model.predict(X)

성능은 sklearn.metrics 모듈을 사용하며, 분류 모델은 classification_reportaccuracy_score를 사용한다.

from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

print(classification_report(y, y_pred))
print("accuracy = ", accuracy_score(y, y_pred))
------------------------------------------------------
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        59
           1       1.00      1.00      1.00        71
           2       1.00      1.00      1.00        48

    accuracy                           1.00       178
   macro avg       1.00      1.00      1.00       178
weighted avg       1.00      1.00      1.00       178

accuracy =  1.0

정확도가 100%이 나온 이유는 다음 포스팅에서 설명한다.
to be continue.......

0개의 댓글