标签:sum end 改变 des dom div 扩展程序 二维数组 括号
import numpy as np np.array([1,2,3])
np.array([[1,2,3],[4,5,6]])
注意:
import matplotlib.pylab as plt # 读取图片二进制数据 img_arr = plt.imread(‘img_dir‘) # 将图片展示 plt.imshow(img_arr) # 将图片所有数据减少 plt.imshow(img_arr - 100) # 操作该numpy数据,该操作会同步到图片中
np.linspace(0,100,num=20) out: array([ 0. , 5.26315789, 10.52631579, 15.78947368, 21.05263158, 26.31578947, 31.57894737, 36.84210526, 42.10526316, 47.36842105, 52.63157895, 57.89473684, 63.15789474, 68.42105263, 73.68421053, 78.94736842, 84.21052632, 89.47368421, 94.73684211, 100. ])
np.arange(0,100,2) out: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98])
#固定随机性 #随机因子:系统的时间 np.random.seed(100) arr = np.random.randint(0,100,size=(4,5))
np.random.random(size=(4,5)) out: array([[0.56229626, 0.00581719, 0.30742321, 0.95018431, 0.12665424], [0.07898787, 0.31135313, 0.63238359, 0.69935892, 0.64196495], [0.92002378, 0.29887635, 0.56874553, 0.17862432, 0.5325737 ], [0.64669147, 0.14206538, 0.58138896, 0.47918994, 0.38641911]])
arr.shape
arr.size
img_arr.size
arr.dtype
print(arr) out: array([[ 8, 24, 67, 87, 79], [48, 10, 94, 52, 98], [53, 66, 98, 14, 34], [24, 15, 60, 58, 16]])
print(arr[1])
out: array([48, 10, 94, 52, 98])
#获取二维数组前两行 arr[0:2] #获取二维数组前两列 arr[:,0:2] #arr[hang,lie] #获取二维数组前两行和前两列数据 arr[0:2,0:2] #将数组的行倒序 arr[::-1] #列倒序 arr[:,::-1] #全部倒序 arr[::-1,::-1]
import numpy as np arr_1 = np.random.randint(0, 100, size=(20,)) arr_1.reshape((2, 10)) # 把其中一维的数据设为-1,会自动计算 arr_1.reshape((5, -1))
2. 将多维数组变形成一维数组
arr_1 = arr.reshape(20,)
# 按照行来进行级联 np.concatenate((arr,arr,arr),axis=1) # 按照列来进行级联 np.concatenate((arr,arr,arr),axis=0)
级联需要注意的点:
arr.sum(axis=1)
arr.max()
arr.min()
其他聚合操作
Function Name NaN-safe Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute mean of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
np.power 幂运算
np.sort()与ndarray.sort()都可以,但有区别:
np.sort(arr,axis=0)
标签:sum end 改变 des dom div 扩展程序 二维数组 括号
原文地址:https://www.cnblogs.com/lucas-big-data/p/14230426.html