1

I have a quite sophisticated problem (for me). I have written a code that calculates interpolation for a given data set. After that it calculates the Akaike information criterium to check which interpolation suite best.

Looks like that:

    polyfit=np.polyfit(x,y,5)  # x,y are data set and 5 is a root of polynomial
    poly1d=np.poly1d(polyfit)    
    print poly1d  #show final polynomial

my=[]
for i in x: 
x_=poly1d(i)
my.append(x_) #calculate list of values 

def AIC(i,j): 
for i in y:
for j in my:
    RSS=(i-j)**2

AIC=36-np.log(RSS) 
print AIC

And I don't like this code because if I want to change root of polynomial I have to change the code. I know that I have to use for loop in the beginning like:

for i in xrange(40):
    polyfit=np.polyfit(x,y,i)

but I can't figure out how to save all of my polynomials to a list. If I knew that I would use it to calculate def AIC() for any root, and then I could find the best curve fit for my data. Please help guys, it has been bothering me for 2 weeks already. If there is anything not clear to you, please ask.

merv
  • 67,214
  • 13
  • 180
  • 245
wiedzminYo
  • 531
  • 4
  • 11
  • 24
  • It seems your snippet misses the first line. That being said, what is that you can't store in a list? (I mean which variable) In python you can have a list of any object... – Cristián Antuña Mar 20 '15 at 21:07
  • @CristiánAntuña Actually I know,that I can store in a list but I don't know how.I mean I don't know in this specific problem.How to store in list all of my polynomials which it calculate from first three lines.And after that how to calculate all values for every polynomial? – wiedzminYo Mar 20 '15 at 21:19
  • In your for declaration, you can save each polyfit in a list using `poly_list.append(polyfit)` where `poly_list` was a previously declared empty list (i.e. `poly_list = []`). Without your full code I am not sure this is what you are looking for. – Cristián Antuña Mar 20 '15 at 21:28
  • @CristiánAntuña It might amuse you, but this is entire code ,well except my data and plot graph :) – wiedzminYo Mar 20 '15 at 21:28
  • 1
    Then you should probably spend a little time reviewing Python, half of your lines are wrongly indented. If you get why is that, you will probably be able to fix this problem yourself. – Cristián Antuña Mar 20 '15 at 21:32

0 Answers0