Skip to content

🎬 Crop Recommendation Project

This notebook orchestrates data preparation, analysis, modeling, and predicting using clean modular functions.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression

from data_preprocessing import get_prepared_data
from eda import run_all_plots, run_all_descriptive_eda, run_full_eda_pipeline
from hypothesis_tests import run_all_stat_tests
from modeling.classification_models import run_classification_models, prepare_and_train_classifier, predict_crop
from modeling.clustering import run_kmeans_clustering

from config import FILENAME, SEED
sns.set_theme(style="whitegrid")
plt.figure(figsize=(10, 6))
df = get_prepared_data(FILENAME)
display(df.head())
run_all_plots(df)
run_all_stat_tests(df)
run_classification_models(df)
run_kmeans_clustering(df)
rf = RandomForestClassifier(random_state=SEED)
model, encoder, scaler, feature_names = prepare_and_train_classifier(df, rf)
new_sample = pd.DataFrame([{
    'N': 57, 'P': 6, 'K': 65, 'temperature': 36,
    'humidity': 14, 'ph':14, 'rainfall': 20
}])

prediction = predict_crop(model, encoder, scaler, feature_names, new_sample)

print("Recommended crop:", prediction)