Skip to content

Commit 5a26c0d

Browse files
committed
machine learning projects UTA
1 parent 876a91b commit 5a26c0d

33 files changed

+3624
-0
lines changed

Ada_Boosting.ipynb

+235
Large diffs are not rendered by default.

Feed_Forward.ipynb

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Feed_Forward.ipynb",
7+
"provenance": []
8+
},
9+
"kernelspec": {
10+
"name": "python3",
11+
"display_name": "Python 3"
12+
},
13+
"language_info": {
14+
"name": "python"
15+
}
16+
},
17+
"cells": [
18+
{
19+
"cell_type": "code",
20+
"metadata": {
21+
"id": "F-iLrdCBCna3"
22+
},
23+
"source": [
24+
"import numpy as np\n",
25+
"from matplotlib import pyplot as plt"
26+
],
27+
"execution_count": 42,
28+
"outputs": []
29+
},
30+
{
31+
"cell_type": "code",
32+
"metadata": {
33+
"id": "NQ-Eo2eaCzER"
34+
},
35+
"source": [
36+
"#Sigmoid Activation Function\n",
37+
"def sigmoid(x, derive=False):\n",
38+
" if derive:\n",
39+
" return x * (1 - x)\n",
40+
" return 1 / (1 + np.exp(-x))"
41+
],
42+
"execution_count": 43,
43+
"outputs": []
44+
},
45+
{
46+
"cell_type": "code",
47+
"metadata": {
48+
"colab": {
49+
"base_uri": "https://localhost:8080/"
50+
},
51+
"id": "FQQHEwg9C8GZ",
52+
"outputId": "b5c46499-c8ef-4dac-fb3a-dd1cd9eb188a"
53+
},
54+
"source": [
55+
"#Define the Data set OR logic\n",
56+
"#Initialize the 8 samples of the OR function.\n",
57+
"X = np.array([\n",
58+
" [1, 1, 1, 1],\n",
59+
" [1, 1, -1, 1],\n",
60+
" [1, 0, 1, 1],\n",
61+
" [1, -1, -1, 1],\n",
62+
" [-1, 1, 1, 1],\n",
63+
" [-1, 1, -1, 1],\n",
64+
" [-1, -1, 1, 1],\n",
65+
" [-1, -1, -1, 1],\n",
66+
"])\n",
67+
"indices_one = X == -1\n",
68+
"X[indices_one] = 0 # replacing 1s with 0s\n",
69+
"print(X)\n",
70+
"#Define the labels of each sample.\n",
71+
"y = np.array([[1],\n",
72+
" [1],\n",
73+
" [1],\n",
74+
" [1],\n",
75+
" [1],\n",
76+
" [1],\n",
77+
" [1],\n",
78+
" [-1]\n",
79+
" ])\n",
80+
"\n",
81+
"indices_one = y == -1\n",
82+
"y[indices_one] = 0 # replacing 1s with 0s\n",
83+
"print(y)"
84+
],
85+
"execution_count": 38,
86+
"outputs": [
87+
{
88+
"output_type": "stream",
89+
"text": [
90+
"[[1 1 1 1]\n",
91+
" [1 1 0 1]\n",
92+
" [1 0 1 1]\n",
93+
" [1 0 0 1]\n",
94+
" [0 1 1 1]\n",
95+
" [0 1 0 1]\n",
96+
" [0 0 1 1]\n",
97+
" [0 0 0 1]]\n",
98+
"[[1]\n",
99+
" [1]\n",
100+
" [1]\n",
101+
" [1]\n",
102+
" [1]\n",
103+
" [1]\n",
104+
" [1]\n",
105+
" [0]]\n"
106+
],
107+
"name": "stdout"
108+
}
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"metadata": {
114+
"id": "Zdd6KielEfLX"
115+
},
116+
"source": [
117+
"# Define a learning rate\n",
118+
"lr = 0.1\n",
119+
"# Define the number of epochs for learning\n",
120+
"epochs = 3\n",
121+
"\n",
122+
"# Initialize the weights with random numbers\n",
123+
"w01 = np.random.random((len(X[0]), 4)) #Initialize the hidden layer weights with ones. Size of hidden layer is 4.\n",
124+
"w12 = np.random.random((4, 1)) #Initialize the output layer weights with ones. Size of output layer is 1.\n"
125+
],
126+
"execution_count": 44,
127+
"outputs": []
128+
},
129+
{
130+
"cell_type": "code",
131+
"metadata": {
132+
"colab": {
133+
"base_uri": "https://localhost:8080/"
134+
},
135+
"id": "zhVQi-EQE-qu",
136+
"outputId": "b49bf2f5-a3d7-4eb3-f997-32f6a9b11448"
137+
},
138+
"source": [
139+
"# Start feeding forward and backpropagate *epochs* times.\n",
140+
"for epoch in range(epochs):\n",
141+
" # Feed forward\n",
142+
" z_h = np.dot(X, w01) #Calculate the input of the hidden layer zh using matrix multiplication.\n",
143+
" #print(z_h)\n",
144+
" a_h = sigmoid(z_h) #Calculate the output of the hidden layer ah using sigmoid activation function.\n",
145+
" #print(a_h)\n",
146+
"\n",
147+
" z_o = np.dot(a_h, w12) #Calculate the input of the output layer zo using matrix multiplication.\n",
148+
" #print(z_o)\n",
149+
" a_o = sigmoid(z_o) #Calculate the output of the output layer ao using sigmoid activation function.\n",
150+
" #print(a_o)\n",
151+
"\n",
152+
" # Calculate the error\n",
153+
" a_o_error = ((1 / 2) * (np.power((a_o - y), 2))) # Calculate the error of the output neuron ao using squared error function.\n",
154+
" #print(a_o_error)\n",
155+
"\n",
156+
" # Backpropagation\n",
157+
" ## Output layer\n",
158+
" delta_a_o_error = a_o - y #Calculate the derivate of the error of the output layer.\n",
159+
" #print(delta_a_o_error)\n",
160+
" delta_z_o = sigmoid(a_o,derive=True) #Calculate the derivate of the sigmoid function of the output layer.\n",
161+
" #print(delta_z_o)\n",
162+
" delta_w12 = a_h #Calculate the derivate of the input of the output layer zo with respect to the weights w.\n",
163+
" #print(a_h)\n",
164+
" delta_output_layer = np.dot(delta_w12.T,(delta_a_o_error * delta_z_o)) # Calculate the update matrix for the output layer using matrix multiplication and hadamard product.\n",
165+
" #print(delta_output_layer)\n",
166+
"\n",
167+
" ## Hidden layer\n",
168+
" delta_a_h = np.dot(delta_a_o_error * delta_z_o, w12.T) #Calculate the derivate of the Error function E with respect to the output of the hidden layer ah.\n",
169+
" #print(delta_a_h)\n",
170+
" delta_z_h = sigmoid(a_h,derive=True) #Calculate the derivate of the sigmoid of the hidden layer.\n",
171+
" #print(delta_z_h)\n",
172+
" delta_w01 = X #Calculate the derivate of the input of the hidden layer zh with respect to the weight matrix w01.\n",
173+
" #print(X)\n",
174+
" delta_hidden_layer = np.dot(delta_w01.T, delta_a_h * delta_z_h) #Calculate the update matrix for the hidden layer using matrix multiplication and hadamard product.\n",
175+
" #print(delta_hidden_layer)\n",
176+
"\n",
177+
" w01 = w01 - lr * delta_hidden_layer\n",
178+
" w12 = w12 - lr * delta_output_layer\n",
179+
" print(w01)\n",
180+
" print(w12)"
181+
],
182+
"execution_count": 41,
183+
"outputs": [
184+
{
185+
"output_type": "stream",
186+
"text": [
187+
"[[0.06385209 0.09828322 0.62829311 0.62183754]\n",
188+
" [0.52752095 0.93706289 0.92957938 0.16109411]\n",
189+
" [0.74522796 0.35365902 0.1055096 0.33429338]\n",
190+
" [0.96567855 0.52858341 0.41518875 0.51060044]]\n",
191+
"[[0.07433442]\n",
192+
" [0.4339805 ]\n",
193+
" [0.71333701]\n",
194+
" [0.37330689]]\n",
195+
"[[0.06400836 0.09942675 0.62990356 0.6227455 ]\n",
196+
" [0.52764484 0.93788195 0.9309297 0.16207118]\n",
197+
" [0.74535412 0.35477809 0.10745507 0.3353267 ]\n",
198+
" [0.96573524 0.52915359 0.41600555 0.5112246 ]]\n",
199+
"[[0.08688154]\n",
200+
" [0.44605262]\n",
201+
" [0.72589386]\n",
202+
" [0.38487862]]\n",
203+
"[[0.06418173 0.10054108 0.63145583 0.62363204]\n",
204+
" [0.52778183 0.93867695 0.93222662 0.16302311]\n",
205+
" [0.74549411 0.35586926 0.10933259 0.33633675]\n",
206+
" [0.9657873 0.52964381 0.41668266 0.51177708]]\n",
207+
"[[0.09832664]\n",
208+
" [0.45710974]\n",
209+
" [0.73743114]\n",
210+
" [0.39546889]]\n"
211+
],
212+
"name": "stdout"
213+
}
214+
]
215+
}
216+
]
217+
}

Gaussian_Naive_Bayes_Algorithm.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
import math
3+
4+
5+
file = open('TrainingSet.csv', 'r')
6+
training = file.readlines()
7+
for i in range(0, len(training)):
8+
training[i] = training[i][0:-1]
9+
training[i] = training[i].split(',')
10+
11+
MeanHM = 0.0
12+
MeanHW = 0.0
13+
MeanWM = 0.0
14+
MeanWW = 0.0
15+
MeanAM = 0.0
16+
MeanAW = 0.0
17+
18+
VarHM = 0.0
19+
VarHW = 0.0
20+
VarWM = 0.0
21+
VarWW = 0.0
22+
VarAM = 0.0
23+
VarAW = 0.0
24+
25+
TM = 0.0
26+
TW = 0.0
27+
28+
for i in range(0, len(training)):
29+
if training[i][3] == 'M':
30+
TM += 1
31+
MeanHM += int(training[i][0])
32+
MeanWM += int(training[i][1])
33+
MeanAM += int(training[i][1])
34+
else:
35+
TW += 1
36+
MeanHW += int(training[i][0])
37+
MeanWW += int(training[i][1])
38+
MeanAW += int(training[i][1])
39+
40+
PM = TM / len(training)
41+
PW = TW / len(training)
42+
43+
MeanHM = MeanHM / TM
44+
MeanHW = MeanHW / TW
45+
MeanWM = MeanWM / TM
46+
MeanWW = MeanWW / TW
47+
MeanAM = MeanAM / TM
48+
MeanAW = MeanAW / TW
49+
50+
51+
for i in range(0, len(training)):
52+
if training[i][3] == 'M':
53+
VarHM += pow((int(training[i][0]) - MeanHM), 2)
54+
VarWM += pow((int(training[i][1]) - MeanWM), 2)
55+
VarAM += pow((int(training[i][2]) - MeanAM), 2)
56+
else:
57+
VarHW += pow((int(training[i][0]) - MeanHW), 2)
58+
VarWW += pow((int(training[i][1]) - MeanWW), 2)
59+
VarAW += pow((int(training[i][2]) - MeanAW), 2)
60+
61+
VarHM /= (TM-1)
62+
VarWM /= (TM-1)
63+
VarAM /= (TM-1)
64+
VarHW /= (TW-1)
65+
VarWW /= (TW-1)
66+
VarAW /= (TW-1)
67+
68+
69+
CHM = (1 / math.sqrt(2*math.pi*VarHM))
70+
CWM = (1 / math.sqrt(2*math.pi*VarWM))
71+
CAM = (1 / math.sqrt(2*math.pi*VarAM))
72+
CHW = (1 / math.sqrt(2*math.pi*VarHW))
73+
CWW = (1 / math.sqrt(2*math.pi*VarWW))
74+
CAW = (1 / math.sqrt(2*math.pi*VarAW))
75+
76+
77+
CommonM = math.log(PM, math.e)+math.log(CHM, math.e) + \
78+
math.log(CWM, math.e)+math.log(CAM, math.e)
79+
CommonW = math.log(PW, math.e)+math.log(CHW, math.e) + \
80+
math.log(CWW, math.e)+math.log(CAW, math.e)
81+
82+
83+
condition = True
84+
while condition:
85+
M = 0
86+
W = 0
87+
case = input("1: Gaussian Naive Bayes")
88+
if case == '1':
89+
print("Enter data for prediction.")
90+
h = int(input("Height : "))
91+
w = int(input("Weight : "))
92+
a = int(input("Age : "))
93+
94+
exp = (-0.5)*((pow((h-MeanHM), 2)/VarHM) +
95+
(pow((w-MeanWM), 2)/VarWM) + (pow((a-MeanAM), 2)/VarAM))
96+
ProbM = CommonM+exp
97+
98+
exp = (-0.5)*((pow((h-MeanHW), 2)/VarHW) +
99+
(pow((w-MeanWW), 2)/VarWW) + (pow((a-MeanAW), 2)/VarAW))
100+
ProbW = CommonW+exp
101+
102+
if ProbM > ProbW:
103+
print("\n\nClass : M\n\n")
104+
else:
105+
print("\n\nClass : W\n\n")
106+
else:
107+
print("Incorrect choice.")

ID3Algo_entropy.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Reference Credits
2+
# 1) https://www.python-course.eu/Decision_Trees.php
3+
# 2) https://dhirajkumarblog.medium.com/decision-tree-from-scratch-in-python-629631ec3e3a
4+
5+
import math
6+
7+
# Calculates the entropy and frequency of the given data set for the target attribute.
8+
def cal_entropy(data, target_attr):
9+
value_freq = {}
10+
data_entropy = 0.0
11+
for record in data:
12+
# Calculate the frequency of each of the values in the target attr
13+
if record[target_attr] in value_freq:
14+
value_freq[record[target_attr]] += 1.0
15+
else:
16+
value_freq[record[target_attr]] = 1.0
17+
18+
# Calculate the entropy of the data for the target attribute
19+
for freq in value_freq.values():
20+
data_entropy += (-freq/len(data)) * math.log(freq/len(data), 2)
21+
return data_entropy
22+
23+
24+
# Calculates the information gain (reduction in entropy) that would
25+
# result by splitting the data on the chosen attribute (attr).
26+
27+
def info_gain(data, attr, target_attr):
28+
value_freq = {}
29+
sub_entropy = 0.0
30+
31+
# Calculate the frequency of each of the values in the target attribute
32+
for record in data:
33+
if record[attr] in value_freq:
34+
value_freq[record[attr]] += 1.0
35+
else:
36+
value_freq[record[attr]] = 1.0
37+
38+
# Calculate the sum of the entropy for each subset of records weighted
39+
# by their probability of occuring in the training set.
40+
print("For attribute '%s' \nEach value frequency is :%s " % (attr, value_freq))
41+
for value in value_freq.keys():
42+
value_prob = value_freq[value] / sum(value_freq.values())
43+
data_subset = [record for record in data if record[attr] == value]
44+
sub_entropy += value_prob * cal_entropy(data_subset, target_attr)
45+
# Subtract the entropy of the chosen attribute from the entropy of the
46+
# whole data set with respect to the target attribute (and return it)
47+
entropy_gain = (cal_entropy(data, target_attr) - sub_entropy)
48+
print("For Attribute : %s" % attr)
49+
print("Entropy Gain: %f" % entropy_gain)
50+
return entropy_gain

0 commit comments

Comments
 (0)