-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.py
More file actions
168 lines (108 loc) · 3.5 KB
/
oop.py
File metadata and controls
168 lines (108 loc) · 3.5 KB
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
import numpy as np
import scipy
#Object Orientated Programming in Python
x = 1
print(type('hello')) #'hello' is an object of the class str
print(type(x)) # x is an object of class int
def hello():
print('hello')
print(type(hello)) #hello is an object of the class 'function'
x = 1
y = 'hello'
# print(x+y) Unsupported operand type for +: 'int' and 'str'
string = 'hello'
print(string.upper()) #upper is a method
print(type(string.upper()))
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def identify(self):
print(self.name, self.age)
def get_age(self):
return self.age
boy = Dog('good', 'boy')
boy.identify()
boy.get_age()
print(boy.get_age())
class Course:
def __init__(self, name, max_students):
self.name = name
self.max_students = max_students
self.students = []
def add_student(self, student):
if len(self.students) < self.max_students:
self.students.append(student)
else:
print('CLASS IS FULL')
physics = Course('Physics', 3)
physics.add_student('Sam Petzold')
physics.add_student('Wee Wee')
physics.add_student('foo foo')
physics.add_student('cha cha')
print('hello world')
print('i love it. (upper)'.upper())
print('i love it. (rjust)'.rjust(20))
print('i love it. '.upper().rjust(20))
print('i love it'.capitalize())
print(' I love it (strip'.strip())
print(f'{print} (print a function)')
print(f'{type(229)} (print a type)')
np.array([1,2])
array_1d = np.array([1,2,3,4])
print(array_1d, type(array_1d))
array_1d = np.array([1,2,3,4])
array_4by1 = np.array([[1,2,3,4]])
large_array = np.array([i for i in range(400)])
large_array = large_array.reshape((20,20))
from_list = np.array([1,2,3])
from_list_2d = np.array([[1,2,3.0], [4,5,6]])
from_list_bad_type = np.array([1,2,3,'a'])
print(f'Data type of integer is {from_list.dtype}')
print(f'Data type of float is {from_list_2d.dtype}')
print(array_1d,array_4by1,large_array,from_list_2d,from_list_bad_type)
print(type(from_list_bad_type))
print('\n\n', array_1d + 5)
print('\n\n', array_1d*5)
print(np.sqrt(array_1d), np.power(array_1d,2), np.exp(array_1d), np.log(array_1d))
array_1d @ array_1d
print(array_1d @ array_1d)
array_1d.dot(array_1d)
print(array_1d.dot(array_1d), np.dot(array_1d, array_1d))
weight_matrix = np.array([1,2,3,4]).reshape(2,2)
sample = np.array([[50,60]]).T
np.matmul(weight_matrix, sample)
print(weight_matrix)
print(sample)
print(np.matmul(weight_matrix, sample))
mat1 = np.array([[1,2], [3,4]])
mat2 = np.array([[5,6], [7,9]])
print(np.matmul(mat1,mat2))
a = np.array([i for i in range(10)])
print(a, a*a, np.multiply(a,10) )
samples = np.random.random((15,5))
print('\n\n', samples)
expanded1 = np.expand_dims(samples, axis=1)
tile1 = np.tile(expanded1, (1, samples.shape[0], 1))
print(tile1)
expanded2 = np.expand_dims(samples, axis=0)
tile2 = np.tile(expanded2, (samples.shape[0], 1, 1))
print(expanded2, tile2)
diff = tile2 - tile1
print(diff)
distances = np.linalg.norm(diff, axis=1)
print(distances)
diff = samples[: , np.newaxis, :] - samples[np.newaxis, :, :]
print(diff)
distances = np.linalg.norm(diff, axis=-1)
print(distances)
# print(distances = np.linalg.norm(diff, axis=-1))
# print(distances = scipy.spatial.distance.cdist(samples, samples))
#vectorizing your code makes it run much faster
a = np.random.random(50000)
b = np.random.random(50000)
dot = 0.0
for i in range(len(a)):
dot += a[i] * b[i]
print(dot)
print(np.array(a).dot(np.array(b)))