<jupyter_start><jupyter_text>Importing the Dataset<jupyter_code># each row of the dataset corresponds to a different employee

df = pd.read_csv("Salary_Data.csv")
df.head()
len(df)
df.dtypes
df.describe()<jupyter_output><empty_output><jupyter_text>Check for missing values<jupyter_code>df.isnull().sum()

# Conclusion: No missing values<jupyter_output><empty_output><jupyter_text>Check for outliers - Linear Regression is sensitive to outliers<jupyter_code>figure = df.boxplot(column="YearsExperience")
figure = df.boxplot(column="Salary")

# Conclusion: No outliers<jupyter_output><empty_output><jupyter_text>Create matrix of features and label<jupyter_code># X and y are numpy arrays of 2D shape 

X = df.iloc[:,:-1].values # feature
y = df.iloc[:,-1].values # label

# for simple linear regression, X -> one feature only
X
y
X.shape,y.shape<jupyter_output><empty_output><jupyter_text>Splitting the dataset into Train and Test Set<jupyter_code>from sklearn.model_selection import train_test_split

# Train - 80% data, Test - 20% data
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)
X_train.shape,y_train.shape,X_test.shape,y_test.shape<jupyter_output><empty_output><jupyter_text>Training The Simple Linear Regression Model on Train Set<jupyter_code>from sklearn.linear_model import LinearRegression
model = LinearRegression()

# Train model by fit() method. Train set --> train model on existing observations

# fit() method will take X_train and y_train and will compute the parameters theta0 and theta1 
# for h(x) = theta0 + theta1(x) [hypothesis function/regression equation - straight line]

model.fit(X_train,y_train) # training model<jupyter_output><empty_output><jupyter_text>Evaluate Model - Predicting the Test results* Check out [Scikit-Learn documentation for evaluation metrics](https://scikit-learn.org/stable/modules/model_evaluation.html)<jupyter_code>plt.style.use("seaborn")
def evaluate_preds(y_test, y_pred):
  """
  Perform evaluation comparison methods on y_test(true labels) vs y_pred -- for regression problems
  """
  from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error

  accuracy = r2_score(y_test, y_pred)
  mse = mean_squared_error(y_test, y_pred) # Gradient Descent
  mae = mean_absolute_error(y_test, y_pred)
  metric_dict = {
      "r2": round(accuracy,2),
      "mse": round(mse,2),
      "mae": round(mae,2)
  }

  print("Regression model metrics on the test set:")
  print(f"Co-efficient of R^2: {accuracy * 100:.2f}%") # accuracy of model
  print(f"Mean Squared Error: {mse:.2f}")
  print(f"Mean absolute Error: {mae:.2f}")

  return metric_dict
# Test set --> evaluate model on new observations (future data)

# 1) Input the features to predict the label(estimated), returns a vector(matrix)
y_pred = model.predict(X_test) 

# 2) Then compare the predicted label with the actual label from the dataset (y_test --> true labels)
evaluation_metrics = evaluate_preds(y_test, y_pred)<jupyter_output>Regression model metrics on the test set:
Co-efficient of R^2: 98.82%
Mean Squared Error: 12823412.30
Mean absolute Error: 2446.17<jupyter_text>Visualize Data  Visualize Train Set<jupyter_code># Plot the train set
plt.scatter(X_train,y_train, color = 'red') # real data

# Plot the regression line (hypothesis function)
y_train_pred = model.predict(X_train) # predict a label for a training sample
plt.plot(X_train,y_train_pred,color='blue') # predicted

plt.title("Salary vs Experience (Test set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")<jupyter_output><empty_output><jupyter_text>Visualize Test Set<jupyter_code># Plot the test set - new observations
plt.scatter(X_test,y_test, color = 'red')

# Plot the regression line (hypothesis function)
plt.plot(X_test,y_pred,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>Save Model<jupyter_code>from joblib import dump

# Save a model to a file
dump(model,filename='simple_linear_regression.pkl')<jupyter_output><empty_output><jupyter_text>Making a single prediction (for example the salary of an employee with 12 years of experience) **Important note:** Notice that the value of the feature (12 years) was input in a double pair of square brackets. That's because the "predict" method always expects a 2D array as the format of its inputs. And putting 12 into a double pair of square brackets makes the input exactly a 2D array.12→scalar[12]→1D array[[12]]→2D array<jupyter_code>years = 12
salary = model.predict([[years]])
salary<jupyter_output><empty_output><jupyter_text>Getting the final linear regression equation with the values of the coefficients<jupyter_code>theta1 = model.coef_ # gradient
theta0 = model.intercept_ # y-intercept
cols = df.columns
feature = cols[0]
label = cols[1]
print("Hypothesis Equation/Linear Regression Equation")
print(f"{label} = {theta0} + {theta1} * {feature}")<jupyter_output>Hypothesis Equation/Linear Regression Equation
Salary = 26780.09915062818 + [9312.57512673] * YearsExperience
{'fraction_non_alphanumeric': 0.10549019607843137, 'fraction_numerical': 0.02, 'mean_word_length': 6.371387283236994, 'pattern_counts': {'":': 3, '<': 47, '<?xml version=': 0, '>': 51, 'https://': 1, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '442696', 'license': 'MIT', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1689, 'n_words': 464, 'path': 'Supervised Learning/Regression Algorithms/Simple Linear Regression/Simple_Linear_Regression_SKlearn.ipynb', 'repo_name': 'Jawwad-Fida/Machine-Learning-Algorithms', 'size': '5100'}