1. Iris DataSet

 

API Reference

This is the class and function reference of scikit-learn. Please refer to the full user guide for further details, as the class and function raw specifications may not be enough to give full guidel...

scikit-learn.org


1. 데이터 세트 로드 및 탐색

from sklearn.datasets import load_iris
iris = load_iris() # 붓꽃 데이터 세트 로드
print(iris['DESCR']) # 데이터 세트 설명 출력
  • load_iris() 함수를 사용하여 붓꽃 데이터 세트를 로드합니다.
  • iris['DESCR']는 데이터 세트에 대한 자세한 설명을 제공합니다.

2. 데이터프레임 생성 및 확인

import pandas as pd

data = iris['data'] # 실제 데이터(독립 변수)
feature_names = iris['feature_names'] # 데이터의 특성 이름
df_iris = pd.DataFrame(data, columns=feature_names) # 데이터프레임 생성
df_iris.head() # 데이터프레임의 처음 5행 출력
  • feature_names는 각 특성(열)의 이름입니다.

3. 타켓 변수 추가

target = iris['target'] # 타겟 변수(종속 변수)
df_iris['target'] = target # 데이터프레임에 타겟 변수 추가

 

4. 데이터 분활

from sklearn.model_selection import train_test_split

# 학습용 데이터와 테스트용 데이터로 분할
X_train, X_test, y_train, y_test = train_test_split(df_iris.drop('target', axis=1),
                                                    df_iris['target'],
                                                    test_size=0.2,
                                                    random_state=2023)
  • train_test_split 함수를 사용하여 데이터를 학습 세트와 테스트 세트로 분할합니다.
  • test_size=0.2는 데이터의 20%를 테스트 세트로 사용한다는 의미입니다.
  • random_state는 재현 가능한 결과를 위해 설정합니다.

5. 모델 학습

from sklearn.svm import SVC
svc = SVC() # SVC 모델 인스턴스 생성
svc.fit(X_train, y_train) # 학습 데이터로 모델 학습
  • SVC 모델을 사용하여 분류 문제를 해결합니다.
  • fit 메소드로 모델을 학습 데이터에 맞게 학습시킵니다.

6. 모델 예측 및 평가

from sklearn.metrics import accuracy_score

y_pred = svc.predict(X_test) # 테스트 데이터에 대한 예측 수행
print('정답률:', accuracy_score(y_test, y_pred)) # 예측 정확도 출력
# [출력] : 정답률 1.0
  • predict 메소드로 테스트 데이터에 대한 예측을 수행합니다.
  • accuracy_score 함수로 예측의 정확도를 평가합니다.

7. 새로운 데이터에 대한 예측

y_pred = svc.predict([[6.4, 2.6, 5.1, 1.9]]) # 새로운 데이터에 대한 예측 수행
y_pred # 예측 결과 출력