ValueError: matrices are not aligned for copy error and x[:]

与世无争的帅哥 提交于 2019-12-04 05:46:39

问题


I got the ValueError as given below.

ValueError: matrices are not aligned for copy error

It was traced to the following line (I did not write this code, I am trying to use it):

x1[:] =  _dotproduct(x1, u)

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?

Edit:
I have now traced the error to x1[:], so instead of this can I do the following:

hh=len(x1)

x1[0:hh]=_dotproduct(x1, u)?


回答1:


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.



来源:https://stackoverflow.com/questions/25372947/valueerror-matrices-are-not-aligned-for-copy-error-and-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!