# Python Numpy介绍和用法经典指南

2021年3月19日18:47:19 发表评论 1,010 次浏览

Numpy

#### numpy中的数组

Numpy中的Array是所有相同类型的元素(通常是数字)表, 由正整数元组索引。在Numpy中, 数组的维数称为数组的秩。给出每个方向的数组大小的整数元组称为数组的形状。 Numpy中的数组类称为ndarray。 Numpy数组中的元素可以使用方括号访问, 并且可以使用嵌套的Python列表进行初始化。

Numpy中的数组可以通过多种方式创建, 具有不同数量的Rank, 用于定义数组的大小。也可以使用各种数据类型(例如列表, 元组等)来创建数组。结果数组的类型是根据序列中元素的类型推导出来的。

# Python program for
# Creation of Arrays
import numpy as np

# Creating a rank 1 Array
arr = np.array([1, 2, 3])
print("Array with Rank 1: \n", arr)

# Creating a rank 2 Array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array with Rank 2: \n", arr)

# Creating an array from tuple
arr = np.array((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)

Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]

Array created using passed tuple:
[1 3 2]

# Python program to demonstrate
# indexing in numpy array
import numpy as np

# Initial Array
arr = np.array([[-1, 2, 0, 4], [4, -0.5, 6, 0], [2.6, 0, 7, 8], [3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)

# Printing a range of Array
# with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
" alternate columns(0 and 2):\n", sliced_arr)

# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3], [3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)

Initial Array:
[[-1.   2.   0.   4. ]
[ 4.  -0.5  6.   0. ]
[ 2.6  0.   7.   8. ]
[ 3.  -7.   4.   2. ]]
Array with first 2 rows and alternate columns(0 and 2):
[[-1.  0.]
[ 4.  6.]]

Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[ 0. 54.  2.  3.]

# Python program to demonstrate
# basic operations on single array
import numpy as np

# Defining Array 1
a = np.array([[1, 2], [3, 4]])

# Defining Array 2
b = np.array([[4, 3], [2, 1]])

# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)

# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)

# sum of array elements
# Performing Unary operations
print ("\nSum of all array "
"elements: ", a.sum())

# Performing Binary operations
print ("\nArray sum:\n", a + b)

[[2 3]
[4 5]]

Subtracting 2 from each element:
[[ 2  1]
[ 0 -1]]

Sum of all array elements:  10

Array sum:
[[5 5]
[5 5]]

• Numpy中的基本阵列操作
• Numpy中的高级阵列操作
• NumPy Python中的基本切片和高级索引

#### Numpy中的数据类型

# Python Program to create
# a data type object
import numpy as np

# Integer datatype
# guessed by Numpy
x = np.array([1, 2])
print("Integer Datatype: ")
print(x.dtype)

# Float datatype
# guessed by Numpy
x = np.array([1.0, 2.0])
print("\nFloat Datatype: ")
print(x.dtype)

# Forced Datatype
x = np.array([1, 2], dtype = np.int64)
print("\nForcing a Datatype: ")
print(x.dtype)

Integer Datatype:
int64

Float Datatype:
float64

Forcing a Datatype:
int64

DataType数组上的数学运算

：用于添加数组元素,

Ť

：用于元素的转置等。

# Python Program to create
# a data type object
import numpy as np

# First Array
arr1 = np.array([[4, 7], [2, 6]], dtype = np.float64)

# Second Array
arr2 = np.array([[3, 6], [2, 8]], dtype = np.float64)

print(Sum)

# Addition of all Array elements
# using predefined sum method
Sum1 = np.sum(arr1)
print(Sum1)

# Square root of Array
Sqrt = np.sqrt(arr1)
print("\nSquare root of Array1 elements: ")
print(Sqrt)

# Transpose of Array
# using In-built function 'T'
Trans_arr = arr1.T
print("\nTranspose of Array: ")
print(Trans_arr)

[[ 7. 13.]
[ 4. 14.]]

19.0

Square root of Array1 elements:
[[2.         2.64575131]
[1.41421356 2.44948974]]

Transpose of Array:
[[4. 2.]
[7. 6.]]

• NumPy中的数据类型Object(dtype)

#### numpy中的方法

 all() arange() dot() any() apply_along_axis() apply_over_axes() argmin() argmax() nanargmin() nanargmax() amax() amin() isneginf() rint() insert() isposinf() flip() fliplr() flipud() triu() tril() tri() fix() empty_like() zeros() zeros_like() ones() ones_like() full_like() diag() diagflat() diag_indices() asmatrix() bmat() eye() roll() identity()
 arange() place() extract() compress() rot90() tile() reshape() ravel() isinf() isrealobj() isscalar() isneginf() isposinf() iscomplex() isnan() iscomplexobj() isreal() isfinite() isfortran() exp() exp2() fix() hypot() absolute() ceil() floor() degrees() radians() npv() fv() pv() power() float_power() log() log1() log2() log10()
 dot() vdot() trunc() divide() floor_divide() true_divide() random.rand() random.randn() ndarray.flat() expm1() bincount() rint() equal() not_equal() less() less_equal() greater() Greater_equal() prod() square() cbrt() logical_or() logical_and() logical_not() logical_xor() array_equal() array_equiv() sin() cos() tan() sinh() cosh() tanh() arcsin() arccos() arctan() arctan2()

### 最近关于Numpy的文章

Numpy程序

• Python |检查列表是否为空
• Python |从列表中获取唯一值
• Python |将列表中的所有数字相乘(3种不同方式)
• 在Python中以单行转置矩阵
• 在Python中使用Numpy在单行中将两个矩阵相乘
• Python程序使用numpy打印nxn的棋盘图案
• Python中的图形绘制套装1, 套装2, 套装3

• Python中的矩阵处理
• NumPy Python中的基本切片和高级索引
• Flatten()和Ravel()之间的区别
• python中Numpy.random中的rand与normal