Sowing Success: How Machine Learning Helps Farmers Select the Best Crops
Measuring essential soil metrics such as nitrogen, phosphorous, potassium levels, and pH value is an important aspect of assessing soil condition. However, it can be an expensive and time-consuming process, which can cause farmers to prioritize which metrics to measure based on their budget constraints.
Farmers have various options when it comes to deciding which crop to plant each season. Their primary objective is to maximize the yield of their crops, taking into account different factors. One crucial factor that affects crop growth is the condition of the soil in the field, which can be assessed by measuring basic elements such as nitrogen and potassium levels. Each crop has an ideal soil condition that ensures optimal growth and maximum yield.
A farmer reached out to you as a machine learning expert for assistance in selecting the best crop for his field. They've provided you with a dataset called soil_measures.csv, which contains:
"N": Nitrogen content ratio in the soil"P": Phosphorous content ratio in the soil"K": Potassium content ratio in the soil"pH"value of the soil"crop": categorical values that contain various crops (target variable).
Each row in this dataset represents various measures of the soil in a particular field. Based on these measurements, the crop specified in the "crop" column is the optimal choice for that field.
In this project, you will build multi-class classification models to predict the type of "crop" and identify the single most importance feature for predictive performance.
# All required libraries are imported here for you.
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn import metrics- Load the dataset and print the first few columns to get an overview of whats in the dataset.
# Load the dataset
crops = pd.read_csv("soil_measures.csv")
crops.head()- get a look at the dataset to know if their is any missing column
crops.isna().sum()- get a look at the column we are predicting to confirm it is categorical and observe its unique values
print(crops["crop"].unique())- split the data
X = crops.drop("crop", axis=1)
y = crops["crop"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)Evaluation of Individual Feature Predictive Power
This section assesses the predictive capability of each individual feature (N, P, K, pH) in determining the type of crop. A logistic regression model is employed to make predictions based on each feature independently. The performance metrics used for evaluation are balanced accuracy and F1 score. The results are compiled into a dictionary and presented for each feature.
feature_performance = {}
for feature in X.columns:
log_reg = LogisticRegression(multi_class="multinomial")
log_reg.fit(X_train[[feature]], y_train)
y_pred = log_reg.predict(X_test[[feature]])
b_accuracy = metrics.balanced_accuracy_score(y_test, y_pred)
f1_score = metrics.f1_score(y_test, y_pred, average="weighted")
feature_performance[feature] = f1_score
print(f"Feature: {feature}, Balanced_accuracy: {b_accuracy}, f1_score: {f1_score}")max_key = max(feature_performance, key=feature_performance.get)
best_predictive_feature = {max_key:feature_performance[max_key]}
best_predictive_featureEvaluation of Soil Features for Crop Prediction
We tested how well each soil feature—Nitrogen (N), Phosphorous (P), Potassium (K), and pH—can predict crop type using a simple model.
Key Findings:
-
Nitrogen (N)
- Accuracy: 16.77%
- F1 Score: 0.0756
-
Phosphorous (P)
- Accuracy: 18.72%
- F1 Score: 0.0992
-
Potassium (K)
- Accuracy: 29.15%
- F1 Score: 0.2463
-
pH
- Accuracy: 9.87%
- F1 Score: 0.0239
Conclusion
Potassium (K) is the best single predictor, but overall, no feature alone is very accurate.