from sklearn.datasets import load_diabetes from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_absolute_error, r2_score from sklearn.model_selection import train_test_split X, y = load_diabetes(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, ) model = LinearRegression() model.fit(X_train, y_train) predictions = model.predict(X_test) print(f"train rows: {X_train.shape[0]}") print(f"test rows: {X_test.shape[0]}") print(f"intercept: {model.intercept_:.2f}") print(f"coefficients: {model.coef_.round(2).tolist()}") print(f"first predictions: {predictions[:3].round(1).tolist()}") print(f"mean absolute error: {mean_absolute_error(y_test, predictions):.2f}") print(f"r2 score: {r2_score(y_test, predictions):.3f}")