import numpy as np
array=np.array([[1,2,3],[2,3,4]],dtype=np.float32)
print(array)
print(array.shape)
print(array.ndim)
print(array.size)
print(array.dtype)
b=np.zeros((3,4))
print(b)
c=np.ones((3,4),dtype=np.int)
print(c)
d=np.empty((3,4))
print(d)
e=np.arange(10,20,2)
print(e)
f=np.arange(12).reshape((3,4))
print(f)
g=np.linspace(1,10,6).reshape(2,3)
print(g)
'''
[[1. 2. 3.]
[2. 3. 4.]]
(2, 3)
2
6
float32
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[10 12 14 16 18]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]
'''
import numpy as np
a=np.array([10,20,30,40])
b=np.arange(4)
c=a+b
print(c)
d=a-b
print(d)
e=b/2
print(e)
f=b**2
print(f)
g=10*np.sin(b)
print(g)
print(b<2)
'''
[10 21 32 43]
[10 19 28 37]
[0. 0.5 1. 1.5]
[0 1 4 9]
[0. 8.41470985 9.09297427 1.41120008]
[ True True False False]
'''
import numpy as np
a=np.array([1,1,0,1]).reshape(2,2)
b=np.arange(4).reshape(2,2)
c=a*b
d=np.dot(a,b)
print(c)
print(d)
'''
[[0 1]
[0 3]]
[[2 4]
[2 3]]
'''
c=np.array([[1,2,3],[4,5,6]])
print(np.sum(c))
print(np.sum(c,axis=1))
print(np.min(c))
print(np.max(c,axis=0))
d=np.random.random((2,4))
print(d)
'''
[[0 1]
[0 3]]
[[2 4]
[2 3]]
21
[ 6 15]
1
[4 5 6]
[[0.78218451 0.54274926 0.18765615 0.56376942]
[0.29984435 0.59091555 0.70656785 0.80401227]]
'''
import numpy as np
a=np.arange(14,2,-1).reshape((3,4))
print(a)
print(np.argmax(a))
print(np.average(a))
print(np.median(a))
print(np.cumsum(a))
print(np.diff(a))
print(np.nonzero(a))#返回非零的X,Y
print(np.sort(a))
print(np.transpose(a))#同下
print(a.T)#转置
print(np.clip(a,5,9))#上下界
print(np.mean(a,axis=0))#0对列计算
print(np.mean(a,axis=1))#1对行计算
'''
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 3]]
0
8.5
8.5
[ 14 27 39 50 60 69 77 84 90 95 99 102]
[[-1 -1 -1]
[-1 -1 -1]
[-1 -1 -1]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
[[11 12 13 14]
[ 7 8 9 10]
[ 3 4 5 6]]
[[14 10 6]
[13 9 5]
[12 8 4]
[11 7 3]]
[[14 10 6]
[13 9 5]
[12 8 4]
[11 7 3]]
[[9 9 9 9]
[9 9 8 7]
[6 5 5 5]]
[10. 9. 8. 7.]
[12.5 8.5 4.5]
'''
import numpy as np
a=np.arange(3,15)
print(a)
print(a[2])
a=a.reshape(3,4)
print(a[2])#第2行所有数
print(a[2,])#第2行所有数
print(a[,2])#第2列所有数
print(a[2,13])#第2行第1第2的数
print(a[2][2])#第二行第二位
print(a[2,2])#同上
for row in a#遍历行
print(row)
for column in a.T#遍历列(不存在的)
print(column)
for item in a.flat#变成一维,返回迭代器
print(item)
print(a.flatten())#返回一个array
'''
[ 3 4 5 6 7 8 9 10 11 12 13 14]
5
[11 12 13 14]
[11 12 13 14]
[ 5 9 13]
[12 13]
13
13
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
3
4
5
6
7
8
9
10
11
12
13
14
[ 3 4 5 6 7 8 9 10 11 12 13 14]
'''
import numpy as np
a=np.array([1,1,1])#一定要注意这个是零阶
b=np.array([2,2,2])
c=np.vstack((a,b))
d=np.hstack((a,b))
print(a,a.shape)
print(b,b.shape)
print(c,c.shape)
print(d,d.shape)
'''
[1 1 1] (3,)
[2 2 2] (3,)
[[1 1 1]
[2 2 2]] (2, 3)
[1 1 1 2 2 2] (6,)
'''
a=np.array([1,1,1])#一维
print(a.shape)
print(a.T.shape)#一阶的转置不变
c=a[np.newaxis,:]#行升维,变二阶
d=a[:,np.newaxis]#列升维,变二阶
print(c,c.shape)#在行上加维度
print(d,d.shape)#在列上加维度
print(c.T)
'''
(3,)
(3,)
[[1 1 1]] (1, 3)
[[1]
[1]
[1]] (3, 1)
[[1]
[1]
[1]]
'''
'''
关于numpy的阶与维是同概念的
一阶就是一个数组(没有行列之分,shape是(x,))
两阶就是行与列形成二维数组(即使仅1行也是二维)
三阶就是行与列与深度
'''
d=np.array([[1,2,3],[4,5,6],[7,8,9]])
e=np.array([[1,2,3],[4,5,6],[7,8,9]])
f=np.concatenate((d,e),axis=0)#行合并
g=np.concatenate((d,e),axis=1)#列合并
print(f)
print(g)
'''
[[1 2 3]
[4 5 6]
[7 8 9]
[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3 1 2 3]
[4 5 6 4 5 6]
[7 8 9 7 8 9]]
'''
import numpy as np
a=np.arange(12).reshape((3,4))
print(a,a.shape)
b,c=np.split(a,2,axis=1)#按列分割
print(b,b.shape)
print(c,c.shape)
d,e,f=np.split(a,3,axis=0)
print(d,d.shape)
print(e,e.shape)
print(f,f.shape)
#下面的等阶
print(np.vsplit(a,3))
print(np.hsplit(a,2))
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]] (3, 4)
[[0 1]
[4 5]
[8 9]] (3, 2)
[[ 2 3]
[ 6 7]
[10 11]] (3, 2)
[[0 1 2 3]] (1, 4)
[[4 5 6 7]] (1, 4)
[[ 8 9 10 11]] (1, 4)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
'''
import numpy as np
a=np.arange(4)
b=a#软拷贝,AB等价
c=a.copy()#硬拷贝,AC不等价
a[0]=11
print(a)
print(b)
print(c)
'''
[11 1 2 3]
[11 1 2 3]
[0 1 2 3]
'''