ANN11 - Colab

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

4/15/24, 3:07 AM ANN11 - Colab

#ANN ASSIGNMENT 11
#HARSH MEHTA
#TA41
import numpy as np
import tensorflow as tf
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load and preprocess the breast cancer dataset


data = load_breast_cancer()
X = data.data
y = data.target

# Normalize the features


X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)

# Split the data into train and test sets


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

# Convert data to TensorFlow Dataset


train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)).batch(32)
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test)).batch(32)

# Define the logistic regression model


model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, activation='sigmoid', input_shape=(X_train.shape[1],))
])

# Compile the model


model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model


model.fit(train_dataset, epochs=10, verbose=1)

# Evaluate the model


test_loss, test_accuracy = model.evaluate(test_dataset, verbose=0)

print(f'Test Loss: {test_loss}')


print(f'Test Accuracy: {test_accuracy}')

# Save the trained model


model.save('logistic_regression_model')

output Epoch 1/10


15/15 [==============================] - 1s 3ms/step - loss: 0.8912 - accuracy: 0.4132
Epoch 2/10
15/15 [==============================] - 0s 3ms/step - loss: 0.7814 - accuracy: 0.5055
Epoch 3/10
15/15 [==============================] - 0s 3ms/step - loss: 0.6877 - accuracy: 0.6154
Epoch 4/10
15/15 [==============================] - 0s 3ms/step - loss: 0.6095 - accuracy: 0.6967
Epoch 5/10
15/15 [==============================] - 0s 3ms/step - loss: 0.5449 - accuracy: 0.7604
Epoch 6/10
15/15 [==============================] - 0s 5ms/step - loss: 0.4918 - accuracy: 0.8022
Epoch 7/10
15/15 [==============================] - 0s 3ms/step - loss: 0.4478 - accuracy: 0.8352
Epoch 8/10
15/15 [==============================] - 0s 3ms/step - loss: 0.4111 - accuracy: 0.8571
Epoch 9/10
15/15 [==============================] - 0s 4ms/step - loss: 0.3803 - accuracy: 0.8791
Epoch 10/10
15/15 [==============================] - 0s 3ms/step - loss: 0.3542 - accuracy: 0.9011
Test Loss: 0.31584468483924866
Test Accuracy: 0.9473684430122375

https://colab.research.google.com/drive/1Eu97nWFgodM3W39Xf7l6vGM9v6AFh-7o#scrollTo=Jqtk87dESS-h&printMode=true 1/2
4/15/24, 3:07 AM ANN11 - Colab

https://colab.research.google.com/drive/1Eu97nWFgodM3W39Xf7l6vGM9v6AFh-7o#scrollTo=Jqtk87dESS-h&printMode=true 2/2

You might also like