Views
No views yet
StandardScalerRandomForestRegressor for numerical targets using a MultiOutputRegressorRandomForestClassifier for categorical targets using a MultiOutputClassifierLoading the models and preprocessor.
Defining the categorical and numerical targets.
Loading the label encoders.
Creating a function make_predictions that processes the input data, makes predictions, and decodes the categorical predictions.1import pandas as pd
2from joblib import load
3from huggingface_hub import hf_hub_download
4from sklearn.preprocessing import LabelEncoder
5
6# Load models and preprocessor
7preprocessor_path = hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename='preprocessor.joblib')
8numerical_model_path = hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename='numerical_model.joblib')
9categorical_model_path = hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename='categorical_model.joblib')
10
11preprocessor = load(preprocessor_path)
12numerical_model = load(numerical_model_path)
13categorical_model = load(categorical_model_path)
14
15# Define categorical targets
16categorical_targets = [
17 'Lime Application - Instruction',
18 'Lime Application',
19 'Organic Matter Application - Instruction',
20 'Organic Matter Application',
21 '1st Application',
22 '1st Application - Type fertilizer (1)',
23 '1st Application - Type fertilizer (2)',
24 '2nd Application',
25 '2nd Application - Type fertilizer (1)',
26 '1st Application_1',
27 '1st Application - Type fertilizer (1)_3',
28 '1st Application - Type fertilizer (2)_5',
29 '2nd Application_6',
30 '1st Application_21',
31 '1st Application - Type fertilizer (1)_23',
32 '1st Application - Type fertilizer (2)_25',
33 '2nd Application_26',
34 '2nd Application - Type fertilizer (1)_28'
35]
36
37# Define numerical targets
38numerical_targets = [
39 'Nitrogen (N) Need',
40 'Phosphorus (P2O5) Need',
41 'Potassium (K2O) Need',
42 'Organic Matter Need',
43 'Lime Need',
44 'Lime Application - Requirement',
45 'Organic Matter Application - Requirement',
46 '1st Application - Requirement (1)',
47 '1st Application - Requirement (2)',
48 '2nd Application - Requirement (1)'
49]
50
51# Load label encoders
52label_encoders = {col: load(hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename=f'label_encoder_{col}.joblib')) for col in categorical_targets}
53
54def make_predictions(input_data):
55 # Convert input data to DataFrame
56 input_df = pd.DataFrame([input_data])
57
58 # Preprocess the input data
59 X_transformed = preprocessor.transform(input_df)
60
61 # Predict with numerical model
62 numerical_predictions = numerical_model.predict(X_transformed)
63
64 # Predict with categorical model
65 categorical_predictions_encoded = categorical_model.predict(X_transformed)
66
67 # Decode categorical predictions
68 categorical_predictions_decoded = {}
69 for i, col in enumerate(categorical_targets):
70 le = label_encoders[col]
71 try:
72 categorical_predictions_decoded[col] = le.inverse_transform(categorical_predictions_encoded[:, i])
73 except ValueError as e:
74 categorical_predictions_decoded[col] = ["Unknown"] * len(categorical_predictions_encoded[:, i])
75
76 # Combine numerical and categorical predictions into a dictionary
77 predictions_combined = {col: numerical_predictions[0, i] for i, col in enumerate(numerical_targets)}
78 predictions_combined.update({col: categorical_predictions_decoded[col][0] for col in categorical_targets})
79
80 return predictions_combined
81
82# Example usage
83input_data = {
84 'Crop Name': 'maize(corn)',
85 'Target Yield': 3600.0,
86 'Field Size': 1.0,
87 'pH (water)': 6.1,
88 'Organic Carbon': 11.4,
89 'Total Nitrogen': 1.1,
90 'Phosphorus (M3)': 1.8,
91 'Potassium (exch.)': 3.0,
92 'Soil moisture': 20.0
93}
94
95predictions = make_predictions(input_data)
96
97print("Predicted Fertilizer Requirements:")
98for col, pred_value in predictions.items():
99 print(f"{col}: {pred_value}")
100