728x90
SMALL
- python에서 가장 유명한 머신러닝 라이브러리 중 하나
- 분류(classification),회귀(regression),군집화(clustering),의사결정 트리(decision tree)등 함수 제공
Datamining Basic flow:
- 데이터 로드 (파일 읽기 -> numpy나 pandas 타입으로 변환)
- 데이터 전처리, EDA
- 모델 생성 및 학습
- 모델 평가
모델 생성 및 학습
- 모델 생성(SVM)
>>> from sklearn import svm
>>> model = svm.SVC(gamma=0.001,C=100 )
- 모델 클래스의 methods
• fit(X, y): 입력 X와 라벨 y를 통해 해당 모델 학습
• predict(T): 입력 T에 해당하는 예측값 리턴
모델 평가
Datasets 얻기
- sciket-learn은 머신러닝을 쉽게 배울 수 있도록, 샘플 데이터셋 가지고 있음
>>>from sklearn import datasets #선언
>>>iris = datasets.load_iris() #아이리스 꽃 데이터셋
#load된 데이터셋은 속성-스타일 접근을 제공하는 파있너 딕셔너리, 번치(bunch)객체로 표현
>>>print(iris.DESCR)
#DESCR 속성을 사용해 데이터셋의 정보를 얻을 수 있다.
#얻은결과
-------------------------------------------------------------------------
.. _iris_dataset:
Iris plants dataset
--------------------
**Data Set Characteristics:**
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica
:Summary Statistics:
============== ==== ==== ======= ===== ====================
Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================
:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
:Date: July, 1988
The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.
This is perhaps the best known database to be found in the
pattern recognition literature. Fisher's paper is a classic in the field and
is referenced frequently to this day. (See Duda & Hart, for example.) The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant. One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.
.. topic:: References
- Fisher, R.A. "The use of multiple measurements in taxonomic problems"
Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
Mathematical Statistics" (John Wiley, NY, 1950).
- Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.
(Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.
- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
Structure and Classification Rule for Recognition in Partially Exposed
Environments". IEEE Transactions on Pattern Analysis and Machine
Intelligence, Vol. PAMI-2, No. 1, 67-71.
- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions
on Information Theory, May 1972, 431-433.
- See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II
conceptual clustering system finds 3 classes in the data.
- Many, many more ...
- feature_names
#feature_names 속성으로 피쳐 이름을 알아낼 수 있다.
>>>print(iris.feature_names)
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
column 개수 총 4개 (feature,attribute,...) 꽃받침 길이, 꽃받침 너비,꽃잎 길이,꽃잎 너비
- x.target
>>> print(iris.target) #레이블을 알 수 있다.
[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 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
2 2]
>>> print(iris.target_names) #레이블 이름을 알 수 있다.
['setosa' 'versicolor' 'virginica']
#여기서 0,1,2 순서이다.
- 사이킷런의 모든 샘플 데이터가 feature_names, target_names속성을 지원하는 것은 아니다.
Pandas
- 데이터를 쉽게 다루기 위해, Pandas의 데이터프레임으로 변환하는 것이 유용하다.
>>>import pandas as pd #pandas 라이브러리를 읽어드린다.
>>>df = pd.DataFrame(iris.data)
>>>df.head()
-해볼것!
참고사이트
LIST