我试图在两个不同维数的矩阵之间使用Numpy中的点乘积。W是(1,5),X是(3,5),我不确定我可以使用哪个命令来更改维度,因为我对python还不熟悉。谢谢。
当我尝试运行我的函数时,它会给出一个错误:
ValueError: shapes (1,5) and (3,5) not aligned: 5 (dim 1) != 3 (dim 0)from numpy.core.memmap import ndarray
def L(w, X, y):
"""
Arguments:
w -- vector of size n representing weights of input features n
X -- matrix of size m x n represnting input data, m data sample with n features each
y -- vector of size m (true labels)
Returns:
loss -- the value of the loss function defined above
"""
### START CODE HERE ### (2-4 lines of code)
#w needs to match X matrix
# w = (1, 5)
# x = (3, 5)
yhat = np.dot(w, X)
L1 = y - yhat
loss = np.dot(L1, L1)
### END CODE HERE ###
return loss这是方向图:方向图像
发布于 2022-09-11 02:49:37
两个向量的点积是元素与位置的乘积之和。第一向量的第一元素乘以第二向量的第一元素,以此类推。这些积的和是np.dot()函数可以完成的点积。
由于我们在相同的位置上相乘元素,所以这两个向量必须有相同的长度才能得到一个点积。
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
np.dot(a,b)它将产生以下输出−
[[37 40]
[85 92]] 注意,点乘积计算为−。
[[1*11+2*13, 1*12+2*14],[3*11+4*13, 3*12+4*14]]https://stackoverflow.com/questions/73676388
复制相似问题