<jupyter_start><jupyter_text>Predicting the per capita income based on previous observations<jupyter_code># Importing libraries
import pandas as pd
from sklearn import linear_model
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
data = pd.read_csv('canada_per_capita_income.csv')
data.head()
data.nunique()
data.columns
data.shape
data.describe(percentiles=[0.25,0.50,0.75,0.90,0.95,0.99])
data.columns = ['year','per_capita_income']
data.columns
# Plotting the graph to see if there are any outlier or to get an idea of a dataset
plt.xlabel('Year')
plt.ylabel('Per Capita in US $')
plt.scatter(data[['year']], data[['per_capita_income']])
# Splitting the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(data[['year']],
                                                    data['per_capita_income'],test_size=0.3, random_state=100)
reg = linear_model.LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
y_pred
plt.xlabel('Year')
plt.ylabel('Per capita in $')
plt.scatter(X_test,y_test, color='red',marker='*')
plt.plot(X_test,y_pred, color='blue')
# Model Evaluation
reg.score(X_test,y_test)
# Pickling
import pickle
with open('model_pkl','wb') as f:
    pickle.dump(reg, f)
with open('model_pkl','rb') as f:
    model = pickle.load(f)
model.score(X_test, y_test)
# Using joblib
import joblib
joblib.dump(reg, 'model_joblib')
model_jl = joblib.load('model_joblib')
model_jl.score(X_test, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13043478260869565, 'fraction_numerical': 0.014492753623188406, 'mean_word_length': 5.6722689075630255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '444370', 'license': 'MIT', 'n_tokens_mistral': 597, 'n_tokens_neox': 568, 'n_words': 127, 'path': 'Supervised/Regression/LR_Exercise.ipynb', 'repo_name': 'KarthikKaiplody/Machine_Learning_Algorithms', 'size': '1587'}