# Import necessary libraries import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score # Sample Dataset (replace this with your actual dataset) # Let's assume we have a dataset with features: 'height', 'weight', and 'experience' data = { 'height': [150, 160, 170, 180, 190], 'weight': [50, 60, 70, 80, 90], 'experience': [2, 3, 4, 5, 6], 'age': [25, 28, 30, 35, 40] # This is the target variable } # Create a DataFrame df = pd.DataFrame(data) # Step 2: Data Preprocessing # Define features (X) and target (y) X = df[['height', 'weight', 'experience']] # Independent variables y = df['age'] # Dependent variable (age) # Step 3: Train-Test Split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Step 4: Train the Regression Model (Linear Regression) model = LinearRegression() model.fit(X_train, y_train) # Step 5: Make Predictions y_pred = model.predict(X_test) # Step 6: Model Evaluation # Calculate Mean Squared Error (MSE) mse = mean_squared_error(y_test, y_pred) print(f"Mean Squared Error: {mse}") # Calculate R-squared (R²) value r2 = r2_score(y_test, y_pred) print(f"R-squared value: {r2}")
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