numpy2

NumPy Introduction 2

function

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np  
np.random.seed(0)

def compute_reciprocals(values):
output = np.empty(len(values)) #set up a new array
for i in range(len(values)):
output[i] = 1.0 / values[i] #put 1/value into it
return output
values = np.random.randint(1, 10, size=5)
compute_reciprocals(values)

#Out:array([ 0.16666667, 1. , 0.25 , 0.25 , 0.125 ])

A faster way

1
2
3
big_array = np.random.randint(1, 100, size=1000000)  
%timeit compute_reciprocals(big_array)
#out:1 loop, best of 3: 2.91 s per loop

prove:

1
2
3
4
5
6
7
print(compute_reciprocals(values)) 
print(1.0 / values)
'''
out:
[ 0.16666667 1. 0.25 0.25 0.125 ]
[ 0.16666667 1. 0.25 0.25 0.125 ]
'''

*arange calculation

1
2
np.arange(5) / np.arange(1, 6)  #[0 1 2 3 4] [1 2 3 4 5]
#Out: array([ 0. , 0.5 , 0.66666667, 0.75 , 0.8 ])
1
2
3
4
5
6
7
x = np.arange(9).reshape((3, 3))  
2 ** x
'''
Out: array([[ 1, 2, 4],
[ 8, 16, 32],
[ 64, 128, 256]])
'''

add,subtract,multiply,divide

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
x = np.arange(4)  
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
print("x / 2 =", x / 2)
print("x // 2 =", x // 2)
'''
x = [0 1 2 3]
x + 5 = [5 6 7 8]
x - 5 = [-5 -4 -3 -2]
x * 2 = [0 2 4 6]
x / 2 = [ 0. 0.5 1. 1.5]
x // 2 = [0 0 1 1]
'''

others

1
2
3
4
5
6
7
8
print("-x = ", -x)  
print("x ** 2 = ", x ** 2)
print("x % 2 = ", x % 2)
'''
-x = [ 0 -1 -2 -3]
x ** 2 = [0 1 4 9]
x % 2 = [0 1 0 1]
'''

get them together

1
2
3
4
 -(0.5*x + 1) ** 2  
'''
Out: array([-1. , -2.25, -4. , -6.25])
'''

.add()

1
2
np.add(x, 2)  #add 2 to every value
#Out: array([2, 3, 4, 5])
运算符 对应的通用函数 描述 example
+ np.add 加法运算 (即 1 + 1 = 2)
- np.subtract 减法运算 (即 3 - 2 = 1)
- np.negative 负数运算 (即 -2)
* np.multiply 乘法运算 (即 2 * 3 = 6)
/ np.divide 除法运算 (即 3 / 2 = 1.5)
// np.floor_divide 地板除法运算 (flfloor division,即 3 // 2 = 1)
** np.power 指数运算 (即 2 ** 3 = 8)
% np.mod 模 / 余数 (即 9 % 4 = 1)

abs() absolute()

1
2
3
4
5
6
x = np.array([-2, -1, 0, 1, 2])  
abs(x)
#Out: array([2, 1, 0, 1, 2])

np.absolute(x)
#Out[12]: array([2, 1, 0, 1, 2])
1
2
3
4
5
6
7
8
9
10
11
12
13
theta = np.linspace(0, np.pi, 3)

print("theta = ", theta)
print("sin(theta) = ", np.sin(theta))
print("cos(theta) = ", np.cos(theta))
print("tan(theta) = ", np.tan(theta))

'''
theta = [ 0. 1.57079633 3.14159265]
sin(theta) = [ 0.00000000e+00 1.00000000e+00 1.22464680e-16]
cos(theta) = [ 1.00000000e+00 6.12323400e-17 -1.00000000e+00]
tan(theta) = [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]
'''

reverse:

1
2
3
4
5
6
7
8
9
10
11
x = [-1, 0, 1]  
print("x = ", x)
print("arcsin(x) = ", np.arcsin(x))
print("arccos(x) = ", np.arccos(x))
print("arctan(x) = ", np.arctan(x))
'''
x = [-1, 0, 1]
arcsin(x) = [-1.57079633 0. 1.57079633]
arccos(x) = [ 3.14159265 1.57079633 0. ]
arctan(x) = [-0.78539816 0. 0.78539816]
'''

log and ln

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
x = [1, 2, 3]  
print("x =", x)
print("e^x =", np.exp(x))
print("2^x =", np.exp2(x))
print("3^x =", np.power(3, x))
'''
x = [1, 2, 3]
e^x = [ 2.71828183 7.3890561 20.08553692]
2^x = [ 2. 4. 8.]
3^x = [ 3 9 27]
'''


x = [1, 2, 4, 10]
print("x =", x)
print("ln(x) =", np.log(x))
print("log2(x) =", np.log2(x))
print("log10(x) =", np.log10(x))
'''
x = [1, 2, 4, 10]
ln(x) = [ 0. 0.69314718 1.38629436 2.30258509]
log2(x) = [ 0. 1. 2. 3.32192809]
log10(x) = [ 0. 0.30103 0.60205999 1. ]
'''


#It can be very accurate
x = [0, 0.001, 0.01, 0.1]
print("exp(x) - 1 =", np.expm1(x))
print("log(1 + x) =", np.log1p(x))
'''
exp(x) - 1 = [ 0. 0.0010005 0.01005017 0.10517092]
log(1 + x) = [ 0. 0.0009995 0.00995033 0.09531018]
'''

more advanced

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x = np.arange(5)  #from 0 to 4
y = np.empty(5)
np.multiply(x, 10, out=y)
print(y)

#[ 0. 10. 20. 30. 40.]


y = np.zeros(10)
np.power(2, x, out=y[::2])
print(y)

#[ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]

y[::2] = 2 ** x
y
#array([ 1., 0., 2., 0., 4., 0., 8., 0., 16., 0.])

reduce()

1
2
3
4
5
6
7
8
x = np.arange(1, 6)  #[1 2 3 4 5]
np.add.reduce(x) #1+2+3+4+5
#Out: 15

np.add.accumulate(x) #show all the steps
#Out: array([ 1, 3, 6, 10, 15])
np.multiply.accumulate(x)
#Out: array([ 1, 2, 6, 24, 120])

outer()

1
2
3
4
5
6
7
8
9
10
x = np.arange(1, 6)  
np.multiply.outer(x, x)
'''
Out:
array([[ 1, 2, 3, 4, 5],
[ 2, 4, 6, 8, 10],
[ 3, 6, 9, 12, 15],
[ 4, 8, 12, 16, 20],
[ 5, 10, 15, 20, 25]])
'''

sum()

1
2
3
4
5
6
L = np.random.random(100)  
sum(L)
#Out: 55.61209116604941

np.sum(L)
#Out: 55.612091166049424

*faster way:

1
2
3
big_array = np.random.rand(1000000)  
%timeit sum(big_array)
%timeit np.sum(big_array)

python has its own function min and max, but numpy’s is faster.

1
2
np.min(big_array), np.max(big_array)  
#Out: (1.1717128136634614e-06, 0.9999976784968716)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
M = np.random.random((3, 4))  
print(M)
'''
out:
[[ 0.8967576 0.03783739 0.75952519 0.06682827]
[ 0.8354065 0.99196818 0.19544769 0.43447084]
[ 0.66859307 0.15038721 0.37911423 0.6687194]]
'''

M.sum()
#Out: 6.0850555667307118

M.min(axis=0) #perpendicular is 0
#Out: array([ 0.66859307, 0.03783739, 0.19544769, 0.06682827])

M.max(axis=1)
#Out: array([ 0.8967576 , 0.99196818, 0.6687194])

Ul4gOS.png

example: (USA presidents’ height)

data: (.csv)

order,name,height(cm)

1,George Washington,189

2,John Adams,170

3,Thomas Jefferson,189

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
import numpy as np
import pandas as pd #pandas is used to deal with csv files
data = pd.read_csv('C:\\Users\\huawei\\ipy\\president_heights.csv')
heights = np.array(data['height(cm)']) #put height in array
print(heights)

#[189 170 189 163 183 171 185 168 173 183 173 173 175 178 183 193 178 173 174 183 183 168 170 178 182 180 183 178 182 188 175 179 183 193 182 183 177 185 188 188 182 185]


#find different factors in the data
print("Mean height: ", heights.mean())
print("Standard deviation:", heights.std())
print("Minimum height: ", heights.min())
print("Maximum height: ", heights.max())
'''
Mean height: 179.738095238
Standard deviation: 6.93184344275
Minimum height: 163
Maximum height: 193
'''

print("25th percentile: ", np.percentile(heights, 25))
print("Median: ", np.median(heights))
print("75th percentile: ", np.percentile(heights, 75))
'''
25th percentile: 174.25
Median: 182.0
75th percentile: 183.0
'''

%matplotlib inline #matplotlib is used to plot graghs(matrix plot library)
import matplotlib.pyplot as plt #(python plot)
import seaborn; seaborn.set() # 设置绘图风格
plt.hist(heights) #choose histogram to plot heights
plt.title('Height Distribution of US Presidents') #print the title
plt.xlabel('height (cm)') #print labels
plt.ylabel('number');
UlH7lR.png