Views
No views yet
StandardScalerRandomForestRegressor for numerical targets using a MultiOutputRegressorRandomForestClassifier for categorical targets using a MultiOutputClassifier1from huggingface_hub import hf_hub_download
2import pandas as pd
3from joblib import load
4import numpy as np
5from sklearn.preprocessing import LabelEncoder
6from googletrans import Translator
7
8# Initialize translator
9translator = Translator()
10
11# Download models from Hugging Face Hub
12preprocessor_path = hf_hub_download(repo_id='your-username/your-repo', filename='preprocessor.joblib')
13numerical_model_path = hf_hub_download(repo_id='your-username/your-repo', filename='numerical_model.joblib')
14categorical_model_path = hf_hub_download(repo_id='your-username/your-repo', filename='categorical_model.joblib')
15
16# Load the preprocessor and trained models
17preprocessor = load(preprocessor_path)
18numerical_model = load(numerical_model_path)
19categorical_model = load(categorical_model_path)
20
21# Define categorical targets (same as used during training)
22categorical_targets = [
23 'Lime Application - Instruction',
24 'Lime Application',
25 'Organic Matter Application - Instruction',
26 'Organic Matter Application',
27 '1st Application',
28 '1st Application - Type fertilizer (1)',
29 '1st Application - Type fertilizer (2)',
30 '2nd Application',
31 '2nd Application - Type fertilizer (1)'
32]
33
34# Example input data
35new_data = {
36 'Crop Name': 'maize(corn)',
37 'Target Yield': 3600.0,
38 'Field Size': 1.0,
39 'pH (water)': 6.1,
40 'Organic Carbon': 11.4,
41 'Total Nitrogen': 1.1,
42 'Phosphorus (M3)': 1.8,
43 'Potassium (exch.)': 3.0,
44 'Soil moisture': 20.0
45}
46
47# Preprocess the input data
48input_df = pd.DataFrame([new_data])
49input_transformed = preprocessor.transform(input_df)
50
51# Make numerical predictions
52numerical_predictions = numerical_model.predict(input_transformed)
53
54# Make categorical predictions
55categorical_predictions = categorical_model.predict(input_transformed)
56
57# Load label encoders from Hugging Face Hub (if they are saved separately)
58label_encoders = {col: load(hf_hub_download(repo_id='your-username/your-repo', filename=f'label_encoder_{col}.joblib')) for col in categorical_targets}
59
60# Decode categorical predictions
61categorical_predictions_decoded = {}
62for i, col in enumerate(categorical_targets):
63 le = label_encoders[col]
64 try:
65 decoded_labels = le.inverse_transform(categorical_predictions[:, i])
66 # Translate to English
67 translated_labels = [translator.translate(label, dest='en').text for label in decoded_labels]
68 categorical_predictions_decoded[col] = translated_labels
69 except ValueError as e:
70 print(f"Error decoding predictions for {col}: {e}")
71 categorical_predictions_decoded[col] = ["Unknown"] * len(categorical_predictions[:, i])
72
73# Define numerical targets (same as used during training)
74numerical_targets = [
75 'Nitrogen (N) Need',
76 'Phosphorus (P2O5) Need',
77 'Potassium (K2O) Need',
78 'Organic Matter Need',
79 'Lime Need',
80 'Lime Application - Requirement',
81 'Organic Matter Application - Requirement',
82 '1st Application - Requirement (1)',
83 '1st Application - Requirement (2)',
84 '2nd Application - Requirement (1)'
85]
86
87# Combine predictions into a single dictionary
88predictions_combined = {**{col: numerical_predictions[0, i] for i, col in enumerate(numerical_targets)}, **categorical_predictions_decoded}
89
90print("Predicted Fertilizer Requirements:")
91for col, pred_value in predictions_combined.items():
92 print(f"{col}: {pred_value}")