You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 KiB

None <html lang="en"> <head> </head>
In [21]:
# data ussing a linear model
import numpy as np
import matplotlib.pyplot as plt

m = 500
theta0 = 2.5
theta1 = 5
disp = 4
x = np.linspace(0, 5, m)
y = theta0+theta1*x+disp*np.random.randn(m)

$(x_m,y_m)$

In [22]:
plt.scatter(x,y,color='black', alpha=0.3, marker='o')
plt.show()
No description has been provided for this image
In [24]:
# Pearson coefficient
xMean = x.mean()
yMean = y.mean()

#Sxx 
xSum = 0 
for i in range(len(x)):
    xSum = xSum+(x[i]-xMean)**2
    pass
Sxx = xSum

Syy = np.sum((y-yMean)**2)

Sxy = np.sum((x-xMean)*(y-yMean))
r = Sxy/np.sqrt(Sxx*Syy)
r
Out[24]:
np.float64(0.8681163370978059)
In [26]:
#GDP vs life satisfaction:
import pandas as pd
data = pd.read_csv('gdp-satisfaction.csv')
data
Out[26]:
Country GDP per capita Life satisfaction
0 Russia 9054.914 6.0
1 Turkey 9437.372 5.6
2 Hungary 12239.894 4.9
3 Poland 12495.334 5.8
4 Slovak Republic 15991.736 6.1
5 Estonia 17288.083 5.6
6 Greece 18064.288 4.8
7 Portugal 19121.592 5.1
8 Slovenia 20732.482 5.7
9 Spain 25864.721 6.5
10 Korea 27195.197 5.8
11 Italy 29866.581 6.0
12 Japan 32485.545 5.9
13 Israel 35343.336 7.4
14 New Zealand 37044.891 7.3
15 France 37675.006 6.5
16 Belgium 40106.632 6.9
17 Germany 40996.511 7.0
18 Finland 41973.988 7.4
19 Canada 43331.961 7.3
20 Netherlands 43603.115 7.3
21 Austria 43724.031 6.9
22 United Kingdom 43770.688 6.8
23 Sweden 49866.266 7.2
24 Iceland 50854.583 7.5
25 Australia 50961.865 7.3
26 Ireland 51350.744 7.0
27 Denmark 52114.165 7.5
28 United States 55805.204 7.2
In [39]:
x = data["GDP per capita"]
y = data["Life satisfaction"]
labels = data["Country"]
labels
Out[39]:
0              Russia
1              Turkey
2             Hungary
3              Poland
4     Slovak Republic
5             Estonia
6              Greece
7            Portugal
8            Slovenia
9               Spain
10              Korea
11              Italy
12              Japan
13             Israel
14        New Zealand
15             France
16            Belgium
17            Germany
18            Finland
19             Canada
20        Netherlands
21            Austria
22     United Kingdom
23             Sweden
24            Iceland
25          Australia
26            Ireland
27            Denmark
28      United States
Name: Country, dtype: str
In [47]:
plt.scatter(x,y, color="black", alpha=0.4, marker="v")
# for i, row in data.iterrows():
#     plt.annotate(data["Country"], (data["Life satisfaction"], data["GDP per capita"]))
#     pass
# #plt.ylim([0,20])
plt.show()
No description has been provided for this image
</html>