ChaloKisaanAI / app.py
ombhojane's picture
Create app.py
d173086 verified
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'])
# Preprocessing
# One-hot encoding for categorical features
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)']])
# Splitting features and target
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_
# Model Evaluation
predictions = best_model.predict(X_test)
print(classification_report(y_test, predictions))
print("Accuracy:", accuracy_score(y_test, predictions))