Spaces:
Sleeping
Sleeping
File size: 1,664 Bytes
8a0aa94 | 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 | import numpy as np
import pandas as pd
import pickle
import warnings
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
warnings.filterwarnings("ignore")
# Load dataset
data = pd.read_csv("D:/Project/PG Projects/Fire Prediction 2/fire_prediction_dataset_large.csv")
# Convert to NumPy array
data = np.array(data)
# Extract features and target variable
X = data[1:, 1:-1].astype('float')
y = data[1:, -1].astype('int')
# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)
# Define pipeline with scaling and logistic regression
pipeline = Pipeline([
('scaler', StandardScaler()),
('log_reg', LogisticRegression(solver='liblinear'))
])
# Hyperparameter tuning
param_grid = {
'log_reg__C': [0.01, 0.1, 1, 10, 100],
'log_reg__penalty': ['l1', 'l2']
}
grid_search = GridSearchCV(pipeline, param_grid, cv=5, scoring='accuracy', n_jobs=-1)
grid_search.fit(X_train, y_train)
# Best model
best_model = grid_search.best_estimator_
# Evaluate model
y_pred = best_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"Best Model Accuracy: {accuracy:.4f}")
print("Classification Report:\n", report)
# Save the best model
with open('advanced_model.pkl', 'wb') as model_file:
pickle.dump(best_model, model_file)
|