<jupyter_start><jupyter_text>Import libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load dataset<jupyter_code>dataset = pd.read_csv('datasets/Salary_Data.csv')
dataset.head(5)
X = dataset.iloc[:, 0:-1].values
y = dataset.iloc[:, -1].values
print(X[:5])<jupyter_output>[[1.1]
 [1.3]
 [1.5]
 [2. ]
 [2.2]]<jupyter_text>Clean data<jupyter_code>from sklearn.impute import SimpleImputer

imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')
imp_mean.fit(X)
X = imp_mean.transform(X)
print(X[:5])<jupyter_output>[[1.1]
 [1.3]
 [1.5]
 [2. ]
 [2.2]]<jupyter_text>Split data between train and test<jupyter_code>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=1)<jupyter_output><empty_output><jupyter_text>Feature scaling<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
print('X_test', X_test[:5])
print('X_train', X_train[:5])<jupyter_output>X_test [[ 0.14042184]
 [ 0.74948043]
 [-0.3332904 ]
 [ 0.37727796]
 [-0.13027087]]
X_train [[ 1.12168291]
 [-0.9085124 ]
 [-1.14536852]
 [ 1.39237561]
 [-0.63781969]]<jupyter_text>Create a model and fit<jupyter_code>from sklearn.linear_model import LinearRegression

lr = LinearRegression()
lr.fit(X_train, y_train)

predicted = lr.predict(X_test)
plt.style.use('ggplot')

f = plt.figure(figsize=(12,5))
ax1 = f.add_subplot(1, 2, 1)
ax2 = f.add_subplot(1, 2, 2)
ax1.scatter(X_train, y_train, c='r')
ax1.plot(X_train, lr.predict(X_train))
ax1.set_xlabel('Experience')
ax1.set_ylabel('Salary')
ax1.set_title('Train data')

ax2.scatter(X_test, y_test, c='r')
ax2.plot(X_test, lr.predict(X_test))
ax2.set_xlabel('Experience')
ax2.set_ylabel('Salary')
ax2.set_title('Test data')

plt.tight_layout()
from matplotlib import gridspec
plt.style.use('ggplot')

f = plt.figure(figsize=(12,5))
gs = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[2, 1])
ax1 = f.add_subplot(gs[0])
ax2 = f.add_subplot(gs[1])
ax1.scatter(X_train, y_train, c='r')
ax1.plot(X_train, lr.predict(X_train))
ax1.set_xlabel('Experience')
ax1.set_ylabel('Salary')
ax1.set_title('Train data')

ax2.scatter(X_test, y_test, c='r')
ax2.plot(X_test, lr.predict(X_test))
ax2.set_xlabel('Experience')
ax2.set_ylabel('Salary')
ax2.set_title('Test data')

plt.tight_layout()
print('lr coefficients', lr.coef_)
print('lr intercept', lr.intercept_)
print('line equation would be')
print(f'salary = {lr.coef_[0]} x Experience + {lr.intercept_} ')<jupyter_output>lr coefficients [27065.7877089]
lr intercept 70874.75
line equation would be
salary = 27065.78770889806 x Experience + 70874.75
{'fraction_non_alphanumeric': 0.1615549215406562, 'fraction_numerical': 0.0723965763195435, 'mean_word_length': 7.106936416184971, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '623472', 'license': 'MIT', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1205, 'n_words': 193, 'path': 'regression/linear.ipynb', 'repo_name': 'KrishnanandSingh/datascience', 'size': '2804'}