[ML_1] Let's make iris classification model by Scikitlearn

2025. 7. 9. 22:13python/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)