1
2
#numpy的使用
import numpy as np
1
2
data1 = [6,7.5,8,0,1]
arr1 = np.array(data1)
1
2
data2 = [[1,2,3,4],[5,6,7,8]]
arr2 = np.array(data2)
1
2
3
4
print(arr2.ndim)
print(arr2.shape)
print(arr1.dtype)
print(arr2.dtype)
2
(2, 4)
float64
int64
1
2
#数组初始化
np.zeros(10)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
1
2
#多维数组
np.zeros((3,6))
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])
1
np.ones((2,2))
array([[ 1.,  1.],
       [ 1.,  1.]])
1
2
#empty返回的并非全零
np.empty((2,3))
array([[  0.00000000e+000,   2.32035477e+077,   2.14249175e-314],
       [  2.15324591e-314,   8.50045777e-096,   4.17201433e-309]])
1
2
3
4
5
#与标量进行计算
print(arr1)
print(arr1**0.5)
print(arr1/2)
print(arr1)
[ 6.   7.5  8.   0.   1. ]
[ 2.44948974  2.73861279  2.82842712  0.          1.        ]
[ 3.    3.75  4.    0.    0.5 ]
[ 6.   7.5  8.   0.   1. ]
1
2
3
4
#取元素
print(arr2[1])
print(arr2[1,2])
print(arr2[1][2])
[5 6 7 8]
7
7
1
2
3
4
5
6
#修改元素
print(arr2[1,2])
old_value = arr2[1,2]
arr2[1,2]=10
print(arr2)
print(old_value)
7
[[ 1  2  3  4]
 [ 5  6 10  8]]
7
1
2
3
4
#多轴切片
print(arr2)
print(arr2[:1])
print(arr2[:1,2:])
[[ 1  2  3  4]
 [ 5  6 10  8]]
[[1 2 3 4]]
[[3 4]]
1
2
3
4
5
6
7
8
9
#布尔索引
names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
data = np.random.randn(7,4) #正态分布随机数7行,行数与names的元素个数相同
print(data)
mask = names == 'Bob' #布尔数组
print(mask)
print(data[mask]) #利用布尔数组切片,取mask中为True的值所对应的行
print(data[names=='Bob',2:])

[[ 0.01189226 -0.09766365 -0.28287861  0.67107291]
 [-0.90166172 -0.17293546  0.26721339 -0.16874216]
 [ 1.12201125  0.98301059 -1.65128433  0.48168967]
 [ 1.13573473  1.20286523  0.6301167   0.48295353]
 [ 0.22856006  0.96182658 -1.43194442  0.00415171]
 [ 1.0237387   0.22361819  1.23276475 -0.50240715]
 [-0.22619522  1.32128889  0.44926209  0.71592239]]
[ True False False  True False False False]
[[ 0.01189226 -0.09766365 -0.28287861  0.67107291]
 [ 1.13573473  1.20286523  0.6301167   0.48295353]]
[[-0.28287861  0.67107291]
 [ 0.6301167   0.48295353]]
1
2
3
4
5
#使用布尔条件过滤并设置值
data[data<0] = 0
print(data)


[[ 0.02538029  0.86959398  0.          0.        ]
 [ 0.          0.          0.          0.        ]
 [ 0.99970368  0.          0.          1.23342   ]
 [ 1.5112379   0.          0.2387815   0.        ]
 [ 0.          0.0224426   0.          0.        ]
 [ 0.61640087  0.          0.81184444  0.        ]
 [ 0.          0.73155     0.          0.        ]]
1
2
3
4
5
6
7
#花式索引 Fancy indexing
arr = np.empty((8,4))

for i in range(8):
    arr[i] = i

print(arr)
[[ 0.  0.  0.  0.]
 [ 1.  1.  1.  1.]
 [ 2.  2.  2.  2.]
 [ 3.  3.  3.  3.]
 [ 4.  4.  4.  4.]
 [ 5.  5.  5.  5.]
 [ 6.  6.  6.  6.]
 [ 7.  7.  7.  7.]]
1
2
3
#单数组花式索引
print(arr[[4,3,0,6]]) #花式索引
print(arr[[-3,-5,-7]]) #花式索引,负数
[[ 4.  4.  4.  4.]
 [ 3.  3.  3.  3.]
 [ 0.  0.  0.  0.]
 [ 6.  6.  6.  6.]]
[[ 5.  5.  5.  5.]
 [ 3.  3.  3.  3.]
 [ 1.  1.  1.  1.]]
[[ 0  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]]
[ 4 23 29 10]
1
2
3
4
#多数组花式索引
arr = np.arange(32).reshape((8,4))
print(arr)
print(arr[[1,5,7,2],[0,3,1,2]]) #2行第1个,6行第8个,8行第2个,3行的3个
[[ 0  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]]
[ 4 23 29 10]