<jupyter_start><jupyter_text>numpynumpy定义了python进行矩阵数值计算的基础> np.add(A,B)> np.subtract(A,B)> np.dot(C,D)> np.matmul(C,D)            C@D> np.multiply(C,D)          C*D<jupyter_code>import numpy as np
A = np.array([1,2,3])
B = np.array([4,3,0])
print('A+B = ', np.add(A,B))
print('A-B = ', np.subtract(A,B))
C = np.array([[1,2,3],
              [1,1,1]])
D = np.array([[4,3,9,9],
              [1,1,2,2],
              [4,2,1,1]])
# 矩阵乘法
print('np.dot(C,D) = \n',np.dot(C,D))
print('np.matmul(C,D) = \n',np.matmul(C,D))
print('        C@D    = \n',np.matmul(C,D))
# 元素与元素相乘
D = np.array([[4,3,9,9],
              [1,1,2,2],
              [4,2,1,1]])
print('np.multiply(D,D)= \n',np.multiply(D,D))
print('          D*D   = \n', D*D)<jupyter_output>np.multiply(D,D)= 
 [[16  9 81 81]
 [ 1  1  4  4]
 [16  4  1  1]]
          D*D   = 
 [[16  9 81 81]
 [ 1  1  4  4]
 [16  4  1  1]]<jupyter_text>np.dot 和 np.matmul的区别* 都是矩阵乘法* np.matmul中，禁止矩阵与标量的乘法* np.matmul中，在矢量乘矢量的内积运算中，matmul与dot没有差别* np.matmul中，多维的矩阵，将前n-2维视为后2维的元素后，进行乘法操作  矢量内积无区别，结果是标量<jupyter_code>t1 = np.array([1,2,3])
t2 = np.array([1,2,3])
print(np.dot(t1,t2))
print(np.matmul(t1,t2))
print(t1*t2) # 元素1 1 乘积<jupyter_output>14
14
[1 4 9]<jupyter_text>1维与2维矩阵的乘法<jupyter_code>a = np.array([1,2,3])
b = np.array([[1,1,1],
              [2,2,2],
              [3,3,3]])
print("np.dot(a,b): ", np.dot(a,b))
print("np.matmul(a,b): ", np.matmul(a,b))
#注意
a*b # 扩展维度后，元素乘积
print("a*b: \n", a*b)<jupyter_output>np.dot(a,b):  [14 14 14]
np.matmul(a,b):  [14 14 14]
a*b: 
 [[1 2 3]
 [2 4 6]
 [3 6 9]]<jupyter_text>多维矩阵乘积的比较<jupyter_code>m_4_4 = np.array([
                       [1,2,3,4],
                       [3,2,1,4],
                       [5,4,6,7],
                       [11,12,13,14]
                      ])


m_3_4_2 = np.array([
                             [[2,3],
                              [11,9],
                              [32,21],
                              [28,17]],
    
                             [[2,3],
                              [1,9],
                              [3,21],
                              [28,7]],
    
                             [[2,3],
                              [1,9],
                              [3,21],
                              [28,7]]
                            ])
print(m_4_4.shape)
print(m_3_4_2.shape)
print('4x4 * 3x4x2 dot:\n {}\n'.format(np.dot(m_4_4,m_3_4_2)))
print('4x4 * 3x4x2 matmul:\n {}\n'.format(np.matmul(m_4_4,m_3_4_2)))
print('4 4 矩阵 与 3 4 2 矩阵的乘积结果')
print('dot 结果的维度    =',np.dot(m_4_4,m_3_4_2).shape)
print('matmul 结果的维度 =',np.matmul(m_4_4,m_3_4_2).shape)<jupyter_output>4 4 矩阵 与 3 4 2 矩阵的乘积结果
dot 结果的维度    = (4, 3, 2)
matmul 结果的维度 = (3, 4, 2)<jupyter_text>**检验以下m_4_4与m_3_4_2最后两维的矩阵乘积结果**<jupyter_code>np.dot(m_4_4,m_3_4_2[0,:,:])
np.matmul(m_4_4,m_3_4_2[:,:,0].T)<jupyter_output><empty_output><jupyter_text>np.dot 定义：> dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])<jupyter_code># np.random.random 是在半开放区间[0.0, 1.0) 之间生成随机数字
#Return random floats in the half-open interval [0.0, 1.0).

a = np.random.random([3,3]) 
a
np.random.randn(3,3)
b = np.arange(24).reshape(6,4)
b<jupyter_output><empty_output><jupyter_text>关于 axis： axis=0 是沿着每一行每一行操作  axis=1 是沿着每一列每一列操作<jupyter_code>b.sum(axis=0) # sum of each column
b.sum(axis=1) # sum of each row
b.cumsum(axis=1) # 累计相加 cumulative sum along each row<jupyter_output><empty_output><jupyter_text>Slicing<jupyter_code>a = np.arange(10)**2
print("a: ", a)
print("a[2]: ", a[2])
print("a[2:6]: ", a[2:6])
a[0:3] = 100
print("a: ", a)
a_slice = a[0:5]
print("a_slice: ", a_slice)

# 注意： 当我们选取部分 np array 的时候，我们创建了一个view。
# 换句话说，我们没有copy以前的值，而是直接改了原来的 array 

a_slice[0] = 233
print("a_slice: ", a_slice)
print("a: ", a)
# 如果不想对原来的 array 进行修改，我们可以把选取的部分 copy 出来

a_slice_2 = a[0:5].copy()
print("a_slice: ", a_slice)
a_slice_2[0] = 500
print("a_slice_2: ", a_slice_2)<jupyter_output>a_slice:  [233 100 100   9  16]
a_slice_2:  [500 100 100   9  16]<jupyter_text>Multidimensional arrays.<jupyter_code>def f(x,y):
    return 4 * x + y
b = np.fromfunction(f,(3,2),dtype=int)
b

# derive from locations of the matrix
#[[00],[01],[10],[11],[20],[21]]
b[1]
b[1][0] #b[1,0]
b[:, 1]
c = np.zeros ([5,5])
c
column_length = c.shape[1]
column_length
for i in range(column_length):
    c[i] = i
    print(c[i])
for i in range(5):
    c[i]=i+1
c<jupyter_output><empty_output><jupyter_text>Boolean Indexing<jupyter_code>week_days=np.array(['Monday','Tuesday','Wednesday','Thursday','Friday'])
work_time=np.round(np.random.randn(5,5)+8.0, 2)
work_time
week_days == 'Tuesday'
work_time[week_days == 'Tuesday']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20204214642624374, 'fraction_numerical': 0.07842711275255268, 'mean_word_length': 2.773770491803279, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '356729', 'license': 'Apache-2.0', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2428, 'n_words': 330, 'path': 'Numpy.ipynb', 'repo_name': 'elsieyin/NotesKaggle', 'size': '4603'}