<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Feb 2, 2014 at 3:46 PM, Kipton Moravec <span dir="ltr"><<a href="mailto:kip@kdream.com" target="_blank">kip@kdream.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I am new to Python, and I do not know how to traverse lists like I<br>
traverse arrays in C. This is my first program other than "Hello World".<br>
I have a Raspberry Pi and they say Python is the language of choice for<br>
that little machine. So I am going to try to learn it.<br>
<br>
<br>
I have data in the form of x, y pairs where y = f(x) and is non linear.<br>
It comes from a .csv file.<br>
<br>
In this case x is an integer from 165 to 660 so I have 495 data sets.<br>
<br>
I need to find the optimal locations of three values of x to piecewise<br>
linear estimate the function.<br>
<br>
<br>
So I need to find i, j, k so 165 < i < j < k < 660 and the 4 line<br>
segments [(165, f(165)), (i, f(i))], [(i, f(i)), (j, f(j))], [(j, f(j),<br>
(k, f(k))], [(k, f(k)), (660, f(660))].<br>
<br></blockquote><div>So as you iterate through the minimize function below, you want something like this, I believe:<br><br>    if (165 < i) and (i < j) and (j < k) and (k < 660):<br>        print 'true'<br>
    else:<br></div><div>        print 'false'<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
The value I need to minimize is the square of the difference between the<br>
line estimate and the real value at each of the 495 points.<br>
<br>
<br>
I can do this in C. To keep it simple to understand I will assume the<br>
arrays x[] and y[] and minsum, mini, minj, and mink are global.<br>
<br>
<br>
I have not tested this but this is what I came up with in C. Assuming<br>
x[] and y[] are already filled with the right values.<br>
<br>
<br>
int x[495];<br>
double y[495];<br>
max_elements = 495;<br>
double minsum;<br>
int mini, minj, mink<br>
<br></blockquote><div>#We define some vars before utilizing them x,and y are list comprehension<br># You said int for x, so it's created in ints, and y is in floats<br>x = [num for num in range(0,496)]<br># You said double for y, so the it's created in floats, and x is an int list<br>
y = [float(num) for num in range(0,496)] <br>i = 0<br>j = 0<br>k = 0<br>max_elements = 495<br>mini = 0<br>minj = 0<br>mink = 0 <br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<br>
void minimize(int max_elements)<br>
<br></blockquote><div><br> def minimize(max_elements): <br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">{<br>
    minsum = 9999999999999.0; // big big number<br>
<br></blockquote><div><br> minsum = float(9999999999999.0) # used float just to show as double</div><div><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
    for (i=2; i<max_elements-6;i++)<br>
        for (j=i+2; j< max_elements -4; j++)<br>
             for (k=j+2; k< max_elements-2;k++)<br></blockquote><div><br>for i in range(2,max_elements-6):<br>    print i<br>    for j in range(i+2,max_elements-4):<br>        print j<br>        for k in range(j+2,max_elements-2):<br>
            print k<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
             {<br>
                sum = error(0,i);<br>
                sum += error(i,j);<br>
                sum += error(j,k);<br>
                sum += error(k,495)<br>
<br></blockquote><div><br></div><div>                sum = error(0,i)<br>
                sum += error(i,j)<br>
                sum += error(j,k)<br>
                sum += error(k,495)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                if (sum < minsum)<br>
                {<br>
                    minsum = sum;<br>
                    mini = i;<br>
                    minj = j;<br>
                    mink = k;<br>
                }<br>
             }<br>
}<br>
<br></blockquote><div><br><br>                if sum < minsum:<br>                    minsum = sum<br>                    mini = i<br>                    minj = j<br>                    mink = k<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<br>
double error(int istart, int iend)<br>
{<br>
<br></blockquote><div><br>error(int istart, int iend):<br><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
// linear interpolation I can optimize but left<br>
// it not optimized for clarity of the function<br>
<br>
    int m;<br>
<br>
    double lin_estimate;<br>
<br>
    double errorsq;<br>
<br>
    errorsq = 0;<br>
<br></blockquote><div><br><br>
    int m<br>
<br>
    lin_estimate = <br>
<br>
    errorsq = float(0) #just to show double = float type again<br>
<br>
    errorsq = 0<br><br>
    <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
    for (m=istart; m<iend; m++)<br>
    {<br>
        lin_estimate = y[istart] + ((y[iend] – y[istart]) *<br>
                       ((x[m] – x[istart]) / (x[iend] – x[istart])));<br>
<br>
        errorsq += (lin_estimate – y[m]) * (lin_estimate – y[m]);<br>
    }<br>
<br>
    return (errorsq);<br>
<br>
}<br>
<br>
<br>
<br></blockquote><div><br><br> for iterator in range(istart; iend):<br>
        lin_estimate = y[istart] + ((y[iend] – y[istart]) * ((x[m] – x[istart]) / (x[iend] – x[istart])))<br>
<br>
        errorsq += (lin_estimate – y[m]) * (lin_estimate – y[m])<br>
<br>
    return errorsq<br>
<br><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
At the end the three values I want are mini, minj, mink;<br>
or x[mini], x[minj], x[mink]<br>
<br>
<br>
So how do I do this (or approach this) in Python?<br>
</div><br></blockquote><div>Loose Translation <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<br>
Kip<br>
<br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/mailman/listinfo/tutor</a><br>
</div></blockquote></div><br></div><div class="gmail_extra">I'll look at it a little more later on, unless someone' is less rusty in C and Python than I am.<br clear="all"></div><div class="gmail_extra"><br>-- <br>
Best Regards,<br><span style="font-family:arial,helvetica,sans-serif">David Hutto</span><br><i><b>CEO:</b></i> <u><a href="http://www.hitwebdevelopment.com" target="_blank">http://www.hitwebdevelopment.com</a></u><br> 
</div></div>