Practical Examples of Machine Learning & Artificial Intelligence with Python – Blogs Overflow operated by Overflow Orbits Dive into the world of regression algorithms with this comprehensive guide featuring practical examples in Python. Explore various regression techniques including Linear Regression, Polynomial Regression, Decision Tree Regression, Random Forest Regression, Support Vector Regression, and Neural Network Regression. Follow along with step-by-step Python code and a CSV dataset to learn how to implement each regression model effectively.
First, let’s assume we have a CSV file named “house_prices.csv” with the following columns: ‘area’, ‘bedrooms’, ‘bathrooms’, ‘garage’, and ‘price’. Each row represents data about a specific house.
Practical Examples of Machine Learning & Artificial Intelligence with Python – Here’s how you can implement regression models for this dataset:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, r2_score
# Load the dataset
data = pd.read_csv("house_prices.csv")
# Split the data into features (X) and target variable (y)
X = data.drop(columns=['price'])
y = data['price']
# Split the 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)
# Feature scaling
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Linear Regression
lr_model = LinearRegression()
lr_model.fit(X_train_scaled, y_train)
lr_pred = lr_model.predict(X_test_scaled)
lr_mse = mean_squared_error(y_test, lr_pred)
lr_r2 = r2_score(y_test, lr_pred)
print("Linear Regression - MSE:", lr_mse, "R-squared:", lr_r2)
# Polynomial Regression
poly_features = PolynomialFeatures(degree=2)
X_train_poly = poly_features.fit_transform(X_train_scaled)
X_test_poly = poly_features.transform(X_test_scaled)
poly_model = LinearRegression()
poly_model.fit(X_train_poly, y_train)
poly_pred = poly_model.predict(X_test_poly)
poly_mse = mean_squared_error(y_test, poly_pred)
poly_r2 = r2_score(y_test, poly_pred)
print("Polynomial Regression - MSE:", poly_mse, "R-squared:", poly_r2)
# Decision Tree Regression
dt_model = DecisionTreeRegressor(random_state=42)
dt_model.fit(X_train_scaled, y_train)
dt_pred = dt_model.predict(X_test_scaled)
dt_mse = mean_squared_error(y_test, dt_pred)
dt_r2 = r2_score(y_test, dt_pred)
print("Decision Tree Regression - MSE:", dt_mse, "R-squared:", dt_r2)
# Random Forest Regression
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train_scaled, y_train)
rf_pred = rf_model.predict(X_test_scaled)
rf_mse = mean_squared_error(y_test, rf_pred)
rf_r2 = r2_score(y_test, rf_pred)
print("Random Forest Regression - MSE:", rf_mse, "R-squared:", rf_r2)
# Support Vector Regression (SVR)
svr_model = SVR(kernel='linear')
svr_model.fit(X_train_scaled, y_train)
svr_pred = svr_model.predict(X_test_scaled)
svr_mse = mean_squared_error(y_test, svr_pred)
svr_r2 = r2_score(y_test, svr_pred)
print("SVR - MSE:", svr_mse, "R-squared:", svr_r2)
# Neural Network Regression
nn_model = MLPRegressor(hidden_layer_sizes=(100, 50), activation='relu', solver='adam', max_iter=1000, random_state=42)
nn_model.fit(X_train_scaled, y_train)
nn_pred = nn_model.predict(X_test_scaled)
nn_mse = mean_squared_error(y_test, nn_pred)
nn_r2 = r2_score(y_test, nn_pred)
print("Neural Network Regression - MSE:", nn_mse, "R-squared:", nn_r2)
In this code:
- We load the dataset and split it into features (X) and target variable (y).
- Then, we split the data into training and testing sets.
- We perform feature scaling to normalize the features.
- We train various regression models including Linear Regression, Polynomial Regression, Decision Tree Regression, Random Forest Regression, Support Vector Regression (SVR), and Neural Network Regression.
- Finally, we evaluate each model using mean squared error (MSE) and R-squared (R2) score.
- Practical Examples of Machine Learning & Artificial Intelligence with Python.
You’ll need to replace “house_prices.csv” with the actual path to your CSV file. This example assumes you’re using a CSV file structured as described earlier. Adjustments may be necessary based on the structure of your dataset.
Here are the top five real-world use cases where linear regression is commonly applied:
- Economic Forecasting: Linear regression is widely used in economics to forecast various economic indicators such as GDP growth, inflation rates, unemployment rates, and stock prices. By analyzing historical data and identifying trends, economists can build linear regression models to make predictions about future economic conditions, which helps policymakers, investors, and businesses in decision-making.
- Marketing Analytics: Linear regression is extensively used in marketing to analyze the effectiveness of marketing campaigns, understand consumer behavior, and predict sales. Marketers can build regression models to determine the impact of advertising spending, pricing strategies, promotional activities, and other factors on sales or customer engagement metrics. This information allows companies to optimize their marketing strategies and allocate resources more efficiently.
- Healthcare Analytics: Linear regression plays a crucial role in healthcare analytics for various applications such as predicting patient outcomes, estimating healthcare costs, and analyzing medical data. Researchers and healthcare providers use regression models to identify risk factors for diseases, assess the effectiveness of treatments, and allocate resources for healthcare services based on population demographics and health trends.
- Financial Risk Management: In finance, linear regression is employed for risk management and portfolio optimization. Analysts use regression models to assess the risk-return profile of financial assets, predict asset prices, and estimate the covariance matrix for portfolio diversification. Linear regression helps investors and financial institutions in making informed decisions about asset allocation, hedging strategies, and risk mitigation.
- Supply Chain Management: Linear regression is applied in supply chain management to forecast demand, optimize inventory levels, and improve logistics operations. By analyzing historical sales data, market trends, and external factors (e.g., weather, seasonality), companies can build regression models to predict future demand for products or services. This enables them to streamline production schedules, manage inventory more effectively, and minimize stockouts or excess inventory costs.
These are just a few examples of how linear regression is used across different industries for solving various real-world problems. Its simplicity, interpretability, and versatility make it a valuable tool for data analysis and decision-making in diverse domains.
Download a sample csv file :