data=pandas.read_csv('temperatures.txt',sep='\s*')  # cleaned version

data

plot(data['Year'],data['J-D'],'-o')
xlabel('Year')
ylabel('Temperature Deviation')

"""
Explanation: Some Data Sets

Surface Temperature Data - http://data.giss.nasa.gov/gistemp/
Solar Spot Number Data

Global Surface Temperature
Global surface temperature data from http://data.giss.nasa.gov/gistemp/.
End of explanation
"""


x=data['Year']
y=data['J-D']/100.0
plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')

"""
Explanation: Actually, the deviation is 1/100 of this, so let's adjust...
End of explanation
"""


xls = pandas.ExcelFile('temperatures.xls')

print xls.sheet_names

data=xls.parse('Sheet 1')
data

"""
Explanation: Or if you like Excel
End of explanation
"""


data=pandas.read_csv('station.txt',sep='\s*')
data

"""
Explanation: Station Data
This data is from http://data.giss.nasa.gov/gistemp/station_data/
End of explanation
"""


x,y=data['YEAR'],data['metANN']
plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')

"""
Explanation: This plot will look weird, because of the 999's.
End of explanation
"""


y[y>400]=NaN

plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')

"""
Explanation: replace the 999's with Not-a-Number (NaN) which is ignored in plots.
End of explanation
"""


model=pandas.ols(x=x,y=y)
print model.summary
print "Beta",model.beta

m,b=model.beta['x'],model.beta['intercept']
plot(x,y,'-o')
x1=linspace(1890,2000,100)
y1=x1*m+b
plot(x1,y1,'-')
xlabel('Year')
ylabel('Temperature Deviation')

data

"""
Explanation: Fitting
First, ordinary least squares (ols)
End of explanation
"""


result=fit(x,y,'power',2)

xfit = linspace(1850,2000,100)
yfit = fitval(result,xfit)
plot(x,y,'-o')
plot(xfit,yfit,'-')
xlabel('Year')
ylabel('Temperature Deviation')

"""
Explanation: Next, try fitting a polynomial
End of explanation
"""


result

"""
Explanation: printing out the results of the fit.
End of explanation
"""


result=fit(x,y,'quadratic')

result

"""
Explanation: This should do the same thing.
End of explanation
"""


result=fit(x,y,'power',4)
xfit = linspace(1890,1980,100)
yfit = fitval(result,xfit)
plot(x,y,'-o')
plot(xfit,yfit,'-')
xlabel('Year')
ylabel('Temperature Deviation')

result

"""
Explanation: Do a super-crazy high polynomial
End of explanation
"""


