Skip to content

Instantly share code, notes, and snippets.

@starfys
Created June 29, 2016 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starfys/cc7b1c7435c8785f468cefb56e440512 to your computer and use it in GitHub Desktop.
Save starfys/cc7b1c7435c8785f468cefb56e440512 to your computer and use it in GitHub Desktop.
Does cool analysis stuff
#!/bin/env python3
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
from math import log
with open('resilience.txt','r') as res_file:
data = list(map(str.rstrip, res_file.readlines()))
data = list(map(lambda line: tuple(map(float, line.split(',')))[1], data))
min_denominator = 2
max_denominator = len(data)
data = data[min_denominator - 1:max_denominator + 1]
x = list(range(min_denominator, max_denominator + 1))
y = data
#Clear data from mem
del data
"""
Plot your data
"""
plt.plot(x, y, 'ro',label="Original Data")
"""
brutal force to avoid errors
"""
x = np.array(x, dtype=float) #transform your data in a numpy array of floats
y = np.array(y, dtype=float) #so the curve_fit can work
"""
create a function to fit with your data. a, b, c and d are the coefficients
that curve_fit will calculate for you.
In this part you need to guess and/or use mathematical knowledge to find
a function that resembles your data
"""
"""
def func(x, a, b, c):
return a * np.log(x - b) + c
"""
def func(x, a, b, c):
return a / (x - b) + c
"""
make the curve_fit
"""
popt, pcov = curve_fit(func, x, y)
"""
The result is:
popt[0] = a , popt[1] = b, popt[2] = c and popt[3] = d of the function,
so f(x) = popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3].
"""
print("a = %s , b = %s, c = %s" % (popt[0], popt[1], popt[2]))
"""
Print the coefficients and plot the funcion.
"""
plt.plot(x, func(x, *popt), label="Fitted Curve") #same as line above \/
#plt.plot(x, popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3], label="Fitted Curve")
plt.legend(loc='upper left')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment