numpy学习笔记,为了tensorflow

废话少说,来

import numpy as np
a=np.arange(4) #产生0,1,2,3
c=a**2         #python的平方这样表达
print(c)

[0 1 4 9]

a = np.arange(24)  
print (a.ndim)             # a 现只有一个维度
#现在调整其大小
b = a.reshape(2,4,3)  # b 现在拥有三个维度
print (b.ndim)
import numpy as np  
a = np.array([[1,2,3],[4,5,6]])  
print (a.shape)

2行3列

a = np.array([[1,2,3],[4,5,6]]) 
b = a.reshape(3,2)  
print (b)

[[1 2]
[3 4]
[5 6]]
这里的2行3列变3行2列,注意是顺序取的。

# 数组的 dtype 为 int8(一个字节)  
x = np.array([1,2,3,4,5], dtype = np.int8)  
print (x.itemsize)
 
# 数组的 dtype 现在为 float64(八个字节) 
y = np.array([1,2,3,4,5], dtype = np.float64)  
print (y.itemsize)
# 默认为浮点数
x = np.zeros(5) 
print(x)
# 设置类型为整数
y = np.zeros((5,), dtype = np.int) 
print(y)
# 自定义类型
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])  
print(z)

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

 import numpy as np 
x = np.ones([3,2], dtype = int) 
print (x)

[[1 1]
[1 1]
[1 1]]
初始化一个3行2列的int型矩阵,其中内容全为1.

import numpy as np 
x =  [1,2,3] 
a = np.asarray(x)  
print (a)

[1 2 3],,将列表转化为ndarray(类似多维矩阵)

import numpy as np 
x =  [(1,2,3),(4,5)] 
a = np.asarray(x)  
print (a)

[(1, 2, 3) (4, 5)]将列表元组转化为ndarray

import numpy as np 
x =  [1,2,3] 
a = np.asarray(x, dtype =  float)  
print (a)

[ 1. 2. 3.]将数据类型进行转换。

import numpy as np
a = np.linspace(1,10,10)
print(a)

[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.],linspace是将1到10生成一个等差数列。
import numpy as np

import numpy as np
a = np.logspace(0,9,10,base=2)
print (a)

[ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]
logspace将生成从0到9,10个数,其底数为2。

a=np.linspace(1,10,6).reshape((2,3))
print(a)

[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]这个写法更常用。

a=np.array([[1,1],
           [0,1]])
b=np.arange(4).reshape((2,2))
c=a*b
c_dot=np.dot(a,b)
print(a)
print(b)
print(c)
print(c_dot)

[[1 1]
[0 1]]
[[0 1]
[2 3]]
[[0 1]
[0 3]]
[[2 4]
[2 3]]
以上可以看出,直接a*b得到的是对应位置相乘,而dot是矩阵的乘法运算。

a=np.random.random((2,4))
print(a)#生成随机数,默认从0到1
print(np.max(a))
print(np.min(a))
print(np.sum(a))
print(np.sum(a,axis=1))#axis=1表示对行进行操作
print(np.sum(a,axis=0))#axis=0表示对列进行操作

[[0.76292457 0.57398576 0.86433834 0.07714718]
[0.40482697 0.01157704 0.97195411 0.64192302]]
0.9719541081708305
0.011577037146380142
4.3086769793581094
[2.27839585 2.03028113]
[1.16775154 0.5855628 1.83629245 0.7190702 ]

a=np.arange(2,14).reshape((3,4))
print(np.argmin(a))
print(np.argmax(a))#这两个函数以后经常使用,他在tensorflow标签中充当下标的作用

0 11

a=np.arange(2,14).reshape((3,4))
print(a)
print(np.cumsum(a))#求前面的和
print(np.diff(a))#求两两之差
print(np.transpose(a))#将矩阵转置
print((a.T).dot(a))#a的转置乘以a,为4*4的矩阵
print(np.clip(a,4,9))#比4小的全变为4,比9大的全变为9
print(a[:,1])#行随便,列取第一列
print(a[1,1:3])#行取第一行,列取1,2.注意不包括3

[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[ 2 5 9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
[1 1 1]
[1 1 1]]
[[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]]
[[140 158 176 194]
[158 179 200 221]
[176 200 224 248]
[194 221 248 275]]
[[4 4 4 5]
[6 7 8 9]
[9 9 9 9]]
[ 3 7 11]
[7 8]

a=np.arange(2,14).reshape((3,4))
print(a.flatten())#把a变为一行,变平嘛

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

a=np.array([1,1,1])
b=np.array([2,2,2])
c=np.vstack((a,b))#纵向合并矩阵,vertical stack
d=np.hstack((a,b))#横向合并矩阵,horizontal stack
print(c)
print(d)
print(np.split(c,1,axis=1))#结果多了个逗号,对行进行操作,分割为2个矩阵

[[1 1 1]
[2 2 2]]
[1 1 1 2 2 2]
[array([[1, 1, 1],
[2, 2, 2]])]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值