Which are from https://github.com/rougier/numpy-100.
%run `python -c "import numpy; numpy.info(numpy.add)"`
用到的时候直接把这行命令复制过去
Z = np.arange(50)
Z = Z[::-1]
print(Z)
Z[]中[]的含义是某种“选定”,这题展示的是一位的切片,实际上“选定”的条件可以更加复杂
Z = np.random.random((3,3,3))
print(Z)
hint: min, max
Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
hint: mean
Z = np.random.random(30)
m = Z.mean()
print(m)
hint: array[1:-1, 1:-1]
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
hint: np.pad
Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)
# Using fancy indexing
Z[:, [0, -1]] = 0
Z[[0, -1], :] = 0
print(Z)
0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
hint: NaN = not a number, inf = infinity
print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)
hint: np.diag
Z = np.diag(1+np.arange(4),k=-1)
print(Z)
hint: array[::2]
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
print(Z)
hint: np.unravel_index
print(np.unravel_index(99,(6,7,8)))
hint: np.tile
Z = np.tile( np.array([[0,1],[1,0]]), (4,4))
print(Z)
hint: (x -mean)/std
Z = np.random.random((5,5))
Z = (Z - np.mean (Z)) / (np.std (Z))
print(Z)
hint: np.dtype
color = np.dtype([("r", np.ubyte),
("g", np.ubyte),
("b", np.ubyte),
("a", np.ubyte)])
hint:
Z = np.dot(np.ones((5,3)), np.ones((3,2)))
print(Z)
# Alternative solution, in Python 3.5 and above
Z = np.ones((5,3)) @ np.ones((3,2))
print(Z)
hint: >, <
# Author: Evgeni Burovski
Z = np.arange(11)
Z[(3 < Z) & (Z < 8)] *= -1
print(Z)
# Author: Jake VanderPlas
print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
hint: np.sum
# Author: Jake VanderPlas
print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
No hints provided...
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
No hints provided...
print(np.array(0) / np.array(0))
print(np.array(0) // np.array(0))
print(np.array([np.nan]).astype(int).astype(float))
hint: np.uniform, np.copysign, np.ceil, np.abs, np.where
# Author: Charles R Harris
Z = np.random.uniform(-10,+10,10)
print(np.copysign(np.ceil(np.abs(Z)), Z))
# More readable but less efficient
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
hint: np.intersect1d
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print(np.intersect1d(Z1,Z2))
hint: np.seterr, np.errstate
# Suicide mode on
defaults = np.seterr(all="ignore")
Z = np.ones(1) / 0
# Back to sanity
_ = np.seterr(**defaults)
# Equivalently with a context manager
with np.errstate(all="ignore"):
np.arange(3) / 0
np.sqrt(-1) == np.emath.sqrt(-1)
hint: imaginary number
np.sqrt(-1) == np.emath.sqrt(-1)
hint: np.datetime64, np.timedelta64
yesterday = np.datetime64('today') - np.timedelta64(1)
today = np.datetime64('today')
tomorrow = np.datetime64('today') + np.timedelta64(1)