본문 바로가기
코딩

RandomForest(랜덤포레스트) 가장 많이 사용하는 파이썬 알고리즘 part3.

by 노마드랩스 2023. 2. 5.
728x90
반응형

이 예에서는 scikit-learn의 load_iris 함수를 사용하여 iris 데이터셋을 로드합니다. 

그런 다음 데이터는 train_test_split 함수를 사용하여 교육 및 테스트 세트로 분할됩니다. 

RandomForestClassifier 클래스는 학습 데이터에서 임의 포레스트 분류기를 학습하는 데 사용됩니다. 

fit 함수는 훈련 데이터에 분류기를 맞추는 데 사용되고 점수 방법은 테스트 데이터에서 분류기를 평가하는 데 사용됩니다. 

마지막으로 분류기의 정확도가 출력되어 테스트 데이터에서 얼마나 잘 수행되었는지 보여줍니다.

저는 정확도가 100%가 나왔네요.

import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load the iris dataset
iris = load_iris()
X = iris.data
y = iris.target

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Train the Random Forest classifier
clf = RandomForestClassifier(n_estimators=100, random_state=0)
clf.fit(X_train, y_train)

# Evaluate the classifier on the test data
accuracy = clf.score(X_test, y_test)
print("Accuracy: {:.2f}%".format(accuracy * 100))
728x90
반응형

댓글