Loading…
DS · Regression · Program 12 · In the record
Multiple Linear Regression with model evaluation.
1import pandas as pd2from sklearn.model_selection import train_test_split3from sklearn.linear_model import LinearRegression4from sklearn.metrics import mean_squared_error, r2_score5 6df = pd.DataFrame({7 'Experience': range(1, 16),8 'Education': [12, 14, 16, 16, 14, 18, 16, 18, 20, 16, 18, 20, 18, 20, 22],9 'Projects': [2, 3, 5, 4, 6, 7, 6, 8, 9, 8, 10, 11, 10, 12, 13],10 'Salary': [39000, 44500, 49500, 50500, 53000, 59500, 61000, 68500,11 71000, 69000, 76500, 81500, 81000, 85000, 91500]})12X, y = df.drop(columns='Salary'), df['Salary']13X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)14 15model = LinearRegression().fit(X_train, y_train)16y_pred = model.predict(X_test)17print("Coefficients:", model.coef_)18print("Intercept:", model.intercept_)19print("MSE:", mean_squared_error(y_test, y_pred))20print("R2 Score:", r2_score(y_test, y_pred))Coefficients: [2866.65373507 1107.60162076 96.64209664] Intercept: 22505.043320832818 MSE: 2101589.657324817 R2 Score: 0.9804974976120563

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