2025. 7. 9. 22:13ㆍpython/ML
First, we import the necessary modules:
import sklearn
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import pandas as pd
We start by importing the scikit-learn library (sklearn) along with the datasets module, the Decision Tree algorithm, and the train_test_split utility. We also import pandas for handling data in DataFrame format.
After preparing the data, we split it into training and test sets using train_test_split:
iris = load_iris()
iris_data = iris.data
iris_label = iris.target
iris_df = pd.DataFrame( data = iris.data , columns = iris.feature_names)
iris_df['label'] = iris.target
iris_df
We then create a DataFrame containing the feature values and add a column for the target labels:
As you can see, this DataFrame consists of four feature columns describing the iris flowers and an additional label column indicating the class.
After preparing the data, we split it into training and test sets using train_test_split:
X_train, X_test, y_train, y_test = train_test_split(iris_data, iris_label, test_size = 0.2, random_state=11)
Then, we initialize the Decision Tree model and train it on the training data:
dt_clf = DecisionTreeClassifier(random_state = 11)
dt_clf.fit(X_train, y_train)
Finally, we use the trained model to make predictions on the test set:
pred = dt_clf.predict(X_test)
At the end, we can evaluate our model’s performance.
from sklearn.metrics import accuracy_score
accuracy_score(y_test, pred)'python > ML' 카테고리의 다른 글
| [ML_6] Prediction of pima diabetes using Scikitlearn (0) | 2025.07.17 |
|---|---|
| [ML_5] Prediction of Titanic survival by Scikitlearn (0) | 2025.07.15 |
| [ML_4] Data preprocessing (2) | 2025.07.13 |
| [ML_3] cross_validation (0) | 2025.07.12 |
| [ML_2] Let's convert pd.Dataframe to train_test dataset. (1) | 2025.07.10 |