置信区间一文读懂
numpy 100题练习
上次发的numpy 100题练习一不知道大家学的咋样了大概又放在收藏夹里吃灰了吧,我们加班加点终于把后一半给翻译出来啦~希望各位观众老爷们喜欢~
Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)
random翻译创建一个表示位置(x,y)和颜(r,g,b)的结构化数组
Z = np.zeros(10, [ (position, [ (x, float, 1), (y, float, 1)]), (color, [ (r, float, 1), (g, float, 1), (b, float, 1)])])print(Z) Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
对一个表示坐标形状为(100,2)的随机向量,到点与点的距离
Z = np.random.random((10,2))X,Y = np.atleast_2d(Z[:,0], Z[:,1])D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)print (D)# 方法2,用scipy会快很多import scipyimport scipy.spatialD = scipy.spatial.d
istance.cdist(Z,Z)print (D)
How to convert a float (32 bits) array into an integer (32 bits) in place?(★★☆)
如何将32位的浮点数(float)转换为对应的整数(integer)?
Z = np.arange(10, dtype=np.int32)Z = Z.astype(np.float32, copy=False)print (Z)
How to read the following file? (★★☆)
如何读取以下文件?
1, 2, 3, 4, 56, , , 7, 8 , , 9,10,11Z = np.genfromtxt(s, delimiter=, dtype=np.int)print(Z)
What is the equivalent of enumerate for numpy arrays? (★★☆)对于numpy数组,enumerate的等价操作是什么?
Z = np.arange(9).reshape(3,3)for index, value in np.ndenumerate(Z): print (index, value)for index in np.ndindex(Z.shape): print (index, Z[index])
Generate a generic 2D Gaussian-like array (★★☆)
生成一个通用的二维Gaussian-like数组
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))D = np.sqrt(X*X+Y*Y)sigma, mu = 1.0, 0.0G = np.exp(-( (D-mu)**2 - ( 2.0 * sigma**2 ) ) )print (G) How to randomly place p elements in a 2D array? (★★☆)
对一个二维数组,如何在其内部随机放置p个元素?
n = 10p = 3Z = np.zeros((n,n))np.put(Z, np.random.choice(range(n*n), p, replace=False),1)print (Z) Subtract the mean of each row of a matrix (★★☆)
减去一个矩阵中的每一行的平均值
X = np.random.rand(5, 10)# numpy最新版本的方法Y = X - X.mean(axis=1, keepdims=True)print(Y)# numpy之前版本的方法Y = X
- X.mean(axis=1).reshape(-1, 1)print (Y)
How to I sort an array by the nth column? (★★☆)
如何通过第n列对一个数组进行排序?
Z = np.random.randint(0,10,(3,3))print (Z)print (Z[Z[:,1].argsort()])
How to tell if a given 2D array has null columns? (★★☆)
如何检查一个二维数组是否有空列?
Z = np.random.randint(0,3,(3,10))print ((~Z.any(axis=0)).any())
Find the nearest value from a given value in an a rray (★★☆)从数组中的给定值中出最近的值
Z = np.random.uniform(0,1,10)z = 0.5m = Z.flat[np.abs(Z - z).argmin()]print (m)
Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)
如何用迭代器(iterator)计算两个分别具有形状(1,3)和(3,1)的数组?
A = np.arange(3).reshape(3,1)
B = np.arange(3).reshape(1,3)it = np.nditer([A,B,None])for x,y,z in it: z[.] = x + yprint (it.operands[2])
Create an array class that has a name attribute (★★☆)
创建一个具有name属性的数组类
class NamedArray(np.ndarray): def __new__(cls, array, name=no name): obj = np.asarray(array).view(cls) www.doczj/doc/1ea55d474a2fb4daa58da0116c175f0e7dd11935.html = name return obj def __array_finalize__(self, obj): if obj is None: return www.doczj/doc/1ea55d474a2fb4daa58da0116c175f0e7dd11935.html = getattr(obj, name, no name)Z = NamedArray(np.arange(10), range_10)print (www.doczj/doc/1ea55d474a2fb4daa58da0116c175f0e7dd11935.html )
Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)考虑一个给定的向量,如何对由第二个向量索引的每个元素加1(小心重复的索引)?
Z = np.ones(10)I = np.random.randint(0,len(Z),20)Z += np.bincount(I, minlength=len(Z))print(Z)# 方法2np.add.at(Z, I, 1)print(Z)
How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)
根据索引列表(I),如何将向量(X)的元素累加到数组(F)?
X = [1,2,3,4,5,6]I = [1,3,9,3,4,1]F = np.bincount(I,X)print (F) Considering a (w,h,3) image of (dtype=ubyte), compute the number o f unique colors (★★★)
考虑一个(dtype=ubyte) 的 (w,h,3)图像,计算其唯一颜的数量
w,h = 16,16I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)F = I[.,0]*(256*256) + I[.,1]*256 +I[.,2]n = len(np.unique(F))print
Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
考虑一个四维数组,如何一次性计算出最后两个轴(axis)的和?
A = np.random.randint(0,10,(3,4,3,4))sum = A.sum(axis=(-2,-1))print (sum)# 方法2sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)print (sum)
Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论