Reputation: 51
I am new in machine learning as well as python programming, I want to implement algorithm from scratch using numpy only which is perceptron learning rule on linnerud dataset.
If perceptron does not converge will need to run for 1000 iterations and also need to test this algorithm on the Linnerrud dataset using all 3 attributes, and only the chinups outcome.
Vector needs to be defined as binary classes to the outcome of chinups as follows:
if(chinups>median(chinups)) then chinups=0 else chinups=1
I need to use these classes (0/1) to train the perceptron and build the probability table. Eventually I need 20 prediction values output by perceptron where each value is a weighted sum (dot product of perceptron’s weights with attribute values)
What I am getting seems to be incorrect. If anyone could possibly help me with it would be appreciable. Following is my code :
import numpy as np
import matplotlib.pyplot as plot
import os
from numpy import arange
from sklearn.datasets import load_linnerud
def perceptron(linnerud):
pathName = os.path.dirname(os.path.abspath(__file__))
myfile = open(pathName+'\perceptron_results.txt', 'w')
data = linnerud['data']
target = linnerud['target']
chinup = []
for i in target:
chinup.append(i[0])
median = np.sum(chinup) / 20
#print(median)
binary = []
for j in chinup:
#print(j)
if j > median:
binary.append(0)
else:
binary.append(1)
weights = np.zeros([3,1])
iteration = 1000
for i in arange(0,iteration):
counter = 0
converged = True
for rowVal in data:
predVal = np.dot(rowVal,weights)
if predVal < 0:
predicted = 0
else:
predicted = 1
if predicted != binary[counter]:
converged = False
if binary[counter] == 0 :
weights = weights - np.expand_dims(rowVal,1)
else:
weights = weights + np.expand_dims(rowVal,1)
counter = counter + 1
if converged == True:
print("Error occurred")
break
finalPred = np.dot(data,weights)
myfile.write(str(finalPred)+'\n')
#print(finalPred)
print("\nProbability values appended in gnb_result,txt file")
plot.plot(finalPred,'bo');
plot.plot([0,20],[0,0])
plot.show()
My Output is this:
[[ 2845.]
[ -2316.]
[ -1906.]
[ 8874.]
[ 8926.]
[ 1693.]
[ 5421.]
[ 4877.]
[ 15905.]
[-14406.]
[ 13369.]
[ 2546.]
[ 5238.]
[ -4733.]
[ 3337.]
[ 954.]
[ 2243.]
[ 7887.]
[ 11835.]
[ 489.]]
Upvotes: 2
Views: 303
Reputation: 51
It works for me after modifying few parts in code
import numpy as np
import matplotlib.pyplot as plot
import os
from numpy import arange
from sklearn.datasets import load_linnerud
dataN = dataset.get('data')
target = dataset.get('target')
chinup = []
for i in dataN:
chinup.append(i[0])
median = np.median(chinup)
binary = []
for j in chinup:
#print(j)
if j > median:
binary.append(0)
else:
binary.append(1)
weights = np.zeros([3,1])
iteration = 1000
for i in arange(0,iteration):
counter = 0
converged = True
for row_val in target:
pred_val = np.dot(row_val,weights)
if pred_val < 0:
predicted = 0
else:
predicted = 1
if predicted != binary[counter]:
converged = False
if binary[counter] == 0 :
weights = weights - np.expand_dims(row_val,1)
else:
weights = weights + np.expand_dims(row_val,1)
counter = counter + 1
if converged == True:
print("Loop broken")
break
final_pred = np.dot(target,weights)
plot.plot(final_pred,'bo');
plot.plot([0,20],[0,0])
plot.show()
[output][1] [1]: https://i.sstatic.net/6vA5M.png
Upvotes: 1
Reputation: 1
median = np.sum(chinup) / 20 --> 9.45 (it is mean)
median = np.median(chinup) -->11.5 (it is median)
median is not mean, they are different
and you plot plot.plot([0,20],[0,0])
, why this linear regression is put at this position of this case? and it has with 0 slope? or you just draw it with your idea...
Upvotes: 0
Reputation: 1
for i in target: chinup.append(i[0]) median = np.median(chinup) this median is 176,it is wrong
should be: for i in data: chinup.append(i[0]) median = np.median(chinup) this median is 11.5
in load_linnerud dataset: 'target_names': ['Weight', 'Waist', 'Pulse'],and 'feature_names': ['Chins', 'Situps', 'Jumps'] if you wanna get "if(chinups>median(chinups)) then chinups=0 else chinups=1", you should use "for i in data:"
Upvotes: 0