|
from datasets import load_dataset |
|
import pandas as pd |
|
from sklearn.model_selection import train_test_split, GridSearchCV |
|
from sklearn.ensemble import RandomForestClassifier |
|
from sklearn.preprocessing import StandardScaler |
|
from sklearn.metrics import classification_report, accuracy_score |
|
from category_encoders import OneHotEncoder |
|
|
|
dataset = load_dataset("ombhojane/ckv3") |
|
df = pd.DataFrame(dataset['train']) |
|
|
|
|
|
|
|
encoder = OneHotEncoder(cols=['Biodiversity', 'Existing Infrastructure'], use_cat_names=True) |
|
df_encoded = encoder.fit_transform(df) |
|
|
|
scaler = StandardScaler() |
|
df_encoded[['Land Size (hectares)', 'Budget (INR)']] = scaler.fit_transform(df_encoded[['Land Size (hectares)', 'Budget (INR)']]) |
|
|
|
|
|
X = df_encoded.drop('Service', axis=1) |
|
y = df_encoded['Service'] |
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
model = RandomForestClassifier() |
|
param_grid = { |
|
'n_estimators': [100, 200, 300], |
|
'max_depth': [None, 10, 20, 30], |
|
'min_samples_split': [2, 5, 10] |
|
} |
|
|
|
grid_search = GridSearchCV(model, param_grid, cv=5, scoring='accuracy') |
|
grid_search.fit(X_train, y_train) |
|
|
|
best_model = grid_search.best_estimator_ |
|
|
|
|
|
predictions = best_model.predict(X_test) |
|
print(classification_report(y_test, predictions)) |
|
print("Accuracy:", accuracy_score(y_test, predictions)) |