Spaces:
Sleeping
Sleeping
Update pages/EDA.py
Browse files- pages/EDA.py +57 -19
pages/EDA.py
CHANGED
|
@@ -129,13 +129,41 @@ feature_mapping = {
|
|
| 129 |
"pH Value Distribution per Crop": "pH_Value",
|
| 130 |
"Rainfall Distribution per Crop": "Rainfall",
|
| 131 |
}
|
|
|
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
plot_feature(feature_mapping[viz_option], viz_option)
|
| 134 |
|
|
|
|
|
|
|
| 135 |
crop_counts = df['Crop'].value_counts()
|
| 136 |
-
explode = [0.1 if i == 0 else 0 for i in range(len(crop_counts))]
|
| 137 |
|
| 138 |
-
fig, ax = plt.subplots(figsize=(
|
| 139 |
ax.pie(
|
| 140 |
crop_counts, labels=crop_counts.index, autopct='%1.1f%%',
|
| 141 |
startangle=140, explode=explode, colors=sns.color_palette("rainbow", len(crop_counts))
|
|
@@ -143,13 +171,10 @@ ax.pie(
|
|
| 143 |
ax.set_title("Proportion of Crops")
|
| 144 |
st.pyplot(fig)
|
| 145 |
|
|
|
|
| 146 |
st.markdown("### πΎ Crop Recommendations Based on Soil Nutrients & Climate Factors")
|
| 147 |
st.markdown(
|
| 148 |
-
"<p style='color:green; font-size:16px;'>β Crops that require above-average soil nutrients & climate factors
|
| 149 |
-
unsafe_allow_html=True
|
| 150 |
-
)
|
| 151 |
-
st.markdown(
|
| 152 |
-
"<p style='color:blue; font-size:16px;'>β Crops that require below-average soil nutrients & climate factors</p>",
|
| 153 |
unsafe_allow_html=True
|
| 154 |
)
|
| 155 |
|
|
@@ -167,7 +192,7 @@ avg_potassium = df['Potassium'].mean()
|
|
| 167 |
# Function to plot bar charts
|
| 168 |
def plot_nutrient_crops(data, title):
|
| 169 |
crop_counts = data.value_counts()
|
| 170 |
-
fig, ax = plt.subplots(figsize=(
|
| 171 |
crop_counts.plot(kind='bar', color='skyblue', ax=ax)
|
| 172 |
ax.set_xlabel('Crops')
|
| 173 |
ax.set_ylabel('Count')
|
|
@@ -183,14 +208,17 @@ if viz_option == "Crops in Nutrient-Rich Soil":
|
|
| 183 |
]['Crop']
|
| 184 |
plot_nutrient_crops(more_avg_of_soil_nutrients, "Crops Growing in Nutrient-Rich Soil")
|
| 185 |
st.markdown(
|
| 186 |
-
"<p style='color:green; font-size:
|
| 187 |
unsafe_allow_html=True
|
| 188 |
)
|
| 189 |
st.markdown(
|
| 190 |
-
"<p style='color:green; font-size:
|
| 191 |
unsafe_allow_html=True
|
| 192 |
)
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
elif viz_option == "Crops in Nutrient-Poor Soil":
|
| 195 |
less_avg_of_soil_nutrients = df[
|
| 196 |
(df['Nitrogen'] < avg_nitrogen) &
|
|
@@ -198,11 +226,21 @@ elif viz_option == "Crops in Nutrient-Poor Soil":
|
|
| 198 |
(df['Potassium'] < avg_potassium)
|
| 199 |
]['Crop']
|
| 200 |
plot_nutrient_crops(less_avg_of_soil_nutrients, "Crops Growing in Nutrient-Poor Soil")
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
"pH Value Distribution per Crop": "pH_Value",
|
| 130 |
"Rainfall Distribution per Crop": "Rainfall",
|
| 131 |
}
|
| 132 |
+
viz_option = st.selectbox("Select a feature visualization:", options=list(feature_mapping.keys()))
|
| 133 |
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
# Feature Visualization
|
| 138 |
+
feature_mapping = {
|
| 139 |
+
"Nitrogen Requirement per Crop": "Nitrogen",
|
| 140 |
+
"Phosphorus Requirement per Crop": "Phosphorus",
|
| 141 |
+
"Potassium Requirement per Crop": "Potassium",
|
| 142 |
+
"Temperature Distribution": "Temperature",
|
| 143 |
+
"Humidity Distribution": "Humidity",
|
| 144 |
+
"pH Value Distribution": "pH_Value",
|
| 145 |
+
"Rainfall Distribution": "Rainfall",
|
| 146 |
+
}
|
| 147 |
+
viz_option = st.select_slider("Select a feature visualization:", options=list(feature_mapping.keys()))
|
| 148 |
+
|
| 149 |
+
# Function to plot feature distribution
|
| 150 |
+
def plot_feature(feature, title):
|
| 151 |
+
fig, ax = plt.subplots(figsize=(6, 3)) # Reduced plot size
|
| 152 |
+
sns.histplot(df[feature], bins=20, kde=True, ax=ax, color="skyblue")
|
| 153 |
+
ax.set_title(title)
|
| 154 |
+
ax.set_xlabel(feature)
|
| 155 |
+
ax.set_ylabel("Density")
|
| 156 |
+
st.pyplot(fig)
|
| 157 |
+
|
| 158 |
+
# Display selected feature plot
|
| 159 |
plot_feature(feature_mapping[viz_option], viz_option)
|
| 160 |
|
| 161 |
+
# Pie Chart for Crop Proportions (Displayed Separately)
|
| 162 |
+
st.subheader("π Crop Proportions")
|
| 163 |
crop_counts = df['Crop'].value_counts()
|
| 164 |
+
explode = [0.1 if i == 0 else 0 for i in range(len(crop_counts))]
|
| 165 |
|
| 166 |
+
fig, ax = plt.subplots(figsize=(6, 3)) # Reduced plot size
|
| 167 |
ax.pie(
|
| 168 |
crop_counts, labels=crop_counts.index, autopct='%1.1f%%',
|
| 169 |
startangle=140, explode=explode, colors=sns.color_palette("rainbow", len(crop_counts))
|
|
|
|
| 171 |
ax.set_title("Proportion of Crops")
|
| 172 |
st.pyplot(fig)
|
| 173 |
|
| 174 |
+
# Crop Recommendations
|
| 175 |
st.markdown("### πΎ Crop Recommendations Based on Soil Nutrients & Climate Factors")
|
| 176 |
st.markdown(
|
| 177 |
+
"<p style='color:green; font-size:16px;'>β Crops that require above-average soil nutrients & climate factors: Banana, Rice, Papaya, Jute.</p>",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
unsafe_allow_html=True
|
| 179 |
)
|
| 180 |
|
|
|
|
| 192 |
# Function to plot bar charts
|
| 193 |
def plot_nutrient_crops(data, title):
|
| 194 |
crop_counts = data.value_counts()
|
| 195 |
+
fig, ax = plt.subplots(figsize=(6, 3)) # Further reduced plot size
|
| 196 |
crop_counts.plot(kind='bar', color='skyblue', ax=ax)
|
| 197 |
ax.set_xlabel('Crops')
|
| 198 |
ax.set_ylabel('Count')
|
|
|
|
| 208 |
]['Crop']
|
| 209 |
plot_nutrient_crops(more_avg_of_soil_nutrients, "Crops Growing in Nutrient-Rich Soil")
|
| 210 |
st.markdown(
|
| 211 |
+
"<p style='color:green; font-size:18px;'>1. Banana is the most frequent crop, indicating it requires high soil nutrients.</p>",
|
| 212 |
unsafe_allow_html=True
|
| 213 |
)
|
| 214 |
st.markdown(
|
| 215 |
+
"<p style='color:green; font-size:18px;'>2. Rice, Papaya, and Jute also need good nutrients but less than Banana.</p>",
|
| 216 |
unsafe_allow_html=True
|
| 217 |
)
|
| 218 |
+
st.markdown(
|
| 219 |
+
"<p style='color:blue; font-size:16px;'>β Crops that require below-average soil nutrients & climate factors: Orange, Mango, Coconut.</p>",
|
| 220 |
+
unsafe_allow_html=True
|
| 221 |
+
)
|
| 222 |
elif viz_option == "Crops in Nutrient-Poor Soil":
|
| 223 |
less_avg_of_soil_nutrients = df[
|
| 224 |
(df['Nitrogen'] < avg_nitrogen) &
|
|
|
|
| 226 |
(df['Potassium'] < avg_potassium)
|
| 227 |
]['Crop']
|
| 228 |
plot_nutrient_crops(less_avg_of_soil_nutrients, "Crops Growing in Nutrient-Poor Soil")
|
| 229 |
+
# Additional Insights Section
|
| 230 |
+
st.markdown("<h2 style='color: #2E86C1; font-size: 23px;'> Insights :</h2>", unsafe_allow_html=True)
|
| 231 |
+
st.markdown("""
|
| 232 |
+
<style>
|
| 233 |
+
.insights-box {
|
| 234 |
+
font-size: 18px; /* Decreased font size */
|
| 235 |
+
color: #333;
|
| 236 |
+
background-color: #f9f9f9;
|
| 237 |
+
padding: 10px;
|
| 238 |
+
border-radius: 10px;
|
| 239 |
+
border-left: 5px solid #2E86C1;
|
| 240 |
+
}
|
| 241 |
+
</style>
|
| 242 |
+
<div class='insights-box'>
|
| 243 |
+
1. Orange, Mango, and Coconut are the most frequent crops found in nutrient-poor soil.<br>
|
| 244 |
+
2. These crops are well-adapted to low-fertility conditions and can grow even when nutrient levels are below average.
|
| 245 |
+
</div>
|
| 246 |
+
""", unsafe_allow_html=True)
|