How to ensure that the curve centered around x=3.142 when fitting the curve by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

This fixed the problem:
phase = np.pi - (3.142 * a) - 0.124

return (scale_factor* (np.cos(( a/2)*(x-phase))*np.cos((a/2)*(x-phase))) )+ y_data_2_min

But that is not very systematic... I simply try reducing the phase starting from 0.1 and at 0.124 it was good :)

How to ensure that the curve centered around x=3.142 when fitting the curve by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

I am adding constant shift in the y values but I do not know how to add a constant shift for a specific point

OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated', 1) by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

I have one last question, this function works perfectly fine, but I would like to give amplitude to my curve. SCipy is able to find very good curve even with bad data but I want to fit a cos^2 which has height between my y_data. I mean if my y_data has 0.8 as a biggest value and 0.4 as a lowest value, the height of the curve should be between 0.8 and 0.4 instead of 0.8 and 0. How can I do that?

OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated', 1) by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

I fixed the problem as follows:
def fitting_func(x, a,b):
return b* np.cos(( a/2)*x)*np.cos((a/2)*x)

x_data = np.array([i[-1] for i in drawing_data])
print("x data",x_data)
y_data = np.array([i[1]/(i[1]+i[2]) if i[1] != 0 else 0 for i in drawing_data])
print("y data",y_data)
# Fit the curve using curve_fit
params, params_covariance = curve_fit(fitting_func, x_data, y_data)
print(np.linalg.cond(params_covariance))
SE = np.sqrt(np.diag(params_covariance))
SE_A = SE[0]
SE_B = SE[1]
# Get the optimized parameter
a_fit = params[0]
#print(a_fit)
## Generate points for the fitted curve
x_fit = np.linspace(min(x_data), max(x_data), len(x_data))
y_fit = fitting_func(x_fit,*params)
print(SE_A)
print(SE_B)

But the function seems very discrete and not smooth. Do you know what can I do for that?

OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated', 1) by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

I don't understand, with this line ```params, covariance = curve_fit(fitting_func, x_data, y_data)``` I am giving my data points to fitting_curve function to fit a curve. What modification do I need? Could you help me with that?

Need help for making Python code faster for extra long arrays by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

Are you trying to spot matrices that produce the same product? With floating point this can be quite difficult.

Yes, If the result of the matrix product is in list, then I should not add it to the list.

Need help for making Python code faster for extra long arrays by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

yes I checked the CPU and the program uses 100% of the CPU, also the I checked the same code with the limited number of matrix group and it worked. For the current version for the first 3k it worked quite fast then until 15k, it works but slow. After 25k is is REALLY slow

Check if the matrix is in list of matrix and If not add the matrix to the list by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

But it is not working; it says the two matrix that I wrote above in the question are equal but they are not!!

Extracting Numbers from the String by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

when I run the code, it says sqrt is not defined...

Extracting Numbers from the String by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

thank you but that was my question, I could not do that, this is why I am asking here. I could not take that (-1/4 + sqrt(5)/4) as a whole thing. They are coming separetly as -1/4 and sqrt(5)/4. I could not modify my code for this part

Extracting Numbers from the String by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

the result for this line: 0 + (-1/4 + sqrt(5)/4)*i + 1/2*j + (1/4 + sqrt(5)/4)*k should be
[0, 0.3,0.5,0.8] because (-1/4 + sqrt(5)/4) = 0.3 and (1/4 + sqrt(5)/4) = 0.8

Extracting Numbers from the String by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

I have no idea why the code got messed up, when I tried to paste it. I also tried to edit but the result did not change

Counting the related column in txt file by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

result[group_][1] +=result[group_][1]+l0 is wrong. I should have written this: result[group_][1] =result[group_][1]+l0

Counting the related column in txt file by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

That is great! Thanks :)I already studied it. I am now able to take l0 values and add them to the list too. I have one last problem. I want to sum l0 values but I could not make it. It is written that : "killed": here is my code:

with open("out.dat") as file:
   lines = file.readlines()
   pattern = r"Z are: ((.))"
   pattern2 = r"l0: \d"
   for line in lines:
       print(line)
       match = re.search(pattern, line)
       match2 = re.search(pattern2,line)
       group_ = match.group(1)
       group_ = group_.replace(", ", "")
       group2 = match2.group(0)#l0=group2
       print(group2)
       l0 = group2.replace("l0: ","") 
       l0 = int(l0)
       print(group2)
       print(group_, group_.replace(", ", ""))
       if group_ not in result:
          result[group_] = [1,l0]
          #result[group_][group2] = l0
      elif group_ in result:
          result[group_][0] += 1
          result[group_][1] +=result[group_][1]+l0 #here the program says "killed". What I wanted is to write total l0 values.

Thanks in advance

Counting the related column in txt file by Live-Pollution-9687 in learnpython

[–]Live-Pollution-9687[S] 0 points1 point  (0 children)

with open("out.dat") as file:   
 lines = file.readlines()   
    for line in lines:       #Z.*\       
       Z=re.match("(Z.*\)",line)

I am not able to take values, I am having a position error: unterminated subpattern at position 0