## 一、Numpy库简介

Anaconda集成了NumPy，可以更方便的使用该库。

import numpyvector=numpy.array([5,10,15,20])matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])print (vector)print (matrix)
vector=numpy.array([1,2,3,4])print (vector.shape) #查看，常在debug使用
import numpynumbers=numpy.array([1,2,3,4])  #这里元素必须是相同类型print (numbers)numbers.dtype  #输出数据类型

## 二、 一些数组运算

np.dot(a,b)np.inner(a,b)np.outer(a,b)

np.sum()

np.mean()

np.std()

## 三、Numpy的一些操作

Numpy是Python的科学计算的库，提供了矩阵运算的功能，其一般与Scipy、matplotlib一起使用。

### 1.导入numpy

import numpy as npprint (np.version.version)

### 2.NumPy矩阵与数组的区别

NumPy函数库中的matrix与MATLAB中matricses等价。矩阵和数组显然是两种不同的数据类型。调用mat()函数可以将数组转化为矩阵，例如：

import numpy as nprandMat = np.mat(np.random.rand(4,4))print (randMat)

[[ 0.68961916 0.17159703 0.59995799 0.99626607]
[ 0.92436968 0.97710376 0.03818586 0.30673391]
[ 0.3851621 0.58558701 0.94255313 0.31884537]
[ 0.83106025 0.61143961 0.82635502 0.95168527]]

### 3.多维数组 numpy.ndarray

print (np.array([1,2,3,4]))print (np.array((1.2,2,3,4)))print (type(np.array((1.2,2,3,4))))

print (np.array((1.2,2,3,4),dtype=np.int32))[1 2 3 4]

print (np.arange(15))print (type(np.arange(15)))<class 'numpy.ndarray'>

import numpyvector=numpy.array([5,10,15,20])vector == 10

array([False, True, False, False], dtype=bool)

matrix=numpy.array([ [5,10,15], [20,25,30], [35,40,45]])matrix==25

array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool)

vector=numpy.array([5,10,15,20])equal_to_len = (vector==10)print (equal_to_len)print (vector[equal_to_len])

[False True False False]
[10]

vector = numpy.array(["1","2","3"])print (vector.dtype)print (vector)vector = vector.astype(float)print (vector.dtype)print (vector)

matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])matrix.sum(axis=1)

array([ 30, 75, 120])

matrix.sum(axis=0)
array([60, 75, 90])

import numpy as nprandMat = np.mat(np.random.rand(4,4))print (randMat.I)

[[ 1.01943747 -0.25509941 -1.51081141 1.64622233]
[ 0.87747412 -0.56051253 1.17143183 -1.09210262]
[-2.21496654 0.77250249 1.49641539 -0.12661331]
[ 1.05598907 1.25225748 -2.77331073 1.30848761]]

import numpy as npprint (np.arange(15))a=np.arange(15).reshape(3,5) #把原来的元素转为矩阵，3行5列print (a)

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]

ndarray的属性

• a.shape 返回维度
import numpy as npa=np.arange(15).reshape(3,5) #把原来的元素转为矩阵，3行5列print (a.shape)

(3, 5)

• 维度
a.ndim

2

• a.dtype.name
输出：
是int32 等
• a.size
一共多少个元素
• np.zeros((3,4)) 初始化全零矩阵，维度是2
import numpy as npnp.zeros((3,4))

array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])

• 初始化1
np.ones((2,3,4),dtype=np.int32) 初始化1
• numpy.eye构造特定的矩阵
• 等差数列
np.arange(10,30,5)array([10, 15, 20, 25])
• 随机值
np.random.random((2,3))
• linspace
from numpy import pinp.linspace(0,2*pi,100)   #从0到2pi当中找100个值，a=np.array([20,30,40,50])b=np.arange(4)c=a-bprint (a)print (b)print (c)

[20 30 40 50]
[0 1 2 3]
[20 29 38 47]

c = c-1

[19 28 37 46]

print (b**2)

[0 1 4 9]

print (a<5)

[False False False False]

import numpy as npfor x in np.linspace(1,3,3):    print (x)

1.0
2.0
3.0

A = np.array([[1,1],[0,1]])B = np.array([[2,0],[3,4]])print (A)print (B)

[[1 1]
[0 1]]
[[2 0]
[3 4]]

print (A*B)  #对应位置相乘

[[2 0]
[0 4]]

print (A.dot(B))  #正常的矩阵相乘，或print (np.dot(A,B))

[[5 4]
[3 4]]

import numpy as npB=np.arange(3)print (B)print (np.exp(B))print (np.sqrt(B))

[0 1 2]
[ 1. 2.71828183 7.3890561 ]
[ 0. 1. 1.41421356]

a = np.floor(10*np.random.random((3,4)))print (a)

[[ 1. 7. 9. 0.]
[ 8. 7. 8. 2.]
[ 7. 8. 5. 0.]]

print (a.ravel())

a.shape=(6,2)

a.T

import numpy as npa = np.floor(10*np.random.random((2,2)))b = np.floor(10*np.random.random((2,2)))print (a)print (np.vstack((a,b)))

[[ 5. 9.]
[ 1. 6.]
[ 0. 9.]
[ 1. 2.]]

print (np.hstack((a,b)))

a = np.floor(10*np.random.random((2,12)))print (np.hsplit(a,3)) #平均切分 print (np.hsplit(a,(3,4)))

[array([[ 4., 1., 4., 5.],
[ 9., 4., 9., 9.]]), array([[ 3., 5., 5., 7.],
[ 9., 7., 6., 3.]]), array([[ 2., 1., 7., 5.],
[ 5., 6., 8., 9.]])]
[array([[ 4., 1., 4.],
[ 9., 4., 9.]]), array([[ 5.],
[ 9.]]), array([[ 3., 5., 5., 7., 2., 1., 7., 5.],
[ 9., 7., 6., 3., 5., 6., 8., 9.]])]

vsplit竖着切

a = np.arange(12)b = a  #a和b是同一个对象。b.shape = 3,4print (a.shape)print (id(a))print (id(b))

(3, 4)
2134912537744
2134912537744

c=a.view()c.shape=2,6print (a.shape)c[0,4] = 1234print (a)print (id(a))print (id(c))

(3, 4)
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
2134912537744
2134912459520

d = a.copy()d is ad[0,0]=9999print (d)print (a)

[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]

import numpy as npdata = np.sin(np.arange(20)).reshape(5,4)print (data)ind = data.argmax(axis=0)print (ind)data_max = data[ind]print (data_max)

[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
[2 0 3 1]
[[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[ 0. 0.84147098 0.90929743 0.14112001]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]]

import numpy as npa=np.arange(0,40,10)print (a)np.tile(a,(4,3)) #把a 扩到4行三列，按倍数扩充print (b)

[ 0 10 20 30]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]

import numpy as npa = np.arange(0,40,10)a.sort()print (a)

[ 0 10 20 30]

argsort

import numpy as npx = np.array([3, 1, 2])np.argsort(x)

array([1, 2, 0], dtype=int64)