python-numpy最全攻略十-random_sample, ranf, bitwise

2021-01-08 10:18:30 浏览数 (1)

参考链接: Python中的numpy.right_shift

np.random_sample() 

 importing numpy 

import numpy as np

# output random value 

out_val = np.random.random_sample() 

print ("Output random float value : ", out_val)

Output random float value :  0.2450768662139805

import numpy as geek 

# output array 

out_arr = geek.random.random_sample(size =(1, 3)) 

print ("Output 2D Array filled with random floats : ", out_arr)  

Output 2D Array filled with random floats :  [[0.15468058 0.26536462 0.54954387]]

import numpy as geek 

# output array 

out_arr = geek.random.random_sample((3, 2, 1)) 

print ("Output 3D Array filled with random floats : ", out_arr)  

Output 3D Array filled with random floats :  [[[0.6380224 ]

  [0.73029307]]

 [[0.51149673]

  [0.3413279 ]]

 [[0.20853883]

  [0.61981115]]]

random.ranf() 

# numpy.random.ranf() is one of the function for doing random sampling in numpy. It returns an array of specified shape 

# and fills it with random floats in the half-open interval [0.0, 1.0).

import numpy as np

# output random float value 

out_val = np.random.ranf() 

print ("Output random float value : ", out_val)  

Output random float value :  0.44112568416235265

# importing numpy 

import numpy as np

# output array 

out_arr = np.random.ranf(size =(2, 1)) 

print ("Output 2D Array filled with random floats : ", out_arr)  

Output 2D Array filled with random floats :  [[0.69303583]

 [0.8020658 ]]

import numpy as geek 

# output array 

out_arr = geek.random.ranf((3, 3, 2)) 

print ("Output 3D Array filled with random floats : ", out_arr)  

Output 3D Array filled with random floats :  [[[0.50709171 0.02493862]

  [0.51112692 0.8210353 ]

  [0.98668934 0.20536282]]

 [[0.0707417  0.38774696]

  [0.01399582 0.14022261]

  [0.47580447 0.70451949]]

 [[0.07844355 0.28663839]

  [0.84763223 0.98383207]

  [0.6413255  0.63548128]]]

numpy.random.random.randint()

np.bitwise-function 

# Python code to demonstrate bitwise-function 

import numpy as np 

# construct an array of even and odd numbers 

even = np.array([0, 2, 4, 6, 8, 16, 32]) 

odd = np.array([1, 3, 5, 7, 9, 17, 33]) 

# bitwise_and 

print('bitwise_and of two arrays: ') 

print(np.bitwise_and(even, odd)) 

# bitwise_or 

print('bitwise_or of two arrays: ') 

print(np.bitwise_or(even, odd)) 

# bitwise_xor 

print('bitwise_xor of two arrays: ') 

print(np.bitwise_xor(even, odd)) 

# invert or not 

print('inversion of even no. array: ') 

print(np.invert(even)) 

# left_shift  

print('left_shift of even no. array: ') 

print(np.left_shift(even, 1)) 

# right_shift  

print('right_shift of even no. array: ') 

print(np.right_shift(even, 1)) 

bitwise_and of two arrays: 

[ 0  2  4  6  8 16 32]

bitwise_or of two arrays: 

[ 1  3  5  7  9 17 33]

bitwise_xor of two arrays: 

[1 1 1 1 1 1 1]

inversion of even no. array: 

[ -1  -3  -5  -7  -9 -17 -33]

left_shift of even no. array: 

[ 0  4  8 12 16 32 64]

right_shift of even no. array: 

[ 0  1  2  3  4  8 16]

0 人点赞