0% found this document useful (0 votes)
20 views2 pages

Lab 14 Task 1

The document contains a Python script that implements a simple machine learning model using a perceptron algorithm. It splits a dataset into training and testing sets, updates weights based on predictions, and evaluates accuracy on the training data. The script includes error handling and prints the updated weights and predictions for both training and testing datasets.

Uploaded by

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

Lab 14 Task 1

The document contains a Python script that implements a simple machine learning model using a perceptron algorithm. It splits a dataset into training and testing sets, updates weights based on predictions, and evaluates accuracy on the training data. The script includes error handling and prints the updated weights and predictions for both training and testing datasets.

Uploaded by

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

task_1

import numpy as n
from sklearn.model_selection import train_test_split

dataset=[[1,0,0,1],[1,0,0,1],[1,1,0,1],[1,1,1,0]]
W=[w0,w1,w2]=[0,0,0]
threshold=0.5
learning_rate=0.1
addition=[]
x_train_data,x_test_data=train_test_split(dataset,test_size=0.2,random_state=1)
print('x train',x_train_data)
print('x test',x_test_data)
error=[]
iterations=5
label=[]
f=[]
classes=[]
feature=[]
feature_matrix=[]
total_classes=[]
predicted_class=[]
accuracy_training=0
train_data_p=[]
weight=[]

for iter in range(iterations=5):


for sample in x_train_data:
feature=sample[0:len(x_train_data)-1]
classes = sample[-1]
o=n.dot(W,feature)
if o>=threshold:
o=1
predicted_class=o
else:
o=0
predicted_class=o
error=(learning_rate)*(classes-o)
for j in range(len(W)):
W[j]=W[j]+(feature[j]*(error))
print('W',W)
if W not in weight:
weight.append(update_w)
else:
print('UPDATED WEIGHT = ', W)
break

for i in x_test_data:
x = i[0:3]
y = i[-1]
o2=n.dot(update_w,x)
if o2 >= threshold:
o2 = 1
predict_train_data = o2
if(predict_train_data==y):
print("predicted")
else:
print("not pred")
else:
o2 = 0
predict_train_data = o2
if(predict_train_data==y):
print("predicted")
else:
print("not pred")
for k in x_train_data:
s=k[0:3]
z=k[-1]
o1 = n.dot(update_w, s)
if o1 >= threshold:
o1 = 1
predict_train_data = o1
train_data_p.append(predict_train_data)
if predict_train_data==classes:
accuracy_training = accuracy_training + 1
else:
accuracy_training = accuracy_training + 0

else:
o1 = 0
predict_train_data = o1
train_data_p.append(predict_train_data)
if predict_train_data==classes:
accuracy_training = accuracy_training + 1
else:
accuracy_training = accuracy_training + 0
if (accuracy_training/len(train_data_p))==1:
print('accurate pred')
break
else:
continue
break

You might also like