<jupyter_start><jupyter_text>Data Preprocessing<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Importing the Datasets<jupyter_code>dataset=pd.read_csv("E:\\Edu\\Data Science and ML\\Machinelearningaz\\Datasets\\Part 2 - Regression\\Section 4 - Simple Linear Regression\\Salary_Data.csv")
dataset.head()


dataset.shape
dataset.describe()
X=dataset.iloc[:,:-1].values    #Takes all values except last colum values  (Matrix)
y=dataset.iloc[:,1].values    #Take last colum values   (Vector)
print(X)
print(y)<jupyter_output>[[ 1.1]
 [ 1.3]
 [ 1.5]
 [ 2. ]
 [ 2.2]
 [ 2.9]
 [ 3. ]
 [ 3.2]
 [ 3.2]
 [ 3.7]
 [ 3.9]
 [ 4. ]
 [ 4. ]
 [ 4.1]
 [ 4.5]
 [ 4.9]
 [ 5.1]
 [ 5.3]
 [ 5.9]
 [ 6. ]
 [ 6.8]
 [ 7.1]
 [ 7.9]
 [ 8.2]
 [ 8.7]
 [ 9. ]
 [ 9.5]
 [ 9.6]
 [10.3]
 [10.5]]
[ 39343.  46205.  37731.  43525.  39891.  56642.  60150.  54445.  64445.
  57189.  63218.  55794.  56957.  57081.  61111.  67938.  66029.  83088.
  81363.  93940.  91738.  98273. 101302. 113812. 109431. 105582. 116969.
 112635. 122391. 121872.]<jupyter_text>Splitting Dataset into TrainingSet and TestSet<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=1/3,random_state=0)   #testset 20%
print(X_train)
print(X_test)
print(y_train)
print(y_test)<jupyter_output>[[ 2.9]
 [ 5.1]
 [ 3.2]
 [ 4.5]
 [ 8.2]
 [ 6.8]
 [ 1.3]
 [10.5]
 [ 3. ]
 [ 2.2]
 [ 5.9]
 [ 6. ]
 [ 3.7]
 [ 3.2]
 [ 9. ]
 [ 2. ]
 [ 1.1]
 [ 7.1]
 [ 4.9]
 [ 4. ]]
[[ 1.5]
 [10.3]
 [ 4.1]
 [ 3.9]
 [ 9.5]
 [ 8.7]
 [ 9.6]
 [ 4. ]
 [ 5.3]
 [ 7.9]]
[ 56642.  66029.  64445.  61111. 113812.  91738.  46205. 121872.  60150.
  39891.  81363.  93940.  57189.  54445. 105582.  43525.  39343.  98273.
  67938.  56957.]
[ 37731. 122391.  57081.  63218. 116969. 109431. 112635.  55794.  83088.
 101302.]<jupyter_text>Fitting Simple Linear Regression to Training Set<jupyter_code>from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Predicting The Test set Results<jupyter_code>y_pred=regressor.predict(X_test)
print(y_pred)
# display coefficients
print(regressor.coef_)
print(regressor.intercept_)
print("Line eqn is y=",regressor.coef_[0],"x+",regressor.intercept_)<jupyter_output>[9345.94244312]
26816.19224403119
Line eqn is y= 9345.94244312237 x+ 26816.19224403119<jupyter_text>Visualising the Training Set results<jupyter_code>plt.scatter(X_train,y_train,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.title("Salary vs Experience (training Set)")
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()<jupyter_output><empty_output><jupyter_text>Visualising the Test Set results<jupyter_code>plt.scatter(X_test,y_test,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.title("Salary vs Experience (Test Set)")
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code># Let's check how best our line has fitted. An r square value close to 1 means the line has a perfect fit wih the data.
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print("r2 score : ", r2_score(y_test, y_pred))
mean_squared_error(y_test, y_pred)<jupyter_output>r2 score :  0.9749154407708353
{'fraction_non_alphanumeric': 0.1563049853372434, 'fraction_numerical': 0.15219941348973606, 'mean_word_length': 4.199695121951219, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '603813', 'license': 'MIT', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1484, 'n_words': 283, 'path': '2 Regression/1 Simple Linear Regression ( Salary vs Experience).ipynb', 'repo_name': 'BheemashankarJakkani/Machine_Learning', 'size': '3410'}