-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numpy.py
140 lines (114 loc) · 2.6 KB
/
Numpy.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
# import package numpy
# Appending Elements to Arrays
import numpy as np
arr1 = np.array([1,2,3])
# these do not work:
#arr1.append(4)
#arr1 = arr1 + [5]
arr1 = np.append(arr1,4)
arr1 = np.append(arr1,[5])
for e in arr1:
print(e)
arr2 = arr1 + arr1
for e in arr2:
print(e)
# Appending Elements to Arrays
import numpy as np
arr1 = np.array([1,2,3])
arr1 = np.append(arr1,4)
for e in arr1:
print(e)
arr1 = np.array([1,2,3])
arr1 = np.append(arr1,4)
arr2 = arr1 + arr1
for e in arr2:
print(e)
# Multiply Lists and Arrays
import numpy as np
list1 = [1,2,3]
arr1 = np.array([1,2,3])
print('list: ',list1)
print('arr1: ',arr1)
print('2*list:',2*list)
print('2*arr1:',2*arr1)
# Doubling the Elements in a List
import numpy as np
list1 = [1,2,3]
list2 = []
for e in list1:
list2.append(2*e)
print('list1:',list1)
print('list2:',list2)
import numpy as np
list1 = [1,2,3]
list2 = []
for e in list1:
list2.append(e*e) # e*e = squared
print('list1:',list1)
print('list2:',list2)
# Arrays and Exponents
import numpy as np
arr1 = np.array([1,2,3])
arr2 = arr1**2
arr3 = arr1**3
print('arr1:',arr1)
print('arr2:',arr2)
print('arr3:',arr3)
# Math Operations and Arrays
import numpy as np
arr1 = np.array([1,2,3])
sqrt = np.sqrt(arr1)
log1 = np.log(arr1)
exp1 = np.exp(arr1)
print('sqrt:',sqrt)
print('log1:',log1)
print('exp1:',exp1)
# Working with “-1” Subranges with Vectors
import numpy as np
# -1 => "all except the last element in …" (row or col)
arr1 = np.array([1,2,3,4,5])
print('arr1:',arr1)
print('arr1[0:-1]:',arr1[0:-1])
print('arr1[1:-1]:',arr1[1:-1])
print('arr1[::-1]:', arr1[::-1]) # reverse!
# Working with “-1” Subranges with Arrays
import numpy as np
# -1 => "the last element in …" (row or col)
arr1 = np.array([(1,2,3),(4,5,6),(7,8,9),(10,11,12)])
print('arr1:', arr1)
print('arr1[-1,:]:', arr1[-1,:])
print('arr1[:,-1]:', arr1[:,-1])
print('arr1[-1:,-1]:',arr1[-1:,-1])
# Arrays and Vector Operations
import numpy as np
a = np.array([[1,2], [3, 4]])
b = np.array([[5,6], [7,8]])
print('a: ', a)
print('b: ', b)
print('a + b: ', a+b)
print('a _ b: ', a_b)
print('a * b: ', a*b)
print('a / b: ', a/b)
print('b / a: ', b/a)
print('a.dot(b):',a.dot(b))
# NumPy and Dot Products (1)
import numpy as np
a = np.array([1,2])
b = np.array([2,3])
dot2 = 0
for e,f in zip(a,b):
dot2 += e*f
print('a: ',a)
print('b: ',b)
print('a*b: ',a*b)
print('dot1:',a.dot(b))
print('dot2:',dot2)
import numpy as np
a = np.array([1,2])
b = np.array([2,3])
print('a: ',a)
print('b: ',b)
print('a.dot(b): ’,a.dot(b))
print('b.dot(a): ',b.dot(a))
print('np.dot(a,b):',np.dot(a,b))
print('np.dot(b,a):',np.dot(b,a))