In [1]:
from science import *
Global surface temperature data from http://data.giss.nasa.gov/gistemp/.
In [2]:
data=pandas.read_csv('temperatures.txt',sep='\s*') # cleaned version
In [3]:
data
Out[3]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 132 entries, 0 to 131
Data columns:
Year 132 non-null values
Jan 132 non-null values
Feb 132 non-null values
Mar 132 non-null values
Apr 132 non-null values
May 132 non-null values
Jun 132 non-null values
Jul 132 non-null values
Aug 132 non-null values
Sep 132 non-null values
Oct 132 non-null values
Nov 132 non-null values
Dec 132 non-null values
J-D 132 non-null values
D-N 132 non-null values
DJF 132 non-null values
MAM 132 non-null values
JJA 132 non-null values
SON 132 non-null values
Year.1 132 non-null values
dtypes: int64(20)
In [4]:
plot(data['Year'],data['J-D'],'-o')
xlabel('Year')
ylabel('Temperature Deviation')
Out[4]:
<matplotlib.text.Text at 0xb0640d0>
Actually, the deviation is 1/100 of this, so let's adjust...
In [5]:
x=data['Year']
y=data['J-D']/100.0
plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')
Out[5]:
<matplotlib.text.Text at 0xb0c0ad0>
In [6]:
xls = pandas.ExcelFile('temperatures.xls')
print xls.sheet_names
data=xls.parse('Sheet 1')
data
[u'Sheet 1']
Out[6]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 132 entries, 0 to 131
Data columns:
Year 132 non-null values
Jan 132 non-null values
Feb 132 non-null values
Mar 132 non-null values
Apr 132 non-null values
May 132 non-null values
Jun 132 non-null values
Jul 132 non-null values
Aug 132 non-null values
Sep 132 non-null values
Oct 132 non-null values
Nov 132 non-null values
Dec 132 non-null values
J-D 132 non-null values
D-N 132 non-null values
DJF 132 non-null values
MAM 132 non-null values
JJA 132 non-null values
SON 132 non-null values
Year.1 132 non-null values
dtypes: float64(20)
This data is from http://data.giss.nasa.gov/gistemp/station_data/
In [7]:
data=pandas.read_csv('station.txt',sep='\s*')
data
Out[7]:
YEAR
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC
D-J-F
M-A-M
J-J-A
S-O-N
metANN
0
1896
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
7.1
1.6
-1.7
999.9
999.9
999.9
7.3
999.90
1
1897
-4.9
-4.1
-1.6
5.6
10.1
15.1
21.8
18.7
17.1
11.2
2.1
-3.1
-3.6
4.7
18.5
10.1
7.45
2
1898
-3.4
-4.6
1.2
4.7
11.1
16.9
20.1
18.1
15.7
9.6
0.4
-4.7
-3.7
5.7
18.4
8.6
7.22
3
1899
-7.3
-9.9
-5.2
1.9
12.4
16.2
17.4
19.2
12.1
10.3
999.9
999.9
-7.3
3.0
17.6
8.0
5.34
4
1900
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.90
5
1901
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
1.8
-5.0
999.9
999.9
999.9
999.9
999.90
6
1902
-5.3
-6.9
0.8
4.3
10.8
14.0
18.8
999.9
999.9
8.0
5.5
-3.2
-5.7
5.3
16.7
9.7
6.50
7
1903
-6.3
999.9
2.9
3.7
11.5
14.8
19.1
15.5
14.5
9.9
0.4
-7.8
-5.2
6.0
16.5
8.3
6.39
8
1904
999.9
999.9
999.9
999.9
999.9
15.5
18.0
999.9
14.6
8.7
4.7
999.9
999.9
999.9
17.0
9.3
999.90
9
1905
-7.3
-9.8
-1.1
5.1
9.1
15.2
19.1
19.0
15.8
9.3
1.6
999.9
-7.7
4.4
17.8
8.9
5.83
10
1906
-1.7
-5.5
-3.6
999.9
999.9
999.9
999.9
20.6
17.5
8.1
2.9
-3.3
-2.8
999.9
999.9
9.5
999.90
11
1907
-6.3
-6.9
1.1
0.6
6.8
15.4
18.5
17.3
15.8
7.4
1.8
-1.6
-5.5
2.8
17.1
8.3
5.68
12
1908
-3.9
-6.1
-0.4
4.9
11.4
999.9
20.9
17.3
17.9
11.1
3.9
-2.4
-3.9
5.3
18.2
11.0
7.65
13
1909
-4.6
-3.4
-1.9
3.0
11.1
17.1
18.7
19.4
14.5
999.9
4.5
-3.9
-3.5
4.1
18.4
9.7
7.17
14
1910
-5.0
-6.3
4.3
7.7
7.6
17.1
19.8
18.4
14.0
10.7
999.9
-4.2
-5.1
6.5
18.4
9.2
7.27
15
1911
-4.8
-3.0
-1.1
4.6
13.2
18.4
20.5
18.0
13.9
8.2
0.9
-0.8
-4.0
5.6
19.0
7.7
7.05
16
1912
999.9
-8.6
-5.2
5.7
11.6
14.4
17.8
16.4
15.5
9.9
3.7
-0.3
-5.1
4.0
16.2
9.7
6.21
17
1913
-3.3
-7.5
-1.6
5.6
9.7
16.1
18.7
19.0
14.8
9.3
5.1
1.0
-3.7
4.6
17.9
9.7
7.13
18
1914
-2.7
-9.6
-1.7
4.1
11.7
15.4
19.4
18.7
15.1
12.2
3.0
-4.4
-3.8
4.7
17.8
10.1
7.22
19
1915
-5.5
-2.2
-1.5
8.1
9.5
14.0
17.5
16.0
15.4
10.1
4.1
-2.9
-4.0
5.4
15.8
9.9
6.76
20
1916
-3.3
-7.1
-3.8
4.8
10.6
13.6
22.1
19.8
14.3
8.6
2.9
-4.2
-4.4
3.9
18.5
8.6
6.63
21
1917
-6.5
-9.9
-1.0
3.4
7.8
13.2
18.3
16.9
13.4
4.6
2.4
-6.9
-6.9
3.4
16.1
6.8
4.87
22
1918
-11.2
-7.3
0.4
4.3
11.8
14.1
17.3
19.3
11.8
9.9
4.8
0.4
-8.5
5.5
16.9
8.8
5.69
23
1919
-2.0
-3.1
0.3
5.3
10.8
20.4
20.0
18.6
15.9
9.9
1.5
-6.4
-1.6
5.5
19.7
9.1
8.17
24
1920
-9.5
-7.3
0.0
2.8
9.9
15.3
16.3
17.6
16.4
13.2
2.9
-0.6
-7.7
4.2
16.4
10.8
5.93
25
1921
-2.2
-2.6
1.8
8.1
12.6
19.3
22.7
18.8
18.1
10.2
1.6
-2.0
-1.8
7.5
20.3
10.0
8.98
26
1922
-5.6
-4.7
0.5
5.4
14.5
16.5
18.3
18.7
16.3
9.8
5.1
-3.7
-4.1
6.8
17.8
10.4
7.73
27
1923
-4.7
-8.1
-4.1
3.9
10.7
16.4
19.5
16.7
14.6
8.4
4.0
1.8
-5.5
3.5
17.5
9.0
6.13
28
1924
-7.5
-5.7
-1.7
4.9
7.5
14.4
16.6
17.6
12.4
11.6
3.0
-5.3
-3.8
3.6
16.2
9.0
6.24
29
1925
-6.4
-3.6
0.1
7.4
8.0
15.5
18.1
18.9
16.3
5.0
2.0
-3.4
-5.1
5.2
17.5
7.8
6.33
30
1926
-4.7
-4.4
-4.3
1.9
10.5
12.7
18.7
18.6
14.4
8.6
1.6
-5.1
-4.2
2.7
16.7
8.2
5.85
31
1927
-5.5
-2.6
2.3
5.6
9.7
13.8
17.6
15.8
16.6
11.2
4.0
-3.0
-4.4
5.9
15.7
10.6
6.95
32
1928
-4.5
-4.3
-1.0
3.2
10.1
13.3
18.8
18.4
13.5
10.5
3.7
-0.2
-3.9
4.1
16.8
9.2
6.56
33
1929
-8.5
-7.7
1.4
5.9
8.9
14.1
18.8
17.1
14.6
8.6
1.4
-2.9
-5.5
5.4
16.7
8.2
6.20
34
1930
-6.9
-2.1
-1.1
6.1
11.7
15.8
18.8
19.5
15.9
8.6
4.8
-2.2
-4.0
5.6
18.0
9.8
7.35
35
1931
-1.9
-0.8
-0.6
5.6
9.7
18.1
21.3
19.3
18.1
11.7
7.3
1.3
-1.6
4.9
19.6
12.4
8.80
36
1932
-0.2
-2.0
-3.0
5.0
11.0
18.0
19.6
19.9
15.5
8.9
1.6
-2.4
-0.3
4.3
19.2
8.7
7.97
37
1933
-0.5
-5.5
-1.2
5.9
12.3
18.7
21.0
18.8
17.6
8.7
0.8
-3.5
-2.8
5.7
19.5
9.0
7.85
38
1934
-2.1
-9.3
-3.0
4.8
12.9
17.9
20.4
17.7
15.4
10.1
4.9
-4.5
-5.0
4.9
18.7
10.1
7.18
39
1935
-5.5
-5.2
0.3
4.3
8.3
14.0
21.3
19.8
13.9
8.8
2.0
-4.0
-5.1
4.3
18.4
8.2
6.46
40
1936
-7.0
-11.3
-0.1
2.5
11.9
13.9
20.8
19.5
16.4
8.2
0.9
-1.5
-7.4
4.8
18.1
8.5
5.97
41
1937
-4.5
-4.3
-2.7
5.1
10.9
15.5
20.0
20.9
15.0
7.4
2.3
-4.6
-3.4
4.4
18.8
8.2
7.01
42
1938
-6.0
-3.5
2.4
5.9
11.8
15.7
18.8
21.1
14.0
11.5
4.5
-1.4
-4.7
6.7
18.5
10.0
7.63
43
1939
-3.5
-5.3
-1.6
3.2
10.0
16.6
19.6
19.3
15.5
9.5
3.0
0.4
-3.4
3.9
18.5
9.3
7.07
44
1940
-6.8
-3.6
-2.7
4.0
9.3
15.0
19.2
18.7
14.8
9.9
3.0
-0.9
-3.3
3.5
17.6
9.2
6.77
45
1941
-3.6
-4.9
-2.5
7.3
999.9
16.5
18.2
18.2
999.9
11.1
4.2
0.2
-3.1
5.3
17.6
10.6
7.61
46
1942
-4.9
-6.1
1.2
8.7
12.1
17.5
19.8
19.5
14.6
9.8
3.1
-4.7
-3.6
7.3
18.9
9.2
7.96
47
1943
-7.6
-4.8
-3.1
4.2
10.4
18.3
20.0
20.1
13.5
8.8
1.5
-3.1
-5.7
3.8
19.5
7.9
6.38
48
1944
-2.0
-3.8
-2.4
4.7
14.0
18.5
20.7
20.4
15.8
8.5
4.5
-4.6
-3.0
5.4
19.9
9.6
7.98
49
1945
-8.5
-4.9
5.7
7.4
9.3
14.9
18.7
19.2
14.9
8.1
3.1
-5.3
-6.0
7.5
17.6
8.7
6.94
50
1946
-4.2
-5.8
5.2
6.7
11.0
16.2
20.5
18.3
16.0
11.9
4.2
-2.6
-5.1
7.6
18.3
10.7
7.89
51
1947
-4.0
-6.7
-2.6
4.5
8.7
15.9
18.8
22.8
17.3
14.4
0.9
-3.1
-4.4
3.5
19.2
10.9
7.28
52
1948
-8.2
-6.0
-1.3
8.5
9.7
16.2
20.3
20.0
17.2
9.2
5.2
-2.3
-5.8
5.6
18.8
10.5
7.31
53
1949
-3.4
-4.6
-0.6
5.9
12.1
19.5
21.9
20.3
13.8
12.0
2.2
-2.2
-3.4
5.8
20.6
9.3
8.07
54
1950
-3.7
-5.3
-3.2
2.2
11.8
16.2
18.0
16.9
14.6
11.7
0.8
-4.8
-3.7
3.6
17.0
9.0
6.48
55
1951
-5.6
-4.7
-0.7
5.2
13.6
16.0
18.7
17.6
14.4
10.0
-1.5
-3.3
-5.0
6.0
17.4
7.6
6.52
56
1952
-3.7
-3.2
-1.5
7.4
11.3
18.3
21.3
19.2
16.2
6.7
4.0
-1.2
-3.4
5.7
19.6
9.0
7.72
57
1953
-3.6
-3.3
0.7
4.5
12.3
18.2
20.9
20.5
16.3
11.6
5.2
-0.9
-2.7
5.8
19.9
11.0
8.51
58
1954
-5.8
-0.8
999.9
7.4
10.6
18.6
19.8
18.4
15.3
9.4
3.5
-2.7
-2.5
6.0
18.9
9.4
7.95
59
1955
-5.2
-4.4
-0.8
9.6
13.0
17.1
22.6
22.0
14.8
10.1
0.8
-4.3
-4.1
7.3
20.6
8.6
8.07
60
1956
-4.8
-4.4
-2.4
5.1
10.7
19.2
18.2
19.1
13.6
11.9
3.2
-1.6
-4.5
4.5
18.8
9.6
7.09
61
1957
-7.5
-3.6
-0.6
6.6
11.4
18.0
20.2
18.5
14.5
8.9
3.4
-1.1
-4.2
5.8
18.9
8.9
7.35
62
1958
-4.0
-6.1
0.2
6.8
11.0
14.3
19.2
19.3
15.5
10.8
3.3
-7.3
-3.7
6.0
17.6
9.9
7.43
63
1959
-8.1
-6.5
-1.8
5.4
14.2
18.2
19.3
21.5
16.3
7.5
-0.7
0.1
-7.3
5.9
19.7
7.7
6.50
64
1960
-2.8
-4.0
-4.4
7.4
11.3
15.7
18.2
19.1
16.6
9.0
4.4
-4.9
-2.2
4.8
17.7
10.0
7.55
65
1961
-6.6
-3.1
1.3
4.2
9.6
16.6
19.0
19.3
17.9
10.5
3.4
-3.6
-4.9
5.0
18.3
10.6
7.27
66
1962
-7.1
-7.2
-0.8
6.0
15.2
17.4
18.3
19.0
13.4
10.3
2.6
-4.4
-6.0
6.8
18.2
8.8
6.96
67
1963
-9.7
-10.0
-0.4
6.4
10.5
17.8
19.6
17.2
14.2
13.3
4.9
-5.5
-8.0
5.5
18.2
10.8
6.62
68
1964
-3.0
-3.9
-0.5
7.5
13.7
18.0
20.9
17.9
14.4
8.4
5.2
-3.9
-4.1
6.9
18.9
9.3
7.76
69
1965
-5.8
-5.8
-3.5
4.7
14.2
16.0
18.0
17.9
14.9
9.0
3.2
0.6
-5.2
5.1
17.3
9.0
6.57
70
1966
-7.9
-4.2
1.4
5.0
8.9
17.5
21.3
18.7
14.6
9.3
2.5
-2.7
-3.8
5.1
19.2
8.8
7.31
71
1967
-3.5
-7.0
-0.6
6.3
9.1
18.0
18.6
17.2
14.6
9.4
1.1
-2.1
-4.4
4.9
17.9
8.4
6.71
72
1968
-5.9
-7.7
2.6
7.5
10.4
17.0
19.2
19.4
16.3
10.9
3.4
-3.7
-5.2
6.8
18.5
10.2
7.58
73
1969
-6.4
-5.9
-2.6
6.9
11.9
14.4
20.1
999.9
15.8
8.9
3.0
-3.5
-5.3
5.4
17.5
9.2
6.71
74
1970
-8.4
-6.9
-4.0
9.1
12.5
999.9
999.9
20.2
16.4
10.5
3.4
-3.4
-6.3
5.9
999.9
10.1
6.96
75
1971
999.9
999.9
-4.5
5.4
9.9
18.8
19.6
18.1
16.2
14.0
3.0
-1.3
999.9
3.6
18.8
11.1
7.29
76
1972
-6.2
-5.7
-2.4
4.9
13.9
14.8
18.8
19.6
15.1
7.6
2.1
-3.9
-4.4
5.5
17.7
8.3
6.77
77
1973
-3.4
-5.2
4.6
6.2
10.5
18.6
999.9
20.8
15.7
12.5
3.4
-4.1
-4.2
7.1
20.3
10.5
8.45
78
1974
-4.5
-6.2
-0.1
7.1
11.0
16.6
999.9
999.9
13.4
8.5
3.4
-1.2
-4.9
6.0
999.9
8.4
6.89
79
1975
-3.9
-4.5
-1.9
3.5
14.0
17.9
20.2
19.9
13.9
10.8
6.7
-3.1
-3.2
5.2
19.3
10.5
7.95
80
1976
-7.2
-2.2
0.8
7.4
9.9
18.9
20.6
18.1
999.9
6.9
-0.4
-6.7
-4.2
6.0
19.2
6.2
6.83
81
1977
-10.1
-6.1
3.1
8.6
16.2
15.6
21.2
17.5
999.9
7.7
3.8
-4.2
-7.6
9.3
18.1
8.7
7.13
82
1978
-7.9
-9.2
-2.1
5.8
11.3
17.0
18.9
19.7
17.1
8.4
999.9
-3.8
-7.1
5.0
18.5
9.6
6.50
83
1979
-8.5
-9.0
0.3
5.3
999.9
17.3
19.9
18.1
16.0
999.9
3.7
-0.1
-7.1
5.7
18.4
10.0
6.77
84
1980
-4.7
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
-2.9
999.9
999.9
999.9
999.90
This plot will look weird, because of the 999's.
In [8]:
x,y=data['YEAR'],data['metANN']
plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')
Out[8]:
<matplotlib.text.Text at 0xb6b4c50>
replace the 999's with Not-a-Number (NaN) which is ignored in plots.
In [9]:
y[y>400]=NaN
In [10]:
plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')
Out[10]:
<matplotlib.text.Text at 0xb70f410>
In [11]:
model=pandas.ols(x=x,y=y)
print model.summary
print "Beta",model.beta
-------------------------Summary of Regression Analysis-------------------------
Formula: Y ~ <x> + <intercept>
Number of Observations: 79
Number of Degrees of Freedom: 2
R-squared: 0.0668
Adj R-squared: 0.0546
Rmse: 0.7631
F-stat (1, 77): 5.5081, p-value: 0.0215
Degrees of Freedom: model 1, resid 77
-----------------------Summary of Estimated Coefficients------------------------
Variable Coef Std Err t-stat p-value CI 2.5% CI 97.5%
--------------------------------------------------------------------------------
x 0.0087 0.0037 2.35 0.0215 0.0014 0.0160
intercept -9.8244 7.1897 -1.37 0.1758 -23.9162 4.2674
---------------------------------End of Summary---------------------------------
Beta x 0.008698
intercept -9.824377
In [12]:
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')
Out[12]:
<matplotlib.text.Text at 0xc047390>
In [13]:
data
Out[13]:
YEAR
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC
D-J-F
M-A-M
J-J-A
S-O-N
metANN
0
1896
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
7.1
1.6
-1.7
999.9
999.9
999.9
7.3
NaN
1
1897
-4.9
-4.1
-1.6
5.6
10.1
15.1
21.8
18.7
17.1
11.2
2.1
-3.1
-3.6
4.7
18.5
10.1
7.45
2
1898
-3.4
-4.6
1.2
4.7
11.1
16.9
20.1
18.1
15.7
9.6
0.4
-4.7
-3.7
5.7
18.4
8.6
7.22
3
1899
-7.3
-9.9
-5.2
1.9
12.4
16.2
17.4
19.2
12.1
10.3
999.9
999.9
-7.3
3.0
17.6
8.0
5.34
4
1900
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
NaN
5
1901
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
1.8
-5.0
999.9
999.9
999.9
999.9
NaN
6
1902
-5.3
-6.9
0.8
4.3
10.8
14.0
18.8
999.9
999.9
8.0
5.5
-3.2
-5.7
5.3
16.7
9.7
6.50
7
1903
-6.3
999.9
2.9
3.7
11.5
14.8
19.1
15.5
14.5
9.9
0.4
-7.8
-5.2
6.0
16.5
8.3
6.39
8
1904
999.9
999.9
999.9
999.9
999.9
15.5
18.0
999.9
14.6
8.7
4.7
999.9
999.9
999.9
17.0
9.3
NaN
9
1905
-7.3
-9.8
-1.1
5.1
9.1
15.2
19.1
19.0
15.8
9.3
1.6
999.9
-7.7
4.4
17.8
8.9
5.83
10
1906
-1.7
-5.5
-3.6
999.9
999.9
999.9
999.9
20.6
17.5
8.1
2.9
-3.3
-2.8
999.9
999.9
9.5
NaN
11
1907
-6.3
-6.9
1.1
0.6
6.8
15.4
18.5
17.3
15.8
7.4
1.8
-1.6
-5.5
2.8
17.1
8.3
5.68
12
1908
-3.9
-6.1
-0.4
4.9
11.4
999.9
20.9
17.3
17.9
11.1
3.9
-2.4
-3.9
5.3
18.2
11.0
7.65
13
1909
-4.6
-3.4
-1.9
3.0
11.1
17.1
18.7
19.4
14.5
999.9
4.5
-3.9
-3.5
4.1
18.4
9.7
7.17
14
1910
-5.0
-6.3
4.3
7.7
7.6
17.1
19.8
18.4
14.0
10.7
999.9
-4.2
-5.1
6.5
18.4
9.2
7.27
15
1911
-4.8
-3.0
-1.1
4.6
13.2
18.4
20.5
18.0
13.9
8.2
0.9
-0.8
-4.0
5.6
19.0
7.7
7.05
16
1912
999.9
-8.6
-5.2
5.7
11.6
14.4
17.8
16.4
15.5
9.9
3.7
-0.3
-5.1
4.0
16.2
9.7
6.21
17
1913
-3.3
-7.5
-1.6
5.6
9.7
16.1
18.7
19.0
14.8
9.3
5.1
1.0
-3.7
4.6
17.9
9.7
7.13
18
1914
-2.7
-9.6
-1.7
4.1
11.7
15.4
19.4
18.7
15.1
12.2
3.0
-4.4
-3.8
4.7
17.8
10.1
7.22
19
1915
-5.5
-2.2
-1.5
8.1
9.5
14.0
17.5
16.0
15.4
10.1
4.1
-2.9
-4.0
5.4
15.8
9.9
6.76
20
1916
-3.3
-7.1
-3.8
4.8
10.6
13.6
22.1
19.8
14.3
8.6
2.9
-4.2
-4.4
3.9
18.5
8.6
6.63
21
1917
-6.5
-9.9
-1.0
3.4
7.8
13.2
18.3
16.9
13.4
4.6
2.4
-6.9
-6.9
3.4
16.1
6.8
4.87
22
1918
-11.2
-7.3
0.4
4.3
11.8
14.1
17.3
19.3
11.8
9.9
4.8
0.4
-8.5
5.5
16.9
8.8
5.69
23
1919
-2.0
-3.1
0.3
5.3
10.8
20.4
20.0
18.6
15.9
9.9
1.5
-6.4
-1.6
5.5
19.7
9.1
8.17
24
1920
-9.5
-7.3
0.0
2.8
9.9
15.3
16.3
17.6
16.4
13.2
2.9
-0.6
-7.7
4.2
16.4
10.8
5.93
25
1921
-2.2
-2.6
1.8
8.1
12.6
19.3
22.7
18.8
18.1
10.2
1.6
-2.0
-1.8
7.5
20.3
10.0
8.98
26
1922
-5.6
-4.7
0.5
5.4
14.5
16.5
18.3
18.7
16.3
9.8
5.1
-3.7
-4.1
6.8
17.8
10.4
7.73
27
1923
-4.7
-8.1
-4.1
3.9
10.7
16.4
19.5
16.7
14.6
8.4
4.0
1.8
-5.5
3.5
17.5
9.0
6.13
28
1924
-7.5
-5.7
-1.7
4.9
7.5
14.4
16.6
17.6
12.4
11.6
3.0
-5.3
-3.8
3.6
16.2
9.0
6.24
29
1925
-6.4
-3.6
0.1
7.4
8.0
15.5
18.1
18.9
16.3
5.0
2.0
-3.4
-5.1
5.2
17.5
7.8
6.33
30
1926
-4.7
-4.4
-4.3
1.9
10.5
12.7
18.7
18.6
14.4
8.6
1.6
-5.1
-4.2
2.7
16.7
8.2
5.85
31
1927
-5.5
-2.6
2.3
5.6
9.7
13.8
17.6
15.8
16.6
11.2
4.0
-3.0
-4.4
5.9
15.7
10.6
6.95
32
1928
-4.5
-4.3
-1.0
3.2
10.1
13.3
18.8
18.4
13.5
10.5
3.7
-0.2
-3.9
4.1
16.8
9.2
6.56
33
1929
-8.5
-7.7
1.4
5.9
8.9
14.1
18.8
17.1
14.6
8.6
1.4
-2.9
-5.5
5.4
16.7
8.2
6.20
34
1930
-6.9
-2.1
-1.1
6.1
11.7
15.8
18.8
19.5
15.9
8.6
4.8
-2.2
-4.0
5.6
18.0
9.8
7.35
35
1931
-1.9
-0.8
-0.6
5.6
9.7
18.1
21.3
19.3
18.1
11.7
7.3
1.3
-1.6
4.9
19.6
12.4
8.80
36
1932
-0.2
-2.0
-3.0
5.0
11.0
18.0
19.6
19.9
15.5
8.9
1.6
-2.4
-0.3
4.3
19.2
8.7
7.97
37
1933
-0.5
-5.5
-1.2
5.9
12.3
18.7
21.0
18.8
17.6
8.7
0.8
-3.5
-2.8
5.7
19.5
9.0
7.85
38
1934
-2.1
-9.3
-3.0
4.8
12.9
17.9
20.4
17.7
15.4
10.1
4.9
-4.5
-5.0
4.9
18.7
10.1
7.18
39
1935
-5.5
-5.2
0.3
4.3
8.3
14.0
21.3
19.8
13.9
8.8
2.0
-4.0
-5.1
4.3
18.4
8.2
6.46
40
1936
-7.0
-11.3
-0.1
2.5
11.9
13.9
20.8
19.5
16.4
8.2
0.9
-1.5
-7.4
4.8
18.1
8.5
5.97
41
1937
-4.5
-4.3
-2.7
5.1
10.9
15.5
20.0
20.9
15.0
7.4
2.3
-4.6
-3.4
4.4
18.8
8.2
7.01
42
1938
-6.0
-3.5
2.4
5.9
11.8
15.7
18.8
21.1
14.0
11.5
4.5
-1.4
-4.7
6.7
18.5
10.0
7.63
43
1939
-3.5
-5.3
-1.6
3.2
10.0
16.6
19.6
19.3
15.5
9.5
3.0
0.4
-3.4
3.9
18.5
9.3
7.07
44
1940
-6.8
-3.6
-2.7
4.0
9.3
15.0
19.2
18.7
14.8
9.9
3.0
-0.9
-3.3
3.5
17.6
9.2
6.77
45
1941
-3.6
-4.9
-2.5
7.3
999.9
16.5
18.2
18.2
999.9
11.1
4.2
0.2
-3.1
5.3
17.6
10.6
7.61
46
1942
-4.9
-6.1
1.2
8.7
12.1
17.5
19.8
19.5
14.6
9.8
3.1
-4.7
-3.6
7.3
18.9
9.2
7.96
47
1943
-7.6
-4.8
-3.1
4.2
10.4
18.3
20.0
20.1
13.5
8.8
1.5
-3.1
-5.7
3.8
19.5
7.9
6.38
48
1944
-2.0
-3.8
-2.4
4.7
14.0
18.5
20.7
20.4
15.8
8.5
4.5
-4.6
-3.0
5.4
19.9
9.6
7.98
49
1945
-8.5
-4.9
5.7
7.4
9.3
14.9
18.7
19.2
14.9
8.1
3.1
-5.3
-6.0
7.5
17.6
8.7
6.94
50
1946
-4.2
-5.8
5.2
6.7
11.0
16.2
20.5
18.3
16.0
11.9
4.2
-2.6
-5.1
7.6
18.3
10.7
7.89
51
1947
-4.0
-6.7
-2.6
4.5
8.7
15.9
18.8
22.8
17.3
14.4
0.9
-3.1
-4.4
3.5
19.2
10.9
7.28
52
1948
-8.2
-6.0
-1.3
8.5
9.7
16.2
20.3
20.0
17.2
9.2
5.2
-2.3
-5.8
5.6
18.8
10.5
7.31
53
1949
-3.4
-4.6
-0.6
5.9
12.1
19.5
21.9
20.3
13.8
12.0
2.2
-2.2
-3.4
5.8
20.6
9.3
8.07
54
1950
-3.7
-5.3
-3.2
2.2
11.8
16.2
18.0
16.9
14.6
11.7
0.8
-4.8
-3.7
3.6
17.0
9.0
6.48
55
1951
-5.6
-4.7
-0.7
5.2
13.6
16.0
18.7
17.6
14.4
10.0
-1.5
-3.3
-5.0
6.0
17.4
7.6
6.52
56
1952
-3.7
-3.2
-1.5
7.4
11.3
18.3
21.3
19.2
16.2
6.7
4.0
-1.2
-3.4
5.7
19.6
9.0
7.72
57
1953
-3.6
-3.3
0.7
4.5
12.3
18.2
20.9
20.5
16.3
11.6
5.2
-0.9
-2.7
5.8
19.9
11.0
8.51
58
1954
-5.8
-0.8
999.9
7.4
10.6
18.6
19.8
18.4
15.3
9.4
3.5
-2.7
-2.5
6.0
18.9
9.4
7.95
59
1955
-5.2
-4.4
-0.8
9.6
13.0
17.1
22.6
22.0
14.8
10.1
0.8
-4.3
-4.1
7.3
20.6
8.6
8.07
60
1956
-4.8
-4.4
-2.4
5.1
10.7
19.2
18.2
19.1
13.6
11.9
3.2
-1.6
-4.5
4.5
18.8
9.6
7.09
61
1957
-7.5
-3.6
-0.6
6.6
11.4
18.0
20.2
18.5
14.5
8.9
3.4
-1.1
-4.2
5.8
18.9
8.9
7.35
62
1958
-4.0
-6.1
0.2
6.8
11.0
14.3
19.2
19.3
15.5
10.8
3.3
-7.3
-3.7
6.0
17.6
9.9
7.43
63
1959
-8.1
-6.5
-1.8
5.4
14.2
18.2
19.3
21.5
16.3
7.5
-0.7
0.1
-7.3
5.9
19.7
7.7
6.50
64
1960
-2.8
-4.0
-4.4
7.4
11.3
15.7
18.2
19.1
16.6
9.0
4.4
-4.9
-2.2
4.8
17.7
10.0
7.55
65
1961
-6.6
-3.1
1.3
4.2
9.6
16.6
19.0
19.3
17.9
10.5
3.4
-3.6
-4.9
5.0
18.3
10.6
7.27
66
1962
-7.1
-7.2
-0.8
6.0
15.2
17.4
18.3
19.0
13.4
10.3
2.6
-4.4
-6.0
6.8
18.2
8.8
6.96
67
1963
-9.7
-10.0
-0.4
6.4
10.5
17.8
19.6
17.2
14.2
13.3
4.9
-5.5
-8.0
5.5
18.2
10.8
6.62
68
1964
-3.0
-3.9
-0.5
7.5
13.7
18.0
20.9
17.9
14.4
8.4
5.2
-3.9
-4.1
6.9
18.9
9.3
7.76
69
1965
-5.8
-5.8
-3.5
4.7
14.2
16.0
18.0
17.9
14.9
9.0
3.2
0.6
-5.2
5.1
17.3
9.0
6.57
70
1966
-7.9
-4.2
1.4
5.0
8.9
17.5
21.3
18.7
14.6
9.3
2.5
-2.7
-3.8
5.1
19.2
8.8
7.31
71
1967
-3.5
-7.0
-0.6
6.3
9.1
18.0
18.6
17.2
14.6
9.4
1.1
-2.1
-4.4
4.9
17.9
8.4
6.71
72
1968
-5.9
-7.7
2.6
7.5
10.4
17.0
19.2
19.4
16.3
10.9
3.4
-3.7
-5.2
6.8
18.5
10.2
7.58
73
1969
-6.4
-5.9
-2.6
6.9
11.9
14.4
20.1
999.9
15.8
8.9
3.0
-3.5
-5.3
5.4
17.5
9.2
6.71
74
1970
-8.4
-6.9
-4.0
9.1
12.5
999.9
999.9
20.2
16.4
10.5
3.4
-3.4
-6.3
5.9
999.9
10.1
6.96
75
1971
999.9
999.9
-4.5
5.4
9.9
18.8
19.6
18.1
16.2
14.0
3.0
-1.3
999.9
3.6
18.8
11.1
7.29
76
1972
-6.2
-5.7
-2.4
4.9
13.9
14.8
18.8
19.6
15.1
7.6
2.1
-3.9
-4.4
5.5
17.7
8.3
6.77
77
1973
-3.4
-5.2
4.6
6.2
10.5
18.6
999.9
20.8
15.7
12.5
3.4
-4.1
-4.2
7.1
20.3
10.5
8.45
78
1974
-4.5
-6.2
-0.1
7.1
11.0
16.6
999.9
999.9
13.4
8.5
3.4
-1.2
-4.9
6.0
999.9
8.4
6.89
79
1975
-3.9
-4.5
-1.9
3.5
14.0
17.9
20.2
19.9
13.9
10.8
6.7
-3.1
-3.2
5.2
19.3
10.5
7.95
80
1976
-7.2
-2.2
0.8
7.4
9.9
18.9
20.6
18.1
999.9
6.9
-0.4
-6.7
-4.2
6.0
19.2
6.2
6.83
81
1977
-10.1
-6.1
3.1
8.6
16.2
15.6
21.2
17.5
999.9
7.7
3.8
-4.2
-7.6
9.3
18.1
8.7
7.13
82
1978
-7.9
-9.2
-2.1
5.8
11.3
17.0
18.9
19.7
17.1
8.4
999.9
-3.8
-7.1
5.0
18.5
9.6
6.50
83
1979
-8.5
-9.0
0.3
5.3
999.9
17.3
19.9
18.1
16.0
999.9
3.7
-0.1
-7.1
5.7
18.4
10.0
6.77
84
1980
-4.7
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
999.9
-2.9
999.9
999.9
999.9
NaN
In [14]:
result=fit(x,y,'power',2)
In [15]:
xfit = linspace(1850,2000,100)
yfit = fitval(result,xfit)
plot(x,y,'-o')
plot(xfit,yfit,'-')
xlabel('Year')
ylabel('Temperature Deviation')
Out[15]:
<matplotlib.text.Text at 0xc05e330>
printing out the results of the fit.
In [16]:
result
Out[16]:
{'label': 'y= -2.8541e-04 x^2 + 1.1156e+00 x + -1.0830e+03',
'pandas_result':
-------------------------Summary of Regression Analysis-------------------------
Formula: Y ~ <x> + <x2> + <intercept>
Number of Observations: 79
Number of Degrees of Freedom: 3
R-squared: 0.1000
Adj R-squared: 0.0763
Rmse: 0.7543
F-stat (2, 76): 4.2218, p-value: 0.0183
Degrees of Freedom: model 2, resid 76
-----------------------Summary of Estimated Coefficients------------------------
Variable Coef Std Err t-stat p-value CI 2.5% CI 97.5%
--------------------------------------------------------------------------------
x 1.1156 0.6608 1.69 0.0954 -0.1795 2.4108
x2 -0.0003 0.0002 -1.68 0.0980 -0.0006 0.0000
intercept -1082.9510 640.6373 -1.69 0.0950 -2338.6001 172.6980
---------------------------------End of Summary---------------------------------
,
'params': [-0.00028541005674405877, 1.1156306833035785, -1082.9510493876501],
'type': 'power'}
This should do the same thing.
In [17]:
result=fit(x,y,'quadratic')
In [18]:
result
Out[18]:
{'label': 'y= -2.8541e-04 x^2 + 1.1156e+00 x + -1.0830e+03',
'pandas_result':
-------------------------Summary of Regression Analysis-------------------------
Formula: Y ~ <x> + <x2> + <intercept>
Number of Observations: 79
Number of Degrees of Freedom: 3
R-squared: 0.1000
Adj R-squared: 0.0763
Rmse: 0.7543
F-stat (2, 76): 4.2218, p-value: 0.0183
Degrees of Freedom: model 2, resid 76
-----------------------Summary of Estimated Coefficients------------------------
Variable Coef Std Err t-stat p-value CI 2.5% CI 97.5%
--------------------------------------------------------------------------------
x 1.1156 0.6608 1.69 0.0954 -0.1795 2.4108
x2 -0.0003 0.0002 -1.68 0.0980 -0.0006 0.0000
intercept -1082.9510 640.6373 -1.69 0.0950 -2338.6001 172.6980
---------------------------------End of Summary---------------------------------
,
'params': [-0.00028541005674405877, 1.1156306833035785, -1082.9510493876501],
'type': 'quadratic'}
In [19]:
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')
Out[19]:
<matplotlib.text.Text at 0xc3b07b0>
In [20]:
result
Out[20]:
{'label': 'y= -7.8138e-11 x^4 + 3.0347e-07 x^3 + -2.9274e-04 x^2 + -4.5306e-07 x + -4.6748e-10',
'pandas_result':
-------------------------Summary of Regression Analysis-------------------------
Formula: Y ~ <x> + <x2> + <x3> + <x4> + <intercept>
Number of Observations: 79
Number of Degrees of Freedom: 3
R-squared: 0.1010
Adj R-squared: 0.0774
Rmse: 0.7539
F-stat (4, 76): 4.2714, p-value: 0.0036
Degrees of Freedom: model 2, resid 76
-----------------------Summary of Estimated Coefficients------------------------
Variable Coef Std Err t-stat p-value CI 2.5% CI 97.5%
--------------------------------------------------------------------------------
x -0.0000 2393.3152 -0.00 1.0000 -4690.8977 4690.8977
x2 -0.0003 1.8523 -0.00 0.9999 -3.6307 3.6301
x3 0.0000 0.0006 0.00 0.9996 -0.0012 0.0012
x4 -0.0000 0.0000 -0.00 0.9992 -0.0000 0.0000
intercept -0.0000 1159760.3491 -0.00 1.0000 -2273130.2841 2273130.2841
---------------------------------End of Summary---------------------------------
,
'params': [-7.8138279533884736e-11,
3.0347034092619317e-07,
-0.00029273634031567233,
-4.5305780613546115e-07,
-4.674847735217661e-10],
'type': 'power'}
In [20]:
Content source: bblais/Python-for-Science
Similar notebooks: