Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +39 -0
- selector.h5 +3 -0
- selector.ipynb +212 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import joblib
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
model = joblib.load('selector.h5')
|
6 |
+
|
7 |
+
prices = pd.read_csv("crop_prices.csv")
|
8 |
+
|
9 |
+
st.title("Crop Selection App")
|
10 |
+
|
11 |
+
st.header("Input Soil Data:")
|
12 |
+
nitrogen = st.number_input("Nitrogen", min_value=0, max_value=100, value=50)
|
13 |
+
phosphorus = st.number_input("Phosphorus", min_value=0, max_value=100, value=50)
|
14 |
+
potassium = st.number_input("Potassium", min_value=0, max_value=100, value=50)
|
15 |
+
temperature = st.number_input("Temperature", min_value=0.0, max_value=100.0, value=25.0,step=1.,format="%.4f")
|
16 |
+
humidity = st.number_input("Humidity", min_value=0.0, max_value=100.0, value=50.0,step=1.,format="%.4f")
|
17 |
+
ph = st.number_input("pH", min_value=0.0, max_value=14.0, value=7.0,step=1.,format="%.4f")
|
18 |
+
rainfall = st.number_input("Rainfall", min_value=0.0, max_value=1000.0, value=500.0,step=1.,format="%.4f")
|
19 |
+
|
20 |
+
user_input = [[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]]
|
21 |
+
|
22 |
+
if st.button("Predict"):
|
23 |
+
predicted_crop = model.predict_proba(user_input)
|
24 |
+
|
25 |
+
crop_probabilities = list(zip(model.classes_, predicted_crop[0]))
|
26 |
+
|
27 |
+
# Sort crops based on probability estimates
|
28 |
+
sorted_crops = sorted(crop_probabilities, key=lambda x: x[1], reverse=True)
|
29 |
+
|
30 |
+
# Display the sorted crops
|
31 |
+
st.header("Top 3 Crops to grow:")
|
32 |
+
for i, (crop, probability) in enumerate(sorted_crops[:3]):
|
33 |
+
prob_percent = probability*100
|
34 |
+
#st.write(f"{i+1}. {crop}: {prob_percent:.2f}%")
|
35 |
+
average_price = prices.loc[prices['CROP'] == crop, 'AVG PRICES'].values[0]
|
36 |
+
|
37 |
+
st.write(f"{i+1}. {crop}: {prob_percent:.2f}% || Average Price: Rs.{average_price} / Quintal")
|
38 |
+
|
39 |
+
st.text("Created by Analytical Aces")
|
selector.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7feea986c37dd5b44b1149825352a28c2843a761e2893b65f75227e95555ed70
|
3 |
+
size 4887
|
selector.ipynb
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"data": {
|
10 |
+
"text/plain": [
|
11 |
+
"['selector.h5']"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
"execution_count": 2,
|
15 |
+
"metadata": {},
|
16 |
+
"output_type": "execute_result"
|
17 |
+
}
|
18 |
+
],
|
19 |
+
"source": [
|
20 |
+
"import pandas as pd\n",
|
21 |
+
"import streamlit as st\n",
|
22 |
+
"from sklearn.naive_bayes import GaussianNB\n",
|
23 |
+
"import joblib\n",
|
24 |
+
"\n",
|
25 |
+
"prices = pd.read_csv(r\"C:\\Users\\Kush\\Desktop\\hackathons\\IITB\\crop_selector\\crop_prices.csv\")\n",
|
26 |
+
"dataset = pd.read_csv(r\"C:\\Users\\Kush\\Desktop\\hackathons\\IITB\\crop_selector\\Crop_selection.csv\")\n",
|
27 |
+
"\n",
|
28 |
+
"features = dataset[['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall']]\n",
|
29 |
+
"target = dataset['label']\n",
|
30 |
+
"\n",
|
31 |
+
"model = GaussianNB()\n",
|
32 |
+
"model.fit(features, target)\n",
|
33 |
+
"\n",
|
34 |
+
"joblib.dump(model, 'selector.h5')"
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "code",
|
39 |
+
"execution_count": 3,
|
40 |
+
"metadata": {},
|
41 |
+
"outputs": [
|
42 |
+
{
|
43 |
+
"name": "stdout",
|
44 |
+
"output_type": "stream",
|
45 |
+
"text": [
|
46 |
+
"Overwriting app.py\n"
|
47 |
+
]
|
48 |
+
}
|
49 |
+
],
|
50 |
+
"source": [
|
51 |
+
"%%writefile app.py\n",
|
52 |
+
"import streamlit as st\n",
|
53 |
+
"\n",
|
54 |
+
"model = joblib.load('selector.h5')\n",
|
55 |
+
"\n",
|
56 |
+
"st.title(\"Crop Selection App\")\n",
|
57 |
+
"\n",
|
58 |
+
"st.header(\"Input Soil Data:\")\n",
|
59 |
+
"nitrogen = st.number_input(\"Nitrogen\", min_value=0, max_value=100, value=50)\n",
|
60 |
+
"phosphorus = st.number_input(\"Phosphorus\", min_value=0, max_value=100, value=50)\n",
|
61 |
+
"potassium = st.number_input(\"Potassium\", min_value=0, max_value=100, value=50)\n",
|
62 |
+
"temperature = st.number_input(\"Temperature\", min_value=0.0, max_value=100.0, value=25.0,step=1.,format=\"%.4f\")\n",
|
63 |
+
"humidity = st.number_input(\"Humidity\", min_value=0.0, max_value=100.0, value=50.0,step=1.,format=\"%.4f\")\n",
|
64 |
+
"ph = st.number_input(\"pH\", min_value=0.0, max_value=14.0, value=7.0,step=1.,format=\"%.4f\")\n",
|
65 |
+
"rainfall = st.number_input(\"Rainfall\", min_value=0.0, max_value=1000.0, value=500.0,step=1.,format=\"%.4f\")\n",
|
66 |
+
"\n",
|
67 |
+
"user_input = [[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]]\n",
|
68 |
+
"\n",
|
69 |
+
"if st.button(\"Predict\"):\n",
|
70 |
+
" predicted_crop = model.predict_proba(user_input)\n",
|
71 |
+
"\n",
|
72 |
+
" crop_probabilities = list(zip(model.classes_, predicted_crop[0]))\n",
|
73 |
+
"\n",
|
74 |
+
" # Sort crops based on probability estimates\n",
|
75 |
+
" sorted_crops = sorted(crop_probabilities, key=lambda x: x[1], reverse=True)\n",
|
76 |
+
"\n",
|
77 |
+
" # Display the sorted crops\n",
|
78 |
+
" st.header(\"Top 3 Crops to grow:\")\n",
|
79 |
+
" for i, (crop, probability) in enumerate(sorted_crops[:3]):\n",
|
80 |
+
" prob_percent = probability*100\n",
|
81 |
+
" #st.write(f\"{i+1}. {crop}: {prob_percent:.2f}%\")\n",
|
82 |
+
" average_price = prices.loc[prices['CROP'] == crop, 'AVG PRICES'].values[0]\n",
|
83 |
+
" \n",
|
84 |
+
" st.write(f\"{i+1}. {crop}: {prob_percent:.2f}% || Average Price: Rs.{average_price} / Quintal\")\n"
|
85 |
+
]
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"cell_type": "code",
|
89 |
+
"execution_count": null,
|
90 |
+
"metadata": {},
|
91 |
+
"outputs": [],
|
92 |
+
"source": [
|
93 |
+
"%%writefile app.py\n",
|
94 |
+
"import streamlit as st\n",
|
95 |
+
"import pandas as pd\n",
|
96 |
+
"import joblib\n",
|
97 |
+
"from sklearn.preprocessing import LabelEncoder\n",
|
98 |
+
"\n",
|
99 |
+
"# Load the saved model\n",
|
100 |
+
"best_rf_classifier = joblib.load('moal.h5')\n",
|
101 |
+
"\n",
|
102 |
+
"# Load the label encoder\n",
|
103 |
+
"label_encoder = LabelEncoder()\n",
|
104 |
+
"\n",
|
105 |
+
"# Streamlit App\n",
|
106 |
+
"st.title(\"Loan Approval Prediction App\")\n",
|
107 |
+
"\n",
|
108 |
+
"# Sidebar\n",
|
109 |
+
"st.sidebar.header(\"User Input\")\n",
|
110 |
+
"\n",
|
111 |
+
"# Gender\n",
|
112 |
+
"gender_options = ['Male', 'Female']\n",
|
113 |
+
"gender = st.sidebar.radio(\"Gender\", gender_options)\n",
|
114 |
+
"\n",
|
115 |
+
"# Married\n",
|
116 |
+
"married_options = ['Yes', 'No']\n",
|
117 |
+
"married = st.sidebar.radio(\"Marital Status\", married_options)\n",
|
118 |
+
"\n",
|
119 |
+
"# Dependents\n",
|
120 |
+
"dependents = st.sidebar.selectbox(\"Number of Dependents\", ['0', '1', '2', '3+'])\n",
|
121 |
+
"\n",
|
122 |
+
"# Applicant Income\n",
|
123 |
+
"applicant_income = st.sidebar.number_input(\"Applicant Income\", min_value=0)\n",
|
124 |
+
"\n",
|
125 |
+
"# Coapplicant Income\n",
|
126 |
+
"coapplicant_income = st.sidebar.number_input(\"Coapplicant Income\", min_value=0)\n",
|
127 |
+
"\n",
|
128 |
+
"# Loan Amount\n",
|
129 |
+
"loan_amount = st.sidebar.number_input(\"Loan Amount\", min_value=0)\n",
|
130 |
+
"\n",
|
131 |
+
"# Loan Amount Term\n",
|
132 |
+
"loan_amount_term = st.sidebar.number_input(\"Loan Amount Term\", min_value=0)\n",
|
133 |
+
"\n",
|
134 |
+
"# Land Area\n",
|
135 |
+
"land_area = st.sidebar.number_input(\"Land Area\", min_value=0.0)\n",
|
136 |
+
"\n",
|
137 |
+
"# Crop Type\n",
|
138 |
+
"crop_type_options = ['rabi', 'kharif']\n",
|
139 |
+
"crop_type = st.sidebar.radio(\"Crop Type\", crop_type_options)\n",
|
140 |
+
"\n",
|
141 |
+
"# Land Type\n",
|
142 |
+
"land_type_options = ['irrigated', 'rainfed']\n",
|
143 |
+
"land_type = st.sidebar.radio(\"Land Type\", land_type_options)\n",
|
144 |
+
"\n",
|
145 |
+
"# Irrigation Type\n",
|
146 |
+
"irrigation_type_options = ['drip', 'rainfed', 'sprinkle', 'borewell']\n",
|
147 |
+
"irrigation_type = st.sidebar.selectbox(\"Irrigation Type\", irrigation_type_options)\n",
|
148 |
+
"\n",
|
149 |
+
"# Combine user inputs into a DataFrame\n",
|
150 |
+
"user_inputs = pd.DataFrame({\n",
|
151 |
+
" 'Gender': [gender],\n",
|
152 |
+
" 'Married': [married],\n",
|
153 |
+
" 'Dependents': [dependents],\n",
|
154 |
+
" 'ApplicantIncome': [applicant_income],\n",
|
155 |
+
" 'CoapplicantIncome': [coapplicant_income],\n",
|
156 |
+
" 'LoanAmount': [loan_amount],\n",
|
157 |
+
" 'Loan_Amount_Term': [loan_amount_term],\n",
|
158 |
+
" 'land_area': [land_area],\n",
|
159 |
+
" 'crop_type': [crop_type],\n",
|
160 |
+
" 'land_type': [land_type],\n",
|
161 |
+
" 'irrigation_type': [irrigation_type]\n",
|
162 |
+
"})\n",
|
163 |
+
"\n",
|
164 |
+
"# Encode categorical variables\n",
|
165 |
+
"user_inputs['Gender'] = label_encoder.fit_transform(user_inputs['Gender'])\n",
|
166 |
+
"user_inputs['Married'] = label_encoder.fit_transform(user_inputs['Married'])\n",
|
167 |
+
"user_inputs['crop_type'] = label_encoder.fit_transform(user_inputs['crop_type'])\n",
|
168 |
+
"user_inputs['land_type'] = label_encoder.fit_transform(user_inputs['land_type'])\n",
|
169 |
+
"user_inputs['irrigation_type'] = label_encoder.fit_transform(user_inputs['irrigation_type'])\n",
|
170 |
+
"\n",
|
171 |
+
"# Handle '3+' in 'Dependents' column\n",
|
172 |
+
"user_inputs['Dependents'] = user_inputs['Dependents'].replace('3+', 3)\n",
|
173 |
+
"\n",
|
174 |
+
"if st.sidebar.button(\"Predict\"):\n",
|
175 |
+
" # Make prediction\n",
|
176 |
+
" prediction = best_rf_classifier.predict(user_inputs)\n",
|
177 |
+
"\n",
|
178 |
+
" # Interpret the predicted value directly for binary classification\n",
|
179 |
+
" predicted_loan_status = \"Approved\" if prediction[0] == 1 else \"Not Approved\"\n",
|
180 |
+
"\n",
|
181 |
+
" # Display prediction\n",
|
182 |
+
" st.subheader(\"Prediction Result\")\n",
|
183 |
+
" st.write(f\"The predicted loan status is: {predicted_loan_status}\")\n",
|
184 |
+
"\n",
|
185 |
+
"\n",
|
186 |
+
"# Credits\n",
|
187 |
+
"st.sidebar.text(\"Created by Analytical Aces\")\n"
|
188 |
+
]
|
189 |
+
}
|
190 |
+
],
|
191 |
+
"metadata": {
|
192 |
+
"kernelspec": {
|
193 |
+
"display_name": "Python 3",
|
194 |
+
"language": "python",
|
195 |
+
"name": "python3"
|
196 |
+
},
|
197 |
+
"language_info": {
|
198 |
+
"codemirror_mode": {
|
199 |
+
"name": "ipython",
|
200 |
+
"version": 3
|
201 |
+
},
|
202 |
+
"file_extension": ".py",
|
203 |
+
"mimetype": "text/x-python",
|
204 |
+
"name": "python",
|
205 |
+
"nbconvert_exporter": "python",
|
206 |
+
"pygments_lexer": "ipython3",
|
207 |
+
"version": "3.10.5"
|
208 |
+
}
|
209 |
+
},
|
210 |
+
"nbformat": 4,
|
211 |
+
"nbformat_minor": 2
|
212 |
+
}
|