import numpy as np
import matplotlib.pyplot as plt
x = open(r'''C:\Users\Documents\ex.txt''')
[INPUT ex.txt file:
-1.642195902 0.751055263
0.496998351 -1.306558434
-0.490237525 -0.188855324
-1.357284374 0.282238191
-0.160982328 -1.115393803
1.167022948 0.564800286
-2.050084963 0.262537079
0.883449292 -0.276800002
1.017703957 -0.044710318
0.194025051 -1.392772391
0.209566571 -0.314937244
1.840331474 -1.544109096
0.493878872 -0.405593557
]
ls = x.readlines()
x1 = np.array([])
x2 = np.array([])
for l in ls:
col = l.split()
x1 = np.append(x1,float(col[0]))
x2 = np.append(x2,float(col[1]))
x3 = np.polyfit(x1,x2,1)
print(x3)
Output is:
[-0.22867408 0.37552763]
[-0.96186389 -0.82851367]
[-0.95783751 -0.77027985]
[-0.90374253 -0.79169204]
[-0.97569702 -0.93091431]
[-0.29008702 -0.26483726]
[-0.27811928 -0.26755408]
.........
[-0.06747052 -0.0218189 ]
My goal is to use the values for x3 and subtract from the original values x1 and x2, I was thinking in saving the output as a .txt file but the output is several 1 dimensional arrays.
Does anyone know how could I save the result from x3 such that I would be able to substract it from my original values? I am not sure how to code it. My goal is to detrend a set of data.
Many thanks
Try this:
import numpy as np
import matplotlib.pyplot as plt
x = open(r'''C:\Users\Documents\ex.txt''')
ls = x.readlines()
x1 = np.array([])
x2 = np.array([])
x_array = np.array([])
x3_array = np.array([])
for l in ls:
col = l.split()
x_array = np.append(x_array, [float(col[0]), float(col[1])])
x1 = np.append(x1,float(col[0]))
x2 = np.append(x2,float(col[1]))
x3_array = np.append(x3_array, np.polyfit(x1,x2,1))
sub_res = x_array - x3_array
print (sub_res)
first regarding importing your data:
data = np.genfromtxt('c:/Users/kaufnbnj/Desktop/ex.txt', names = ['x1', 'x2'])
array([(-1.6421959 , 0.75105526), ( 0.49699835, -1.30655843),
(-0.49023753, -0.18885532), (-1.35728437, 0.28223819),
(-0.16098233, -1.1153938 ), ( 1.16702295, 0.56480029),
(-2.05008496, 0.26253708), ( 0.88344929, -0.2768 ),
( 1.01770396, -0.04471032), ( 0.19402505, -1.39277239),
( 0.20956657, -0.31493724), ( 1.84033147, -1.5441091 ),
( 0.49387887, -0.40559356)],
dtype=[('x1', '<f8'), ('x2', '<f8')])
accessing first column:
data['x1']
array([-1.6421959 , 0.49699835, -0.49023753, -1.35728437, -0.16098233,
1.16702295, -2.05008496, 0.88344929, 1.01770396, 0.19402505,
0.20956657, 1.84033147, 0.49387887])
Then the fitting part:
x3 = np.polyfit(data['x1'], data['x2'], 1)
Note that polyfit
Returns the coefficients of your Fitting function, not a sampled array for your input data:
x3
array([-0.33019369, -0.3484815 ])
So you can plot input data and fit in a plot like this:
x_fit = x3[0]*data['x1'] + x3[1]
plt.plot(data['x1'], data['x2'], 'o')
plt.plot(data['x1'], x_fit)
... and x2 after detrend would be:
x_detrend = data['x2'] - x_fit
plt.plot(data['x1'], x_detrend, 'o')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.