Implement the PLA algorithm using Python (Only language I know).
Generate linearly separable data using at least 20 points with two features, it is not required that the points be evenly divided between the positive and negative class.
Run and test your code.
Please show working code with explanations to help me understand how to implement my own.
P.S. You guys are awesome
Regarding Comment: "No information providing regarding dataset, features" It is a data set of nothing in particular, just numbers on their own, and each point must have 2 features.
import numpy as np
from math import e
epoches=100 #no of epoches for
training
lr=0.1
# learning rate
def sigmoid(z): # sigmoid activation function
return 1/(1+e**(-z))
signum = lambda x : 1 if(x>=0) else 0 # signum function for rounding the perceptron result
# 20 linearly seperable 2D points
x=np.array([[[0,0]],[[0,1]],[[0,2]],[[0,3]],[[0,4]],[[1,0]],[[2,0]],[[3,0]],[[4,0]],[[5,0]],[[1,5]],[[2,5]],[[3,5]],[[4,5]],[[5,5]],[[6,0]],[[6,1]],[[6,2]],[[6,3]],[[6,4]]])
w=np.zeros((1,2)) # initial weights
# desired output
d=np.array([[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[1]],[[1]],[[1]],[[1]],[[1]],[[1]],[[1]],[[1]],[[1]],[[1]]])
b=np.array([-1]) # bias
print("Output before training.")
for i in
range(x.shape[0]):
# printing the output before training the perceptron
print(x[i],signum(np.matmul(x[i],w.reshape(2,1))+b)) #
all outputs are 0 because weights are initialied with 0
# function for training perceptron using stocastic gradient
descent
def inference(x,w,d,b):
for i in range(epoches):
z=np.matmul(x,w.reshape(2,1))+b
#weighted sum of inputs
y=sigmoid(z) # applying activation
function
for j in
range(x.shape[0]):
w=w+lr*x[j]*y[j]*(1-y[j])*(d[j]-y[j]) # updating weights using
squared error loss function
b=b+lr*y[j]*(1-y[j])*(d[j]-y[j])
# updating bias
print("Output after training.")
for i in
range(x.shape[0]):
# printing final output after training perceptron
print(x[i],signum(np.matmul(x[i],w.reshape(2,1))+b))
inference(x,w,d,b) # calling the inference function
##############################################################################################
if you want to plot the 2D points add the below code lines to your code:
from matplotlib import pyplot as plt
plt.scatter(x[:10].reshape(10,2)[:,0],
x[:10].reshape(10,2)[:,1],label = '0')
plt.scatter(x[10:].reshape(10,2)[:,0],
x[10:].reshape(10,2)[:,1],label = '1')
plt.show()
Implement the PLA algorithm using Python (Only language I know). Generate linearly separable data using at...
Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm to solve a classification problem. The kNN is a simple and robust classifier, which is used in different applications. The goal is to train kNN algorithm to distinguish the species from one another. The dataset can be downloaded from UCI Machine Learning Repository: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/ (Links to an external site.)Links to an external site.. Download `iris.data` file from the Data Folder. The Data Set description...
Python. Just work in the def sierpinski. No output needed. Will give thumbs up for any attempt beginning this code. Your task is to implement this algorithm in Python, returning a random collection of inum-100, 000 points. You should then plot the points to see the structure. Please complete the following function: def sierpinski (po, v, f, inum) The four arguments are ·po the initial point. You may assume this is the origin, i.e., po = [0, 0] . v:...