1.np.random.seed(n)的理解
np.random.seed(n)函数用于生成指定随机数,把seed()中的参数比喻成“堆”;eg. seed(5):表示第5堆种子。利用随机数种子,每次生成的随机数相同
import numpy as np
if __name__ == '__main__':
i = 0
while (i < 6):
if (i < 3):
np.random.seed(0)
print(np.random.randn(1, 5))
else:
print(np.random.randn(1, 5))
pass
i += 1
print("---------1--------")
i = 0
while (i < 2):
print(np.random.randn(1, 5))
i += 1
print(np.random.randn(2, 5))
np.random.seed(0)
print("---------2--------")
i = 0
while (i<8):
print(np.random.randn(1, 5))
i += 1
输出:
[[1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]]
[[1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]]
[[1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]]
[[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
[[0.14404357 1.45427351 0.76103773 0.12167502 0.44386323]]
[[ 0.33367433 1.49407907 -0.20515826 0.3130677 -0.85409574]]
---------1--------
[[-2.55298982 0.6536186 0.8644362 -0.74216502 2.26975462]]
[[-1.45436567 0.04575852 -0.18718385 1.53277921 1.46935877]]
[[ 0.15494743 0.37816252 -0.88778575 -1.98079647 -0.34791215]
[ 0.15634897 1.23029068 1.20237985 -0.38732682 -0.30230275]]
---------2--------
[[1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]]
[[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
[[0.14404357 1.45427351 0.76103773 0.12167502 0.44386323]]
[[ 0.33367433 1.49407907 -0.20515826 0.3130677 -0.85409574]]
[[-2.55298982 0.6536186 0.8644362 -0.74216502 2.26975462]]
[[-1.45436567 0.04575852 -0.18718385 1.53277921 1.46935877]]
[[ 0.15494743 0.37816252 -0.88778575 -1.98079647 -0.34791215]]
[[ 0.15634897 1.23029068 1.20237985 -0.38732682 -0.30230275]]
可以看出第一个seed(0)在i<3的时候有效,将生成的随机数复制了三遍;第二个seed(0)是将上面的(6+2=)8组随机数组复制了三遍
2seed(0)是对其后面的语句有效。如下:
# import numpy as np
#
# if __name__ == '__main__':
# i = 0
# while (i < 6):
# if (i < 3):
# np.random.seed(0)
# print(np.random.randn(1, 5))
# else:
# print(np.random.randn(1, 5))
# pass
# i += 1
# print("---------1--------")
# i = 0
# while (i < 2):
# print(np.random.randn(1, 5))
# i += 1
# print(np.random.randn(2, 5))
# np.random.seed(0)
# print("---------2--------")
# i = 0
# while (i<8):
# print(np.random.randn(1, 5))
# i += 1
import numpy as np
if __name__ == '__main__':
i=0
while (i<3):
print(np.random.randn(1,5))
i+=1
print("-----------")
i=0
np.random.seed(0)
while(i<3):
print(np.random.randn(1,5))
i+=1
print("-----------")
np.random.seed(0)
print(np.random.randn(3,5))
输出:
[[-0.9536341 -0.60058234 0.22583927 -0.54636182 1.58224466]]
[[-0.00902322 -1.04163799 -0.98666388 -0.4519225 -0.1888058 ]]
[[-0.07268378 1.95723857 0.31272976 -1.22294344 0.33528483]]
-----------
[[1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]]
[[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
[[0.14404357 1.45427351 0.76103773 0.12167502 0.44386323]]
-----------
[[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799]
[-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]
[ 0.14404357 1.45427351 0.76103773 0.12167502 0.44386323]]
可以看出,只是对第二个for循环的数组进行了复制。
3.if name==‘main’:的理解
(1)test.py
print("---this is test1----")
print('__name__:',__name__)
if __name__=='__main__':
print("---this is main---")
输出:
(2).import test.py
import test1
可以看出在test1.py中,所有代码都有运行一遍,而在import中,没有运行“if name==‘main’:”下面的语句,并且__name__的内容也不一样。
我自己总结: