一些Numpy用法(二)

1.使用5种不同的方法提取随机数组的整数部分

>>>Z = np.random.uniform(0,10,10)

>>>print (Z - Z%1)
>>>print (np.floor(Z))
>>>print (np.ceil(Z)-1)
>>>print (Z.astype(int))
>>>print (np.trunc(Z))

[7. 3. 2. 6. 5. 8. 3. 0. 7. 9.]
[7. 3. 2. 6. 5. 8. 3. 0. 7. 9.]
[7. 3. 2. 6. 5. 8. 3. 0. 7. 9.]
[7 3 2 6 5 8 3 0 7 9]
[7. 3. 2. 6. 5. 8. 3. 0. 7. 9.]

2.创建一个大小为10的向量,其值从0到1不等,这两个值都被排除在外

>>>Z = np.linspace(0,1,11,endpoint=False)[1:]
>>>print(Z)

[0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455
 0.63636364 0.72727273 0.81818182 0.90909091]

3.创建一个大小为10的随机向量并对其排序

>>>Z = np.random.random(10)
>>>Z.sort()
>>>print(Z)

[0.01820192 0.11438729 0.16666859 0.16765364 0.17045471 0.17213683
 0.54831481 0.70133871 0.92568074 0.96673577]

4.如何比np.sum更快地对一个小数组求和?

>>>Z = np.arange(10)
>>>print(np.add.reduce(Z))

45

5.使数组不可变

>>>Z = np.zeros(10)
>>>Z.flags.writeable = False
>>>Z[0]== 1
>>>print(Z)

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

 

你可能感兴趣的:(python)