iris
iris
예측모형먼저 iris
품종 분류를 위한 예측모형을 개발해 본다. 이를 위한 작업흐름을 다음과 같이 잡아 실행한다.
이를 통해 iris
예측모형의 분류 정확성, 즉 모형 성능을 파악할 수 있다.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
## 데이터 가져오기
iris_df = pd.read_csv("data/iris_ws.csv")
## 데이터 전처리
iris_df.dropna(inplace=True)
X = iris_df.drop(columns=['variety'], axis=1)
y = iris_df['variety']
## 훈련/시험 데이터셋 분리
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42)
## 예측모형 적합
model = RandomForestClassifier()
model.fit(X, y)
## 예측모형 성능
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=10,
n_jobs=None, oob_score=False, random_state=None,
verbose=0, warm_start=False)
/anaconda3/lib/python3.6/site-packages/sklearn/ensemble/forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
"10 in version 0.20 to 100 in 0.22.", FutureWarning)
1.0
iris
예측모형 배포iris
품종분류 예측모형으로 Random Forest를 사용하여 모형 성능 예측력 100%를 달성하였다. 이를 배포해보자. 먼저 앞서 모형 개발에 사용된 코드에서 일부를 바꿔야 한다. 데이터를 가져와서 결측값 제거 등 작업을 수행하고 Random Forest 예측모형을 data/
디렉토리 rf_model.pkl
파일로 떨궈둔다.
# iris_rf.py 파일명으로 저장
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import pickle
## 데이터 가져오기
iris_df = pd.read_csv("data/iris_ws.csv")
## 데이터 전처리
iris_df.dropna(inplace=True)
X = iris_df.drop(columns=['variety'], axis=1)
y = iris_df['variety']
## 예측모형 적합
model = RandomForestClassifier()
model.fit(X, y)
## 예측모형 배포
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=10,
n_jobs=None, oob_score=False, random_state=None,
verbose=0, warm_start=False)
/anaconda3/lib/python3.6/site-packages/sklearn/ensemble/forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
"10 in version 0.20 to 100 in 0.22.", FutureWarning)
iris_rf_model = 'data/rf_model.pkl'
with open(iris_rf_model, 'wb') as f:
pickle.dump(model, f)
print(f"sucessfully deployed!!!")
sucessfully deployed!!!
ls -alh
명령어로 data/
디렉토리 rf_model.pkl
파일을 확인한다.
-rw-r--r-- 1 statkclee staff 21K Oct 11 13:47 data/rf_model.pkl
iris
예측모형 배포 자동화script
디렉토리 iris_rf.py
파일명으로 저장시킨다. 그리고 나서 이를 python
명령어로 실행시킨다.
/anaconda3/lib/python3.6/site-packages/sklearn/ensemble/forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
"10 in version 0.20 to 100 in 0.22.", FutureWarning)
sucessfully deployed!!!
파이썬을 통해 데이터 과학, 특히 앞서 개발한 예측모형 배포를 위한 자동화를 위해서 다음과 같은 작업흐름을 갖추게 된다.
.py
파이썬 스크립트로 작업 자동화파이썬 작업흐름도
파이썬은 아나콘다 혹은 파이썬에서 설치하여 코딩을 하는 것이 일반적이다.
which python
명령어를 통해서 파이썬 버전을 확인한다.
/anaconda3/bin/python
파이썬의 진정한 힘은 강력하고 다양한 파이썬 팩키지에서 나온다고 할 수 있다. 이를 손쉽게 설치할 수 있게 도와주는 것이 Pypi
팩키지 관리자다.
Do I need to install pip?을 참조하여 PyPi를 설치할 수 있다.
apt-get install python3-pip
curl
명령어로 pip
설치 파일을 가져와서 python
으로 실행하여 설치한다.Pypi
버전 매칭데이터 사이언스가 미래다!!!