python基础语法(一)——— numpy的一些用法

1、np.arange生成序列:

使用格式为如np.arange[first:step:last)

a=np.arange(1,5,.5)

print(a)


一个例子,比较np.arange与range

a=np.arange(1,5,.5)     #步长可以为小数
print(a)

b=list(range(1,5,2))    #步长只能为整数
print(b)

结果:

E:\Python36\python.exe E:/Pycharm/test/test.py
[1.  1.5 2.  2.5 3.  3.5 4.  4.5]
[1, 3]


Process finished with exit code 0


2、np.zeros(N)

用来生成一些全为0的数字



3、np.linspace

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

a=np.linspace(1, 10, 5)
print(a)

[ 1.    3.25  5.5   7.75 10.  ]


a=np.linspace(1, 10, 9,endpoint=False)
print(a)
[1. 2. 3. 4. 5. 6. 7. 8. 9.]


a=np.linspace(1, 10, 9,retstep=True)    #retstep用来设置是否现实步长
print(a)
(array([ 1.   ,  2.125,  3.25 ,  4.375,  5.5  ,  6.625,  7.75 ,  8.875,

       10.   ]), 1.125)






4、np.random.normal()

mu, sigma = 0, .1
s = np.random.normal(loc=mu, scale=sigma, size=1000)
print(s)

结果

[ 1.34706614e-01  7.27098324e-03  5.03454840e-03  1.00849012e-01
 -8.27379913e-03  1.16168511e-02 -7.91991555e-02 -6.88258372e-02
 -2.36020778e-01 -1.54646594e-02  1.43802819e-01  1.06134402e-01
  1.13429440e-01 -7.34686819e-02 -6.02254276e-02 -4.94231847e-02

  1.52000387e-01  1.41815695e-01 -2.39745654e-02  2.25509004e-02 .........]


 
 

mu, sigma = 0, .1
s = np.random.normal(loc=mu, scale=sigma, size=(2,3))
print(s)

结果

[[ 0.20542985  0.01118872  0.02855233]
 [ 0.13037334 -0.02400409 -0.19085698]]





5、np.sqrt

np.sqrt对矩阵中每个元素都开方








猜你喜欢

转载自blog.csdn.net/qiurisiyu2016/article/details/80186238