How to square the individual matrix value using python?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to square the individual matrix value using python?



I am trying to implement the Cost function in python.



Assume my data have X (loading from txt file)
and my theta value is [[0]
[0]]
For that I have implemented as below:


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import fractional_matrix_power
load_data = pd.read_csv('C:python_programex1data1.txt',sep = ",",header = None)
feature_vale = load_data[0]
y = np.matrix(load_data[1])
m = len(feature_vale)
#print(m)
#plt.scatter(load_data[0],load_data[1])
df = pd.DataFrame(pd.Series(1,index= range(0,m)))
df[1] = load_data[0]
X = np.matrix(df)
row_theta = np.zeros(2,dtype = int)
theta = np.array([row_theta]) # Transpose the array
print(theta.T)
prediction = np.matmul(X,theta.T)
error = (prediction-y)
print(error)



Output of the error I got as expected(Compare with Matlab and Octiva):


[[-17.592 -9.1302 -13.662 ... -0.14454 -9.0551 -0.61705]
[-17.592 -9.1302 -13.662 ... -0.14454 -9.0551 -0.61705]
[-17.592 -9.1302 -13.662 ... -0.14454 -9.0551 -0.61705]
...
...
[-17.592 -9.1302 -13.662 ... -0.14454 -9.0551 -0.61705]
[-17.592 -9.1302 -13.662 ... -0.14454 -9.0551 -0.61705]
[-17.592 -9.1302 -13.662 ... -0.14454 -9.0551 -0.61705]]



But next step I need to square the individual value of the matrix.



I have tired the following commands to square the individual value of the matrix,still I am not able to get the expected result as per matlab.


sqerror = np.float_power(error,2.0,where=True)
print(sqerror)


sqerror = np.np.power(error,2.0)
print(sqerror)



sqerror = np.square(error)
print(sqerror)

Output:
[[3.09478464e+02 8.33605520e+01 1.86650244e+02 ... 2.08918116e-02
8.19948360e+01 3.80750702e-01]
[3.09478464e+02 8.33605520e+01 1.86650244e+02 ... 2.08918116e-02
8.19948360e+01 3.80750702e-01]
[3.09478464e+02 8.33605520e+01 1.86650244e+02 ... 2.08918116e-02
8.19948360e+01 3.80750702e-01]
...
...
[3.09478464e+02 8.33605520e+01 1.86650244e+02 ... 2.08918116e-02
8.19948360e+01 3.80750702e-01]
[3.09478464e+02 8.33605520e+01 1.86650244e+02 ... 2.08918116e-02
8.19948360e+01 3.80750702e-01]
[3.09478464e+02 8.33605520e+01 1.86650244e+02 ... 2.08918116e-02
8.19948360e+01 3.80750702e-01]]



But expected value of first element is :


-17.592 * -17.592 = 309.478464
-9.1302 * -9.1302 = 83.360552



Please help to resolve the above issue



Data reference:https://searchcode.com/codesearch/view/5404318/




1 Answer
1



repeat the following steps and let me know


error_matrix = np.asmatrix(error)
error_square = np.square(error_matrix)





Still it's not working - Data you can download from searchcode.com/codesearch/view/5404318
– thangaraj1980
4 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Regex - How to capture all iterations of a repeating pattern?