Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- EDA and modelling.ipynb +0 -0
- app.py +106 -0
- clean_water_requirement_data.csv +0 -0
- main.py +53 -0
- requirements.txt +9 -0
EDA and modelling.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model_path = 'random_forest_regressor_model.pkl'
|
| 8 |
+
model = joblib.load(model_path)
|
| 9 |
+
|
| 10 |
+
# Encoding mappings
|
| 11 |
+
rainfall_mapping = {'moderate': 2, 'low': 1, 'high': 0}
|
| 12 |
+
soil_type_mapping = {'sandy': 3, 'silty': 4, 'clay': 0, 'peaty': 2, 'loamy': 1}
|
| 13 |
+
drainage_mapping = {'poor': 2, 'moderate': 1, 'good': 0}
|
| 14 |
+
crop_mapping = {'rice': 2, 'wheat': 4, 'soybean': 3, 'cotton': 0, 'maize': 1}
|
| 15 |
+
growth_stage_mapping = {'flowering': 0, 'fruiting': 1, 'vegetative': 5, 'seedling': 4, 'reproductive': 3, 'maturity': 2}
|
| 16 |
+
|
| 17 |
+
# Function to preprocess user inputs
|
| 18 |
+
def preprocess_input(data):
|
| 19 |
+
data['rainfall_pattern'] = rainfall_mapping[data['rainfall_pattern']]
|
| 20 |
+
data['soil_type'] = soil_type_mapping[data['soil_type']]
|
| 21 |
+
data['drainage_properties'] = drainage_mapping[data['drainage_properties']]
|
| 22 |
+
data['crop_type'] = crop_mapping[data['crop_type']]
|
| 23 |
+
data['growth_stage'] = growth_stage_mapping[data['growth_stage']]
|
| 24 |
+
return np.array(list(data.values())).reshape(1, -1)
|
| 25 |
+
|
| 26 |
+
# Streamlit App
|
| 27 |
+
st.title("Optimal Water Requirement Prediction for Agricultural Farms")
|
| 28 |
+
st.write("This application predicts the optimal water requirement for different crops based on environmental and soil properties.")
|
| 29 |
+
|
| 30 |
+
# User Inputs
|
| 31 |
+
st.sidebar.header("Input Features")
|
| 32 |
+
|
| 33 |
+
# Numerical inputs
|
| 34 |
+
temperature = st.sidebar.slider("Temperature (°C):", min_value=-10.0, max_value=50.0, value=25.0, step=0.1)
|
| 35 |
+
humidity = st.sidebar.slider("Humidity (%):", min_value=0.0, max_value=100.0, value=50.0, step=0.1)
|
| 36 |
+
wind_speed = st.sidebar.slider("Wind Speed (m/s):", min_value=0.0, max_value=20.0, value=5.0, step=0.1)
|
| 37 |
+
evapotranspiration = st.sidebar.slider("Evapotranspiration (mm/day):", min_value=0.0, max_value=10.0, value=3.0, step=0.1)
|
| 38 |
+
soil_moisture_levels = st.sidebar.slider("Soil Moisture Levels (%):", min_value=0.0, max_value=100.0, value=50.0, step=0.1)
|
| 39 |
+
water_retention_capacity = st.sidebar.slider("Water Retention Capacity (%):", min_value=0.0, max_value=100.0, value=60.0, step=0.1)
|
| 40 |
+
crop_water_requirement = st.sidebar.slider("Actual Water Requirement (mm/day)):", min_value=0.0, max_value=25.0, value=10.0, step=0.1)
|
| 41 |
+
|
| 42 |
+
# Categorical inputs
|
| 43 |
+
rainfall_pattern = st.sidebar.selectbox("Rainfall Pattern:", list(rainfall_mapping.keys()))
|
| 44 |
+
soil_type = st.sidebar.selectbox("Soil Type:", list(soil_type_mapping.keys()))
|
| 45 |
+
drainage_properties = st.sidebar.selectbox("Drainage Properties:", list(drainage_mapping.keys()))
|
| 46 |
+
crop_type = st.sidebar.selectbox("Crop Type:", list(crop_mapping.keys()))
|
| 47 |
+
growth_stage = st.sidebar.selectbox("Growth Stage:", list(growth_stage_mapping.keys()))
|
| 48 |
+
|
| 49 |
+
# Prediction
|
| 50 |
+
if st.sidebar.button("Predict Water Requirement"):
|
| 51 |
+
input_data = {
|
| 52 |
+
'temperature': temperature,
|
| 53 |
+
'humidity': humidity,
|
| 54 |
+
'wind_speed': wind_speed,
|
| 55 |
+
'evapotranspiration': evapotranspiration,
|
| 56 |
+
'soil_moisture_levels': soil_moisture_levels,
|
| 57 |
+
'water_retention_capacity': water_retention_capacity,
|
| 58 |
+
'rainfall_pattern': rainfall_pattern,
|
| 59 |
+
'soil_type': soil_type,
|
| 60 |
+
'drainage_properties': drainage_properties,
|
| 61 |
+
'crop_type': crop_type,
|
| 62 |
+
'growth_stage': growth_stage,
|
| 63 |
+
'crop_water_requirement':crop_water_requirement
|
| 64 |
+
}
|
| 65 |
+
processed_data = preprocess_input(input_data)
|
| 66 |
+
prediction = model.predict(processed_data)[0]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
st.write("### Predicted Water Requirement")
|
| 70 |
+
st.success(f"The predicted water requirement is **{prediction:.2f} mm/day**.")
|
| 71 |
+
|
| 72 |
+
st.header("Optimization Suggestions")
|
| 73 |
+
|
| 74 |
+
suggestions = []
|
| 75 |
+
|
| 76 |
+
# Check soil type
|
| 77 |
+
if soil_type == "Sandy":
|
| 78 |
+
suggestions.append("Consider improving soil structure by adding organic matter or compost to enhance water retention.")
|
| 79 |
+
|
| 80 |
+
# Check evapotranspiration
|
| 81 |
+
if evapotranspiration > 8.0:
|
| 82 |
+
suggestions.append("High evapotranspiration detected. Use mulching to reduce evaporation and conserve soil moisture.")
|
| 83 |
+
|
| 84 |
+
# Check soil moisture
|
| 85 |
+
if soil_moisture_levels < 15.0:
|
| 86 |
+
suggestions.append("Low soil moisture detected. Ensure proper irrigation scheduling to maintain adequate levels.")
|
| 87 |
+
|
| 88 |
+
# Check water retention
|
| 89 |
+
if water_retention_capacity < 20.0:
|
| 90 |
+
suggestions.append("Low water retention capacity. Incorporate soil amendments to improve retention.")
|
| 91 |
+
|
| 92 |
+
# Check drainage
|
| 93 |
+
if drainage_properties=='good':
|
| 94 |
+
suggestions.append("Poor drainage detected. Improve soil aeration or use soil conditioners to balance drainage.")
|
| 95 |
+
|
| 96 |
+
# Display suggestions
|
| 97 |
+
if suggestions:
|
| 98 |
+
for suggestion in suggestions:
|
| 99 |
+
st.write(f"- {suggestion}")
|
| 100 |
+
else:
|
| 101 |
+
st.write("✅ All parameters are well-balanced for optimal water usage!")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# Footer
|
| 105 |
+
st.write("---")
|
| 106 |
+
#st.write("Developed with ❤️ using Streamlit")
|
clean_water_requirement_data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
main.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import libraries
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import warnings
|
| 6 |
+
warnings.filterwarnings('ignore')
|
| 7 |
+
from sklearn.model_selection import train_test_split, GridSearchCV
|
| 8 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 9 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
| 10 |
+
#importing the dataset
|
| 11 |
+
df= pd.read_csv('clean_water_requirement_data.csv')
|
| 12 |
+
print(df.shape)
|
| 13 |
+
|
| 14 |
+
#finding the categorical and numerical columns
|
| 15 |
+
categorical_columns = df.select_dtypes(include=['object', 'category']).columns
|
| 16 |
+
numerical_columns = df.select_dtypes(include=['float64', 'int64']).columns
|
| 17 |
+
|
| 18 |
+
#encoding the categorical variables
|
| 19 |
+
from sklearn.preprocessing import LabelEncoder
|
| 20 |
+
label_encoder = LabelEncoder()
|
| 21 |
+
for col in categorical_columns:
|
| 22 |
+
df[col] = label_encoder.fit_transform(df[col])
|
| 23 |
+
|
| 24 |
+
#separating input and target features in dataset
|
| 25 |
+
X = df.drop(columns=['water_requirement'])
|
| 26 |
+
y=df.iloc[:,-1]
|
| 27 |
+
|
| 28 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 29 |
+
|
| 30 |
+
from sklearn.preprocessing import StandardScaler
|
| 31 |
+
|
| 32 |
+
# Apply StandardScaler
|
| 33 |
+
scaler = StandardScaler()
|
| 34 |
+
X_train = scaler.fit_transform(X_train)
|
| 35 |
+
X_test = scaler.transform(X_test)
|
| 36 |
+
|
| 37 |
+
# Initialize the model with the provided best parameters
|
| 38 |
+
model = RandomForestRegressor(max_depth=20, n_estimators=200, random_state=42)
|
| 39 |
+
|
| 40 |
+
# Fit the model
|
| 41 |
+
model.fit(X_train, y_train)
|
| 42 |
+
|
| 43 |
+
# Predictions
|
| 44 |
+
y_pred = model.predict(X_test)
|
| 45 |
+
|
| 46 |
+
# Calculate performance metrics
|
| 47 |
+
mse = mean_squared_error(y_test, y_pred)
|
| 48 |
+
mae = mean_absolute_error(y_test, y_pred)
|
| 49 |
+
r2 = r2_score(y_test, y_pred)
|
| 50 |
+
|
| 51 |
+
# Save the model as a .pkl file using joblib
|
| 52 |
+
model_filename = 'random_forest_regressor_model.pkl'
|
| 53 |
+
joblib.dump(model, model_filename)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
joblib
|
| 5 |
+
pandas
|
| 6 |
+
numpy
|
| 7 |
+
joblib
|
| 8 |
+
warnings
|
| 9 |
+
sklearn
|