-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmpowerHer Finance_Algorithm.py
299 lines (226 loc) · 8.79 KB
/
EmpowerHer Finance_Algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding: utf-8 -*-
"""Algorithm_CodeForChange.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1m5iTpkplGDt1z5HGvcs22bvuVAdwA_Jf
# **Predicting financial vulnerability of women-headed households in rural India**
# Develop an accurate model for predicting financial risk in these households.
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import OneHotEncoder
df = pd.read_csv("/content/eda_PRICE_dataset.csv")
print(df)
num_rows, num_cols = df.shape
print("Number of rows:", num_rows)
print("Number of columns:", num_cols)
"""# **Introduce a financial vulnerability index for Indian women-led households.**"""
# prompt: save Financial_Vulnerability_Index column in eda_PRICE_dataset.csv
df.to_csv("eda_PRICE_dataset.csv")
df['Financial_Vulnerability_Index'] = np.where(df['Financial_Vulnerability'] <= 0, 'Yes', 'No')
print(df)
num_rows, num_cols = df.shape
print("Number of rows:", num_rows)
print("Number of columns:", num_cols)
df['Financial_Vulnerability_Index'].value_counts()
plt.figure(figsize=(10,5))
sns.countplot(x='Financial_Vulnerability_Index', data=df)
plt.xlabel('Financial_Vulnerability')
plt.ylabel('Count')
plt.show()
selected_columns = [
'States',
'Age',
'Marital Status',
'Education Level',
'Primary Occupation',
'Number of Members',
'Number of Earners',
'Do you have Mobile?',
'Type of Mobile',
'Do you have Bank Acc?',
'Saving',
'Yojana',
'AccountType',
'Total routine Expenditure',
'Financial_Vulnerability_Index'
]
df = df[selected_columns]
# Check for missing values
print(df.isnull().sum())
# Handle missing values as needed
# One-hot encode categorical variables
df_encoded = pd.get_dummies(df, drop_first=True)
df_encoded.columns
X = df_encoded.drop('Financial_Vulnerability_Index_Yes', axis=1)
Y = df_encoded['Financial_Vulnerability_Index_Yes']
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
"""# **RandomForestClassifier**"""
from sklearn.ensemble import RandomForestClassifier
# Initialize the Random Forest classifier
clf = RandomForestClassifier(random_state=42)
# Train the classifier on the training data
clf.fit(X_train, Y_train)
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Make predictions on the test data
y_pred = clf.predict(X_test)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# Generate classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))
# Visualize confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()
"""# **Logistic Regression**"""
from sklearn.linear_model import LogisticRegression
# Initialize logistic regression model
Logistic_model = LogisticRegression(random_state=42)
# Train the model on the training data
Logistic_model.fit(X_train, Y_train)
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Make predictions on the test data
y_pred = Logistic_model.predict(X_test)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# Generate classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))
# Visualize confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()
"""# **Naive Bayes Classifier-MultinomialNB**"""
from sklearn.naive_bayes import MultinomialNB
# Initialize the Naive Bayes model
MultinomialNBmodel = MultinomialNB()
# Train the model
MultinomialNBmodel.fit(X_train, Y_train)
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Make predictions on the test data
y_pred = MultinomialNBmodel.predict(X_test)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# Generate classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))
# Visualize confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()
"""# **Support Vector Machines (SVM)**"""
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
# Identify numerical columns
numerical_features = [cname for cname in X.columns if X[cname].dtype in ['int64', 'float64']]
# Identify categorical columns (assuming they are of type 'object')
categorical_features = [cname for cname in X.columns if X[cname].dtype == "object"]
# Preprocessing for numerical data
numerical_transformer = StandardScaler()
# Preprocessing for categorical data
categorical_transformer = OneHotEncoder(handle_unknown='ignore')
# Bundle preprocessing for numerical and categorical data
preprocessor = ColumnTransformer(
transformers=[
('num', numerical_transformer, numerical_features),
('cat', categorical_transformer, categorical_features)
])
# Define the model
model = SVC(kernel='linear') # Use 'rbf' for non-linear problems
# Bundle preprocessing and modeling code in a pipeline
clf = Pipeline(steps=[('preprocessor', preprocessor),
('model', model)
])
# Training the SVM model
clf.fit(X_train, Y_train)
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Predicting the test results
y_pred = clf.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print(classification_report(y_test, y_pred))
# Making the Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
# Visualize confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()
def predict_financial_vulnerability(input_data):
"""
Function to predict the Financial Vulnerability Index for input data.
Parameters:
input_data (dict): Input data dictionary containing values for the selected features.
Returns:
str: Predicted Financial Vulnerability Index ('YES' or 'NO').
"""
# Convert input data into a DataFrame
input_df = pd.DataFrame([input_data])
# Preprocess the input data (similar to how you preprocessed your training data)
input_encoded = pd.get_dummies(input_df, drop_first=True) # One-hot encode categorical variables if needed
# Ensure input DataFrame has the same columns as the training data
input_features = input_encoded.reindex(columns=X.columns, fill_value=0)
# Make prediction using the pre-trained model
prediction = clf.predict(input_features)
# Map the predicted label to the corresponding Financial Vulnerability Index value
if prediction[0] == 1:
return 'YES'
else:
return 'NO'
"""# **WE HAVE FINALLY DECIDED TO USE RANDOM FOREST ALGORITHM AS IT HAVE HIGHEST ACCURACY**"""
# Example usage of the prediction function
input_data = {
'States': 'CHHATTISGARH',
'Age': 34,
'Marital Status': 'Unmarried',
'Education Level': 'Middle',
'Primary Occupation': 'Self-employed - Agriculture',
'Number of Members': 6,
'Number of Earners': 2,
'Do you have Mobile?': 'No',
'Type of Mobile': None,
'Do you have Bank Acc?': 'No',
'Saving': 10000,
'Yojana': 0,
'AccountType': 0,
'Total routine Expenditure': 80000,
}
predicted_index = predict_financial_vulnerability(input_data)
print("Predicted Financial Vulnerability Index:", predicted_index)
# Calculate count of 'YES' and 'NO' values by state
state_counts = df['Financial_Vulnerability_Index'].groupby(df['States']).value_counts().unstack().fillna(0)
# Plotting the bar graph
fig, ax = plt.subplots(figsize=(10, 8))
state_counts.plot(kind='bar', stacked=True, ax=ax, color=['skyblue', 'salmon'])
# Adding labels and title
ax.set_title('Financial Vulnerability Index (YES/NO) by State', fontsize=15)
ax.set_xlabel('State', fontsize=12)
ax.set_ylabel('Count', fontsize=12)
ax.legend(title='Financial Vulnerability Index')
# Show plot
plt.xticks(rotation=45) # Rotate x-axis labels for better readability
plt.tight_layout() # Adjust layout to prevent overlapping labels
plt.show()