-
Notifications
You must be signed in to change notification settings - Fork 10
/
program6_LogisticRegression.py
687 lines (482 loc) · 15.4 KB
/
program6_LogisticRegression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import numpy as np
# we now use PyTorch
import torch
# we use Variable
from torch.autograd import Variable
# we use matplotlib for plotting graphs
import matplotlib.pyplot as plt
# we use pandas for .csv files
# we use pandas dataframes
import pandas as pd
# we import our dataset into a pandas dataframe
df = pd.read_csv('Iris.csv')
df[['Species']] = df['Species'].map({'Iris-setosa':0, 'Iris-virginica':1, 'Iris-versicolor':2}) #map text labels to numberical vaules
# we shuffle our dataset
df = df.sample(frac=1)
# this is important for training
# we convert our data into torch tensors
X = torch.Tensor(np.array(df[df.columns[1:-1]])) #pick our features from our dataset
Y = torch.LongTensor(np.array(df[['Species']]).squeeze()) #select our label - squeeze() removes redundant dimensions
# size of the training set
m = 100
# we split our data into training and test set
# training set
x_train = Variable(X[0:m])
y_train = Variable(Y[0:m])
# test set
x_test = Variable(X[m:])
y_test = Variable(Y[m:])
# define model class - inherit useful functions and attributes from torch.nn.Module
class logisticmodel(torch.nn.Module):
def __init__(self):
super().__init__() #call parent class initializer
self.linear = torch.nn.Linear(4, 3) #define linear combination function with 4 inputs and 3 outputs
def forward(self, x):
pred = self.linear(x) #linearly combine our inputs to give 3 outputs
pred = torch.nn.functional.softmax(pred, dim=1) #activate our output neurons to give probabilities of belonging to each of the three class
return pred
# training hyper-parameters
no_epochs = 100
# learning rate, step size
lr = 0.1
#create our model from defined class
mymodel = logisticmodel()
# cross entropy cost function as it is a classification problem
costf = torch.nn.CrossEntropyLoss()
# we use the CE cost function
# we define our optimizer
optimizer = torch.optim.Adam(mymodel.parameters(), lr = lr)
#for plotting costs
costs=[]
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('Epoch')
ax.set_ylabel('Cost')
ax.set_xlim(0, no_epochs)
plt.show()
# training loop - same as last time
for epoch in range(no_epochs):
# forward propagate - calulate our hypothesis
h = mymodel.forward(x_train)
#calculate, plot and print cost
cost = costf(h, y_train)
costs.append(cost.data[0])
ax.plot(costs, 'b')
fig.canvas.draw()
print('Epoch ', epoch, ' Cost: ', cost.data[0])
#calculate gradients + update weights using gradient descent step with our optimizer
optimizer.zero_grad()
cost.backward()
optimizer.step()
# Some laptops are too fast so the plot updates too fast to be visible
# uncomment the following line to fix that problem
#plt.pause(0.0001)
#test accuracy
test_h = mymodel.forward(x_test) #predict probabilities for test set
_, test_h = test_h.data.max(1) #returns the output which had the highest probability
test_y = y_test.data
# perform the element-wise equality operation
correct = torch.eq(test_h, test_y)
# calculate the model's accuracy
accuracy = torch.sum(correct)/correct.shape[0]
# we print the model's accuracy
print('Test accuracy: ', accuracy)
# predict the class of an input using our trained model
inp = [4.6, 3.1, 1.2, 0.3] #define our inputs
inp = Variable(torch.Tensor(inp)) #convert our input to variable
prediction = mymodel.forward(inp) #calculate our output probabilities
_, prediction = prediction.data.max(1) #which class had the highest probability
print(prediction)
# we use: http://interactivepython.org/runestone/static/pythonds/index.html#
# Compute the sum 1/2 + 3/5 + 5/8 + .... for N terms with recursion and with no recursion.
# 1/2 + 3/5 + 5/8 + 7/11 + 9/14 + ....
# sum of 1/2 + 3/5 + 5/8 + 7/11 + 9/14 + .... for N terms with recursion and with no recursion
# sum of N terms with no recursion
def functionSum(n):
sum1 = 0
for i in range(n):
#sum1 += (2*n+1) / (2*n+n+2)
sum1 += (2*i+1) / (2*i+i+2)
return sum1
print(functionSum(1))
print(functionSum(2))
print(functionSum(3))
print(functionSum(4))
print(functionSum(10))
print('')
# sum of N terms with recursion
def functionSum_rec(n):
if n == 1:
return 1/2
#return ((2*(n-1)+1) / (2*(n-1)+(n-1)+2)) + functionSum_rec(n-1)
return ((2*n - 1) / (3*n - 1)) + functionSum_rec(n - 1)
print(functionSum_rec(1))
print(functionSum_rec(2))
print(functionSum_rec(3))
print(functionSum_rec(4))
print(functionSum_rec(10))
print('')
# Find the n-term of the series: a(n) = a(n-1)*2/3 with recursion and with no recursion.
# recursion for a(n) = a(n-1)*2/3
def function1(n):
if n == 0:
return 1
return (2/3) * function1(n-1)
print('')
print(function1(1))
print(function1(2))
print(function1(3))
print(function1(9))
print('')
# no recursion for a(n) = a(n-1)*2/3
def function2(n):
k = 1
for i in range(1,n+1):
k *= 2/3
return k
print('')
print(function2(1))
print(function2(2))
print(function2(3))
print(function2(9))
print('')
# website: http://interactivepython.org/runestone/static/pythonds/index.html#
# we use: http://interactivepython.org/runestone/static/pythonds/BasicDS/toctree.html
# we use lambda expressions in Python
# use: https://docs.python.org/2/reference/expressions.html#lambda
# we use: https://docs.python.org/2/reference/expressions.html
# website: https://docs.python.org/2/reference/expressions.html#lambda
import numpy as np
# we use Python's build-in functions
# use: https://docs.python.org/3/library/functions.html
# we use *args and **kwargs
# https://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
# use one-line code
# write as few lines of code as possible
# use comprehensions
a = [i for i in range(2, 100 + 1, 2)]
print(a)
# we use list comprehensions
a = [i for i in range(1, 101) if i % 2 == 0]
print(a)
# create a generator object, use "(.)"
a = (i for i in range(1, 101) if i % 2 == 0)
# the generator object can be used only once
# the generator object can be used one time only
print(list(a))
print('')
# positional arguments => position matters
# we can call function1 using "function1(y=1, x=2)"
# function with positional arguments x, y
def function1(x, y):
return x - y
# positional arguments: the position matters
print(function1(3, 5))
# named arguments, no matter the order
print(function1(y=3, x=5))
# both positional arguments and named arguments
print(function1(4, y=7))
# in functions, position can matter and can not matter
# positional arguments for function
# positional parameters, function inputs, arguments
print('')
print(max(2,6,9,3))
print(sum([2,6,9,3]))
# functions can have default values
# define a function with default values
def func2(x, y=9, z=1):
# the default value is for z
return (x + y) * z
# If we do not give a value for z, then z=1=(default value)
# we can have default values in functions
# default values go to the end of the arguments
# use: (1) default values, (2) *args, (3) **kwargs
# we use default values, one asterisk (i.e. *) and two asterisks (i.e. **)
# we now use *args and **kwargs
# use: https://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
# default arguments can be only at the end, even more than one
g = func2(2, 5, 7)
print(g)
print('')
for i in range(5):
print(i, "-", i ** 2)
# use *args at the end
# we use un-named arguments *args
# (1) *args at the end of the arguments in a function
# (2) default values at the end of the arguments in a function
# *args must be in the end of the arguments
def apodosi(*apodoseis):
k = 1
for i in apodoseis:
k *= i
return k
# use: (1) *args, and (2) **kwargs
# "**kwargs" is a dictionary dict
# we use keys and values
# "**kwargs" is a dictionary and has keys and values
# **kwargs must be at the end and hence after *args
def apodosi(*apodoseis, **kwargs):
# we use the "max" key in the dictionary
if "max" in kwargs:
n = kwargs["max"]
else:
n = len(apodoseis)
k = 1
for i in range(n):
k *= apodoseis[i]
return k
# **kwargs must be at the end and hence after *args
def apodosi2(*apodoseis, **kwargs):
# we use the "max" key in the dictionary
if "max" in kwargs:
# we use min(., len(apodoseis))
n = min(kwargs["max"], len(apodoseis))
else:
n = len(apodoseis)
k = 1
for i in range(n):
k *= apodoseis[i]
return k
print('')
print(apodosi(1.11, 1.22, 1.31))
print(apodosi2(1.11, 1.22, 1.31))
print('')
m = [2.3, 1.4, 1.8, 1.5, 2.4]
# we use: "*m" amd "myFunction(*m)"
# when we have a list m, then we use "*m" to get its elements
print(apodosi(*m, max=3))
print(apodosi2(*m, max=3))
# use *list1 to break the list
print(apodosi2(*m, max=13))
# the function does not work if we do not use "*"
# use *args and **kwargs in functions
# website: https://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
# use: https://www.geeksforgeeks.org/args-kwargs-python/
# convert to binary
# convert the number n to binary
n = 14
# we use the stack data structure
# define a list that will be used as a stack
stack1 = []
# stack: the last item that enters the stack is the first item out
# the stack data structure is Last In First Out (LIFO)
# the queue data structure is First In First Out (FIFO)
print('')
# every program uses an execution stack
# the execution stack in Python is short
# Every program has a stack that contains the parameters and the local variables of the functions
# that have been called. The stack is LIFO. The last parameter of a function gets out first, i.e. LIFO,
# when many funnctions have been called in a recursion.
# recursion problems
# recursion and memoization
# Fibonacci series and memoization
# the stack overflow error
# stack overflow: when recursion, when the execution stack is full
# we use a while loop
while n != 0:
# d is the last digit
d = n % 2
# print(d)
stack1.insert(0, d)
# we remove the last digit
n = n // 2
# print the elements
for i in stack1:
print(i, end="")
print()
def toBinary(n):
if n == 0:
return
toBinary(n // 2)
print(n % 2, end='')
toBinary(14)
print()
toBinary(14)
print()
# d is the last digit
# d = n % 2
# stack1.insert(0, d)
# we remove the last digit
#n = n // 2
# we use base 8
def toOctal(n):
if n == 0:
return
toOctal(n // 8)
print(n % 8, end='')
# use base 10
def toDecimal(n):
if n == 0:
return
toDecimal(n // 10)
print(n % 10, end='')
# 453%10 = 3 = last digit
# 453//10 = 45 = remove last digit
# x%10 = last digit
# x//10 = remove last digit
# we use base 3
def toTernary(n):
if n == 0:
return
toTernary(n // 3)
print(n % 3, end='')
# sum of N numbers
def sumToN(N):
sum = 0
for i in range(1, N + 1):
sum += i
return sum
# recursion, sum of N numbers
def sumToN_rec(N):
#print(N)
if N == 1:
return 1
# return 1 + sumToN_rec(N-1)
return N + sumToN_rec(N - 1)
print('')
print(sumToN_rec(4))
#print(sumToN_rec(40000))
print(sumToN_rec(40))
# recursion problems
# coding recursion exercises
# programming recursion exercises
# recursion and memoization
# write code with and without recursion
# use one-line code
# lambda expressions => one line only
# comprehensions, list comprehensions => one line only
# use comprehensions: lists or generator objects
# comprehensions with "(.)" => generator objects
# generator objects are created for one time only
# positional arguments
# define functions and call them with positional arguments
# positional arguments or non-positional arguments, default values
# default values go at the end, *args goes at the end
# use *args and **kwargs, **kwargs goes at the end
# use function1(*list1), use "*list1"
# we use "*list1" to break the list to its elements
# dictionary: keys and values
# dictionaries have keys and values
# we use *args and ** kwargs
# website: https://www.geeksforgeeks.org/args-kwargs-python/
# **kwargs => named arguments, dictionary
# dictionary has keys and values
# we use keys as an index to acccess the values
# "if "max" in dict1:": "max" is a key and not a value
# stack data structure => LIFO
# LIFO, last in first out, stack, execution stack
# recursion, memoization, execution stack, stack overflow
# limited stack, limited short execution stack
# recursion, Fibonacci series => stack overflow
# memoization, we use lookup table, memoization to store values
# Find the n-term of the series: a(n) = a(n-1)*2/3 with recursion and with no recursion.
# recursion for a(n) = a(n-1)*2/3
def function1(n):
if n == 0:
return 1
return (2/3) * function1(n-1)
print('')
print(function1(1))
print(function1(2))
print(function1(3))
print(function1(9))
print('')
# no recursion for a(n) = a(n-1)*2/3
def function2(n):
k = 1
for i in range(1,n+1):
k *= 2/3
return k
print('')
print(function2(1))
print(function2(2))
print(function2(3))
print(function2(9))
print('')
# Compute the sum 1/2 + 3/5 + 5/8 + .... for N terms with recursion and with no recursion.
# 1/2 + 3/5 + 5/8 + 7/11 + 9/14 + ....
# sum of 1/2 + 3/5 + 5/8 + 7/11 + 9/14 + .... for N terms with recursion and with no recursion
# sum of N terms with no recursion
def functionSum(n):
sum1 = 0
for i in range(n):
#sum1 += (2*n+1) / (2*n+n+2)
sum1 += (2*i+1) / (2*i+i+2)
return sum1
print(functionSum(1))
print(functionSum(2))
print(functionSum(3))
print(functionSum(4))
print(functionSum(10))
print('')
# sum of N terms with recursion
def functionSum_rec(n):
if n == 1:
return 1/2
#return ((2*(n-1)+1) / (2*(n-1)+(n-1)+2)) + functionSum_rec(n-1)
return ((2*n - 1) / (3*n - 1)) + functionSum_rec(n - 1)
print(functionSum_rec(1))
print(functionSum_rec(2))
print(functionSum_rec(3))
print(functionSum_rec(4))
print(functionSum_rec(10))
# Graphs
# depth first search (DFS)
# DFS => stack => LIFO
def dfs(graph, start):
visited, stack = set(), [start]
while stack:
vertex = stack.pop()
if vertex not in visited:
visited.add(vertex)
stack.extend(graph[vertex] - visited)
return visited
# use depth first search
def dfs_paths(graph, start, goal):
stack = [(start, [start])]
while stack:
(vertex, path) = stack.pop()
for next in graph[vertex] - set(path):
if next == goal:
yield path + [next]
else:
stack.append((next, path + [next]))
# breadth first search (BFS)
# BFS => queue => FIFO
def bfs(graph, start):
'''
help bfs: BFS => queue => FIFO
'''
visited, queue = set(), [start]
while queue:
vertex = queue.pop(0)
if vertex not in visited:
visited.add(vertex)
queue.extend(graph[vertex] - visited)
return visited
# use breadth first search
def bfs_paths(graph, start, goal):
queue = [(start, [start])]
while queue:
(vertex, path) = queue.pop(0)
for next in graph[vertex] - set(path):
if next == goal:
yield path + [next]
else:
queue.append((next, path + [next]))
# create a graph
graph1 = {'A': set(['B', 'C']),
'B': set(['A', 'D', 'E']),
'C': set(['A', 'F']),
'D': set(['D']),
'E': set(['B', 'F']),
'F': set(['C', 'E'])}
print('')
print(dfs(graph1, 'A'))
print(list(dfs_paths(graph1, 'C', 'F')))
print(list(dfs_paths(graph1, 'A', 'F')))
print('')
print(bfs(graph1, 'A'))
print(list(bfs_paths(graph1, 'C', 'F')))
print(list(bfs_paths(graph1, 'A', 'F')))