NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

BACKUP CODES

###IMPORTANT###
1. each code block should be entered in separate cells
2. remove comments (lines beginning with #) after compiling
3. All the datasets used in this are loaded from in built libraries
4. Try using GPU for fasteer runtime

Note: Not all codes might be available.......................sorry in advance :)
Also try to load the csv file from the local drive and then read its head if these codes are used....


========================================================================================================================

A) naive bayes with cm and heatmap

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix

# Load the Breast Cancer dataset
data = load_breast_cancer()

# Split the dataset into training and testing data
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.3, random_state=42)

# Create a Gaussian Naive Bayes classifier
clf = GaussianNB()

# Train the classifier on the training data
clf.fit(X_train, y_train)

# Make predictions on the testing data
y_pred = clf.predict(X_test)

# Print the accuracy score and confusion matrix
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:n", confusion_matrix(y_test, y_pred))

import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix

# assume y_true and y_pred are already defined from previous code
cm = confusion_matrix(y_test, y_pred)

# plot confusion matrix as heatmap
sns.heatmap(cm, annot=True, cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()

========================================================================================================================

B) CIFAR10 (use gpu for faster compile)

import tensorflow as tf
from tensorflow import keras
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np

# Load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Normalize pixel values to be between 0 and 1
x_train = x_train / 255.0
x_test = x_test / 255.0

# Define the model
model = Sequential()
model.add(Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(32,32,3)))
model.add(Conv2D(32, (3,3), activation='relu', padding='same'))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=10, batch_size=64)

# Evaluate the model on test set
test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)

# Make predictions on test set
y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred, axis=1)

# Print confusion matrix and classification report
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("Classification Report:")
print(classification_report(y_test, y_pred))

========================================================================================================================


C) Regression with generalization

# Importing necessary libraries
import pandas as pd
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt

# Loading the dataset
diabetes = load_diabetes()

# Creating dataframe
df = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)
df['target'] = diabetes.target

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df.drop('target', axis=1), df['target'], test_size=0.3, random_state=42)

# Creating linear regression object
lr = LinearRegression()

# Training the model
lr.fit(X_train, y_train)

# Predicting the target values
y_pred = lr.predict(X_test)

# Calculating various metrics
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
adj_r2 = 1 - ((1 - r2) * (len(y_test) - 1)) / (len(y_test) - X_test.shape[1] - 1)

# Printing the metrics
print('MSE:', mse)
print('RMSE:', rmse)
print('R-squared:', r2)
print('Adjusted R-squared:', adj_r2)

# Creating confusion matrix and heatmap
y_pred_class = np.where(y_pred > np.mean(y_pred), 1, 0)
confusion_matrix = pd.crosstab(y_test > np.mean(y_test), y_pred_class, rownames=['Actual'], colnames=['Predicted'])
sns.heatmap(confusion_matrix, annot=True, cmap='Blues')
plt.show()

========================================================================================================================

D) Binary CLassifiaction


# Import required libraries
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt

# Load the dataset
data = load_breast_cancer()

# Create a DataFrame for the dataset
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df[data.feature_names], df['target'], random_state=0)

# Create a logistic regression object
lr = LogisticRegression()

# Train the logistic regression model
lr.fit(X_train, y_train)

# Predict the test set labels using the trained model
y_pred = lr.predict(X_test)

# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)

# Create a confusion matrix
cm = confusion_matrix(y_test, y_pred)
print('Confusion Matrix:n', cm)

# Create a heatmap of the confusion matrix
sns.heatmap(cm, annot=True, cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()

========================================================================================================================

E) 3x3 magic square (ive used gridworld navigation from reinforcement learning lab, so mightt not be correct)

!pip install gym[toy_text]

# importing libraries
import gym
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output

# Creating Custom Map
# S -> Starting position / Initial Robot Position
# F -> Path
# H -> Obstacle / Hole
# G -> Home / Destination / Docking Station / Goal

custom_map = [
"SFFF",
"FHFH",
"FFFH",
"HFFG"
]

# Creating Environment
env=gym.make('FrozenLake-v1', desc= custom_map , is_slippery=False)
env.reset()

action_space = env.action_space.n
observation_space = env.observation_space.n

print(f"action_space = {action_space} observation_space = {observation_space}")

# We re-initialize the Q-table
qtable = np.zeros((observation_space, action_space))
print('Q-table before training:n')
print(qtable)
print("n")

# Hyperparameters
episodes = 1000 # Total number of episodes
alpha = 0.5 # Learning rate
gamma = 0.9 # Discount factor

# List of outcomes to plot
outcomes = []

# Training
for _ in range(episodes):
state = env.reset()
done = False

# By default, we consider our outcome to be a failure
outcomes.append("Failure")

# Until the agent gets stuck in a hole or reaches the goal, keep training it
while not done:
# Choose the action with the highest value in the current state
if np.max(qtable[state]) > 0:
action = np.argmax(qtable[state])

# If there's no best action (only zeros), take a random one
else:
action = env.action_space.sample()

# Implement this action and move the agent in the desired direction
new_state, reward, done, info = env.step(action)

# Update Q(s,a)
qtable[state, action] = qtable[state, action] +
alpha * (reward + gamma * np.max(qtable[new_state]) - qtable[state, action])

# Update our current state
state = new_state

# If we have a reward, it means that our outcome is a success
if reward:
outcomes[-1] = "Success"

print('Q-table after training:n')
print(qtable)
print("n")


# Plot outcomes
plt.rcParams['figure.dpi'] = 300
plt.rcParams.update({'font.size': 17})
plt.figure(figsize=(5, 2))
plt.xlabel("Run number")
plt.ylabel("Outcome")
ax = plt.gca()
ax.set_facecolor('#efeeea')
plt.bar(range(len(outcomes)), outcomes, color="#0A047A", width=1.0)
plt.show()

episodes = 1000000
nb_success = 0

# Evaluation
for _ in range(1000000):
clear_output()
state = env.reset()
done = False
print(f"Episode Completed = {_/episodes*100}%")

# Until the agent gets stuck or reaches the goal, keep training it
while not done:
# Choose the action with the highest value in the current state
if np.max(qtable[state]) > 0:
action = np.argmax(qtable[state])

# If there's no best action (only zeros), take a random one
else:
action = env.action_space.sample()

# Implement this action and move the agent in the desired direction
new_state, reward, done, info = env.step(action)

# Update our current state
state = new_state

# When we get a reward, it means we solved the game
nb_success += reward


# Let's check our success rate!
print (f"Success rate = {nb_success/episodes*100}%")

from IPython.display import clear_output
import time

state = env.reset()
done = False
sequence = []

while not done:
# Choose the action with the highest value in the current state
if np.max(qtable[state]) > 0:
action = np.argmax(qtable[state])

# If there's no best action (only zeros), take a random one
else:
action = env.action_space.sample()

# Add the action to the sequence
sequence.append(action)

# Implement this action and move the agent in the desired direction
new_state, reward, done, info = env.step(action)

# Update our current state
state = new_state

# Update the render
clear_output(wait=True)

time.sleep(1)

print(f"Sequence = {sequence}")

for i in sequence :
if i == 0 :
print("LEFT n")
elif i == 1 :
print("DOWN n")
elif i == 2 :
print("RIGHT n")
elif i == 3 :
print("UP n")
else :
print("error")

========================================================================================================================

F) Student Performance

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.decomposition import PCA
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import roc_auc_score

df = pd.read_csv('StudentsPerformance.csv')

le = LabelEncoder()
df['gender'] = le.fit_transform(df['gender'])
df['race/ethnicity'] = le.fit_transform(df['race/ethnicity'])
df['parental level of education'] = le.fit_transform(df['parental level of education'])
df['lunch'] = le.fit_transform(df['lunch'])
df['test preparation course'] = le.fit_transform(df['test preparation course'])

scaler = StandardScaler()
X = scaler.fit_transform(df)

pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

kmeans = KMeans(n_clusters=3, random_state=42)
kmeans_labels = kmeans.fit_predict(X_pca)

print('confusion Martix')
print(confusion_matrix(df['test preparation course'], kmeans_labels))
print()

print('classifiaction Report')
print(classification_report(df['test preparation course'], kmeans_labels))

agg = AgglomerativeClustering(n_clusters=3)
agg_labels = agg.fit_predict(X_pca)

print('confusion Martix')
print(confusion_matrix(df['test preparation course'], agg_labels))
print()

print('classifiaction Report')
print(classification_report(df['test preparation course'], agg_labels))

dbscan = DBSCAN(eps=1.2, min_samples=12)
dbscan_labels = dbscan.fit_predict(X_pca)

print('confusion Martix')
print(confusion_matrix(df['test preparation course'], dbscan_labels))
print()

print('classifiaction Report')
print(classification_report(df['test preparation course'], dbscan_labels))

fig, ax = plt.subplots(1, 3, figsize=(15, 5))
fig.suptitle('Clustering Algorithms Comparison')

ax[0].scatter(X_pca[:, 0], X_pca[:, 1], c=kmeans_labels)
ax[0].set_title('KMeans Clustering')

ax[1].scatter(X_pca[:, 0], X_pca[:, 1], c=agg_labels)
ax[1].set_title('Agglomerative Clustering')

ax[2].scatter(X_pca[:, 0], X_pca[:, 1], c=dbscan_labels)
ax[2].set_title('DBSCAN Clustering')

plt.show()

print('confusion Martix')
print(confusion_matrix(df['test preparation course'], kmeans_labels))
print('classifiaction Report')
print(classification_report(df['test preparation course'], kmeans_labels))
cm1=confusion_matrix(df['test preparation course'], kmeans_labels)
tp = cm1[0, 0]
tn = cm1[1, 1]
fp = cm1[1, 0]
fn = cm1[0, 1]
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
specificity = tn / (tn + fp)
f1score = 2 * precision * recall / (precision + recall)
roc=roc_auc_score(df['test preparation course'], kmeans_labels)

print('Accuracy:', accuracy)
print('precision:', precision)
print('recall:', recall)
print('specificity:', specificity)
print('f1score:', f1score)
print('roc:', roc)

print('confusion Martix')
print(confusion_matrix(df['test preparation course'], agg_labels))
print('classifiaction Report')
print(classification_report(df['test preparation course'], agg_labels))
cm2=confusion_matrix(df['test preparation course'], agg_labels)
tp = cm2[0, 0]
tn = cm2[1, 1]
fp = cm2[1, 0]
fn = cm2[0, 1]
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
specificity = tn / (tn + fp)
f1score = 2 * precision * recall / (precision + recall)
roc=roc_auc_score(df['test preparation course'], agg_labels)

print('Accuracy:', accuracy)
print('precision:', precision)
print('recall:', recall)
print('specificity:', specificity)
print('f1score:', f1score)
print('roc:', roc)

print('confusion Martix')
print(confusion_matrix(df['test preparation course'], dbscan_labels))
print('classifiaction Report')
print(classification_report(df['test preparation course'], dbscan_labels))
cm3=confusion_matrix(df['test preparation course'], dbscan_labels)
tp = cm3[0, 0]
tn = cm3[1, 1]
fp = cm3[1, 0]
fn = cm3[0, 1]
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
specificity = tn / (tn + fp)
f1score = 2 * precision * recall / (precision + recall)
roc=roc_auc_score(df['test preparation course'], dbscan_labels)

print('Accuracy:', accuracy)
print('precision:', precision)
print('recall:', recall)
print('specificity:', specificity)
print('f1score:', f1score)
print('roc:', roc)

========================================================================================================================
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.