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

Python playful data 1 - numpy ndarray attributes multidimensional array attribute ndim shape size axis

編輯:Python

introduction

This paper mainly introduces NumPy Module and its multidimensional array properties , more Pyton Advanced Series , Please refer to Python Advanced learning Play data series

Summary :

  1. NumPy Module import
  2. NumPy Multidimensional array properties

NumPy Module import

NumPy yes Numerical Python Abbreviation , For more details, please refer to NumPy Official website .
Import NumPy Module and check Its version : Usually np As NumPy Another name for .

import numpy as np
version = np.__version__
print(version)

Output :

1.19.1

NumPy Multidimensional array properties

.ndim: Dimension of array
.size: The total number of array elements or the number of elements of a specific dimension of the array
.shape: It's a tuple , The elements in the code are the number of each dimension element .
axis: Array a dimension

Examples of multidimensional arrays ndimsizeshapeaxis1 Dimension group [1,2,3,4]14(4,)axis=0 The first 1 dimension ( The number of elements is 4)2 Dimension group [[1,2],[3,4],[5,6]]26(3,2)axis=0 The first 1 dimension ( The number of elements is 3), axis=1 The first 2 dimension ( The number of elements is 2)3 Dimension group [[[1,2,3,4],[1,3,4,5]],[[2,4,7,5],[8,4,3,5]],[[2,5,7,3],[1,5,3,7]]]324(3,2,4)axis=0 The first 1 dimension ( The number of elements is 3), axis=1 The first 2 dimension ( The number of elements is 2),axis=2 The first 3 dimension ( The number of elements is 4)

The illustration :

The black frame indicates the 1 dimension
The red box indicates the 2 dimension
The blue box indicates the 3 dimension

Code :

import numpy as np
arr_1_d = np.array ([1,2,3,4])
arr_2_d = np.array ([[1,2],[3,4],[5,6]])
arr_3_d = np.array([[[1,2,3,4],[1,3,4,5]],[[2,4,7,5],[8,4,3,5]],[[2,5,7,3],[1,5,3,7]]])
print("1 dimension array============================")
print("arr_1_d:{}".format(arr_1_d))
print("arr_1_d ndim:{}".format(arr_1_d.ndim))
print("arr_1_d shape:{}".format(arr_1_d.shape))
print("arr_1_d size:{}".format(arr_1_d.size))
print("arr_1_d #No.1 dimension size:{}".format(np.size(arr_1_d, axis=0)))
print("2 dimension array============================")
print("arr_2_d:{}".format(arr_2_d))
print("arr_2_d ndim:{}".format(arr_2_d.ndim))
print("arr_2_d shape:{}".format(arr_2_d.shape))
print("arr_2_d size:{}".format(arr_2_d.size))
print("arr_2_d #No.1 dimension size:{}".format(np.size(arr_2_d, axis=0)))
print("arr_2_d #No.2 dimension size:{}".format(np.size(arr_2_d, axis=1)))
print("3 dimension array============================")
print("arr_3_d:{}".format(arr_3_d))
print("arr_3_d ndim:{}".format(arr_3_d.ndim))
print("arr_3_d shape:{}".format(arr_3_d.shape))
print("arr_3_d size:{}".format(arr_3_d.size))
print("arr_3_d #No.1 dimension size:{}".format(np.size(arr_3_d, axis=0)))
print("arr_3_d #No.2 dimension size:{}".format(np.size(arr_3_d, axis=1)))
print("arr_3_d #No.3 dimension size:{}".format(np.size(arr_3_d, axis=2)))

Output :

1 dimension array============================
arr_1_d:[1 2 3 4]
arr_1_d ndim:1
arr_1_d shape:(4,)
arr_1_d size:4
arr_1_d #No.1 dimension size:4
2 dimension array============================
arr_2_d:[[1 2]
[3 4]
[5 6]]
arr_2_d ndim:2
arr_2_d shape:(3, 2)
arr_2_d size:6
arr_2_d #No.1 dimension size:3
arr_2_d #No.2 dimension size:2
3 dimension array============================
arr_3_d:[[[1 2 3 4]
[1 3 4 5]]
[[2 4 7 5]
[8 4 3 5]]
[[2 5 7 3]
[1 5 3 7]]]
arr_3_d ndim:3
arr_3_d shape:(3, 2, 4)
arr_3_d size:24
arr_3_d #No.1 dimension size:3
arr_3_d #No.2 dimension size:2
arr_3_d #No.3 dimension size:4

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