numpy百题斩(二)

进阶部分

  1. 创建一个 5x5 的二维数组,其中边界值为1,其余值为0
Z = np.ones((5,5))
Z[1:-1,1:-1] = 0
Z
array([[1., 1., 1., 1., 1.],
       [1., 0., 0., 0., 1.],
       [1., 0., 0., 0., 1.],
       [1., 0., 0., 0., 1.],
       [1., 1., 1., 1., 1.]])
  1. 使用数字 0 将一个全为 1 的 5x5 二维数组包围
Z = np.ones((5,5))
Z = np.pad(Z,pad_width=2,mode = "constant",constant_values = 0) # pad_width表示用n行0包围
Z

array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.]])
  1. 创建一个 5x5 的二维数组,并设置值 1, 2, 3, 4 落在其对角线下方
Z= np.diag(1 + np.arange(5),k = 1)#np.info(np.diag)查看np.diag的用法
Z
array([[0, 1, 0, 0, 0, 0],
       [0, 0, 2, 0, 0, 0],
       [0, 0, 0, 3, 0, 0],
       [0, 0, 0, 0, 4, 0],
       [0, 0, 0, 0, 0, 5],
       [0, 0, 0, 0, 0, 0]])

 diag(v, k=0)

Extract a diagonal or construct a diagonal array.

See the more detailed documentation for ``numpy.diagonal`` if you use this
function to extract a diagonal and wish to write to the resulting array;
whether it returns a copy or a view depends on what version of numpy you
are using.

Parameters
----------
v : array_like
    If `v` is a 2-D array, return a copy of its `k`-th diagonal.
    If `v` is a 1-D array, return a 2-D array with `v` on the `k`-th
    diagonal.
k : int, optional
    Diagonal in question. The default is 0. Use `k>0` for diagonals
    above the main diagonal, and `k<0` for diagonals below the main
    diagonal.

Returns
-------
out : ndarray
    The extracted diagonal or constructed diagonal array.

See Also
--------
diagonal : Return specified diagonals.
diagflat : Create a 2-D array with the flattened input as a diagonal.
trace : Sum along diagonals.
triu : Upper triangle of an array.
tril : Lower triangle of an array.

Examples
--------
>>> x = np.arange(9).reshape((3,3))
>>> x
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

>>> np.diag(x)
array([0, 4, 8])
>>> np.diag(x, k=1)
array([1, 5])
>>> np.diag(x, k=-1)
array([3, 7])

>>> np.diag(np.diag(x))
array([[0, 0, 0],
       [0, 4, 0],
       [0, 0, 8]])
  1. 创建一个 10x10 的二维数组,并使得 1 和 0 沿对角线间隔放置
Z = np.zeros((10,10),dtype = int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
Z


array([[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]])
  1. 创建一个 0-10 的一维数组,并将 (1, 9] 之间的数全部反转成负数
Z = np.arange(11)
Z
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
Z[(1<Z) &(Z<=9)] *= -1
Z
array([ 0,  1, -2, -3, -4, -5, -6, -7, -8, -9, 10])
  1. 找出两个一维数组中相同的元素
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print("Z1:",Z1)
print("Z2:",Z2)
np.intersect1d(Z1,Z2)
Z1: [9 0 9 4 7 1 0 3 4 1]
Z2: [8 0 9 3 5 6 7 7 0 9]

array([0, 3, 7, 9])
  1. 使用 NumPy 打印昨天、今天、明天的日期
yesterday = np.datetime64("today","D") - np.timedelta64(1,"D")
today  = np.datetime64("today","D") 
tomorrw = np.datetime64("today","D") +np.timedelta64(1,"D")
print(yesterday)
print(today)
print(tomorrw)
2019-03-28
2019-03-29
2019-03-30
  1. 使用五种不同的方法去提取一个随机数组的整数部分
z = np.random.uniform(0,10,10)
z
array([3.41181266, 6.82533173, 1.34181518, 8.38217372, 8.27615988,
       8.89595855, 9.72320629, 5.45121909, 0.20435793, 3.61237419])
print("原始值:",z)
print("方法一:",z-z%1)
print("方法二:",np.floor(z))
print("方法三:",np.ceil(z)-1)
print("方法四:",z.astype(int))
print("方法五:",np.trunc(z))
原始值: [3.41181266 6.82533173 1.34181518 8.38217372 8.27615988 8.89595855
 9.72320629 5.45121909 0.20435793 3.61237419]
方法一: [3. 6. 1. 8. 8. 8. 9. 5. 0. 3.]
方法二: [3. 6. 1. 8. 8. 8. 9. 5. 0. 3.]
方法三: [3. 6. 1. 8. 8. 8. 9. 5. 0. 3.]
方法四: [3 6 1 8 8 8 9 5 0 3]
方法五: [3. 6. 1. 8. 8. 8. 9. 5. 0. 3.]
59. 创建一个 5x5 的矩阵,其中每行的数值范围从 15
z = np.zeros((5,5))
z += np.arange(1,6)
z
array([[1., 2., 3., 4., 5.],
       [1., 2., 3., 4., 5.],
       [1., 2., 3., 4., 5.],
       [1., 2., 3., 4., 5.],
       [1., 2., 3., 4., 5.]])
  1. 创建一个长度为 5 的等间隔一维数组,其值域范围从 0 到 1,但是不包括 0 和 1
z = np.linspace(0,1,6,endpoint =False)
z
array([0.        , 0.16666667, 0.33333333, 0.5       , 0.66666667,
       0.83333333])
z[1:]
array([0.16666667, 0.33333333, 0.5       , 0.66666667, 0.83333333])
  1. 创建一个长度为10的随机一维数组,并将其按升序排序
z = np.random.random(10)
z.sort()
print(z)#正序
print(z[::-1])#倒序
[0.12281895 0.20993708 0.29406792 0.30965091 0.50447096 0.5537515
 0.64390062 0.70902744 0.85633526 0.96974197]
[0.96974197 0.85633526 0.70902744 0.64390062 0.5537515  0.50447096
 0.30965091 0.29406792 0.20993708 0.12281895]
  1. 创建一个 3x3 的二维数组,并将列按升序排序
z = np.array([[7,4,3],[3,2,1],[4,2,6]])
print("原始数组:\n",z)
z.sort(axis =0)
z
原始数组:
 [[7 4 3]
 [3 2 1]
 [4 2 6]]

array([[3, 2, 1],
       [4, 2, 3],
       [7, 4, 6]])
  1. 创建一个长度为 5 的一维数组,并将其中最大值替换成 0
z = np.random.random(5)
print("原数组:",z)
z[z.argmax()] = 0
z
原数组: [0.67289415 0.60349904 0.98403777 0.95384253 0.88426089]

array([0.67289415, 0.60349904, 0.        , 0.95384253, 0.88426089])
  1. 打印每个 NumPy 标量类型的最小值和最大值
for dtype in [np.int8, np.int32, np.int64]:
   print("The minimum value of {}: ".format(dtype), np.iinfo(dtype).min)
   print("The maximum value of {}: ".format(dtype),np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
   print("The minimum value of {}: ".format(dtype),np.finfo(dtype).min)
   print("The maximum value of {}: ".format(dtype),np.finfo(dtype).max)
The minimum value of <class 'numpy.int8'>:  -128
The maximum value of <class 'numpy.int8'>:  127
The minimum value of <class 'numpy.int32'>:  -2147483648
The maximum value of <class 'numpy.int32'>:  2147483647
The minimum value of <class 'numpy.int64'>:  -9223372036854775808
The maximum value of <class 'numpy.int64'>:  9223372036854775807
The minimum value of <class 'numpy.float32'>:  -3.4028235e+38
The maximum value of <class 'numpy.float32'>:  3.4028235e+38
The minimum value of <class 'numpy.float64'>:  -1.7976931348623157e+308
The maximum value of <class 'numpy.float64'>:  1.7976931348623157e+308
  1. 将 float32 转换为整型
z = np.arange(10,dtype=np.float32)
print(z)
z = z.astype(np.int32,copy = False)
print(z)
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[0 1 2 3 4 5 6 7 8 9]
  1. 将随机二维数组按照第 3 列从上到下进行升序排列
z = np.random.randint(0,10,(5,5))
print("排序前:\n",z)
z[z[:,2].argsort()]
排序前:
 [[6 9 5 6 5]
 [9 3 1 7 7]
 [0 0 3 0 1]
 [8 3 9 1 8]
 [0 0 2 6 4]]

array([[9, 3, 1, 7, 7],
       [0, 0, 2, 6, 4],
       [0, 0, 3, 0, 1],
       [6, 9, 5, 6, 5],
       [8, 3, 9, 1, 8]])
  1. 从随机一维数组中找出距离给定数值(0.5)最近的数
z = np.random.uniform(0,1,20)
print("随机数:\n",z)
a = 0.5
m = z.flat[np.abs(z-a).argmin()]
print(m)
随机数:
 [0.74803699 0.11455862 0.99803515 0.14462869 0.5537112  0.30303048
 0.96955316 0.49956142 0.06782649 0.60886511 0.52904903 0.71247709
 0.11861379 0.07666849 0.59101115 0.26017775 0.87247072 0.38625768
 0.58177161 0.68263192]
0.49956141969291046
  1. 将二维数组的前两行进行顺序交换
A = np.arange(25).reshape(5,5)
print(A)
A[[0,1]] = A[[1,0]]
print(A)
[[ 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]]
[[ 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]]
  1. 找出随机一维数组中出现频率最高的值
z = np.random.randint(0,10,50)
print("随机一位数组:",z)
np.bincount(z).argmax()
随机一位数组: [8 2 5 1 2 3 4 0 0 1 2 4 2 4 5 7 9 7 1 8 3 3 0 8 5 0 2 5 4 1 3 7 4 0 1 0 4
 1 2 9 9 1 7 6 9 1 5 2 7 4]

1
  1. 找出给定一维数组中非 0 元素的位置索引
z= np.nonzero([1,2,0,3,0,6,2])
z
(array([0, 1, 3, 5, 6], dtype=int64),)
  1. 对于给定的 5x5 二维数组,在其内部随机放置 p 个值为 1 的数
p = 3
z = np.zeros((5,5))
np.put(z,np.random.choice(range(5*5),p,replace=False),3)
z
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 3., 0., 3.],
       [3., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
  1. 对于随机的 3x3 二维数组,减去数组每一行的平均值
x = np.random.rand(3,3)
print(x)
y = x-x.mean(axis = 1,keepdims =True)
y
[[0.95448416 0.28157391 0.11756407]
 [0.7902087  0.52327033 0.37404863]
 [0.9034529  0.88961937 0.4843657 ]]

array([[ 0.50327678, -0.16963347, -0.33364331],
       [ 0.22769948, -0.03923889, -0.18846059],
       [ 0.14430691,  0.13047338, -0.27478029]])
  1. 获得二维数组点积结果的对角线数组
A = np.random.uniform(0,1,(3,3))
B = np.random.uniform(0,1,(3,3))
print(A)
print(B)
np.dot(A,B)
[[0.50169276 0.02239518 0.69246797]
 [0.28224578 0.75533929 0.28013729]
 [0.48489567 0.61824741 0.30859325]]
[[0.47332788 0.05524409 0.92841459]
 [0.12871947 0.34830053 0.57392612]
 [0.47672191 0.33371663 0.57694793]]


array([[0.57046252, 0.26660389, 0.87815002],
       [0.36436925, 0.37216395, 0.85717467],
       [0.45620828, 0.34510622, 0.98305479]])
# 较慢的方法
np.diag(np.dot(A, B))
array([0.57046252, 0.37216395, 0.98305479])
np.sum(A*B.T,axis = 1)# 较快的方法
array([0.57046252, 0.37216395, 0.98305479])
# 更快的方法
np.einsum("ij, ji->i", A, B)
array([0.57046252, 0.37216395, 0.98305479])
  1. 找到随机一维数组中前 p 个最大值
z = np.random.randint(1,100,100)
print(z)
p = 5
z[np.argsort(z)[:-p:-1]]
[89 81 44 83 90 65 67 66 70 85 52  4 60 34 76 43 19  7  7 19  5 20 42 58
 62 46 41 74 43 69 33 68 37  3 35 82 60 14 38 11 74 98 71 71 36 81 15 35
 50 59 24 32 45 84 70 62 62 23 77 23 67 90 93 75 74 25  6 20  4 88 20 60
 97 32 39 68 21  7 37  1 67 47  6 70 91 80  8 88 65 50 49 84 34 96 67 26
 79 76 92 48]

array([98, 97, 96, 93])
  1. 计算随机一维数组中每个元素的 4 次方数值
x = np.random.randint(2,5,5)
print(x)
np.power(x,4)
[3 4 4 3 4]

array([ 81, 256, 256,  81, 256], dtype=int32)
  1. 对于二维随机数组中各元素,保留其 2 位小数
z = np.random.random((5,5))
print(z)
np.set_printoptions(precision=2)
print(z)
[[0.54053894 0.51640907 0.30630568 0.70994421 0.25277349]
 [0.96262252 0.65912837 0.41168413 0.42031458 0.26244   ]
 [0.66793675 0.93206986 0.71694181 0.28627584 0.90228097]
 [0.08131304 0.56829273 0.72174004 0.53821596 0.53626988]
 [0.63199761 0.22767542 0.42476124 0.24653665 0.78349649]]
[[0.54 0.52 0.31 0.71 0.25]
 [0.96 0.66 0.41 0.42 0.26]
 [0.67 0.93 0.72 0.29 0.9 ]
 [0.08 0.57 0.72 0.54 0.54]
 [0.63 0.23 0.42 0.25 0.78]]
  1. 使用科学记数法输出 NumPy 数组
z = np.random.random([5,5])
print(z)
z/1e3
[[0.35 0.22 0.73 0.66 0.02]
 [0.2  0.97 0.58 0.76 0.11]
 [0.95 0.79 0.23 0.39 0.28]
 [0.64 0.33 0.58 0.   0.35]
 [0.86 0.33 0.19 0.72 0.84]]

array([[3.47e-04, 2.18e-04, 7.29e-04, 6.61e-04, 1.66e-05],
       [2.03e-04, 9.74e-04, 5.83e-04, 7.59e-04, 1.08e-04],
       [9.48e-04, 7.93e-04, 2.28e-04, 3.94e-04, 2.82e-04],
       [6.38e-04, 3.33e-04, 5.82e-04, 1.96e-06, 3.47e-04],
       [8.57e-04, 3.29e-04, 1.91e-04, 7.16e-04, 8.45e-04]])
  1. 使用 NumPy 找出百分位数(25%,50%,75%)
a = np.arange(15)
print(a)
np.percentile(a,q = [25,50,75])

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

array([ 3.5,  7. , 10.5])
  1. 找出数组中缺失值的总数及所在位置
# 生成含有缺失值的2维数组
z = np.random.rand(10,10)
z[np.random.randint(10,size = 5),np.random.randint(10,size =5)] =np.nan
z
array([[0.57, 0.21, 0.21, 0.62, 0.55, 0.85, 0.17, 0.85, 0.93, 0.05],
       [0.91, 0.13, 0.23, 0.34, 0.1 , 0.82, 0.41, 0.52, 0.54, 0.58],
       [0.6 , 0.74, 0.27, 0.12, 0.63, 0.1 ,  nan, 0.86, 0.97, 0.35],
       [0.22, 0.45, 0.34, 0.39, 0.75, 0.93, 0.45, 0.52, 0.55, 0.82],
       [0.27, 0.39, 0.43, 0.56, 0.24, 0.37,  nan, 0.34, 0.7 , 0.79],
       [0.93, 0.39, 0.88, 0.1 , 0.54, 0.38, 0.93, 0.14, 0.99, 0.73],
       [ nan, 0.15, 0.92, 0.04, 0.6 , 0.86, 0.23, 0.74, 0.81, 0.59],
       [0.13, 0.07, 0.84,  nan, 0.05, 0.45, 0.63, 0.93,  nan, 0.34],
       [0.06, 0.51, 0.94, 0.11, 0.02, 0.15, 0.51, 0.24, 0.99, 0.62],
       [0.08, 0.25, 0.68, 0.34, 0.48, 0.55, 0.67, 0.37, 0.06, 0.16]])
print("缺失值总数:",np.isnan(z).sum())
print("缺失值索引:",np.where(np.isnan(z)))
缺失值总数: 5
缺失值索引: (array([2, 4, 6, 7, 7], dtype=int64), array([6, 6, 0, 3, 8], dtype=int64))
  1. 从随机数组中删除包含缺失值的行
# 沿用 79 题中的含缺失值的 2 维数组
z[np.sum(np.isnan(z),axis =1) ==0]
array([[0.57, 0.21, 0.21, 0.62, 0.55, 0.85, 0.17, 0.85, 0.93, 0.05],
       [0.91, 0.13, 0.23, 0.34, 0.1 , 0.82, 0.41, 0.52, 0.54, 0.58],
       [0.22, 0.45, 0.34, 0.39, 0.75, 0.93, 0.45, 0.52, 0.55, 0.82],
       [0.93, 0.39, 0.88, 0.1 , 0.54, 0.38, 0.93, 0.14, 0.99, 0.73],
       [0.06, 0.51, 0.94, 0.11, 0.02, 0.15, 0.51, 0.24, 0.99, 0.62],
       [0.08, 0.25, 0.68, 0.34, 0.48, 0.55, 0.67, 0.37, 0.06, 0.16]])
  1. 统计随机数组中的各元素的数量
z = np.random.randint(0,100,25).reshape(5,5)
print(z)
[[77 29 88 73 59]
 [71 93  7 56 12]
 [17 50 68 13  1]
 [67 97 26 27 25]
 [49 68 83 10 77]]
np.unique(z,return_counts=True)# 返回值中,第 2 个数组对应第 1 个数组元素的数量
(array([ 1,  7, 10, 12, 13, 17, 25, 26, 27, 29, 49, 50, 56, 59, 67, 68, 71,
        73, 77, 83, 88, 93, 97]),
 array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1,
        1], dtype=int64))
  1. 将数组中各元素按指定分类转换为文本值
# 指定类别如下
# 1 → 汽车
# 2 → 公交车
# 3 → 火车
z =np.random.randint(1,4,10)
print(z)
label_map = {1:"汽车",2:"公交车",3:"火车"}
[label_map[x] for x in z]
[2 2 3 2 2 2 2 3 1 3]

['公交车', '公交车', '火车', '公交车', '公交车', '公交车', '公交车', '火车', '汽车', '火车']
  1. 将多个 1 维数组拼合为单个 Ndarray
z1 = np.arange(3)
z2 = np.arange(3,7)
z3 = np.arange(7,10)

z =np.array([z1,z2,z3])
print(z)

[array([0, 1, 2]) array([3, 4, 5, 6]) array([7, 8, 9])]
np.concatenate(z)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  1. 打印各元素在数组中升序排列的索引
a = np.random.randint(100,size = 10)
print("Array:",a)
a.argsort()
Array: [42 86 84  9 28 69 25 76 40  4]

array([9, 3, 6, 4, 8, 0, 5, 7, 2, 1], dtype=int64)
  1. 得到二维随机数组各行的最大值
z = np.random.randint(1,100,[5,5])
print(z)
np.amax(z,axis =1)
[[64 57  2 27 67]
 [64 65 86 90 83]
 [12 30 69  1 11]
 [54 21 86 41 30]
 [ 7  6 78 16 27]]

array([67, 90, 69, 86, 78])
  1. 得到二维随机数组各行的最小值(区别上面的方法)
z = np.random.randint(1,100,[5,5])
print(z)
np.apply_along_axis(np.min,arr =z ,axis=1)
[[67 49 70 94  8]
 [76 10 79 50  6]
 [12 90 48 45 24]
 [20 23 44 24 27]
 [27  3 77  7  5]]

array([ 8,  6, 12, 20,  3])
  1. 计算两个数组之间的欧氏距离
a = np.array([1,2])
b = np.array([7,8])
#数学计算方法
print(np.sqrt(np.power((8-2),2) + np.power((7-1),2)))
# Numpy计算法
np.linalg.norm(b-a)
8.48528137423857

8.48528137423857
  1. 打印复数的实部和虚部
a = np.array([1 + 2j, 3 + 4j, 5 + 6j])

print("实部:", a.real)
print("虚部:", a.imag)
实部: [1. 3. 5.]
虚部: [2. 4. 6.]
  1. 求解给出矩阵的逆矩阵并验证

matrix = np.array([[1,2],[3,4]])
inverse_matrix = np.linalg.inv(matrix)
# 验证原矩阵和逆矩阵的点积是否为单位矩阵
assert np.allclose(np.dot(matrix, inverse_matrix), np.eye(2))

inverse_matrix
array([[-2. ,  1. ],
       [ 1.5, -0.5]])
  1. 使用 Z-Score 标准化算法对数据进行标准化处理

Z-Score 标准化公式:
Z = X − m e a n ( X ) s d ( X ) Z = \frac{X-\mathrm{mean}(X)}{\mathrm{sd}(X)} Z=sd(X)Xmean(X)

# 根据公式定义函数
def zscore(x,axis = None):
    xmean = x.mean(axis =axis,keepdims = True)
    xstd = np.std(x,axis = axis,keepdims = True)
    zscore = (x - xmean)/xstd
    return zscore
z = np.random.randint(10,size = (5,5))
print(z)
zscore(z)
[[2 1 3 3 1]
 [3 8 3 2 5]
 [6 7 3 6 1]
 [6 6 0 2 3]
 [0 6 5 8 3]]

array([[-0.73, -1.15, -0.31, -0.31, -1.15],
       [-0.31,  1.81, -0.31, -0.73,  0.54],
       [ 0.97,  1.39, -0.31,  0.97, -1.15],
       [ 0.97,  0.97, -1.58, -0.73, -0.31],
       [-1.58,  0.97,  0.54,  1.81, -0.31]])
  1. 使用 Min-Max 标准化算法对数据进行标准化处理

Min-Max 标准化公式:
Y = Z − min ⁡ ( Z ) max ⁡ ( Z ) − min ⁡ ( Z ) Y = \frac{Z-\min(Z)}{\max(Z)-\min(Z)} Y=max(Z)min(Z)Zmin(Z)

def min_max(x,axis = None):
    min = x.min(axis = axis,keepdims = True)
    max = x.max(axis = axis,keepdims = True)
    result = (x - min)/(max - min)
    return result

#生成随机数据
z = np.random.randint(10,size = (5,5))
print(z)
min_max(z)
[[1 2 5 8 4]
 [0 1 4 5 7]
 [3 3 1 5 8]
 [7 9 9 0 9]
 [5 2 1 7 7]]

array([[0.11, 0.22, 0.56, 0.89, 0.44],
       [0.  , 0.11, 0.44, 0.56, 0.78],
       [0.33, 0.33, 0.11, 0.56, 0.89],
       [0.78, 1.  , 1.  , 0.  , 1.  ],
       [0.56, 0.22, 0.11, 0.78, 0.78]])
  1. 使用 L2 范数对数据进行标准化处理

L2 范数计算公式:
L 2 = x 1 2 + x 2 2 + … + x i 2 L_2 = \sqrt{x_1^2 + x_2^2 + \ldots + x_i^2} L2=x12+x22++xi2

# 根据公式定义函数
def l2_normalize(v,axis = -1,order =2):
    l2 = np.linalg.norm(v,ord = order,axis = axis,keepdims =True)
    l2[l2 == 0] =1
    return v/l2
#生成随机数据
z = np.random.randint(10,size = [5,5])
print(z)

l2_normalize(z)
[[6 4 7 7 7]
 [4 5 7 9 6]
 [8 1 5 0 3]
 [4 6 5 4 4]
 [7 2 4 8 9]]

array([[0.43, 0.28, 0.5 , 0.5 , 0.5 ],
       [0.28, 0.35, 0.49, 0.63, 0.42],
       [0.8 , 0.1 , 0.5 , 0.  , 0.3 ],
       [0.38, 0.57, 0.48, 0.38, 0.38],
       [0.48, 0.14, 0.27, 0.55, 0.62]])
  1. 使用 NumPy 计算变量直接的相关性系数
Z = np.array([
    [1, 2, 1, 9, 10, 3, 2, 6, 7], # 特征 A
    [2, 1, 8, 3, 7, 5, 10, 7, 2], # 特征 B
    [2, 1, 1, 8, 9, 4, 3, 5, 7]]) # 特征 C

np.corrcoef(Z)
array([[ 1.  , -0.06,  0.97],
       [-0.06,  1.  , -0.01],
       [ 0.97, -0.01,  1.  ]])

相关性系数取值从 [-1, 1] 变换,靠近 1 则代表正相关性较强,-1 则代表负相关性较强。结果如下所示,变量 A 与变量 A 直接的相关性系数为 1,因为是同一个变量。变量 A 与变量 C 之间的相关性系数为 0.97,说明相关性较强。

  1. 使用 NumPy 计算矩阵的特征值和特征向量
M = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
w,v = np.linalg.eig(M)
# w对应特征值,v对应特征向量
print(w)
print(v)
[ 1.61e+01 -1.12e+00 -1.30e-15]
[[-0.23 -0.79  0.41]
 [-0.53 -0.09 -0.82]
 [-0.82  0.61  0.41]]

我们可以通过 P'AP=M 公式反算,验证是否能得到原矩阵。

v * np.diag(w)*np.linalg.inv(v)
matrix([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])
  1. 使用 NumPy 计算 Ndarray 两相邻元素差值
z = np.random.randint(1,10,10)
print(z)

# 计算z两相邻元素之间的差值
print(np.diff(z,n =1))
# 重复计算2次
print(np.diff(z,n = 2))

#重复计算三次
print(np.diff(z,n = 3))
[6 5 5 8 5 5 1 5 8 9]
[-1  0  3 -3  0 -4  4  3  1]
[ 1  3 -6  3 -4  8 -1 -2]
[ 2 -9  9 -7 12 -9 -1]
  1. 使用 NumPy 将 Ndarray 相邻元素依次累加
z = np.random.randint(1,10,10)
print(z)
"""
[第一个元素, 第一个元素 + 第二个元素, 第一个元素 + 第二个元素 + 第三个元素, ...]
"""
np.cumsum(Z)
[1 6 1 2 9 5 8 9 7 3]

array([  1,   3,   4,  13,  23,  26,  28,  34,  41,  43,  44,  52,  55,
        62,  67,  77,  84,  86,  88,  89,  90,  98, 107, 111, 114, 119,
       126], dtype=int32)
  1. 使用 NumPy 按列连接两个数组
M1 = np.array([1,2,3])
M2 = np.array([4,5,6])
np.c_[M1,M2]
array([[1, 4],
       [2, 5],
       [3, 6]])
  1. 使用 NumPy 按行连接两个数组
M1 = np.array([1, 2, 3])
M2 = np.array([4, 5, 6])

np.r_[M1, M2]
array([1, 2, 3, 4, 5, 6])
  1. 使用 NumPy 打印九九乘法表
np.fromfunction(lambda i, j:(i+1)*(j+1),(9,9) )
array([[ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
       [ 2.,  4.,  6.,  8., 10., 12., 14., 16., 18.],
       [ 3.,  6.,  9., 12., 15., 18., 21., 24., 27.],
       [ 4.,  8., 12., 16., 20., 24., 28., 32., 36.],
       [ 5., 10., 15., 20., 25., 30., 35., 40., 45.],
       [ 6., 12., 18., 24., 30., 36., 42., 48., 54.],
       [ 7., 14., 21., 28., 35., 42., 49., 56., 63.],
       [ 8., 16., 24., 32., 40., 48., 56., 64., 72.],
       [ 9., 18., 27., 36., 45., 54., 63., 72., 81.]])
  1. 使用 NumPy 将实验楼 LOGO 转换为 Ndarray 数组
from io import BytesIO
from PIL import Image
import PIL,requests
# 通过链接下载图像
URL = 'https://upload.jianshu.io/users/upload_avatars/4356789/7180cf5d-ad9d-45e7-b9d7-892e6f141885.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240'
response = requests.get(URL)
# 将内容读取为图像
I = Image.open(BytesIO(response.content))
# 将图像转换为 Ndarray
Datawhale= np.asarray(I)
Datawhale
array([[[ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40],
        ...,
        [ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40]],

       [[ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40],
        ...,
        [ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40]],

       [[ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40],
        ...,
        [ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40]],

       ...,

       [[ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40],
        ...,
        [ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40]],

       [[ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40],
        ...,
        [ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40]],

       [[ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40],
        ...,
        [ 2, 32, 40],
        [ 2, 32, 40],
        [ 2, 32, 40]]], dtype=uint8)

本文章主要作为学习历程的记录,如有侵权,请联系作者删除,原文地址https://www.shiyanlou.com/courses/

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值