파이썬을 해본적이 없어서

GPT에게 간단한 예제를 부탁했다

 

# 필요한 라이브러리 임포트
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# 1. 데이터 로드
iris = load_iris()
X = iris.data  # 특성 데이터
y = iris.target  # 레이블 데이터

# 2. 데이터셋 분할 (학습용 80%, 테스트용 20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 3. 모델 생성
model = RandomForestClassifier(n_estimators=100, random_state=42)

# 4. 모델 학습
model.fit(X_train, y_train)

# 5. 예측 수행
y_pred = model.predict(X_test)

# 6. 평가
accuracy = accuracy_score(y_test, y_pred)
print("모델 정확도:", accuracy)
print("\n분류 리포트:\n", classification_report(y_test, y_pred))

# 7. 예측 예시
sample = np.array([[5.1, 3.5, 1.4, 0.2]])  # 임의의 데이터 샘플
prediction = model.predict(sample)
print("\n예측 결과:", iris.target_names[prediction][0])

 

 

필요한 라이브러리 설치까지 가이드해준다

pip install numpy scikit-learn

 

 

실행하면 다음과 같은 결과를 얻는다

 

 

내용은 앞으로 좀 더 공부해봐야지....

+ Recent posts