# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
# load libraries
"""
# as usual, let us load all the necessary libraries
import numpy as np  # numerical computation with arrays
import pandas as pd # library to manipulate datasets using dataframes
import scipy as sp  # statistical library

# below sklearn libraries for different models
from sklearn.tree import DecisionTreeClassifier as DecisionTree
from sklearn.ensemble import RandomForestClassifier as RandomForest
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LinearRegression
from sklearn.impute import KNNImputer
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler

# plot 
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('../input/agricultural-survey-of-african-farm-households/data.csv')
df.head()
# print a summary of the values for each covariate in the dataset
df.describe()
#describing the dataset statistics
stat = df.describe().T
stat[['mean', 'max', 'min']]
# get a summary of how many rows in the dataset and how many missing values is in each column
df.info()
#Checking nan values in the df Data
df.isna().sum()
"""
# **Pre-process the data**
"""
# Drop unimportant covariates
#here we drop all the variables that are not helpful in the model building
df.drop(columns=['interviewer', 'vname','Unnamed: 0' ],axis=1,inplace=True)

# Step 2: Drop ALL redundant covariates

df.drop(columns=['adjtempshifts1_1','adjtempshifts2_1', 'adjtempshifts3_1','adjrainfallshifts1_1','adjrainfallshifts2_1','adjrainfallshifts3_1'], axis=1, inplace=True)
# drop longtermrainfallshifts3 since it does not contain any record for the whole dataset.
df.drop(columns=['longtermrainfallshifts3' ],axis=1,inplace=True)
#fill the non values.
df = df.fillna(0)
df.head()
low = 0.05
high = 0.95

# Step 1: compute 5% percentile and the 95% percentile of each column in the dataset
quantile_df = df.quantile([low, high])
quantile_df
"""
# Visualizations
"""
#pie chart visualization for gender1
ftype = df['gender1']
List = [1,2]
Count = [sum(map(lambda x: x==i,ftype)) for i in List]
Perc = [x*100.0/sum(Count) for x in Count]
plt.pie(Perc,None,List,autopct='%1.1f%%')
plt.show()
#pie chart visualization for age
ftype = df['gender2']
List = [1,2]
Count = [sum(map(lambda x: x==i,ftype)) for i in List]
Perc = [x*100.0/sum(Count) for x in Count]
plt.pie(Perc,None,List,autopct='%1.1f%%')
plt.show()
#pie chart visualization for age
ftype = df['gender3']
List = [1,2]
Count = [sum(map(lambda x: x==i,ftype)) for i in List]
Perc = [x*100.0/sum(Count) for x in Count]
plt.pie(Perc,None,List,autopct='%1.1f%%')
plt.show()