目录
【养成习惯,点赞再看!】
numpy.dot()函数
dot()返回的是两个数组的点积(dot product)
官方文档:
numpy.dot(a, b, out=None)
Dot product of two arrays. 两个数组的点积
Specifically, 具体来说 分为一维数组,二维数组,m x n 数组
If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation). 一维数组,
If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
eg
np.dot(3, 4)
output:12
np.dot([2j, 3j], [2j, 3j])
output:(-13+0j)
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]
np.dot(a, b)
output:array([[4, 1],
[2, 2]])
a = np.arange(3*4*5*6).reshape((3,4,5,6))
b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
np.dot(a, b)[2,3,2,1,2,2]
output:499128
sum(a[2,3,2,:] * b[1,2,:,2])
output:499128
time()函数
time()函数
返回当前的时间戳
用法:time.time()
eg
import time
a = time.time()
b = time.localtime(time.time())
c = time.asctime(time.localtime(time.time()))
d = time.ctime()
e = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())) #前面是你想要的格式
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
print(b.tm_year, b.tm_min, b.tm_mon)
print(e)
output:
a: 1619078526.7678187
b: time.struct_time(tm_year=2021, tm_mon=4, tm_mday=22, tm_hour=16, tm_min=2, tm_sec=6, tm_wday=3, tm_yday=112, tm_isdst=0)
c: Thu Apr 22 16:02:06 2021
d: Thu Apr 22 16:02:06 2021
2021 2 4
2021-04-22 16:02
for 循环 和 numpy 计算量的差异
import numpy as np
import time
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time() #向量化版本
c = np.dot(a, b)
toc = time.time()
print("vectorized version:" + str(1000*(toc-tic)) + " ms")
#非向量化版本
c = 0
tic = time.time()
for i in range(1000000):
c += a[i]*b[i]
toc = time.time()
print(c)
print("for loop:" + str(1000*(toc-tic)) + " ms")
output:
vectorized version:3.9949417114257812 ms
for loop:543.1191921234131 ms
np.random.rand()函数
返回一个或一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1)。使用方法同np.random.randn();
import numpy as np
a = np.random.rand()
print("a: ", a)
b = np.random.rand(3)
print("b:", b)
c = np.random.rand(2, 3)
print("c:", c)
output:
a: 0.9635017114350218
b: [0.95263675 0.75170318 0.83271804]
c: [[0.75873988 0.46740279 0.54203482]
[0.36647486 0.43679619 0.07545895]]
np.random.randn()函数
np.random.randn(d0,d1,d2……dn)
返回值服从标准正态分布
1)当函数括号内没有参数时,则返回一个浮点数;
2)当函数括号内有一个参数时,则返回秩为1的数组,不能表示向量和矩阵;
3)当函数括号内有两个及以上参数时,则返回对应维度的数组,能表示向量或矩阵;
4)np.random.standard_normal()函数与np.random.randn()类似,但是np.random.standard_normal()的输入参数为元组(tuple).
5)np.random.randn()的输入通常为整数,但是如果为浮点数,则会自动直接截断转换为整数。
import numpy as np
a = np.random.randn()
print("a:", a)
b = np.random.randn(3)
print("b:", b)
c = np.random.randn(2, 3)
print("c:", c)
d = np.random.randn(2, 3, 4)
print("d:", d)
output:
a: -0.4941425335240042
b: [-1.34647941 -1.45126857 0.10243555]
c: [[ 0.79812572 -0.97668914 1.32909788]
[-0.0548105 0.39446006 -0.16442411]]
d: [[[ 0.50301509 -1.29773418 -0.90685689 0.06235317]
[-0.14569457 -0.77423813 0.23141347 -0.52333856]
[ 0.86894426 1.62796432 0.59057944 1.47667696]]
[[-0.4851833 1.03867639 -1.15383316 0.15329894]
[ 0.7305964 0.21787382 0.34420941 -0.07978809]
[-0.67307447 -0.42196031 0.19062177 -1.03030617]]]
致谢:
https://blog.csdn.net/u012149181/article/details/78913167
https://blog.csdn.net/weixin_43598956/article/details/106686260