Question: python for matrix problems, don't know how to use copy for this problem. Assignment versus copying Take a look at the following code. We create
python for matrix problems, don't know how to use copy for this problem.
Assignment versus copying Take a look at the following code. We create one numpy array X_np we let y_np = X_np we change the third element of y_np The third element of x_np is also changed This looks weired and may not make sense for those uses other languages such as MATLAB. The reason for this is that we are not creating a copy of x_np and name it as y_np . What we did is that we give a new name y_np to the same array x_np . Therefore, if one is changed, and the other one is also changed, because they refer to the same array. [59]: X_np = np.array([-1, 0, 2, 3.1]) y_np = x_np y_np[2] = 20.0 X_np [59]: array([-1., 0., 20., 3.1]) DO THIS: There is a method named copy that can be used to create a new array. You can search how it works and fix the code below. If this is done correctly the x_np vector should stay the same and the y_np you now be (-1 0 2 3.1] . [105]: ## modify the following code to copy the x_np instead of just giving it a new name X_np = np.array([-1, 0, 2, 3.1]) y_np = X_np.copy() y_np = X_np print(x_np) print(y_np) [-1. [-1. 2 2. 3.1] 3.1] [106]: from answercheck import checkanswer checkanswer(x_np, 'Oba269d18692155ba252e6addedf37ad'); Testing (-1. 0. 2. 3.1] Answer seems to be correct [107]: from answercheck import checkanswer checkanswer (y_np, '993d5cbc6ddeb10776ed48159780a5d3'); Testing (-1. 0. 2. 3.1] Answer seems to be incorrect AssertionError Traceback (most recent call last)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
