Python NumPy教程

时间:2020-02-23 14:43:05  来源:igfitidea点击:

欢迎使用Python NumPy教程。
在本教程中,我们将安装NumPy,并研究NumPy数组和一些矩阵运算,例如加法,减法,乘法等。

Python NumPy

Python NumPy是Python中科学计算的核心库。
NumPy提供了高性能的多维数组对象和用于处理这些数组的工具。

如果您已经熟悉MATLAB,则可能会更容易理解python numpy教程。
要执行本教程的以下代码,您需要导入numpy模块。
该软件包没有默认的python设置,因此让我们看看如何安装NumPy模块。

Python安装NumPy

您可以从此处查找针对不同类型的操作系统安装NumPy的说明。

我在Mac OS X上并使用python 3.6,我使用以下命令为python 3设置安装NumPy模块。

$pip3.6 install --user numpy

下图显示了为python 3安装numpy模块的终端输出。

Python NumPy数组

Python numpy数组是所有相同类型的值网格。
我们可以使用嵌套的Python列表初始化Python NumPy数组。
然后,我们可以使用它们的索引访问它们。
NumPy中也有一些功能,通过它您可以创建不同类型的Array。

请参阅以下代码,以了解python numpy数组声明和访问元素。

import numpy

# Create a rank 1 array
a = numpy.array([3, 2, 3])
print('print rank 1 array:')
# access the array using their index
print('print using their index: a[0]= ', a[0])
a[0] = 5  # modify the array
print('print using slicing : ', a[1:])  # slicing can be used also
# print the whole list
print('Print the whole array : ', a)

# Create a rank 2 array using nested Python list
b = numpy.array([[10, 20, 30], [40, 50, 60]])
print('\nprint rank 2 array')
# access them using their index
print('print using their index: b[0,0]= ', b[0, 0], ' b[0,1]= ',b[0, 1])
print('print using slicing ', b[1:, :2])  # 1st slice for row, 2nd for column

# initialize a zero matrix
a = numpy.zeros((2, 2))
print('\nprint a 2-by-2 zero matrix:\n', a)

# Create an array of all ones
b = numpy.ones((2, 2))
print('\nprint a 2-by-2 all one matrix:\n', b)

# Create a constant array
c = numpy.full((3, 3), 6)
print('\nprint a 3-by-3 constant matrix for value = 6:\n', c)

# Create a 3x3 identity matrix
d = numpy.eye(3)
print('\nprint a 3-by-3 identity matrix:\n', d)

python numpy数组示例代码的输出为:

print rank 1 array:
print using their index: a[0]=  3
print using slicing :  [2 3]
Print the whole array :  [5 2 3]

print rank 2 array
print using their index: b[0,0]=  10  b[0,1]=  20
print using slicing  [[40 50]]

print a 2-by-2 zero matrix:
 [[ 0.  0.]
 [ 0.  0.]]

print a 2-by-2 all one matrix:
 [[ 1.  1.]
 [ 1.  1.]]

print a 3-by-3 constant matrix for value = 6:
 [[6 6 6]
 [6 6 6]
 [6 6 6]]

print a 3-by-3 identity matrix:
 [[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

Python NumPy教程–矩阵的算术运算

您可以进行算术运算,例如矩阵之间的加,减,乘和除。
在下面的示例中,您可以看到一些矩阵之间的算术运算示例。

import numpy

# initialize two array
x = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
y = numpy.array([[3, 4], [5, 6]], dtype=numpy.float64)

print('Print the two matrices')
print('X = \n', x)
print('Y = \n', y)

# Elementwise sum; both produce the array
print('\nElementwise addition of two matrices: ( X + Y of Matlab )')
print('Add using add operator: \n', x + y)
print('Add using add function: \n', numpy.add(x, y))

# Elementwise difference; both produce the array
print('\nElementwise subtraction of two matrices: ( X - Y of Matlab )')
print('Subtract using operator: \n', x - y)
print('Subtract using function: \n', numpy.subtract(x, y))

# Elementwise product; both produce the array
print('\nElementwise Multiplication of two matrices: ( X .* Y of Matlab )')
print('Multiply using operator: \n', x * y)
print('Multiply using function: \n', numpy.multiply(x, y))

# Elementwise division; both produce the array
print('\nElementwise division of two matrices: ( X ./Y of Matlab )')
print('Division using operator: \n', x/y)
print('Division using function: \n', numpy.divide(x, y))

# Elementwise square root; produces the array
print('\nSquare root each element of X matrix\n', numpy.sqrt(x))

# Matrix Multiplication
print('\nMatrix Multiplication of two matrices: ( X * Y of Matlab )')
print(x.dot(y))

以下是上述numpy矩阵程序产生的输出。

X = 
 [[ 1.  2.]
 [ 3.  4.]]
Y = 
 [[ 3.  4.]
 [ 5.  6.]]

Elementwise addition of two matrices: ( X + Y of Matlab )
Add using add operator: 
 [[  4.   6.]
 [  8.  10.]]
Add using add function: 
 [[  4.   6.]
 [  8.  10.]]

Elementwise subtraction of two matrices: ( X - Y of Matlab )
Subtract using operator: 
 [[-2. -2.]
 [-2. -2.]]
Subtract using function: 
 [[-2. -2.]
 [-2. -2.]]

Elementwise Multiplication of two matrices: ( X .* Y of Matlab )
Multiply using operator: 
 [[  3.   8.]
 [ 15.  24.]]
Multiply using function: 
 [[  3.   8.]
 [ 15.  24.]]

Elementwise division of two matrices: ( X ./Y of Matlab )
Division using operator: 
 [[ 0.33333333  0.5       ]
 [ 0.6         0.66666667]]
Division using function: 
 [[ 0.33333333  0.5       ]
 [ 0.6         0.66666667]]

Square root each element of X matrix
 [[ 1.          1.41421356]
 [ 1.73205081  2.        ]]

Matrix Multiplication of two matrices: ( X * Y of Matlab )
[[ 13.  16.]
 [ 29.  36.]]