Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Project (Issue #16) #38

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions predicting-salary-using-sk-learn-linear-regression.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":608978,"sourceType":"datasetVersion","datasetId":297188}],"dockerImageVersionId":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","source":"# **Using Linear Regression Model to Predict salary.**","metadata":{}},{"cell_type":"markdown","source":"## ****Importing Libraries****","metadata":{}},{"cell_type":"code","source":"#Importing Libraries for Dataset and Visualization\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n#Importing Machine Learning Libraries\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import r2_score,mean_squared_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-30T17:45:20.540618Z","iopub.execute_input":"2024-09-30T17:45:20.541077Z","iopub.status.idle":"2024-09-30T17:45:22.090891Z","shell.execute_reply.started":"2024-09-30T17:45:20.541034Z","shell.execute_reply":"2024-09-30T17:45:22.089678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Reading CSV**","metadata":{}},{"cell_type":"code","source":"#Reading CSV\ndf = pd.read_csv(\"/kaggle/input/random-salary-data-of-employes-age-wise/Salary_Data.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.092985Z","iopub.execute_input":"2024-09-30T17:45:22.093501Z","iopub.status.idle":"2024-09-30T17:45:22.170002Z","shell.execute_reply.started":"2024-09-30T17:45:22.093460Z","shell.execute_reply":"2024-09-30T17:45:22.168661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Performing EDA**","metadata":{}},{"cell_type":"code","source":"#Data Description\ndf.describe()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.171391Z","iopub.execute_input":"2024-09-30T17:45:22.171781Z","iopub.status.idle":"2024-09-30T17:45:22.194179Z","shell.execute_reply.started":"2024-09-30T17:45:22.171741Z","shell.execute_reply":"2024-09-30T17:45:22.192901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data Information\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.197171Z","iopub.execute_input":"2024-09-30T17:45:22.197770Z","iopub.status.idle":"2024-09-30T17:45:22.218637Z","shell.execute_reply.started":"2024-09-30T17:45:22.197715Z","shell.execute_reply":"2024-09-30T17:45:22.217370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking Nulls\ndf.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.220223Z","iopub.execute_input":"2024-09-30T17:45:22.221062Z","iopub.status.idle":"2024-09-30T17:45:22.231880Z","shell.execute_reply.started":"2024-09-30T17:45:22.221005Z","shell.execute_reply":"2024-09-30T17:45:22.230710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking Duplicates\ndf.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.233755Z","iopub.execute_input":"2024-09-30T17:45:22.234837Z","iopub.status.idle":"2024-09-30T17:45:22.244112Z","shell.execute_reply.started":"2024-09-30T17:45:22.234780Z","shell.execute_reply":"2024-09-30T17:45:22.243001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting Data points\nplt.figure(figsize =(10, 6))\nplt.scatter(df['YearsExperience'], df['Salary'])\n\n#Adding Labels\nplt.xlabel(\"Years of Experience\")\nplt.ylabel(\"Salary\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.245725Z","iopub.execute_input":"2024-09-30T17:45:22.246245Z","iopub.status.idle":"2024-09-30T17:45:22.547379Z","shell.execute_reply.started":"2024-09-30T17:45:22.246193Z","shell.execute_reply":"2024-09-30T17:45:22.546321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking Outliers\nsns.boxplot(df['YearsExperience'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.548878Z","iopub.execute_input":"2024-09-30T17:45:22.549237Z","iopub.status.idle":"2024-09-30T17:45:22.685731Z","shell.execute_reply.started":"2024-09-30T17:45:22.549199Z","shell.execute_reply":"2024-09-30T17:45:22.684651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Applying Linear Regression** ","metadata":{}},{"cell_type":"code","source":"#Preparing Data\nX = df['YearsExperience'].values.reshape(-1,1)\ny = df['Salary'].values.reshape(-1,1)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.689636Z","iopub.execute_input":"2024-09-30T17:45:22.690062Z","iopub.status.idle":"2024-09-30T17:45:22.696843Z","shell.execute_reply.started":"2024-09-30T17:45:22.690022Z","shell.execute_reply":"2024-09-30T17:45:22.695405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Spliting Data for Train and Test\nX_train, X_test, y_train, y_test = train_test_split(X , y, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.699014Z","iopub.execute_input":"2024-09-30T17:45:22.699478Z","iopub.status.idle":"2024-09-30T17:45:22.708301Z","shell.execute_reply.started":"2024-09-30T17:45:22.699424Z","shell.execute_reply":"2024-09-30T17:45:22.706949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Applying Linear Regression\nlr = LinearRegression()\nlr.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.710816Z","iopub.execute_input":"2024-09-30T17:45:22.711300Z","iopub.status.idle":"2024-09-30T17:45:22.737890Z","shell.execute_reply.started":"2024-09-30T17:45:22.711244Z","shell.execute_reply":"2024-09-30T17:45:22.736790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = lr.predict(X_test)\ny_hat","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.740419Z","iopub.execute_input":"2024-09-30T17:45:22.741195Z","iopub.status.idle":"2024-09-30T17:45:22.750063Z","shell.execute_reply.started":"2024-09-30T17:45:22.741142Z","shell.execute_reply":"2024-09-30T17:45:22.748979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize= (10, 6))\nplt.scatter(X_train, y_train, color = \"b\")\nplt.plot(X_train, lr.predict(X_train), c = \"orange\")\nplt.xlabel(\"Experience\")\nplt.ylabel(\"Salary\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:45:22.858322Z","iopub.execute_input":"2024-09-30T17:45:22.859100Z","iopub.status.idle":"2024-09-30T17:45:23.127137Z","shell.execute_reply.started":"2024-09-30T17:45:22.859049Z","shell.execute_reply":"2024-09-30T17:45:23.126046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_list = [i for i in range(1, len(y_test)+1, 1)] \nplt.plot(index_list, y_test, color='b', linestyle='-', label=\"Real Values\") \nplt.plot(index_list, y_hat, color='r', linestyle='-', label=\"Predicted Values\") \nplt.title(\"Comparison Between Real Test Values and Predicted Values\")\nplt.legend() # Added legend to distinguish lines\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:46:22.938089Z","iopub.execute_input":"2024-09-30T17:46:22.939344Z","iopub.status.idle":"2024-09-30T17:46:23.373881Z","shell.execute_reply.started":"2024-09-30T17:46:22.939292Z","shell.execute_reply":"2024-09-30T17:46:23.372709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Evaluation of Linear Regression**","metadata":{}},{"cell_type":"code","source":"#Calculate Mean Squared Error\nmse = mean_squared_error(y_test, y_hat)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:48:09.134091Z","iopub.execute_input":"2024-09-30T17:48:09.135009Z","iopub.status.idle":"2024-09-30T17:48:09.143267Z","shell.execute_reply.started":"2024-09-30T17:48:09.134963Z","shell.execute_reply":"2024-09-30T17:48:09.142143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying MSE\nprint('Mean Squared Error :',mse)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T17:48:13.435616Z","iopub.execute_input":"2024-09-30T17:48:13.436044Z","iopub.status.idle":"2024-09-30T17:48:13.441773Z","shell.execute_reply.started":"2024-09-30T17:48:13.436003Z","shell.execute_reply":"2024-09-30T17:48:13.440618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}