程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python numpy array

編輯:Python

1. Array creation :

import numpy
arr = numpy.array([11, 22, 33, 44, 55])
print(arr)
Output :
[11 22 33 44 55]
# Array property view :
print(type(arr))
Output :
<class 'numpy.ndarray'>

2. Array dimensions :

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]])
# View dimension number :
print(arr)
print(arr1)
print(arr2)
print(arr3)
Output :
0
1
1
2

3. Array index :

import numpy
arr = numpy.array([1,2,3,4,5])
arr1 = numpy.array([[11,22],[33,44],[55,66]])
print(arr[3])
Output :
4
print(arr11[2,1])
Output :
66

4. Array iteration :

import numpy
arr = numpy.array([11,22,33])
for i in arr:
print(i)
Output :
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)
Output :
[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)
Output :
1
2
3
4
5
6
7
8
9
10
11
12

5. Array random access :

from numpy import random
# Integer fraction 3 Group 5 individual 
x = random.randint(100,size=(3,5))
# Floating point fractional 3 Group 5 individual 
y = random.rand(3,5)
z = random.choice([11,22,33])
print(x)
Output :
[[83 28 58 86 28]
[74 10 50 64 77]
[74 47 72 52 69]]
print(y)
Output :
[[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)
Output :
22

6. Array merge :

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))
Output :
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
print(numpy.stack((arr8,arr9),axis=1))
Output :
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
print(numpy.vstack((arr8,arr9)))
Output :
[[ 1 2 3]
[ 7 8 9]
[ 4 5 6]
[10 11 12]]
print(numpy.dstack((arr8,arr9)))
Output :
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[ 7 10]
[ 8 11]
[ 9 12]]]
print(numpy.concatenate((arr8,arr9)))
Output :
[[ 1 2 3]
[ 7 8 9]
[ 4 5 6]
[10 11 12]]
print(numpy.hstack((arr8,arr9)))
Output :
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]

7. Array splitting :

import numpy
arr = numpy.array([1,2,3,4,5,6,7,8])
print(numpy.array_split(arr,2))
Output :
[array([1, 2, 3, 4]), array([5, 6, 7, 8])]

8. Array filtering 、 Sort :

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)
Output :
[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))
Output :
[[ 4 6 7]
[10 12 17]]]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved