FirePrediction / model2.py
santhoshkumarganth's picture
devops state 1 operation test
8a0aa94 verified
Raw History Blame Contribute Delete
1.66 kB
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)