0% found this document useful (0 votes)
5 views

Week6_Bai

week6

Uploaded by

rainy.budhathoki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week6_Bai

week6

Uploaded by

rainy.budhathoki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

12/2/24, 11:50 AM Week6_Baidehi

Week 6 Assignment
Baidehi Basnet

Presidential Graduate School, Kathmandu

PRG 330: Python Programming with Data

Prof. Tek Raj Pant

December 2, 2024

Classification

Gaussian Naive Classifier

Load,and prepare dataset

Split data into test and train dataset

In [16]: import sklearn


import IPython
import pandas as pd
from sklearn.datasets import load_iris
from IPython.display import display

# Load the Iris dataset


iris = load_iris()
# create new dataframe from iris data
iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
iris_df['species'] = iris.target
iris_df['species'] = iris_df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virg

display(iris_df.head())
X = iris_df[['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)'
y = iris_df['species'] # Target (species: 0=setosa, 1=versicolor, 2=virginica)

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) species

0 5.1 3.5 1.4 0.2 setosa

1 4.9 3.0 1.4 0.2 setosa

2 4.7 3.2 1.3 0.2 setosa

3 4.6 3.1 1.5 0.2 setosa

4 5.0 3.6 1.4 0.2 setosa

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 1/14


12/2/24, 11:50 AM Week6_Baidehi

Split data into test and train dataset


In [17]: 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_sta

Train the GNB Classifier


In [18]: from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(X_train, y_train)

Out[18]: ▾ GaussianNB i ?

GaussianNB()

Make Prediction
In [19]: # Make prediction
y_pred = gnb.predict(X_test)

Evaluate the model


In [20]: from sklearn.metrics import accuracy_score, classification_report

print("Accuracy:", accuracy_score(y_test, y_pred))


print("\nClassification Report:\n",)
print(classification_report(y_test, y_pred))

Accuracy: 1.0

Classification Report:

precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

Clustering
Clustering is the process of arranging a group of objects in such a manner that the
objects in the same group (which is referred as a cluster) are more similar to each other
than to the objects in any other group

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 2/14


12/2/24, 11:50 AM Week6_Baidehi

K-Means Clustering Algorithm


Choose the Number of Clusters (k): We decide how many clusters you want the data
divided into.

Initialize Cluster Centers: Randomly select k points from the data as initial cluster centers
(also called centroids).

Assign Data Points to Clusters: Each data point is assigned to the cluster whose centroid
is closest (measured by distance, usually Euclidean).

Update Centroids: For each cluster, compute the new centroid as the average of all
points assigned to that cluster.

Repeat: Reassign points to clusters based on the updated centroids and recompute the
centroids. This process is repeated until the centroids stop changing significantly or a
maximum number of iterations is reached.

Finally The algorithm returns the final clusters and their centroids, after n number of
iteration (max_iter)

In [21]: import pandas as pd


home_data = pd.read_csv('data/housing.csv', usecols = ['longitude', 'latitude', 'me
home_data.head()

Out[21]: longitude latitude median_house_value

0 -122.23 37.88 452600.0

1 -122.22 37.86 358500.0

2 -122.24 37.85 352100.0

3 -122.25 37.85 341300.0

4 -122.25 37.85 342200.0

Scattered Plot to Visualize the data


In [22]: import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(10,5))
sns.scatterplot(data = home_data, x = 'longitude', y = 'latitude', hue = 'median_ho
plt.show()

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 3/14


12/2/24, 11:50 AM Week6_Baidehi

Split test/train and normalize data


In [23]: from sklearn.model_selection import train_test_split
from sklearn import preprocessing
X_train, X_test, y_train, y_test = train_test_split(home_data[['latitude', 'longitu

X_train_norm = preprocessing.normalize(X_train)
X_test_norm = preprocessing.normalize(X_test)

Fit the data to the K-Means Model and plot the clusters into
scattered Plot
In [24]: from sklearn.cluster import KMeans

Kmeans = KMeans (n_clusters = 3, random_state = 0, n_init= 'auto')


Kmeans.fit(X_train_norm)

plt.figure(figsize=(10,5))
sns.scatterplot(data = X_train, x = 'longitude', y = 'latitude', hue = Kmeans.label
plt.show()

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 4/14


12/2/24, 11:50 AM Week6_Baidehi

Evaluate Clustering Model using Silhouetee Score (lower score represents a


better fit)

In [25]: from sklearn.metrics import silhouette_score


sil_score = silhouette_score (X_train_norm, Kmeans.labels_, metric='euclidean')
print(sil_score)

0.7499371920703546

Deep Learning
Deep learning is basically repeting the process until the output value reaches close to
your actual number.

Design Simple Artificial Neural Network


1 Input (takes input - image form)
1 hidden layer (identifies pattern)
1 output layer (gives output - text form) - predicted
c = error(cost function) main function of deep learning
e = eulor's number (2.72)

Deep Learning
In [26]: import numpy as np
import matplotlib.pyplot as plt

class Neuralnetwork:
def __init__(self, input_neurons, hidden_neurons, output_neurons, learning_rate
# initialize network parameters

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 5/14


12/2/24, 11:50 AM Week6_Baidehi

self.input_neurons = input_neurons
self.hidden_neurons = hidden_neurons
self.output_neurons = output_neurons
self.learning_rate = learning_rate

# Random initialization of weights and biases


np.random.seed(42)
self.w1 = np.random.randn(input_neurons, hidden_neurons)
self.b1 = np.random.randn(hidden_neurons)
self.w2 = np.random.randn(hidden_neurons, output_neurons)
self.b2 = np.random.randn(output_neurons)

def sigmoid(self, x):


return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward_pass(self, x):
self.z1 = np.dot(x, self.w1)+ self.b1 #w1.x +b
self.a1 = self.sigmoid(self.z1) #apply sigmoid activation function

self.z2 = np.dot(self.a1, self.w2) + self.b2


self.output = self.sigmoid(self.z2) #APPLY SIGMOID ACTIVATION FUNCTION

return self.output
def compute_loss(self, y_pred, y_true):
return np.mean ((y_pred - y_true) ** 2)

def backpropagate (self, x, y_true, y_pred):


error_output = y_pred - y_true

d_output = error_output * self.sigmoid_derivative(y_pred)

error_hidden = d_output.dot(self.w2.T)
d_hidden = error_hidden *self.sigmoid_derivative(self.a1)

self.w2 -= self.a1.T.dot(d_hidden) * self.learning_rate


self.b2 -= np.sum(d_output, axis = 0) * self.learning_rate

self.w1 -= x.T.dot(d_hidden) * self.learning_rate


self.b1 -= np.sum(d_hidden, axis=0) * self.learning_rate

def train (self, X, y, epochs = 5000):


for epoch in range(epochs):
y_pred = self.forward_pass(X)

## compute loss
loss = self.compute_loss(y_pred, y)

# backpropagation and weights update


self.backpropagate(X, y, y_pred)

if epoch % 1000 ==0:


print(self.w1, self.w2)
print(f"Epoch{epoch}, loss: {loss: 4f}")

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 6/14


12/2/24, 11:50 AM Week6_Baidehi

def predict(self, X):


return self.forward_pass(X)

In [27]: # create some synthetic data for house sizes and corresponding prices

np.random.seed(42)
X = np.array([[500], [1000], [1500], [2000], [2500], [3000]])
y = X * 150 + (np.random.randn(*X.shape) *10000) # Random noise

nn= Neuralnetwork(input_neurons =1, hidden_neurons=1, output_neurons=1, learning_ra


nn.train(X, y, epochs = 50000)

predictions = nn.predict(X)
#Plotting the data
plt.scatter(X, y, color='blue', label='Actual prices') # Actual prices
plt.plot(X, predictions, color='red', label='Predicted prices') # Predicted prices
plt.xlabel('House Size (sq ft)')
plt.ylabel('House Price ($)')
plt.title('House Price Prediction')
plt.legend()
plt.show()

Using Hand written Digit Recognization Mpdel Using


Tensorflow
In [3]: import tensorflow as tf
from tensorflow.keras.models import Sequential

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 7/14


12/2/24, 11:50 AM Week6_Baidehi

from tensorflow.keras.layers import Dense, Flatten


from tensorflow.keras.datasets import mnist
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
import numpy as np
(X_train, y_train),(X_test,y_test)=mnist.load_data()

#Normalize data(scale pixel values to range 0-1


X_train =X_train/255.0
X_test= X_test/255.0

#Build the model


model=Sequential([
Flatten(input_shape=(28,28)),
Dense(128,activation='relu'),
Dense(10,activation='softmax')
])
#Set learning rate
learning_rate=0.01
optimizer=Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer,
loss= 'sparse_categorical_crossentropy',
metrics=['accuracy'],
)
early_stopping= EarlyStopping(
monitor='val_loss',
patience=3,
restore_best_weights=True
)
#Train the model
model.fit(X_train, y_train,epochs=5,batch_size=32)
test_loss, test_acc= model.evaluate(X_test,y_test)
print(f'Test accuracy: {test_acc}')

predictions=model.predict(X_test)
print(f'Prediction for first test image: m{np.argmax(predictions[0])}')

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mn


ist.npz
11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 23s 2us/step
c:\Users\Asus\anaconda3\Anaconda3\Lib\site-packages\keras\src\layers\reshaping\flatt
en.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer.
When using Sequential models, prefer using an `Input(shape)` object as the first lay
er in the model instead.
super().__init__(**kwargs)

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 8/14


12/2/24, 11:50 AM Week6_Baidehi

Epoch 1/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 8s 4ms/step - accuracy: 0.8973 - loss: 0.3433
Epoch 2/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.9556 - loss: 0.1557
Epoch 3/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 8s 4ms/step - accuracy: 0.9620 - loss: 0.1340
Epoch 4/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 6s 3ms/step - accuracy: 0.9663 - loss: 0.1257
Epoch 5/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.9685 - loss: 0.1149
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.9577 - loss: 0.1862
Test accuracy: 0.9638000130653381
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step
Prediction for first test image: m7

Save Model
In [ ]: model.save('handwritten-digit-model.keras')

Use Real image and Predict the digit


In [4]: import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
import numpy as np

(X_train, y_train),(X_test, y_test) = mnist.load_data()


#Normalize data(scale pixel values to range 0-1)

X_train = X_train / 255.0


X_test =X_test / 255.0

#bUILD THE MODEL

model = Sequential([
Flatten(input_shape = (28, 28)),
Dense(128, activation = 'relu'),
Dense(10, activation = 'softmax')
])

# Set learning rate


learning_rate = 0.01
optimizer = Adam(learning_rate = learning_rate)
model.compile(optimizer = optimizer,
loss = 'sparse_categorical_crossentropy',
metrics =['accuracy'],
)
early_stopping = EarlyStopping(
monitor = 'val_loss',
patience = 3,

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 9/14


12/2/24, 11:50 AM Week6_Baidehi

restore_best_weights = True
)

# Train the model


model.fit(X_train, y_train, epochs = 5, batch_size = 32)

test_loss, test_acc = model.evaluate(X_test, y_test)


print(f'Test accuracy: {test_acc}')

predictions = model.predict(X_test)

print(f'prediction for first test image: {np.argmax(predictions[0])}')

c:\Users\Asus\anaconda3\Anaconda3\Lib\site-packages\keras\src\layers\reshaping\flatt
en.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer.
When using Sequential models, prefer using an `Input(shape)` object as the first lay
er in the model instead.
super().__init__(**kwargs)
Epoch 1/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 8s 4ms/step - accuracy: 0.8980 - loss: 0.3363
Epoch 2/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 6s 3ms/step - accuracy: 0.9565 - loss: 0.1530
Epoch 3/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 6s 3ms/step - accuracy: 0.9611 - loss: 0.1370
Epoch 4/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.9683 - loss: 0.1130
Epoch 5/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 7s 3ms/step - accuracy: 0.9689 - loss: 0.1158
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.9568 - loss: 0.2054
Test accuracy: 0.9620000123977661
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step
prediction for first test image: 7

Python I/O Operations

File I/O allows reading data from the files and writing data to files.
Python provides the open() function to work with File I/O

Open a File from FileSystem


file = open(filename, mode, buffering)

filename: Name of the file with full path that we want to open

mode: Operation mode (r,w,x,a,b,+)

buffering: Controls buffers, but python controls the buffering

File Operations Table


Mode Description

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 10/14


12/2/24, 11:50 AM Week6_Baidehi

r Read-only mode.
w Write mode. Creates/truncates the file.
x Exclusive creation. Fails if the file exists.
a Append mode. Writes data at the end.
b Binary mode. Used with rb, wb.
t Text mode (default).
+-- Update mode (read and write).

Read a text file and assign the content to a python variable


In [10]: import json
with open('data/test.txt','r') as text_file:
text_content= text_file.read()
print(text_content)

with open('data/test.json','r') as json_file:


data = json.load(json_file)
print(data)

Baidehi Basnet

{'Name': 'Baidehi', 'Address': 'Kathmandu', 'Age': '22'}

Read text file Line and assign the content to a Python Variable
In [9]: # read text content line by line using (nextline()) and while loop

with open('data/test.txt', 'r') as text_file:


line = text_file.readline() # read the next line
while line: # as long as there is a line
print(line, end='')
line = text_file.readline() # read the next line

# using for loop

with open('data/test.txt', 'r') as text_file:


for line in text_file:
print(line, end ='')

# get all lines in a list usin readlines()

with open('data/test.txt', 'r') as text_file:


lines = text_file.readlines()

Baidehi Basnet
Baidehi Basnet

Read Text file Line By Line


In [11]: # read text content line by line using (nectline()) and while loop
with open ('data/test.txt', 'r') as text_file:

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 11/14


12/2/24, 11:50 AM Week6_Baidehi

line = text_file.readline()
while line:
print(line, end='')
line = text_file.readline()

# using for loop


with open('data/test.txt', 'r') as text_file:
for line in text_file:
print(line, end='')

# get all lines in a list using readlines()


with open('data/test.txt','r') as text_file:
lines = text_file.readlines()
print(lines)

Baidehi Basnet
Baidehi Basnet
['Baidehi Basnet\n']

Writing to Files: Open file for writing using open('file','w')


write(content) Write at once
writelines([line1,line2])
Append to files: Open file with open(file,'a')

In [13]: # Write whole content at once


with open('data/new-text-file.txt','w') as new_file:
new_file.write('Hello World!')

#write line by line


with open('data/new-text-file-1.txt','w') as new_file:
new_file.writelines(['Hello\n','World\n'])

with open('data/new-text-file-1.txt', 'a') as file:


file.write('This should append on file-1.txt\n')

# Write python dict to JSON


person = {
'name':'Ram',
'address':'Kathmandu',
'age':32
}
with open('person.json','w') as file:
json.dump(person,file)

Python Handling Directories


The os module provides the various functions for working with Directories

Function Description

os.mkdir(path) Creates a directory.

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 12/14


12/2/24, 11:50 AM Week6_Baidehi

Function Description

os.makedirs(path) Creates intermediate directories.

os.path.exists(path) Checks if a path exists (file or directory).

os.path.isdir(path) Checks if a path is a directory.

os.listdir(path) Lists contents of a directory.

os.chdir(path) Changes the current working directory.

os.rmdir(path) Removes an empty directory.

os.removedirs(path) Removes intermediate empty directories.

os.rename(src, dst) Renames or moves a directory.

os.stat(path) Returns the status of a file or directory (permissions, size, etc.).

os.access(path, mode) Checks permissions of a directory.

In [32]: import os
# Create a New Directories

# # Creating nested directories


# os.makedirs('p-dir/ch-dir')
#Check if the directory exits
if os.path.exists('test-dir'):
print("Directory exists.")
else:
os.mkdir('test-dir')
print("Directory does not exist.")

if os.path.isdir('test-dir'):
print("It's a directory.")
else:
print("It's not a directory.")

# List a contents of a directory


contents = os.listdir('data')
for content in contents:
print(content)

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 13/14


12/2/24, 11:50 AM Week6_Baidehi

Directory exists.
It's a directory.
car.data.csv
handwritten-digit-model.keras
housing.csv
marketing-sales.csv
nep-india-china-gdp.csv
nepal-economy.csv
nepal-economy.xlsx
nepal-population-ethinic-group.json
new-text-file-1.txt
new-text-file.txt
product-data.csv
student-admission.csv
student.csv
student.json
student.xlsx
student2.csv
test.json
test.txt
user_data.csv
user_data.json
user_data.xlsx

Extra Question

In [2]: from PIL import Image


import os

# Function to display the image


def display_image(file_path):
try:
# Check if the file exists and is an image
if os.path.isfile(file_path) and file_path.lower().endswith(('png', 'jpg',
image = Image.open(file_path)
image.show() # Display the image
else:
print("The specified file is not an image or doesn't exist.")
except Exception as e:
print(f"An error occurred while displaying the image: {e}")

# Example usage
file_path = "Ball.jpg" # Replace with the correct file path
display_image(file_path)

file:///C:/Users/Asus/Desktop/Python programming/Week6_Baidehi.html 14/14

You might also like