0% found this document useful (0 votes)
10 views2 pages

Salary Prediction with Linear Regression

The document outlines a Python script that analyzes a salary dataset based on years of experience using linear regression. It includes data preprocessing, visualization with seaborn, and model evaluation metrics such as Mean Absolute Error (MAE) and Mean Squared Error (MSE). The results indicate a coefficient of determination of approximately 0.28, suggesting a weak correlation between years of experience and salary.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Salary Prediction with Linear Regression

The document outlines a Python script that analyzes a salary dataset based on years of experience using linear regression. It includes data preprocessing, visualization with seaborn, and model evaluation metrics such as Mean Absolute Error (MAE) and Mean Squared Error (MSE). The results indicate a coefficient of determination of approximately 0.28, suggesting a weak correlation between years of experience and salary.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import numpy as np

import pandas as pd
import seaborn as sns
import [Link] as plt
from sklearn import preprocessing, svm
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_absolute_error,mean_squared_error
df = pd.read_csv('[Link]')
[Link]()
df_binary = df[['Years of Experience', 'Salary']]
[Link](x ="Years of Experience", y ="Salary", data = df_binary, order = 2,
ci = None)
[Link]()
df_binary.fillna(method ='ffill', inplace = True)
X = [Link](df_binary['Years of Experience']).reshape(-1, 1)
y = [Link](df_binary['Salary']).reshape(-1, 1)

# Separating the data into independent and dependent variables


# Converting each dataframe into a numpy array
# since each dataframe contains only one column
df_binary.dropna(inplace = True)

# Dropping any rows with Nan values


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)

# Splitting the data into training and testing data


regr = LinearRegression()
[Link](X_train, y_train)
print('Coefficient of determination: %.2f' % [Link](X_test, y_test))
y_pred = [Link](X_test)
[Link](X_test, y_test, color ='b')
[Link](X_test, y_pred, color ='k')

[Link]()
mae = mean_absolute_error(y_true=y_test,y_pred=y_pred)
#squared True returns MSE value, False returns RMSE value.
mse = mean_squared_error(y_true=y_test,y_pred=y_pred) #default=True

print("MAE:",mae)
print("MSE:",mse)

Output:

Coefficient of determination:0.27853954081632637

MAE: 1484.71615720524
MSE: 2808565.8168227146

Dataset:

Years of
Experienc
e Salary
5 5000
3 6000
15 4000
7 3000
20 2000

Common questions

Powered by AI

MAE provides an average prediction error magnitude, easily interpretable in the original data units, highlighting error context. MSE, emphasizing larger errors, aids in penalizing models more for larger discrepancies, but an increase in MSE could mask specific model deficiencies due to squared error emphasis. Thus, the choice affects error sensitivity and model refinement priorities .

An R² value of 0.28 indicates that only 28% of the variability in the salary can be explained by the years of experience. This suggests that the linear model may be underfitting the data and that other factors may significantly influence salary, which are not considered in the model .

MAE and MSE measure the average magnitude of errors in predictions without considering their direction and square the differences before taking the average, respectively. The given MAE of 1484.72 and MSE of 2808565.82 indicate relatively high prediction errors, suggesting that the model's predictions deviate significantly from the actual values .

Increasing the 'order' parameter changes the model from linear to polynomial, potentially fitting data with more complexity and capturing non-linear relationships. This can improve model fit if the true relationship is non-linear but introduces risk of overfitting if not justified by the data .

A predictable pattern indicates a potential relationship between the independent and dependent variable, confirming model assumptions. However, as the scatter plot reflects assumptions only based on given variables, any missing influential variables can lead to model underperformance in representing real-world settings .

Data standardization is not critical when the regression model relies on only one feature, as relative range discrepancies don't exist between features. It becomes essential in multi-feature models to normalize varying scales, ensuring model estimates aren't biased to scale-dominant features .

Forward filling replaces missing values with the last observed data point, ensuring the continuity of the dataset. This can introduce bias if the past data point is not representative. Dropping NaN values removes incomplete data rows, potentially reducing the dataset size, affecting model training due to less data being available .

Reshaping data arrays aligns data into a required format for processing by ML algorithms. In linear regression, each sample should be in a single column (feature) format. Reshaping from 1D to 2D ensures compliance with input requirements, facilitating correct model training and predictions .

Splitting data into training and testing sets helps to evaluate how well the model generalizes to unseen data. A 75%-25% split is common as it provides sufficient data for training while maintaining a meaningful portion for testing. This balance allows for robust model evaluation and helps prevent overfitting .

The analysis shows a low R² value, which implies a weak linear relationship between years of experience and salary. This suggests that other factors aside from experience significantly impact salary, indicating that a linear model may be inadequate for capturing the complexity of the relationship .

You might also like