https://github.com/rougier/numpy-100
How to ignore all numpy warnings (not recommended)?
1 2 3 4 5 6
|
defaults = np.seterr(all="ignore") Z = np.ones(1) / 0
_ = np.seterr(**defaults)
|
An equivalent way, with a context manager:
1 2
|
with np.errstate(divide='ignore'): Z = np.ones(1) / 0
|
Is the following expressions true?
1
|
np.sqrt(-1) == np.emath.sqrt(-1)
|
How to get the dates of yesterday, today and tomorrow?
1 2 3
|
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D') today = np.datetime64('today', 'D') tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
|
How to get all the dates corresponding to the month of July 2016?
1 2
|
Z = np.arange('2016-07', '2016-08', dtype='datetime64[D]') print(Z)
|
How to compute ((A+B)*(-A/2)) in place (without copy)?
1 2 3 4 5 6 7
|
A = np.ones(3)*1 B = np.ones(3)*2 C = np.ones(3)*3 np.add(A,B,out=B) np.divide(A,2,out=A) np.negative(A,out=A) np.multiply(A,B,out=A)
|
Extract the integer part of a random array using 5 different methods
1 2 3 4 5 6 7
|
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))
|
Create a 5x5 matrix with row values ranging from 0 to 4
1 2 3
|
Z = np.zeros((5,5)) Z += np.arange(5) print(Z)
|
Consider a generator function that generates 10 integers and use it to build an array
1 2 3 4 5
|
def (): for x in range(10): yield x Z = np.fromiter(generate(),dtype=float,count=-1) print(Z)
|
Create a vector of size 10 with values ranging from 0 to 1, both excluded
1 2
|
Z = np.linspace(0,1,11,endpoint=False)[1:] print(Z)
|
Create a random vector of size 10 and sort it
1 2 3
|
Z = np.random.random(10) Z.sort() print(Z)
|
近期评论