forked from Fall2024CS584/Project2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
42 lines (31 loc) · 1.37 KB
/
testing.py
File metadata and controls
42 lines (31 loc) · 1.37 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
# Import necessary libraries
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
# GradientBoosting class is in a file named gradient_boosting.py
from gradient_boosting import GradientBoosting
def main():
# Load the California housing dataset
data = fetch_california_housing()
X, y = data.data, data.target
# 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)
# Initialize the StandardScaler
scaler = StandardScaler()
# Fit the scaler on the training data and transform it
X_train_scaled = scaler.fit_transform(X_train)
# Transform the testing data with the same scaler
X_test_scaled = scaler.transform(X_test)
# Initialize the GradientBoosting model
model = GradientBoosting(n_estimators=50, learning_rate=0.1, max_depth=3)
# Train the model on the scaled training data
model.fit(X_train_scaled, y_train)
# Predict the scaled test set
predictions = model.predict(X_test_scaled)
# Evaluate the model using mean squared error
mse = mean_squared_error(y_test, predictions)
print("Mean Squared Error on Test Set:", mse)
if __name__ == "__main__":
main()