| import pandas as pd |
| import numpy as np |
| from sklearn.model_selection import train_test_split |
| from sklearn.preprocessing import OneHotEncoder |
| from sklearn.compose import ColumnTransformer |
| from sklearn.preprocessing import StandardScaler |
| from sklearn.ensemble import RandomForestRegressor |
| from sklearn.metrics import mean_absolute_error, r2_score, accuracy_score |
| from sklearn.linear_model import LinearRegression,Lasso, Ridge |
| from sklearn.neighbors import KNeighborsRegressor |
| from sklearn.tree import DecisionTreeRegressor |
| from sklearn.metrics import mean_absolute_error, r2_score, accuracy_score |
| from sklearn.svm import SVR |
| import gradio as gr |
|
|
| df = pd.read_csv("yield_df.csv") |
| df.head() |
|
|
| |
| df.drop_duplicates(inplace = True) |
| df.duplicated().sum() |
| col = ['Year', 'average_rain_fall_mm_per_year','pesticides_tonnes', 'avg_temp', 'Area', 'Item', 'hg/ha_yield'] |
| df = df[col] |
|
|
| x = df.drop('hg/ha_yield', axis = 1) |
| y = df['hg/ha_yield'] |
|
|
| x_train, x_test, y_train, y_test = train_test_split (x,y,test_size = 0.2, random_state= 0, shuffle= True) |
|
|
| ohe = OneHotEncoder(drop='first') |
| scale = StandardScaler() |
|
|
| preprocessor = ColumnTransformer( |
| transformers=[ |
| ('StandardScale', scale, [0, 1, 2, 3]), |
| ('OneHotEncode', ohe, [4, 5]) |
| ], remainder='passthrough' |
| ) |
|
|
| x_train_dummy = preprocessor.fit_transform(x_train) |
| x_test_dummy = preprocessor.transform(x_test) |
|
|
| models = { |
| |
| |
| |
| |
| |
| |
| |
| 'KNN': KNeighborsRegressor(), |
| } |
| for name, md in models.items(): |
| md.fit(x_train_dummy,y_train) |
| y_pred = md.predict(x_test_dummy) |
| print(f"{name}: mae : {mean_absolute_error(y_test, y_pred)}") |
| print(f"{name}: score : {r2_score(y_test, y_pred)}") |
|
|
|
|
| |
| model_K = KNeighborsRegressor() |
| model_K.fit(x_train_dummy, y_train) |
|
|
| |
| def prediction(Year, average_rain_fall_mm_per_year, pesticides_tonnes, avg_temp, Area, Item): |
| features = np.array([[Year, average_rain_fall_mm_per_year, pesticides_tonnes, avg_temp, Area, Item]], dtype=object) |
| transform_features = preprocessor.transform(features) |
| predicted_yield = model_K.predict(transform_features).reshape(-1, 1) |
| return predicted_yield[0][0] |
|
|
| |
| Areas =['Albania', 'Algeria', 'Angola', 'Argentina', 'Armenia', |
| 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', |
| 'Bangladesh', 'Belarus', 'Belgium', 'Botswana', 'Brazil', |
| 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cameroon', 'Canada', |
| 'Central African Republic', 'Chile', 'Colombia', 'Croatia', |
| 'Denmark', 'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador', |
| 'Eritrea', 'Estonia', 'Finland', 'France', 'Germany', 'Ghana', |
| 'Greece', 'Guatemala', 'Guinea', 'Guyana', 'Haiti', 'Honduras', |
| 'Hungary', 'India', 'Indonesia', 'Iraq', 'Ireland', 'Italy', |
| 'Jamaica', 'Japan', 'Kazakhstan', 'Kenya', 'Latvia', 'Lebanon', |
| 'Lesotho', 'Libya', 'Lithuania', 'Madagascar', 'Malawi', |
| 'Malaysia', 'Mali', 'Mauritania', 'Mauritius', 'Mexico', |
| 'Montenegro', 'Morocco', 'Mozambique', 'Namibia', 'Nepal', |
| 'Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Norway', |
| 'Pakistan', 'Papua New Guinea', 'Peru', 'Poland', 'Portugal', |
| 'Qatar', 'Romania', 'Rwanda', 'Saudi Arabia', 'Senegal', |
| 'Slovenia', 'South Africa', 'Spain', 'Sri Lanka', 'Sudan', |
| 'Suriname', 'Sweden', 'Switzerland', 'Tajikistan', 'Thailand', |
| 'Tunisia', 'Turkey', 'Uganda', 'Ukraine', 'United Kingdom', |
| 'Uruguay', 'Zambia', 'Zimbabwe'] |
|
|
| Items = ['Maize', 'Potatoes', 'Rice, paddy', 'Sorghum', 'Soybeans', 'Wheat', |
| 'Cassava', 'Sweet potatoes', 'Plantains and others', 'Yams'] |
|
|
| year_input = gr.Number(label="Year", value=2023) |
| rainfall_input = gr.Number(label="Average Rainfall (mm per year)", value=1500) |
| pesticides_input = gr.Number(label="Pesticides (tonnes)", value=120) |
| temperature_input = gr.Number(label="Average Temperature (°C)", value=16.0) |
| area_input = gr.Dropdown(choices=Areas, label="Area", multiselect=False) |
| item_input = gr.Dropdown(choices=Items, label="Item", multiselect=False) |
|
|
| interface = gr.Interface( |
| fn=prediction, |
| inputs=[year_input, rainfall_input, pesticides_input, temperature_input, area_input, item_input], |
| outputs=gr.Textbox(label="Predicted Crop Yield (hg/ha)"), |
| title="Crop Yield Prediction", |
| description="Enter crop and environmental details to predict the crop yield. Use dropdowns for Area and Item." |
| ) |
|
|
| interface = gr.Interface( |
| fn=prediction, |
| inputs=[year_input, rainfall_input, pesticides_input, temperature_input, area_input, item_input], |
| outputs=gr.Textbox(label="Predicted Crop Yield (hg/ha)"), |
| title="Crop Yield Prediction", |
| description="Enter crop and environmental details to predict the crop yield. Use dropdowns for Area and Item." |
| ) |
|
|
| if __name__ == "__main__": |
| interface.launch() |
|
|