Skip to content

Commit d145b9c

Browse files
Merge branch 'DhanushNehru:master' into master
2 parents 3b639b7 + 1e16c8d commit d145b9c

15 files changed

Lines changed: 2178 additions & 7 deletions

File tree

HarvestPredictor/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn flask_app:app

HarvestPredictor/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Harvest Predictor
2+
3+
Paddy Harvest Prediction using linear regression machine learning algorithm with html and css frontend with flask backend
4+
5+
to run open the terminal from the directory and run `flask run app.py`

HarvestPredictor/Regression.ipynb

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"id": "Z8r4GjxCOUlx"
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import pandas as pd\n",
12+
"from sklearn.model_selection import train_test_split\n",
13+
"from sklearn.linear_model import LinearRegression\n",
14+
"import numpy as np\n",
15+
"from sklearn.linear_model import LinearRegression\n",
16+
"from sklearn.metrics import mean_squared_error, r2_score"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 2,
22+
"metadata": {
23+
"id": "3Sug1VEzHmX7"
24+
},
25+
"outputs": [],
26+
"source": [
27+
"# Load data from an Excel sheet (adjust the file path)\n",
28+
"data = pd.read_excel('paddy.xlsx')"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 3,
34+
"metadata": {
35+
"id": "jUuUVOB7H1rf"
36+
},
37+
"outputs": [],
38+
"source": [
39+
"# Define the independent variables (features) and the dependent variable (target)\n",
40+
"X = data[['Temperature', 'Avg rain(mm)', 'Fertilizer ', 'RAINFED(Hect)']]\n",
41+
"y = data['RAINFED Yeild(Kg)']"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 4,
47+
"metadata": {
48+
"id": "PHRy1VUYIepV"
49+
},
50+
"outputs": [],
51+
"source": [
52+
"# Split the data into training and testing sets\n",
53+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 5,
59+
"metadata": {
60+
"colab": {
61+
"base_uri": "https://localhost:8080/",
62+
"height": 75
63+
},
64+
"id": "UybRqKatIpgK",
65+
"outputId": "c548807e-5cb7-4f0c-d78c-408517d1e962"
66+
},
67+
"outputs": [
68+
{
69+
"data": {
70+
"text/plain": [
71+
"LinearRegression()"
72+
]
73+
},
74+
"execution_count": 5,
75+
"metadata": {},
76+
"output_type": "execute_result"
77+
}
78+
],
79+
"source": [
80+
"# Create and train the linear regression model\n",
81+
"model = LinearRegression()\n",
82+
"model.fit(X_train, y_train)"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 6,
88+
"metadata": {
89+
"id": "gVWGYyyCMM1z"
90+
},
91+
"outputs": [],
92+
"source": [
93+
"# Use the trained model to make predictions\n",
94+
"predictions = model.predict(X_test)"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 7,
100+
"metadata": {
101+
"colab": {
102+
"base_uri": "https://localhost:8080/"
103+
},
104+
"id": "UFXq_Bi8MRl5",
105+
"outputId": "39983a17-cbf9-48c6-eefd-0a5bd2699e18"
106+
},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"Coefficients: [-1.17361588e+02 2.62442795e+00 5.39719178e-02 -2.09136339e-02]\n",
113+
"Intercept: 5483.521027751586\n"
114+
]
115+
}
116+
],
117+
"source": [
118+
"# Print the model's coefficients and intercept\n",
119+
"print(\"Coefficients:\", model.coef_)\n",
120+
"print(\"Intercept:\", model.intercept_)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 8,
126+
"metadata": {
127+
"colab": {
128+
"base_uri": "https://localhost:8080/",
129+
"height": 75
130+
},
131+
"id": "d49PUGJmN6rO",
132+
"outputId": "19212bc4-fca0-40ca-e87f-62e9ebc1645d"
133+
},
134+
"outputs": [
135+
{
136+
"data": {
137+
"text/plain": [
138+
"LinearRegression()"
139+
]
140+
},
141+
"execution_count": 8,
142+
"metadata": {},
143+
"output_type": "execute_result"
144+
}
145+
],
146+
"source": [
147+
"# Fit the model to the training data\n",
148+
"model.fit(X_train, y_train)"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 12,
154+
"metadata": {
155+
"id": "QsVgkflkN9FH"
156+
},
157+
"outputs": [],
158+
"source": [
159+
"# Make predictions on the test data\n",
160+
"y_pred = model.predict(X_test)"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 18,
166+
"metadata": {
167+
"id": "5oWZhxgOOAm0"
168+
},
169+
"outputs": [],
170+
"source": [
171+
"# Evaluate the model\n",
172+
"mse = mean_squared_error(y_test, y_pred)\n",
173+
"r2 = r2_score(y_test, y_pred)\n"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 23,
179+
"metadata": {
180+
"colab": {
181+
"base_uri": "https://localhost:8080/"
182+
},
183+
"id": "NRftqW0MOtnA",
184+
"outputId": "31ad76ee-8af7-447c-bfe4-4aebf240ee8e"
185+
},
186+
"outputs": [
187+
{
188+
"name": "stdout",
189+
"output_type": "stream",
190+
"text": [
191+
"Mean Squared Error: 1318883.1489845559\n",
192+
"R-squared: 0.2244992057784787\n"
193+
]
194+
}
195+
],
196+
"source": [
197+
"print(\"Mean Squared Error:\", mse)\n",
198+
"print(\"R-squared:\", r2)"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": 26,
204+
"metadata": {
205+
"colab": {
206+
"base_uri": "https://localhost:8080/"
207+
},
208+
"id": "St4aiHqHOyQg",
209+
"outputId": "b6ab7c3a-a1bb-4133-8ac2-ad7fd793d4f9"
210+
},
211+
"outputs": [
212+
{
213+
"name": "stdout",
214+
"output_type": "stream",
215+
"text": [
216+
"Predicted Paddy Yield: 3655.265733565367\n"
217+
]
218+
}
219+
],
220+
"source": [
221+
"# Predict paddy yield for a new set of input features\n",
222+
"new_data = np.array([[30.3, 42.4,30569.661, 1596]]) # Replace with your own values\n",
223+
"predicted_yield = model.predict(new_data)\n",
224+
"\n",
225+
"print(\"Predicted Paddy Yield:\", predicted_yield[0])"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": 29,
231+
"metadata": {},
232+
"outputs": [],
233+
"source": [
234+
"import pickle"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 32,
240+
"metadata": {},
241+
"outputs": [],
242+
"source": [
243+
"with open('model_pickle','wb') as f:\n",
244+
" pickle.dump(model,f)"
245+
]
246+
},
247+
{
248+
"cell_type": "code",
249+
"execution_count": 33,
250+
"metadata": {},
251+
"outputs": [],
252+
"source": [
253+
"with open('model_pickle','rb') as f:\n",
254+
" mp = pickle.load(f)"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": 34,
260+
"metadata": {},
261+
"outputs": [
262+
{
263+
"data": {
264+
"text/plain": [
265+
"array([3655.26573357])"
266+
]
267+
},
268+
"execution_count": 34,
269+
"metadata": {},
270+
"output_type": "execute_result"
271+
}
272+
],
273+
"source": [
274+
"mp.predict(new_data)"
275+
]
276+
}
277+
],
278+
"metadata": {
279+
"colab": {
280+
"provenance": []
281+
},
282+
"kernelspec": {
283+
"display_name": "Python 3 (ipykernel)",
284+
"language": "python",
285+
"name": "python3"
286+
},
287+
"language_info": {
288+
"codemirror_mode": {
289+
"name": "ipython",
290+
"version": 3
291+
},
292+
"file_extension": ".py",
293+
"mimetype": "text/x-python",
294+
"name": "python",
295+
"nbconvert_exporter": "python",
296+
"pygments_lexer": "ipython3",
297+
"version": "3.9.7"
298+
}
299+
},
300+
"nbformat": 4,
301+
"nbformat_minor": 1
302+
}

HarvestPredictor/app.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
from flask import Flask, request, jsonify, render_template
3+
import pickle
4+
5+
6+
flask_app = Flask(__name__)
7+
model = pickle.load(open("model_pickle", "rb"))
8+
9+
10+
@flask_app.route("/")
11+
def Home():
12+
background_image = "static/101608227-paddy-field.jpg"
13+
return render_template("index.html", background_image=background_image)
14+
15+
16+
@flask_app.route("/predict", methods=["POST"])
17+
def predict():
18+
float_features = [float(x) for x in request.form.values()]
19+
features = [np.array(float_features)]
20+
prediction = model.predict(features)
21+
return render_template("index.html", prediction_text=" {}".format(prediction))
22+
23+
24+
if __name__ == "__main__":
25+
flask_app.run(debug=True,host='0.0.0.0')

HarvestPredictor/model_pickle

512 Bytes
Binary file not shown.

HarvestPredictor/requirements.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Flask==2.2.5
2+
numpy==1.20.3
3+
4+
gunicorn==20.0.4
5+
Werkzeug==0.16.0
6+
Jinja2==2.10.1
7+
itsdangerous==1.1.0
8+
MarkupSafe==1.1.1
250 KB
Loading

0 commit comments

Comments
 (0)