-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldHappinessReport2019.py
51 lines (43 loc) · 1.47 KB
/
WorldHappinessReport2019.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Importing Libraries
import numpy as np
import pandas as pd
# Taking Data
data = pd.read_csv('WorldHappinessReport-2019.csv')
# Data Imputer
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy='constant',fill_value= 0)
impdata = data.iloc[:,3:9].values
imputer = imputer.fit(impdata[:,3:9])
impdata[:,3:9] = imputer.transform(impdata[:,3:9])
data.iloc[:,3:9] = impdata[:,:]
# Data Eploratory
print(data.columns)
print(data.info())
print(data.describe())
df = data["Country or region"]
dff = data.value_counts()
# Selecting Columns x and y
x = data.iloc[:,3:].values
y = data.iloc[:,2:3].values
# Splitting as Test and Train
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=0)
# Scaling of Data
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(x_train)
X_test = sc.fit_transform(x_test)
Y_train = sc.fit_transform(y_train)
Y_test = sc.fit_transform(y_test)
# Building Model
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train, y_train)
print("b0: ", lr.intercept_)
print("other b: ", lr.coef_)
# Controlling of Success of Data and Model
import statsmodels.regression.linear_model as sm
X = np.append(arr = np.ones((156,1)).astype(int), values=x, axis=1)
r_ols = sm.OLS(endog = y, exog = X)
r = r_ols.fit()
print(r.summary())