vector = np.array([1, 2, 3, 4, 5, 6]) # 选择第二个元素 vector[1] # out: 2 # 创建矩阵 matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 选择第二行第二列 matrix[1,1] # out: 5 # 创建矩阵 tensor = np.array([ [[[1, 1], [1, 1]], [[2, 2]...
1. 转置矩阵或向量 # 加载库 import numpy as np # 创建向量 vector = np.array([1, 2, 3, 4, 5, 6]) # 创建矩阵 matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 转置向量 ve
matrix_vector = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 选择向量的第三个元素 print(vector[2]) # 选择第二行第二列 print(matrix_vector[1, 1]) # 选取一个向量的所有元素 print(vector[:]) # 选取从0开始一直到第3个(包含第3个)元素 print(vector[:3]) # 选取第3个...
importnumpyasnp# 创建一个行向量vector = np.array([1,2,3])# 创建一个列向量vector = np.array([[1],[2],[3]])print(vector)# 创建一个矩阵matrix = np.array([[0,0],[2,0],[0,3]])print(type(matrix))#<class 'numpy.ndarray'># 查看行数和列数print(matrix.shape)#(3, 2)# 查看...
vector = numpy.array([5, 10, 15, 20]) vector == 10 #array([False, True, False, False], dtype=bool) matrix = numpy.array([[5, 10, 15],[20, 25, 30],[35, 40, 45],[2,3,4]]) second_column_25 = (matrix[:,1] == 25) ...
# Matrix / vector product; both produce the rank 1 array [29 67] print(x.dot(v)) print(np.dot(x, v)) # Matrix / matrix product; both produce the rank 2 array # [[19 22] # [43 50]] print(x.dot(y)) print(np.dot(x, y)) ...
# Add a vector to each row of a matrix x = np.array([[1,2,3], [4,5,6]]) # x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3), # giving the following matrix: # [[2 4 6] # [5 7 9]] ...
NumPy: Subtract every row of matrix by vector NumPy: Divide row by row sum How to apply a function / map values of each element in a 2d numpy array/matrix? Slice 2d array into smaller 2d arrays How to Remove Duplicate Elements from NumPy Array? Recover dict from 0-d numpy array Differ...
Thus, there is a function dot, both an array method and a function in the numpy namespace, for matrix multiplication: In [223]: x = np.array([[1., 2., 3.], [4., 5., 6.]]) In [224]: y = np.array([[6., 23.], [-1, 7], [8, 9]]) In [225]: x Out[225]: ...
import numpy as np # We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) vv = np.tile(v, (4, 1)) # Stack 4 copies of v on ...