例题1
8*8棋盘矩阵,其中1、3、5、7行&&0、2、4、6列的元素值为1, 1、3、5、7列&&0、2、4、6行的元素值也为1。示例代码:
>>>import numpy as np
>>>z = np.zeros((8, 8), dtype = int)
>>>z[1::2, ::2] = 1
>>>z[::2, 1::2] = 1
>>>z
array([[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]])
例题2
归一化,将矩阵规格化到0~1,即最小的变成0,最大的变成1,最小与最大的等比缩放。示例代码:
>>>z = 10 * np.random.rand(5, 5)
>>>z
array([[ 6.07608738, 0.07881604, 2.36949897, 6.5653295 , 3.48561715],
[ 9.6485403 , 1.85315985, 8.38311931, 5.92832551, 7.34598467],
[ 9.50648487, 8.68362313, 5.71049914, 9.18061123, 1.22047609],
[ 7.77896973, 7.16390673, 2.50980284, 9.20855074, 6.00073204],
[ 5.4355661 , 9.17817416, 2.76074893, 0.65534421, 4.09743307]])
>>>zmin, zmax = z.min(), z.max()
>>>z = (z - zmin)/(zmax - zmin)
>>>z
array([[ 0.62669218, 0. , 0.2393677 , 0.67781613, 0.35599783],
[ 1. , 0.18541222, 0.86776829, 0.61125162, 0.75939164],
[ 0.98515574, 0.8991698 , 0.58848959, 0.95110318, 0.11929916],
[ 0.80463694, 0.74036519, 0.25402893, 0.95402275, 0.61881783],
[ 0.55976013, 0.95084852, 0.28025185, 0.06024501, 0.41993028]])
例题3
交换矩阵中其中的两行。示例代码:
>>>a = np.arange(25).reshape((5, 5))
>>>a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
>>>a[[0, 1]] = a[[1, 0]] #第一行和第二行元素进行对调
>>>a
array([[ 5, 6, 7, 8, 9],
[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
例题4
找出数组中与给定值最接近的数。示例代码:
>>>z = np.arange(8).reshape((2, 4))
>>>z
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>>a = 5.1
>>>np.abs(z - a).argmin()
例题5
判断二维数组中,有没有一整列元素值为0。示例代码:
>>>z = np.random.randint(0, 3, size = (2, 10))
>>>z
array([[1, 2, 2, 2, 2, 0, 0, 1, 2, 2],
[0, 1, 2, 1, 1, 1, 2, 1, 2, 2]])
>>>z.any(axis = 0)
array([ True, True, True, True, True, True, True, True, True, True], dtype=bool)
说明,通过布尔值判断列值是否都为0,如果都为0,则为False,反之为True。
例题6
生成二维的高斯矩阵。示例代码:
>>>x, y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
>>>D = np.sqrt(x**2 + y**2)
>>>sigma, mu = 1, 0 #标准差为1,均值为0
>>>a = np.exp(-(D-mu)**2/(2*sigma**2))
>>>a
array([[ 0.36787944, 0.44822088, 0.51979489, 0.57375342, 0.60279818,
0.60279818, 0.57375342, 0.51979489, 0.44822088, 0.36787944],
[ 0.44822088, 0.54610814, 0.63331324, 0.69905581, 0.73444367,
0.73444367, 0.69905581, 0.63331324, 0.54610814, 0.44822088],
[ 0.51979489, 0.63331324, 0.73444367, 0.81068432, 0.85172308,
0.85172308, 0.81068432, 0.73444367, 0.63331324, 0.51979489],
[ 0.57375342, 0.69905581, 0.81068432, 0.89483932, 0.9401382 ,
0.9401382 , 0.89483932, 0.81068432, 0.69905581, 0.57375342],
[ 0.60279818, 0.73444367, 0.85172308, 0.9401382 , 0.98773022,
0.98773022, 0.9401382 , 0.85172308, 0.73444367, 0.60279818],
[ 0.60279818, 0.73444367, 0.85172308, 0.9401382 , 0.98773022,
0.98773022, 0.9401382 , 0.85172308, 0.73444367, 0.60279818],
[ 0.57375342, 0.69905581, 0.81068432, 0.89483932, 0.9401382 ,
0.9401382 , 0.89483932, 0.81068432, 0.69905581, 0.57375342],
[ 0.51979489, 0.63331324, 0.73444367, 0.81068432, 0.85172308,
0.85172308, 0.81068432, 0.73444367, 0.63331324, 0.51979489],
[ 0.44822088, 0.54610814, 0.63331324, 0.69905581, 0.73444367,
0.73444367, 0.69905581, 0.63331324, 0.54610814, 0.44822088],
[ 0.36787944, 0.44822088, 0.51979489, 0.57375342, 0.60279818,
0.60279818, 0.57375342, 0.51979489, 0.44822088, 0.36787944]])
- 读取csv文件,将数组中元素为NAN的值替换成数值0,并且求数组中元素最大值。示例代码:
>>>arr = np.genfromtxt("123.csv", delimiter = ",", dtype = "String", skip_header = 1)
>>>empty_value = np.isnan(arr)
>>>arr[empty_value] = '0'
>>>arr = arr.astype(float)
>>>arr
>>>arr.max()
网友评论