英文:
Porting from Python with NP to Java - can't multiply sequence by non-int type of "list" when passing to np.sum
问题
我有两个二维列表/数组变量,分别是values和weighs。两者都是由浮点数构成的列表。我正在移植的代码中有以下这行:
weighted_sum_values = np.sum(values * weights, axis=0)
当我运行这行代码以确定其行为时,我得到以下错误:
TypeError: can't multiply sequence by non-int of type 'list'
因此,虽然我相当确定np.sum会返回一个一维列表,但我不明白为什么在将这两个数组传递给np.sum之前不能将它们相乘 - 到底发生了什么,我该如何将所需功能移植到Java?我是否需要先创建一个临时的二维列表来保存相乘后的值,然后再对它们求和?感谢您的帮助!
英文:
I have two two-dimensional list/array variables, values and weighs. Both are lists of floats. The code I'm porting has the line:
weighted_sum_values = np.sum(values * weights, axis=0)
When I run this line to determine the behavior, the error I get is:
TypeError: can't multiply sequence by non-int of type 'list'
So, while I'm fairly certain np.sum will return a one-dimensional list, I don't understand why it's telling me I can't multiply these two arrays before passing it to np.sum - What's going on exactly, and how can I port the intended function to Java? Will I have to make a temporary 2D list of the multiplied values first before summing them? Thanks for the help!
答案1
得分: 0
经过进一步的尝试,看起来这就是 numpy 对两个数组进行逐元素相乘的方式。
英文:
After further playing around with it, it looks like it's numpy's way of multiplying two arrays, element by element.
专注分享java语言的经验与见解,让所有开发者获益!
评论