-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold.py
More file actions
165 lines (123 loc) · 6.38 KB
/
old.py
File metadata and controls
165 lines (123 loc) · 6.38 KB
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
import numpy as np
import pandas as pd
import os, sys
from sklearn.preprocessing import MinMaxScaler
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn import metrics
from sklearn import svm
#from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
import random
import math
from pandas import Series
from pyspark.sql import Row
# Read Dataset
df = pd.read_csv("survey-lung-cancer.csv")
d = {'NO': 0, 'YES': 1}
df['LUNG_CANCER'] = df['LUNG_CANCER'].map(d)
d2 = {'M': 1, 'F': 0}
df['GENDER'] = df['GENDER'].map(d2)
df=df.T.drop_duplicates().T
# Missing Value Imputation
#df.ca.loc[df.ca=='?'] = '0'
#df.thal.loc[df.thal=='?'] = '3'
# Remove Outlier
#df = df[df.chol<500][df.oldpeak<5]
# Remove Unnecessary Column
df.drop(columns = 'AGE', axis = 1, inplace = True)
# One-Hot Encoding
df = pd.get_dummies(df, columns = ['GENDER','SMOKING','YELLOW_FINGERS','ANXIETY','PEER_PRESSURE','CHRONIC DISEASE','FATIGUE','ALLERGY','WHEEZING','ALCOHOL CONSUMING','COUGHING','SHORTNESS OF BREATH','SWALLOWING DIFFICULTY','CHEST PAIN'], drop_first = True)
# Get features and target variables
target ='LUNG_CANCER'
target, feature_list = 'LUNG_CANCER', [i for i in df.columns if i not in target]
X = df.loc[:, df.columns != target]
y = df.loc[:, target]
# Print list of features and target variable names
print('Feature List\n',feature_list, '\n\nTarget = ',target)
def init_population(n,c):
return np.array([[math.ceil(e) for e in pop] for pop in (np.random.rand(n,c)-0.5)]), np.zeros((2,c))-1
def single_point_crossover(population):
r,c, n = population.shape[0], population.shape[1], np.random.randint(1,population.shape[1])
for i in range(0,r,2):
population[i], population[i+1] = np.append(population[i][0:n],population[i+1][n:c]),np.append(population[i+1][0:n],population[i][n:c])
return population
def flip_mutation(population):
return population.max() - population
def random_selection(population):
r = population.shape[0]
new_population = population.copy()
for i in range(r):
new_population[i] = population[np.random.randint(0,r)]
return new_population
def get_fitness(data, feature_list, target, population):
fitness = []
for i in range(population.shape[0]):
columns = [feature_list[j] for j in range(population.shape[1]) if population[i,j]==1]
fitness.append(predictive_model(data[columns], data[target]))
return fitness
def predictive_model(X,y):
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=7)
rfc = RandomForestClassifier(n_estimators=100, random_state=0, n_jobs=-1)
rfc.fit(X_train,y_train)
return accuracy_score(y_test, rfc.predict(X_test))
predictions = rfc.predict(X_test)
rfc_pred = rfc.predict(X_test)
#rfc_score = accuracy_score(y_test,rfc_pred)
#precission_rfc = precision_score(y_test,rfc_pred)
#recall_rfc = recall_score(y_test,rfc_pred)
#f1_rfc = f1_score(y_test,rfc_pred)
#return ("Accuracy_Score by cross_val_orediction :", Accuracy_Score)
#return ("Precission : ",Precision_Score)
#return ("Recall : ",Recall_Score)
#return ("F1 : ",f1_Score)
def ga(data, feature_list, target, n, max_iter):
c = len(feature_list)
population, memory = init_population(n,c)
#def replace_duplicate(population, memory) :
# population, memory =replace_duplicate(population, memory)
fitness= get_fitness(data, feature_list, target, population)
optimal_value= max(fitness)
optimal_solution = population[np.where(fitness==optimal_value)][0]
for i in range(max_iter):
population = random_selection(population)
population = single_point_crossover(population)
if np.random.rand() < 0.3:
population = flip_mutation(population)
# population, memory = replace_duplicate(population, memory)
fitness = get_fitness(data, feature_list, target, population)
if max(fitness) > optimal_value:
optimal_value = max(fitness)
optimal_solution = population[np.where(fitness==optimal_value)][0]
return optimal_solution, optimal_value
# Execute Genetic Algorithm to obtain Important Feature
feature_set, acc_score= ga(df, feature_list, target, 20, 1000)
# Filter Selected Features
feature_set = [feature_list[i] for i in range(len(feature_list)) if feature_set[i]==1]
# Print List of Features
print('Optimal Feature Set\n',feature_set,'\nOptimal Accuracy =', round(acc_score*100), '%')
#print('Average Accuracy saved', Accuracy_Score, '\n Average Precision', Precision_Score, '\n Average Recall',Recall_Score,'\n Average F1-Score', F1_Score)
rfc = RandomForestClassifier(n_estimators=100, random_state=0, n_jobs=-1)
scores = cross_val_score(estimator=rfc, X=X, y=y, cv=10, scoring='accuracy')
predicted_label = cross_val_predict(estimator=rfc, X=X, y=y, cv=10)
score = round(scores.mean() * 100, 4)
#print(score)
Accuracy_Score = accuracy_score(y, predicted_label)
Precision_Score = precision_score(y, predicted_label, average="macro")
Recall_Score = recall_score(y, predicted_label, average="macro")
F1_Score = f1_score(y, predicted_label, average="macro")
print('Average Accuracy saved', Accuracy_Score, '\n Average Precision', Precision_Score, '\n Average Recall',Recall_Score,'\n Average F1-Score', F1_Score)
import matplotlib.pyplot as plt
model = RandomForestClassifier()
model.fit(X,y)
print(model.feature_importances_)
#use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.show()