Downloads · 30 days
0
Briankabiru/FertiliserAdvisor
FertiliserAdvisor is a machine learning model from Briankabiru. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
This model predicts the fertilizer requirements for various crops based on input features such as crop type, target yield, field size, and soil properties. It utilizes a combination of Random Forest Regressor and Rand…
Downloads · 30 days
0
Access
Public
Updated Jul 24, 2024
Repo size
87.1 MB
Likes
1
Public
Click a slice to open those files.
.joblib87.1 MB · 100%
From the Hugging Face model README
This model predicts the fertilizer requirements for various crops based on input features such as crop type, target yield, field size, and soil properties. It utilizes a combination of Random Forest Regressor and Random Forest Classifier to predict both numerical values (e.g., nutrient needs) and categorical values (e.g., fertilizer application instructions).
The model was trained on a custom dataset containing the following features:
The target variables include:
Numerical Targets:
Categorical Targets:
The model was trained using the following steps:
Data Preprocessing:
StandardScalerModeling:
RandomForestRegressor for numerical targets using a MultiOutputRegressorRandomForestClassifier for categorical targets using a MultiOutputClassifierEvaluation:
The model was evaluated using the following metrics:
The model expects input data in JSON format with the following fields:
from huggingface_hub import hf_hub_download
import pandas as pd
from joblib import load
import numpy as np
from sklearn.preprocessing import LabelEncoder
from googletrans import Translator
# Initialize translator
translator = Translator()
# Download models from Hugging Face Hub
preprocessor_path = hf_hub_download(repo_id='your-username/your-repo', filename='preprocessor.joblib')
numerical_model_path = hf_hub_download(repo_id='your-username/your-repo', filename='numerical_model.joblib')
categorical_model_path = hf_hub_download(repo_id='your-username/your-repo', filename='categorical_model.joblib')
# Load the preprocessor and trained models
preprocessor = load(preprocessor_path)
numerical_model = load(numerical_model_path)
categorical_model = load(categorical_model_path)
# Define categorical targets (same as used during training)
categorical_targets = [
'Lime Application - Instruction',
'Lime Application',
'Organic Matter Application - Instruction',
'Organic Matter Application',
'1st Application',
'1st Application - Type fertilizer (1)',
'1st Application - Type fertilizer (2)',
'2nd Application',
'2nd Application - Type fertilizer (1)'
]
# Example input data
new_data = {
'Crop Name': 'maize(corn)',
'Target Yield': 3600.0,
'Field Size': 1.0,
'pH (water)': 6.1,
'Organic Carbon': 11.4,
'Total Nitrogen': 1.1,
'Phosphorus (M3)': 1.8,
'Potassium (exch.)': 3.0,
'Soil moisture': 20.0
}
# Preprocess the input data
input_df = pd.DataFrame([new_data])
input_transformed = preprocessor.transform(input_df)
# Make numerical predictions
numerical_predictions = numerical_model.predict(input_transformed)
# Make categorical predictions
categorical_predictions = categorical_model.predict(input_transformed)
# Load label encoders from Hugging Face Hub (if they are saved separately)
label_encoders = {col: load(hf_hub_download(repo_id='your-username/your-repo', filename=f'label_encoder_{col}.joblib')) for col in categorical_targets}
# Decode categorical predictions
categorical_predictions_decoded = {}
for i, col in enumerate(categorical_targets):
le = label_encoders[col]
try:
decoded_labels = le.inverse_transform(categorical_predictions[:, i])
# Translate to English
translated_labels = [translator.translate(label, dest='en').text for label in decoded_labels]
categorical_predictions_decoded[col] = translated_labels
except ValueError as e:
print(f"Error decoding predictions for {col}: {e}")
categorical_predictions_decoded[col] = ["Unknown"] * len(categorical_predictions[:, i])
# Define numerical targets (same as used during training)
numerical_targets = [
'Nitrogen (N) Need',
'Phosphorus (P2O5) Need',
'Potassium (K2O) Need',
'Organic Matter Need',
'Lime Need',
'Lime Application - Requirement',
'Organic Matter Application - Requirement',
'1st Application - Requirement (1)',
'1st Application - Requirement (2)',
'2nd Application - Requirement (1)'
]
# Combine predictions into a single dictionary
predictions_combined = {**{col: numerical_predictions[0, i] for i, col in enumerate(numerical_targets)}, **categorical_predictions_decoded}
print("Predicted Fertilizer Requirements:")
for col, pred_value in predictions_combined.items():
print(f"{col}: {pred_value}")