0% found this document useful (0 votes)
15 views3 pages

Aml Lab 41 Ann Hyperparameter Tuning - Ipynb - Colab

The document outlines a Jupyter notebook for hyperparameter tuning of a neural network model using the MLPClassifier from scikit-learn on a dataset for diagnosing cancer. It details the data preprocessing steps, grid search for optimal hyperparameters, and evaluation metrics including accuracy and a confusion matrix. The best model achieved an accuracy of 95.61% with specific parameters identified through the tuning process.

Uploaded by

Aastha Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Aml Lab 41 Ann Hyperparameter Tuning - Ipynb - Colab

The document outlines a Jupyter notebook for hyperparameter tuning of a neural network model using the MLPClassifier from scikit-learn on a dataset for diagnosing cancer. It details the data preprocessing steps, grid search for optimal hyperparameters, and evaluation metrics including accuracy and a confusion matrix. The best model achieved an accuracy of 95.61% with specific parameters identified through the tuning process.

Uploaded by

Aastha Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/3/24, 9:45 AM AML_LAB_41_ANN_HYPERPARAMETER_TUNING.

ipynb - Colab

import numpy as np # linear algebra


import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
import tensorflow as tf
from sklearn.metrics import classification_report, confusion_matrix

df = pd.read_csv("/content/data.csv")
df.head()

id diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean compactness_mean concavity_mean


poin

0 842302 M 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.3001

1 842517 M 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.0869

2 84300903 M 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.1974

3 84348301 M 11.42 20.38 77.58 386.1 0.14250 0.28390 0.2414

4 84358402 M 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.1980

5 rows × 33 columns

df.drop(["id","Unnamed: 32"], axis=1 , inplace=True)

df.diagnosis = [1 if each == "M" else 0 for each in df.diagnosis]

y = df.diagnosis.values.reshape(-1,1)
x = df.iloc[:,1:].values

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

from sklearn.model_selection import GridSearchCV


estimator = MLPClassifier(max_iter=1000)
# Set parameters to search
parameters = {
'hidden_layer_sizes': [(100,), (150, 100), (200, 150, 100)],
'activation': ['logistic', 'tanh', 'relu'],
'solver': ['lbfgs', 'sgd', 'adam']
}

# Perform grid search


grid_search = GridSearchCV(estimator, parameters, n_jobs=-1,
verbose=1, scoring = "accuracy", refit=True)

grid_search.fit(x_train, y_train)

Fitting 5 folds for each of 27 candidates, totalling 135 fits


/usr/local/lib/python3.10/dist-packages/joblib/externals/loky/backend/fork_exec.py:38: RuntimeWarning: os.fork() was called. os.fork
pid = os.fork()
/usr/local/lib/python3.10/dist-packages/joblib/externals/loky/backend/fork_exec.py:38: RuntimeWarning: os.fork() was called. os.fork
pid = os.fork()
/usr/local/lib/python3.10/dist-packages/sklearn/neural_network/_multilayer_perceptron.py:1098: DataConversionWarning: A column-vecto
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/neural_network/_multilayer_perceptron.py:541: ConvergenceWarning: lbfgs failed to co
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
self.n_iter_ = _check_optimize_result("lbfgs", opt_res, self.max_iter)
▸ GridSearchCV
▸ estimator: MLPClassifier
▸ MLPClassifier

https://colab.research.google.com/drive/1cLCGou51IYt9euhPGF4iVEqruH5Fx3KB?authuser=1#printMode=true 1/3
7/3/24, 9:45 AM AML_LAB_41_ANN_HYPERPARAMETER_TUNING.ipynb - Colab
print(f'Best estimator: {grid_search.best_estimator_}')
print(f'Best parameters: {grid_search.best_params_}')

Best estimator: MLPClassifier(max_iter=1000, solver='lbfgs')


Best parameters: {'activation': 'relu', 'hidden_layer_sizes': (100,), 'solver': 'lbfgs'}

estimator_2 = MLPClassifier(activation='relu', solver='lbfgs', max_iter=1000)


parameters_2 = {'hidden_layer_sizes': [(150, 100), (250, 150), (350, 250)]}
grid_search_2 = GridSearchCV(estimator_2, parameters_2, n_jobs=-1,
verbose=1, scoring = "accuracy", refit=True)
grid_search_2.fit(x_train, y_train)

Fitting 5 folds for each of 3 candidates, totalling 15 fits


/usr/local/lib/python3.10/dist-packages/sklearn/neural_network/_multilayer_perceptron.py:1098: DataConversionWarning: A column-vecto
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/neural_network/_multilayer_perceptron.py:541: ConvergenceWarning: lbfgs failed to co
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
self.n_iter_ = _check_optimize_result("lbfgs", opt_res, self.max_iter)
▸ GridSearchCV
▸ estimator: MLPClassifier
▸ MLPClassifier

y_pred=grid_search.predict(x_test)

#DataFlair - Calculate the accuracy of our model


accuracy=accuracy_score(y_true=y_test, y_pred=y_pred)
#DataFlair - Print the accuracy
print("Accuracy: {:.2f}%".format(accuracy*100))

Accuracy: 95.61%

from sklearn.metrics import classification_report


num_classes=2
target_names = ["Class {} ".format(i) for i in range(num_classes)]
print(classification_report(y_test, y_pred, target_names=target_names))

precision recall f1-score support

Class 0 0.95 0.99 0.97 71


Class 1 0.97 0.91 0.94 43

accuracy 0.96 114


macro avg 0.96 0.95 0.95 114
weighted avg 0.96 0.96 0.96 114

# Generate confusion matrix


import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix# Import confusion_matrix instead
import seaborn as sns# Import seaborn for heatmap visualization

# Calculate confusion matrix


matrix=confusion_matrix(y_test,grid_search.predict(x_test))

# Plot confusion matrix using seaborn


sns.heatmap(matrix,annot=True,cmap='Blues',fmt='g')
plt.title('Confusion matrix for our classifier')
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.show()

https://colab.research.google.com/drive/1cLCGou51IYt9euhPGF4iVEqruH5Fx3KB?authuser=1#printMode=true 2/3
7/3/24, 9:45 AM AML_LAB_41_ANN_HYPERPARAMETER_TUNING.ipynb - Colab

https://colab.research.google.com/drive/1cLCGou51IYt9euhPGF4iVEqruH5Fx3KB?authuser=1#printMode=true 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy