1.数组创建:
import numpy
arr = numpy.array([11, 22, 33, 44, 55])
print(arr)
输出:
[11 22 33 44 55]
#数组属性查看:
print(type(arr))
输出:
<class 'numpy.ndarray'>
2.数组维度:
import numpy
arr = numpy.array(10)
arr1 = numpy.array([1, 2, 3, 4, 5])
arr2 = numpy.array((1,2,3,4,5))
arr3 = numpy.array([[8,9,10],[1,2,3],[4,5,6]])
#查看维度数:
print(arr)
print(arr1)
print(arr2)
print(arr3)
输出:
0
1
1
2
3.数组索引:
import numpy
arr = numpy.array([1,2,3,4,5])
arr1 = numpy.array([[11,22],[33,44],[55,66]])
print(arr[3])
输出:
4
print(arr11[2,1])
输出:
66
4.数组迭代:
import numpy
arr = numpy.array([11,22,33])
for i in arr:
print(i)
输出:
11
22
33
arr0 = numpy.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr0:
print(x)
for y in x:
print(y)
for z in y:
print(z)
输出:
[1 2 3]
1
2
3
[4 5 6]
4
5
6
[[ 7 8 9]
[10 11 12]]
[7 8 9]
7
8
9
[10 11 12]
10
11
12
for k in numpy.nditer(arr0):
print(k)
输出:
1
2
3
4
5
6
7
8
9
10
11
12
5.数组随机取数:
from numpy import random
#整数分3组5个
x = random.randint(100,size=(3,5))
#浮点数分3组5个
y = random.rand(3,5)
z = random.choice([11,22,33])
print(x)
输出:
[[83 28 58 86 28]
[74 10 50 64 77]
[74 47 72 52 69]]
print(y)
输出:
[[0.62403419 0.28132926 0.39252042 0.24294266 0.32071179]
[0.00681339 0.22880091 0.96694071 0.72081899 0.52969825]
[0.89168273 0.44716415 0.97038642 0.39105253 0.58724623]]
print(z)
输出:
22
6.数组合并:
import numpy
arr8 = numpy.array([[1, 2, 3],[7,8,9]])
arr9 = numpy.array([[4, 5, 6],[10,11,12]])
arr10 = numpy.array([[4, 7, 6],[10,17,12]])
print(numpy.concatenate((arr8,arr9),axis=1))
输出:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
print(numpy.stack((arr8,arr9),axis=1))
输出:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
print(numpy.vstack((arr8,arr9)))
输出:
[[ 1 2 3]
[ 7 8 9]
[ 4 5 6]
[10 11 12]]
print(numpy.dstack((arr8,arr9)))
输出:
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[ 7 10]
[ 8 11]
[ 9 12]]]
print(numpy.concatenate((arr8,arr9)))
输出:
[[ 1 2 3]
[ 7 8 9]
[ 4 5 6]
[10 11 12]]
print(numpy.hstack((arr8,arr9)))
输出:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
7.数组拆分:
import numpy
arr = numpy.array([1,2,3,4,5,6,7,8])
print(numpy.array_split(arr,2))
输出:
[array([1, 2, 3, 4]), array([5, 6, 7, 8])]
8.数组过滤、排序:
import numpy
arr = numpy.array([1,2,3,4,5,6,7,8])
new_arr = []
for i in arr:
if i%2 == 0:
new_arr.append(True)
else:
new_arr.append(False)
N_arr = arr[new_arr]
print(new_arr)
print(N_arr)
输出:
[False, True, False, True, False,rue, False, True]
[2 4 6 8]
arr0 = numpy.array([[4, 7, 6],[10,17,12]])
print(numpy.sort(arr0))
输出:
[[ 4 6 7]
[10 12 17]]]