ValueError错误:矩阵不是复制错误和X对准[:] [英] ValueError: matrices are not aligned for copy error and x[:]
问题描述
我得到了ValueError错误为如下。
I got the ValueError as given below.
ValueError错误:矩阵不是复制错误对齐
ValueError: matrices are not aligned for copy error
据被曝以下行(我没有写这个code,我想使用它):
It was traced to the following line (I did not write this code, I am trying to use it):
x1[:] = _dotproduct(x1, u)
点积就像numpy的点积,它工作正常,印刷_dotproduct(X1,U)
给出一个有效的答案。这是 X1 [:]
不工作
The dot product is like numpy dot product, it works FINE, printing _dotproduct(x1, u)
give a valid answer. It is x1[:]
that is not working.
这是什么 [:]
是什么意思?我从来没有见过的。
What does [:]
mean? I have never seen that.
此外,我怎么能解决定位的错误?
Also how can I solve the error of alignment?
编辑:结果
我现在已经追查错误 X1 [:]
,所以这不是我可以做到以下几点:
I have now traced the error to x1[:]
, so instead of this can I do the following:
hh=len(x1)
X1 [0:HH] = _数量积(X1,U)
推荐答案
在此情况下,由于它是在 =
标志的左侧,这是一个切片赋值。对象 X1
保持相同的对象,但是其所有内容都与右侧的序列所取代。如果没有 [:]
, X1
将被分配到一个完全不同的对象
In this case, since it's on the left side of the =
sign, it's a slice assignment. The object x1
remains the same object, but all its contents are replaced with the sequence on the right. Without the [:]
, x1
would be assigned to a totally different object.
使用切片分配意味着如果在你的程序中相同的变量其它引用,这些都将看到新的内容。例如,函数的调用者传递一个容器和函数替换其内容。这不会没有切片分配是可能的。
Using a slice assignment means that if there are other references to the same variable in your program, all of these will see the new contents. For example, the caller of a function passes in a container and the function replaces its contents. This wouldn't be possible without the slice assignment.
这篇关于ValueError错误:矩阵不是复制错误和X对准[:]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!