numpy作为一个第三方的Python包,用于科学计算。也可用于深度学习中处理多维数组,编程非常简洁,往往用一行代码可以处理其它语言十几行代码的事情。所以,numpy非常受欢迎。
np.zeros((5,3), dtype=np.int)```
```python
import numpy as np
a = [1,2,3,4]
b = np.array(a)
print(type(b))
#argmax(): 取出列表中最大值对应的索引
print(b.shape, b.argmax(), b.max(), b.mean())
c = [[1,3],[2,4]]
d = np.array(c)
# d.max(axis = 0):求矩阵每个列方向上的最大值
#d.max(axis = 1):求矩阵每个行方向上的最大值
#d.flatten():将数组展开为一维形状
print(type(d), d.shape,d.size, d.argmax(), d.max(), d.max(axis = 0), d.max(axis = 1),d.mean(), d.mean(axis = 1),\
d.flatten())
print(np.ravel(c))
e = np.ones((3,3),dtype=np.float)
print(e)
print(type(e))
f = np.repeat(3,4)
print(type(f))
print(f)
g = np.zeros((2,2,3),dtype=np.uint8)
print(type(g))
print(g)
print(g.shape)
h = g.astype(np.float)
print(h)
l = np.arange(10)
m = np.linspace(0,6,5)
p = np.array([[1,2,3,4],[5,6,7,8]])
print(p)
np.save('p.npy', p)
q = np.load('p.npy')
print(q)
2.numpy可以对多维数组进行丰富的操作,包括数组元素的引用,改变矩阵的形状,进行转置,翻转,滚动。
import numpy as np
a = np.arange(24).reshape((2,3,4))
print(a)
print(a[0][0][0])
print(a[:, 2, :])
print(a[:, :, 1])
print(a[...,1]) #等同于a[:, :, 1]
print(a[:,1:,1:])
g = np.split(np.arange(9),3)
print(g)
h = np.split(np.arange(9),[2,-3])
l0 = np.arange(6,12).reshape(2,3)
print(l0)
m = np.vstack((10,11))
print(m)
p = np.hstack((10,11))
print(p)
#q = np.concatenate((10,11),axis=2)
#print(q)
#r = np.concatenate((10,11),axis=-1)
#print(r)
s = np.stack((10,11))
print(s)
print(s.shape)
e = np.arange(12).reshape(2,3,2)
print(e)
t = e.transpose((1, 0, 2))
print(t)
t = e.transpose((2, 0, 1))
print(t)
#默认转置将维度倒序,对于二维就是横纵轴转换
t = e[0].transpose()
print(t)
v = np.rot90(t,4)
print(v)
w = np.fliplr(v)
print(w)
y = np.flipud(v)
print(y)
#扁平化处理之后,滚动一个单位
x = np.roll(v,1)
print(x)
#沿水平轴滚动2个单位
z = np.roll(v,2,axis=1)
print(z)
import numpy as np
a = np.abs(-1)
b = np.sin(np.pi/2)
c = np.arctanh(0.462118)
d = np.exp(3)
f = np.power(2,3)
g = np.dot([1, 2], [3, 4])
h = np.sqrt(25)
l = np.sum([1,2,3,4,5])
m = np.mean([1,2,3,4])
p = np.std([1,2,3,4,5])
print(a)
print(b)
print(c)
print(d)
print(f)
print(g)
print(h)
print(l)
print(m)
print(p)
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[1,2,3],[1,2,3]])
c = a + b
d = a - b
e = a * b
f = a/b
g = a**2
h = a**b
l = np.array([[1,2,3],[4,5,6],[7,8,9]])
m = np.array([2,2,2])
n = l + m
p = l*m
q = l - 1
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(l)
print(m)
print(n)
print(p)
print(q)
import numpy as np
a = np.array([3,4])
print(np.linalg.norm(a)) #求范数
b = np.array([[1,2,3],[4,5,6],[7,8,9]])
c = np.array([1,0,1])
e = np.dot(b,c)
f = np.dot(c,b.T)
h = np.trace(b)
l = np.linalg.det(b)
m = np.linalg.matrix_rank(b)
d = np.array([[2,1],[1,2]])
u,v = np.linalg.eig(d)
w = np.linalg.cholesky(d)
x = np.dot(w,w.T)
y = np.array([[1,2],[3,4]])
U,s,V = np.linalg.svd(y)
S = np.array([[s[0],0],[0,s[1]]])
z = np.dot(U,np.dot(S,V))
print(e)
print(f)
print(h)
print(l)
print(m)
print(u)
print(v)
print(w)
print(x)
print(y)
print(U)
print(s)
print(V)
print("S:", S)
print(z)
import numpy as np
import numpy.random as random
random.seed(23) #设置随机种子数,每次产生的随机数相同,如果不设置,则每次产生的随机数不同
a = random.rand(1,3)
print("a:", a)
b = random.random((3,3))
print("b:", b)
c = 5*random.random(10) + 1 #产生10个1到6之间的随机数
d = random.randint(1,6,10) #产生10个(1,6)之间的整型随机数
e = random.normal(size=(5,2)) #产生2*5的标准正态分布样本
f = random.binomial(n = 5,p = 0.5, size = 5)
h = np.arange(10)
l = random.choice(h,7)
m = random.permutation(h)
print("c",c)
print("d",d)
print("e",e)
print("f",f)
print("h",h)
print("l",l)
print("m",m)
参考链接:
http://www.360doc.com/content/19/0602/00/7669533_839717717.shtml
https://blog.csdn.net/lxq1997/article/details/83543709