-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_commands.py
More file actions
76 lines (54 loc) · 2.21 KB
/
Copy pathPython_commands.py
File metadata and controls
76 lines (54 loc) · 2.21 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
# python_commands
import numpy as np
# Indexing arrays lecture 9
Define and array: arr = np.arange(0,11)
Call a single entry: arr[8]
Slice of an array: arr[0:5] retrieves [0,1,2,3,4]
arr[2: ] retrieves 2 - end
arr[: 4] retrieves beginning - 3
Copy and array new_array = arr.copy
2d array arr_2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
Call an entry arr_2d[1][2] = 6 -- row 1, column 2
arr-2d[1,2]
size of an array arr_2d.shape[0] Number of rows
arr_2d.shape[1] Number of columns
Transpose of an array arr_2d.T
arr_2d.swapaxes(0,1)
dot product np.dot(arr1,arr2)
element-wise mult arr1*arr2
add arrays np.add(arr1,arr2)
square root np.sqrt(arr1)
element-wise max
between two arrays np.maximum(A,B)
random normal A = np.random.randn(dimension)
# Condition List
for arrays x and y, and boolean condition we can create the array which
picks between arrays dependent on the condition:
array = [(x if condition else y) for x,y,condition in zip(x,y,condition)]
or we can use
np.where(condition,x,y)
# Matplot lib basics
import matplot.lib.pyplot as plt
% matplotlib inline
domain = np.arange(x_0,x_n,no_of_steps)
dx,dy = np.meshgrid(domain,domain)
z = f(dx,dy)
plt.imshow(z)
plt.colorbar()
plt.title('title')
# Basic stat info for array A
column sum A.sum(0)
row sum A.sum(1)
n-dim sum A.sum(dim)
average A.mean(dim)
standard deviation A.std(dim)
variance A.var(dim)
# Boolean array stuff
True for any instances of true Bo.any()
True only if all are true Bo.True()
# List uniquness/check
list = ['a','b','c','d','d']
Return list of unique values np.unique(list) = ['a','b','c','d']
Check if values of a new list
are contained in another np.in1d(['a','e','b']) = [True, False, True]
# Saving arrays (lecture 13)