-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9_linearRegression.py
More file actions
135 lines (112 loc) · 4.43 KB
/
9_linearRegression.py
File metadata and controls
135 lines (112 loc) · 4.43 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#%%
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
# y = mnxn + .... m1x1 + m0x0 + b0
#multivariable linear regression
# make a random dataset
X = np.random.rand(100, 2) # 100 samples, 3 features
coef = np.array([3,5]) # Coefficients for the features
y = np.dot(X, coef) + np.random.normal(0, 0.1, 100) # Linear relation with some noise
"""
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], y, color='b', label='Data Points')
ax.set_xlabel('Feature 1')
ax.set_ylabel('Feature 2')
ax.set_zlabel('Target Variable')
ax.set_title('3D Scatter Plot of Data Points')
ax.legend()
plt.show()"""
LinearRegressionModel = LinearRegression()
# 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)
# Fit the model to the training data
LinearRegressionModel.fit(X_train, y_train)
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], y, color='b', label='Data Points')
ax.set_xlabel('Feature 1')
ax.set_ylabel('Feature 2')
ax.set_zlabel('Target Variable')
np.meshgrid = np.meshgrid(np.linspace(X[:, 0].min(), X[:, 0].max(), 10),
np.linspace(X[:, 1].min(), X[:, 1].max(), 10))
X_grid = np.c_[np.ravel(np.meshgrid[0]), np.ravel(np.meshgrid[1])]
y_grid = LinearRegressionModel.predict(X_grid)
ax.plot_trisurf(X_grid[:, 0], X_grid[:, 1], y_grid, color='r', alpha=0.5, label='Regression Plane')
ax.set_title('3D Scatter Plot with Regression Plane')
ax.legend()
plt.show()
# Make predictions on the test set
y_pred = LinearRegressionModel.predict(X_test)
# Calculate metrics
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")
print(f"R-squared: {r2:.4f}")
# %%
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# Load the diabetes dataset
diabetes = load_diabetes()
# Split the dataset into features and target variable
X = diabetes.data
y = diabetes.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a Linear Regression model
linear_regression_model = LinearRegression()
# Fit the model to the training data
linear_regression_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = linear_regression_model.predict(X_test)
# Calculate metrics
rmse = mean_squared_error(y_test, y_pred) ** 0.5
r2 = r2_score(y_test, y_pred)
print(f"Root Mean Squared Error: {rmse:.4f}")
print(f"R-squared: {r2:.4f}")
# %%
# polynomial regression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
X = 4 * np.random.rand(100, 1) # 100 samples, 1 features
y = 2 + 3 * X + 4 * X**2 + np.random.randn(100, 1) # Quadratic relation with noise
plt.scatter(X, y, color='blue', label='Data Points')
plt.xlabel('Feature')
plt.ylabel('Target Variable')
plt.title('Scatter Plot of Data Points')
plt.legend()
plt.show()
poly_regression_model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression())
# 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)
# Fit the model to the training data
poly_regression_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = poly_regression_model.predict(X_test)
# Calculate metrics
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")
print(f"R-squared: {r2:.4f}")
# Plotting the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data Points')
X_grid = np.linspace(X.min(), X.max(), 100).reshape(-1, 1)
y_grid = poly_regression_model.predict(X_grid)
plt.plot(X_grid, y_grid, color='red', label='Polynomial Regression Curve')
plt.xlabel('Feature')
plt.ylabel('Target Variable')
plt.title('Polynomial Regression Curve')
plt.legend()
plt.show()
# %%