import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score # Sample data data = { 'Feature1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Feature2': [5, 10, 15, 20, 25, 30, 35, 40, 45, 50], 'Target': [2, 4, 5, 7, 10, 13, 14, 16, 18, 20] } df = pd.DataFrame(data) # Split data into features and target X = df[['Feature1', 'Feature2']] y = df['Target'] # Split dataset X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Initialize and fit model linreg = LinearRegression() linreg.fit(X_train, y_train) # Make predictions y_pred = linreg.predict(X_test) # Evaluate model mse = mean_squared_error(y_test, y_pred) mae = mean_absolute_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print(f'Mean Squared Error: {mse:.2f}') print(f'Mean Absolute Error: {mae:.2f}') print(f'R-squared Score: {r2:.2f}')
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter