Loading…
DS · Regression · Program 11 · In the record
Simple Linear Regression using Scikit-learn.
1import pandas as pd2import matplotlib.pyplot as plt3from sklearn.linear_model import LinearRegression4 5df = pd.DataFrame({'Experience': range(1, 11),6 'Salary': [30000, 35000, 41000, 44000, 52000, 55000, 62000, 66000, 71000, 78000]})7X, y = df[['Experience']], df['Salary']8 9model = LinearRegression().fit(X, y)10print("Slope:", model.coef_[0], " Intercept:", model.intercept_)11print("R2 Score:", model.score(X, y))12print("Predicted Salary for 11 years:", model.predict(pd.DataFrame({'Experience': [11]}))[0])13 14plt.scatter(X, y, label="Data")15plt.plot(X, model.predict(X), color='red', label="Regression Line")16plt.title("Simple Linear Regression")17plt.legend()18plt.show()Slope: 5248.48484848485 Intercept: 24533.333333333325 R2 Score: 0.9965768897535254 Predicted Salary for 11 years: 82266.66666666667

Run program 11 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.