사이킷런(scikit-learn)은 파이썬에서 머신러닝 모델을 손쉽게 구현할 수 있도록 개발된 오픈소스 라이브러리이다. 본 라이브러리는 데이터 전처리, 분류, 회귀, 클러스터링, 차원 축소, 모델 평가 등 다양한 머신러닝 작업을 지원한다.
pip 명령어를 사용하여 설치할 수 있다.
pip install scikit-learn
train_test_split
: 데이터를 훈련 세트와 테스트 세트로 나누는 데 사용된다.cross_val_score
: 모델의 일반화 성능을 평가하기 위해 교차 검증 점수를 계산한다.GridSearchCV
: 하이퍼파라미터 튜닝을 위한 그리드 탐색 기법을 제공한다.classification_report
: 정밀도, 재현율, F1-score를 포함한 분류 모델의 성능 지표를 출력한다.confusion_matrix
: 예측 결과와 실제 결과 간의 오차 행렬을 생성한다.Pipeline
: 여러 전처리 및 모델 단계를 일괄 구성하여 코드의 재사용성과 가독성을 높인다.make_pipeline
: 파이프라인을 간단히 생성할 수 있는 헬퍼 함수이다.StandardScaler, MinMaxScaler
: 특성 값을 정규화하거나 표준화하는 데 사용된다.LogisticRegression
: 이진 또는 다중 클래스 분류KNeighborsClassifier
: 최근접 이웃 기반 분류RandomForestClassifier
: 앙상블 학습 기반 분류GradientBoostingClassifier
: 부스팅 기반 분류SVC
: 서포트 벡터 머신 분류LinearRegression
: 선형 회귀Ridge, Lasso
: 정규화를 포함한 회귀 모델SVR
: 서포트 벡터 회귀RandomForestRegressor
: 앙상블 기반 회귀KMeans
: K-평균 군집화DBSCAN
: 밀도 기반 클러스터링AgglomerativeClustering
: 계층적 군집화PCA
: 주성분 분석TruncatedSVD
: 희소 행렬 차원 축소TSNE
: 고차원 시각화아래는 붓꽃(Iris) 데이터를 이용하여 분류 모델을 학습하고 평가하는 예제이다.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
# 데이터 로딩
iris = load_iris()
X = iris.data
y = iris.target
# 훈련/테스트 데이터 분리
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 모델 학습
clf = RandomForestClassifier()
clf.fit(X_train, y_train)
# 예측 및 평가
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))