Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Multiple Regression in Python

Multiple Regression in Python

Multiple Regression in Python

Multiple Regression is an extension of Simple Linear Regression that uses multiple independent variables to predict a dependent variable.

1. Import Required Libraries

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionfrom sklearn.metrics import r2_score

2. Load Data

Let's assume we have a dataset with multiple independent variables (e.g., Experience, Education, Age) and one dependent variable (Salary).

# Sample datasetdata = {    "Experience": [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],    "Education": [10, 12, 12, 14, 16, 16, 18, 18, 20, 22],    "Age": [22, 25, 28, 31, 34, 37, 40, 43, 46, 49],    "Salary": [30000, 35000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 110000]}# Convert to DataFramedf = pd.DataFrame(data)# Display first few rowsprint(df.head())

3. Define Independent and Dependent Variables

X = df[["Experience", "Education", "Age"]]  # Independent variablesy = df["Salary"]  # Dependent variable

4. Split Data into Training and Testing Sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

5. Train the Multiple Regression Model

model = LinearRegression()  # Create modelmodel.fit(X_train, y_train)  # Train model

6. Make Predictions

y_pred = model.predict(X_test)print("Predicted Salaries:", y_pred)

7. Evaluate the Model

r2 = r2_score(y_test, y_pred)print(f"Rē Score: {r2:.2f}")

The Rē score tells us how well the model fits the data (closer to 1 is better).


8. Model Coefficients & Intercept

print("Intercept:", model.intercept_)print("Coefficients:", model.coef_)

Each coefficient represents the impact of a corresponding feature on the dependent variable.


9. Predict Salary for a New Candidate

new_candidate = np.array([[10, 16, 30]])  # Example: 10 years of experience, 16 years of education, 30 years oldpredicted_salary = model.predict(new_candidate)print(f"Predicted Salary: {predicted_salary[0]:.2f}")

10. Visualizing the Results

Although we can't visualize multi-dimensional data easily, we can plot actual vs predicted salaries.

plt.scatter(y_test, y_pred, color="blue")plt.xlabel("Actual Salary")plt.ylabel("Predicted Salary")plt.title("Actual vs Predicted Salary")plt.show()

Summary

StepDescription
1Load dataset
2Define independent (X) and dependent (y) variables
3Split data into training and testing sets
4Train the Multiple Linear Regression model
5Make predictions
6Evaluate using Rē score
7Extract model coefficients & intercept
8Predict new values
9Visualize results

This is how you can implement Multiple Regression in Python using scikit-learn! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql