You are on page 1of 2

9/20/23, 11:50 AM Lab2 - Jupyter Notebook

Lab 2: Introduction to Scikit-Learn Library


In [12]:

import sklearn

In [13]:

from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data
y = iris.target

In [14]:

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42

Model
In [15]:

from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier()

In [16]:

clf.fit(X_train, y_train)

Out[16]:

DecisionTreeClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or
trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page
with nbviewer.org.

In [17]:

y_pred = clf.predict(X_test)

localhost:8888/notebooks/Lab2.ipynb 1/2
9/20/23, 11:50 AM Lab2 - Jupyter Notebook

In [18]:

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)


print("Accuracy:", accuracy)

Accuracy: 1.0

In [19]:

from sklearn.model_selection import cross_val_score

scores = cross_val_score(clf, X, y, cv=5)


print("Cross-Validation Scores:", scores)

Cross-Validation Scores: [0.96666667 0.96666667 0.9 0.93333333 1.


]

In [20]:

from sklearn.model_selection import GridSearchCV

param_grid = {'max_depth': [2, 3, 4, 5]}


grid_search = GridSearchCV(clf, param_grid, cv=5)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_

In [21]:

from joblib import dump, load

dump(clf, 'model.joblib')
loaded_model = load('model.joblib')

In [ ]:

localhost:8888/notebooks/Lab2.ipynb 2/2

You might also like