numpy - syou2020/memo GitHub Wiki
https://www.machinelearningplus.com/python/101-numpy-exercises-python/ How to create a boolean array np.ones((3,3), dtype=bool)
复制数组 np.r_[np.repeat(a, 3), np.tile(a, 3)] ■ How to get the common items between two python numpy arrays: 取相同数据· np.intersect1d(a,b) ■去除在B中的数据: np.setdiff1d(a,b) 去除a中重复数据: np.unique(a) How to make a python function that handles scalars to work on numpy arrays: np.vectorize(funobj) How to swap two columns in a 2d numpy array: arr[:,[2,1,0]] How to extract all numbers between a given range from a numpy array?:a = np.arange(15) # Method 1 index = np.where((a >= 5) & (a <= 10)) a[index] # Method 2: index = np.where(np.logical_and(a>=5, a<=10)) a[index] #> (array([6, 9, 10]),) # Method 3: (thanks loganzk!) a[(a >= 5) & (a <= 10)]How to print only 3 decimal places in python numpy array? np.set_printoptions(precision=3)
How to pretty print a numpy array by suppressing the scientific notation (like 1e10)? np.set_printoptions(suppress=True)
如何向 Python NumPy 导入包含数字和文本的数据集,同时保持文本不变? How to import a dataset with numbers and texts keeping the text intact in python numpy? np.genfromtxt(url, delimiter=',', dtype='object')
How to find the percentile scores of a numpy array? np.percentile(arr,[50])
How to find the correlation between two columns of a numpy array? np.corrcoef(iris[:, 0], iris[:, 2])
■ How to convert a numeric to a categorical (text) array? 如何将一个数值转换为一个类别(文本)数组?
petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])
label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan} petal_length_cat = [label_map[x] for x in petal_length_bin]
■维度增加(1维变为2维,行数不变,增加一列) volume = volume[:, np.newaxis]
How to get the positions of top n values from a numpy array? np.argsort()[-n:] np.argpartition(-a, n)[:n] np.sort(a)[-n:] np.partition(a, kth=-n)[-n:]
如何在 2 维 NumPy 数组中找到每一行的最大值 np.amax(arr, axis=1) np.apply_along_axis(np.max, arr,axis=1) np.argmax(arr,axis=1)
如何在一个 1 维数组中找到所有的局部极大值(peak)? doublediff=np.diff(np.sign(np.diff(arr))) np.where(doublediff==-2)[0]+1
从字符数组中随机选取N个 np.take(list('abcdefgh'), np.random.randint(N, size=30))
np.random.random と np.random.randの違いは何? ▲42. How to do probabilistic sampling in numpy?
- 如何创建一个 Python 函数以对 NumPy 数组执行元素级的操作? pair_max = np.vectorize(maxx, otypes=[float]) a = np.array([5, 7, 9, 8, 6, 4, 5]) b = np.array([6, 3, 4, 8, 9, 7, 1]) pair_max(a, b)
能力大评估 https://www.machinelearningplus.com/python/101-pandas-exercises-python/