-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_linear_regr.py
More file actions
68 lines (39 loc) · 1.53 KB
/
multi_linear_regr.py
File metadata and controls
68 lines (39 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Import necessary libraries
import numpy as np
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, r2_score
# Sample dataset for demonstration
# Create a DataFrame with multiple independent variables and a dependent variable
data = {
'Feature1': [1, 2, 3, 4, 5],
'Feature2': [2, 4, 6, 8, 10],
'Feature3': [1, 3, 5, 7, 9],
'Target': [2.3, 4.1, 6.0, 7.8, 9.6]
}
# Convert the dataset to a pandas DataFrame
df = pd.DataFrame(data)
# Separate independent variables (features) and the dependent variable (target)
X = df[['Feature1', 'Feature2', 'Feature3']] # Features
y = df['Target'] # Target variable
# Split the dataset 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)
# Create a Multiple Linear Regression model
model = LinearRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Make predictions on the test data
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
# Print the results
print("Model Coefficients:", model.coef_)
print("Model Intercept:", model.intercept_)
print("Mean Squared Error (MSE):", mse)
print("R-squared Score:", r2)
# Example prediction
sample_data = pd.DataFrame([[3, 6, 5]], columns=['Feature1', 'Feature2', 'Feature3'])
predicted_target = model.predict(sample_data)
print("Predicted Target Value:", predicted_target[0])