diff --git a/Linear Regression Model/Salary_dataset.csv b/Linear Regression Model/Salary_dataset.csv new file mode 100644 index 000000000..ddeba9ec1 --- /dev/null +++ b/Linear Regression Model/Salary_dataset.csv @@ -0,0 +1,31 @@ +,YearsExperience,Salary +0,1.2000000000000002,39344.0 +1,1.4000000000000001,46206.0 +2,1.6,37732.0 +3,2.1,43526.0 +4,2.3000000000000003,39892.0 +5,3.0,56643.0 +6,3.1,60151.0 +7,3.3000000000000003,54446.0 +8,3.3000000000000003,64446.0 +9,3.8000000000000003,57190.0 +10,4.0,63219.0 +11,4.1,55795.0 +12,4.1,56958.0 +13,4.199999999999999,57082.0 +14,4.6,61112.0 +15,5.0,67939.0 +16,5.199999999999999,66030.0 +17,5.3999999999999995,83089.0 +18,6.0,81364.0 +19,6.1,93941.0 +20,6.8999999999999995,91739.0 +21,7.199999999999999,98274.0 +22,8.0,101303.0 +23,8.299999999999999,113813.0 +24,8.799999999999999,109432.0 +25,9.1,105583.0 +26,9.6,116970.0 +27,9.7,112636.0 +28,10.4,122392.0 +29,10.6,121873.0 diff --git a/Linear Regression Model/linear_regression.py b/Linear Regression Model/linear_regression.py new file mode 100644 index 000000000..ad4e46f16 --- /dev/null +++ b/Linear Regression Model/linear_regression.py @@ -0,0 +1,41 @@ +import pandas as pd +import matplotlib.pyplot as plt +from sklearn.linear_model import LinearRegression + +df=pd.read_csv("/kaggle/input/salary-dataset-simple-linear-regression/Salary_dataset.csv") + +plt.scatter(df['YearsExperience'],df['Salary']) +plt.xlabel("X") +plt.ylabel("Y") +plt.title("Scatter Plot") +plt.show() + +X=df[['YearsExperience']] +y=df['Salary'] + +model=LinearRegression() +model.fit(X,y) #calculates best fitting relation between x and y +slope = model.coef_[0] # represents coeff of x in y = mx + b +intercept = model.intercept_ # point where regression lines crosses y axis + +def linear_regression(x): + return slope * x + intercept + +plt.scatter(df['YearsExperience'],df['Salary'], label='Data Points') +plt.plot(df['YearsExperience'], [linear_regression(xi) for xi in df['YearsExperience']], color='red') +plt.xlabel("X") +plt.ylabel("Y") +plt.legend() +plt.title("Linear Regression") +plt.show() + +new_x = 6 +prediction = linear_regression(new_x) +print(f"Prediction for x = {new_x}: {prediction}") + +new_x = pd.DataFrame({"YearsExperience": [6]}) +print(model.predict(new_x)) + + +new_x = pd.DataFrame({'YearsExperience': [0]}) +print(model.predict(new_x))