numpy基础学习笔记

一、前言

本博文基于Jupyter notebook,演示python的numpy库常用方法,并附加运行结果和必要的注释说明。

二、代码

1、环境准备

(1)使用import导入numpy库,该代码不会有任何打印信息

#导入numpy库
import numpy as np

(2)打印numpy库的版本号,根据系统所安装的版本有所不同,我这里打印的版本号是1.13.3

#打印python库的版本号
print(np.__version__)

################下面是输出结果################
1.13.3

(3)打印numpy库的配置信息,根据系统安装环境有所不同。

#打印python库的配置信息
np.show_config()

################下面是输出结果################
las_mkl_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
blas_opt_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
lapack_mkl_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
lapack_opt_info:
    libraries = ['mkl_rt']
    library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']

2、基本操作

(1)创建1阶向量

#创建一阶向量
Z = np.zeros(10)
#打印Z的类型
print("The type of Z is {0}".format(type(Z)))
#打印Z的值
print("Z = {0}".format(Z))
#打印Z所占空间
print("Z need %d bytes" % (Z.size * Z.itemsize))

################下面是输出结果################
The type of Z is <class 'numpy.ndarray'>
Z = [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Z need 80 bytes

(2)创建2阶矩阵

#创建二阶矩阵
Z = np.zeros((10,10))
#打印Z的类型
print("The type of Z is {0}".format(type(Z)))
#打印Z的值
print("Z = {0}".format(Z))
#打印Z所占空间
print("Z need %d bytes" % (Z.size * Z.itemsize))

################下面是输出结果################
The type of Z is <class 'numpy.ndarray'>
Z = [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
Z need 800 bytes

(3)给成员赋值

#给向量的某个成员赋值
Z = np.zeros(10)
Z[4] = 1
print("Z = {0}".format(Z))

################下面是输出结果################
Z = [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]

(4)使用arange函数

Z = np.arange(10,50)
print(Z)

################下面是输出结果################
Z = [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]

(5)向量翻转

Z = np.arange(20)
print("翻转前:Z = {0}".format(Z))
Z = Z[::-1]
print("翻转后:Z = {0}".format(Z))

################下面是输出结果################
翻转前:Z = [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
翻转后:Z = [19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0]

(6)reshape函数

Z = np.arange(10)
print("向量:\nZ = {0}\n".format(Z))
Z = Z.reshape(2,5)
print("转化成2*5的矩阵为:\n {0}\n".format(Z))
Z = Z.reshape(5,2)
print("转化成5*2的矩阵:\n {0}\n".format(Z))

################下面是输出结果################
向量:
Z = [0 1 2 3 4 5 6 7 8 9]

转化成2*5的矩阵为:
 [[0 1 2 3 4]
 [5 6 7 8 9]]

转化成5*2的矩阵:
 [[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]

(7)nonzero函数:查找非零值的下标

a = [1,0,2,0,0,1,-29,4,0,-9]
nz = np.nonzero(a)

for i in nz[0]:
    print("非零下标:{0},对应的值:{1}".format(i,a[i]))

################下面是输出结果################
非零下标:0,对应的值:1
非零下标:2,对应的值:2
非零下标:5,对应的值:1
非零下标:6,对应的值:-29
非零下标:7,对应的值:4
非零下标:9,对应的值:-9

(8)eye函数:创建单位矩阵

Z = np.eye(4)
print(Z)

################下面是输出结果################
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

(9)求最大值和最小值:

Z = np.random.random((3,3))
print(Z)
Zmin, Zmax = Z.min(), Z.max()
print("最大值:{0},最小值:{1}".format(Zmax,Zmin))

################下面是输出结果################
[[ 0.84013553  0.80264081  0.7633128 ]
 [ 0.2175741   0.42369911  0.90972398]
 [ 0.39003791  0.56833479  0.08838496]]
最大值:0.9097239794423938,最小值:0.08838496063022305

(10)求平均值

Z = np.random.random(30)
print(Z)
m = Z.mean()
print("平均值:{0}".format(m))

################下面是输出结果################
[ 0.63623368  0.41493367  0.74299422  0.50312252  0.14406315  0.04124667
  0.19530372  0.5011411   0.72075647  0.4292777   0.65942497  0.45263011
  0.39280159  0.34797425  0.43313813  0.59542526  0.91938076  0.0604784
  0.08111982  0.0594499   0.44168998  0.85399465  0.28776815  0.05349918
  0.60960594  0.34966823  0.88145119  0.61597093  0.61165211  0.99313293]
平均值:0.46764431241345766

(11)ones函数与挖空操作

Z = np.ones((5,5))
print("挖空前:\n{0}\n".format(Z))
Z[1:-1,1:-1] = 0
print("挖空后:\n{0}".format(Z))

################下面是输出结果################
挖空前:
[[ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]]

挖空后:
[[ 1.  1.  1.  1.  1.]
 [ 1.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  1.]
 [ 1.  1.  1.  1.  1.]]

(12)pad函数

Z = np.ones((5,5))
print("pad前:\n{0}\n".format(Z))

Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print("pad后:\n{0}\n".format(Z))

################下面是输出结果################
pad前:
[[ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]]

pad后:
[[ 0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  1.  1.  1.  1.  0.]
 [ 0.  1.  1.  1.  1.  1.  0.]
 [ 0.  1.  1.  1.  1.  1.  0.]
 [ 0.  1.  1.  1.  1.  1.  0.]
 [ 0.  1.  1.  1.  1.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.]]

(13)nan和inf

print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))

################下面是输出结果################
nan
False
False
nan
True
False

(14)diag函数

a = np.arange(3)
print("a:\n{0}\n".format(a))
print("np.diag(a):\n{0}\n".format(np.diag(a)))

print("(np.diag(a,k=0):\n{0}\n".format((np.diag(a,k=0))))

print("(np.diag(a,k=1):\n{0}\n".format((np.diag(a,k=1))))
print("(np.diag(a,k=2):\n{0}\n".format((np.diag(a,k=2))))

print("(np.diag(a,k=-1):\n{0}\n".format((np.diag(a,k=-1))))
print("(np.diag(a,k=-2):\n{0}\n".format((np.diag(a,k=-2))))


################下面是输出结果################
a:
[0 1 2]

np.diag(a):
[[0 0 0]
 [0 1 0]
 [0 0 2]]

(np.diag(a,k=0):
[[0 0 0]
 [0 1 0]
 [0 0 2]]

(np.diag(a,k=1):
[[0 0 0 0]
 [0 0 1 0]
 [0 0 0 2]
 [0 0 0 0]]

(np.diag(a,k=2):
[[0 0 0 0 0]
 [0 0 0 1 0]
 [0 0 0 0 2]
 [0 0 0 0 0]
 [0 0 0 0 0]]

(np.diag(a,k=-1):
[[0 0 0 0]
 [0 0 0 0]
 [0 1 0 0]
 [0 0 2 0]]

(np.diag(a,k=-2):
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 1 0 0 0]
 [0 0 2 0 0]]

(15)unravel_index函数

print(np.unravel_index(1,(5,5,5,5)))
print(np.unravel_index(2,(5,5,5,5)))
print(np.unravel_index(10,(5,5,5,5)))
print(np.unravel_index(100,(5,5,5,5)))

################下面是输出结果################
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 2, 0)
(0, 4, 0, 0)

(16)tile函数

print("\nnp.tile( np.array([[0,1],[1,0]]), (0)):")
print(np.tile( np.array([[0,1],[1,0]]), (0)))

print("\nnp.tile( np.array([[0,1],[1,0]]), (1)):")
print(np.tile( np.array([[0,1],[1,0]]), (1)))

print("\nnp.tile( np.array([[0,1],[1,0]]), (2)):")
print(np.tile( np.array([[0,1],[1,0]]), (2)))

print("\nnp.tile( np.array([[0,1],[1,0]]), (1,1)):")
print(np.tile( np.array([[0,1],[1,0]]), (1,1)))


print("\nnp.tile( np.array([[0,1],[1,0]]), (1,2)):")
print(np.tile( np.array([[0,1],[1,0]]), (1,2)))

print("\nnp.tile( np.array([[0,1],[1,0]]), (2,2)):")
print(np.tile( np.array([[0,1],[1,0]]), (2,2)))

print("\nnp.tile( np.array([[0,1],[1,0]]), (3,2)):")
print(np.tile( np.array([[0,1],[1,0]]), (3,2)))


################下面是输出结果################
np.tile( np.array([[0,1],[1,0]]), (0)):
[]

np.tile( np.array([[0,1],[1,0]]), (1)):
[[0 1]
 [1 0]]

np.tile( np.array([[0,1],[1,0]]), (2)):
[[0 1 0 1]
 [1 0 1 0]]

np.tile( np.array([[0,1],[1,0]]), (1,1)):
[[0 1]
 [1 0]]

np.tile( np.array([[0,1],[1,0]]), (1,2)):
[[0 1 0 1]
 [1 0 1 0]]

np.tile( np.array([[0,1],[1,0]]), (2,2)):
[[0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]]

np.tile( np.array([[0,1],[1,0]]), (3,2)):
[[0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]]

猜你喜欢

转载自blog.csdn.net/u013638184/article/details/88097651