python-numpy

python:模块调用

  • 1、array创建
  • 2、逐个相乘和矩阵相乘
  • 3、求最值得问题
  • 4、numpy的索引
  • 5、array的合并
  • 6、array的分割
  • 7、numpy的copy,deep copy
  • 7、逐个相乘和矩阵相乘

1、array创建

import numpy as np
#1.array创建
array=np.array([[1,2,3],[4,5,6]],dtype=np.float)
print(array)
print(array.shape)
print(array.dtype)
a=np.zeros((3,4))
b=np.ones((4,4))
print(a)
print(b)
c=np.arange(10,20,2)
print(c)
d=np.arange(12).reshape((3,4))
print(d)
e=np.linspace(1,10,6).reshape((2,3))
print(e)
f=np.array([1,2,3,4,5])
print(f==4)
print("c={0}".format(c))
print("f={0}".format(f))
g=c+f
print("g={0}".format(g))
h=10**np.sin(g)
print(h)
print("#############")

输出为

[[1. 2. 3.]
[4. 5. 6.]]
(2, 3)
float64
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[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. ]]
[False False False True False]
c=[10 12 14 16 18]
f=[1 2 3 4 5]
g=[11 14 17 20 23]
[0.10000226 9.78604832 0.10929556 8.18361615 0.14248843]
#############

2、逐个相乘和矩阵相乘

import numpy as np
j=np.array([[0,1],[2,1]])
k=np.arange(1,5).reshape((2,2))
print("j={0}".format(j))
print("k={0}".format(k))
l=j*k
print("l={0}".format(l))
l_dot=np.dot(j,k)
l_dot_2=j.dot(k)
print("l_dot={0}".format(l_dot))
print("l_dot_2={0}".format(l_dot_2))
print("#############")

输出为

j=[[0 1]
[2 1]]
k=[[1 2]
[3 4]]
l=[[0 2]
[6 4]]
l_dot=[[3 4]
[5 8]]
l_dot_2=[[3 4]
[5 8]]

3、求最值得问题

import numpy as np
x=np.random.random(4).reshape((2,2))
print(x)
print(np.sum(x,axis=1))
print(np.max(x,axis=0))
print(np.min(x,axis=0))
#矩阵中索引的查询
A=np.arange(2,14).reshape((3,4))
print(A)
print(np.argmin(A))#最小的索引
print(np.argmax(A))#最大的索引
print(np.mean(A))#p平均值
#print(A.mean())
print(np.average(A))
print(np.median(A))#求中位数
print(np.cumsum(A))#和
print(np.diff(A))#差
print(np.nonzero(A))#求非零的数
B=np.arange(12,0,-1).reshape((3,4))
print("B={0}".format(B))
print(np.sort(B))#逐行排序
print(np.transpose(B))#求矩阵的反向(转置)
print(B.T)#矩阵的转置不同的写法
print((B.T).dot(B))
print(np.clip(B,5,9))#大于9的数都等于9,小于5的数都等于5
print(np.mean(B,axis=0))#求列的平均值
print("###########")

输出为

[[0.86534795 0.32646424]
[0.68193961 0.1552391 ]]
[1.19181218 0.83717871]
[0.86534795 0.32646424]
[0.68193961 0.1552391 ]
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
0
11
7.5
7.5
7.5
[ 2 5 9 14 20 27 35 44 54 65 77 90]
[[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))
B=[[12 11 10 9]
[ 8 7 6 5]
[ 4 3 2 1]]
[[ 9 10 11 12]
[ 5 6 7 8]
[ 1 2 3 4]]
[[12 8 4]
[11 7 3]
[10 6 2]
[ 9 5 1]]
[[12 8 4]
[11 7 3]
[10 6 2]
[ 9 5 1]]
[[224 200 176 152]
[200 179 158 137]
[176 158 140 122]
[152 137 122 107]]
[[9 9 9 9]
[8 7 6 5]
[5 5 5 5]]
[8. 7. 6. 5.]
###########

4、numpy的索引

import numpy as np
C=np.arange(3,15).reshape((3,4))
print("C={0}".format(C))
print(C[0][1])
print(C[0,1])
print(C[1,:])
print(C[1,1:3])
print(C.flatten())#转换成一维数组(转换成一行)
#迭代每一行输出
for row in C:
    print(row)
#通过转置迭代每一列
for column in C.T:
    print(column)
#迭代出每一项
for item in C.flat:
    print(item)
print("#############")

输出为

C=[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
4
4
[ 7 8 9 10]
[8 9]
[ 3 4 5 6 7 8 9 10 11 12 13 14]
[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
#############

5、array的合并

import numpy as np
S1=np.array([1,1,1])
S2=np.array([2,2,2])
C1=np.vstack((S1,S2))
print(C1)#上下的合并
print(C1.shape)
C2=np.hstack((S1,S2))
print(C2)#左右合并
print(C2.shape)
print(S1[np.newaxis,:])
print(S1[np.newaxis,:].shape)
print(S1[:,np.newaxis])
print(S1[:,np.newaxis].shape)
D1=S1[:,np.newaxis]
D2=S2[:,np.newaxis]
print("D1={0}".format(D1))
print(np.vstack((D1,D2)))
print(np.hstack((D1,D2)))
#多个array合并
F=np.concatenate((D1,D2,D1),axis=1)
print(F)
print("#############")

输出为

[[1 1 1]
[2 2 2]]
(2, 3)
[1 1 1 2 2 2]
(6,)
[[1 1 1]]
(1, 3)
[[1]
[1]
[1]]
(3, 1)
D1=[[1]
[1]
[1]]
[[1]
[1]
[1]
[2]
[2]
[2]]
[[1 2]
[1 2]
[1 2]]
[[1 2 1]
[1 2 1]
[1 2 1]]
#############

6、array的分割

import numpy as np
g=np.arange(12).reshape((3,4))
print("g={0}".format(g))
print(np.split(g,2,axis=1))#切割成两列
print(np.split(g,3,axis=0))#切割成3行
print(np.array_split(g,3,axis=1))#切割成不等的3列
print(np.vsplit(g,3))#纵向分成3块
print(np.hsplit(g,2))#横向分成两块
print("##########")

输出为

g=[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
[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]])]
##########

7、numpy的copy,deep copy

import numpy as np
v=np.arange(4)
v1=v
v2=v1
v3=v2
v[0]=11
print(v)
print(v2)
v3=v.copy()#deep copy
v[1:3]=[22,33]
print(v3)
print(v)

输出为

7、逐个相乘和矩阵相乘

import numpy as np
#1.array创建
array=np.array([[1,2,3],[4,5,6]],dtype=np.float)
print(array)
print(array.shape)
print(array.dtype)
a=np.zeros((3,4))
b=np.ones((4,4))
print(a)
print(b)
c=np.arange(10,20,2)
print(c)
d=np.arange(12).reshape((3,4))
print(d)
e=np.linspace(1,10,6).reshape((2,3))
print(e)
f=np.array([1,2,3,4,5])
print(f==4)
print("c={0}".format(c))
print("f={0}".format(f))
g=c+f
print("g={0}".format(g))
h=10**np.sin(g)
print(h)
print("#############")

输出为

[11 1 2 3]
[11 1 2 3]
[11 1 2 3]
[11 22 33 3]

你可能感兴趣的:(python,numpy,python)