<jupyter_start><jupyter_text>Predict GDP of Canada  Source of dataThe source of the data used here is the World Bank national accounts data, and OECD National Accounts data files. GDP (current US$) - CanadaGDP at purchaser's prices is the sum of gross value added by all resident producers in the economy plus any product taxes and minus any subsidies not included in the value of the products. It is calculated without making deductions for depreciation of fabricated assets or for depletion and degradation of natural resources. Data are in current U.S. dollars. Dollar figures for GDP are converted from domestic currencies using single year official exchange rates. For a few countries where the official exchange rate does not reflect the rate effectively applied to actual foreign exchange transactions, an alternative conversion factor is used. What is in this notebook?Here using machine learing techniques I have created a model to predict GDP (in US$) of Canada.<jupyter_code># To avoid warnings
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

%matplotlib inline

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

df = pd.read_csv('data.csv')
df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>Data Analysis<jupyter_code>print(df.corr()['per capita income (US$)'].sort_values())

# Using Pearson Correlation
plt.figure(figsize=(7,4))
cor = df.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.show()<jupyter_output>year                       0.943884
per capita income (US$)    1.000000
Name: per capita income (US$), dtype: float64<jupyter_text>There is a high correlation between 'year' and 'per capita income (US$)'<jupyter_code>plt.plot(df[['year']], df[['per capita income (US$)']], color='k', linestyle='--')<jupyter_output><empty_output><jupyter_text>Creating Model  Splitting the dataset Since per capita income (US$) is increasing slowly in the beginning and between 2000 and 2010 it increased rapidly, so we have to suffle the dataset so that we can get increasing trend of per capita income correctly.<jupyter_code># Shuffling the dataset
df = df.sample(frac=1, random_state=3)
_feature = 'year'
_target = 'per capita income (US$)'

train_dataset = df[:35]
X_train = np.array(train_dataset[_feature]).reshape(-1, 1)
y_train = np.array(train_dataset[_target]).reshape(-1, 1)

test_dataset = df[36:47]
X_test = np.array(test_dataset[_feature]).reshape(-1, 1)
y_test = np.array(test_dataset[_target]).reshape(-1, 1)<jupyter_output><empty_output><jupyter_text>Feature Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler

X_train = StandardScaler().fit_transform(X_train)
X_test = StandardScaler().fit_transform(X_test)<jupyter_output><empty_output><jupyter_text>Cross Validation<jupyter_code>from sklearn.linear_model import LinearRegression, Ridge
from sklearn.model_selection import KFold, cross_val_score

kf = KFold(n_splits=3)

score = cross_val_score(LinearRegression(), X_train, y_train, cv=kf, scoring='r2')
print(f'LinearRegression: {score.mean()}')

score = cross_val_score(Ridge(), X_train, y_train, cv=kf, scoring='r2')
print(f'Ridge: {score.mean()}')<jupyter_output>LinearRegression: 0.88506418278699
Ridge: 0.8824747785785633<jupyter_text>Learning Curve<jupyter_code>from sklearn.model_selection import learning_curve

_size = np.arange(0.01, 1.01, 0.01)
train_sizes = np.array(_size)
scoring = 'neg_mean_squared_error'

train_sizes_abs, train_scores, cv_scores = learning_curve(LinearRegression(), X_train, y_train, train_sizes=train_sizes, cv=kf, scoring=scoring)
train_scores_mean = []
for row in train_scores:
    _mean = row.mean()
    train_scores_mean.append(_mean)
    
cv_scores_mean = []
for row in cv_scores:
    _mean = row.mean()
    cv_scores_mean.append(_mean)    
    
train_scores_mean = -np.array(train_scores_mean)
cv_scores_mean = -np.array(cv_scores_mean)
plt.plot(train_sizes_abs, train_scores_mean, label='Train')
plt.plot(train_sizes_abs, cv_scores_mean, label='Cross Validation')

plt.legend()<jupyter_output><empty_output><jupyter_text>Fitting the model<jupyter_code>model = LinearRegression()
model.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Optimal Parameters<jupyter_code>coefficient = model.coef_
intercept = model.intercept_

print("Coefficient: ", coefficient)
print("Intercept: ", model.intercept_)
plt.scatter(X_train, y_train,  color='blue')
plt.plot(X_train, coefficient*X_train + intercept, '-r')<jupyter_output><empty_output><jupyter_text>Predicting<jupyter_code>y_test_pred = model.predict(X_test)
y_test_pred
y_test<jupyter_output><empty_output><jupyter_text>Evaluating Model<jupyter_code>from sklearn.metrics import mean_squared_error, r2_score
# *** Out-Of-Sample Forecast ***

# To see how our model performs on data that model has NOT seen

rms_error = mean_squared_error(y_test, y_test_pred, squared=False)
r2_score_value = r2_score(y_test, y_test_pred)

print(f"Root mean squared error: {rms_error}")
print(f"R2-score: {r2_score_value}")<jupyter_output>Root mean squared error: 4692.604463545105
R2-score: 0.879497092141861<jupyter_text>Pipeline<jupyter_code>from sklearn.pipeline import Pipeline
scaling = ('scale', StandardScaler())
model = ('model', LinearRegression())

# Steps in the pipeline
steps = [scaling, model]

pipe = Pipeline(steps=steps)

# Fiitting the model
model = pipe.fit(X_train, y_train)

# Out-Of-Sample Forecast
y_test_pred = model.predict(X_test)

# Evaluation
rms_error = mean_squared_error(y_test, y_test_pred, squared=False)
r2_score_value = r2_score(y_test, y_test_pred)

print(f"Root mean squared error: {rms_error}")
print(f"R2-score: {r2_score_value}")<jupyter_output>Root mean squared error: 4692.604463545105
R2-score: 0.879497092141861<jupyter_text>Saving the model<jupyter_code>from joblib import dump, load

dump(model, 'model.joblib')<jupyter_output><empty_output><jupyter_text>Visualizing our prediction against actual values<jupyter_code>f, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(13, 5))

ax1.plot(np.arange(len(y_test)), y_test, label='Actual')
ax2.plot(np.arange(len(y_test_pred)), y_test_pred, label='Prediction')

ax1.legend()
ax2.legend()

f, ax3 = plt.subplots(nrows=1, ncols=1, figsize=(13, 5))

ax3.plot(np.arange(len(y_test)), y_test, label='Actual')
ax3.plot(np.arange(len(y_test_pred)), y_test_pred, label='Prediction')

ax3.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11232497307278043, 'fraction_numerical': 0.027850438529004463, 'mean_word_length': 7.19672131147541, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '172100', 'license': 'MIT', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2248, 'n_words': 538, 'path': 'venv/src/using-sklearn.ipynb', 'repo_name': 'AkashSDas/predict-gdp-of-canada', 'size': '6499'}