Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afbcc12

Browse files
committed
Add PSO algorithm
1 parent e753650 commit afbcc12

File tree

1 file changed

+160
-1
lines changed

1 file changed

+160
-1
lines changed

‎Algorithms/Searching/PSO Feature Selection.py

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,163 @@ def fitness_without_optimization(df1):
8484
accuracy = fitness_without_optimization(df.copy())
8585
print('Accuracy :' + "{:.2f}".format(accuracy[0]))
8686
print('Precision :' + "{:.2f}".format(accuracy[1]))
87-
print('Recall :' + "{:.2f}".format(accuracy[2]))
87+
print('Recall :' + "{:.2f}".format(accuracy[2]))
88+
89+
class PSO:
90+
def __init__(self, f_count, df):
91+
92+
self.df = df.copy() # data
93+
self.f_count = f_count # Feature count
94+
self.pos_act = [] # Actual Positions radmon prob
95+
self.position = [] # Position prob > 0.5 set as 1 or 0
96+
self.velocity = [] # Velocity random between -1 and 1
97+
self.pos_best = [] # best position
98+
self.y_actual = [] # Y actual
99+
self.y_predict= [] # Y test predicted
100+
self.fit_best = (-1, -1, -1) # best fit accuracy, Recall, Precision
101+
self.fitness = (-1, -1, -1) # accuracy , recall, precsion
102+
103+
self.initialize(f_count)
104+
105+
def initialize(self, f_count):
106+
self.f_count = f_count
107+
self.initalize_position(f_count)
108+
self.initialize_velocity(f_count)
109+
110+
def set_data(self,data):
111+
self.df = data.copy()
112+
print(self.df.head())
113+
114+
#Initialize the positions > 0.5 is set as 1
115+
def initalize_position(self,f_count):
116+
self.pos_act = np.random.uniform(low=0, high=1, size=f_count).tolist()
117+
self.position = [1 if po > 0.5 else 0 for po in self.pos_act]
118+
119+
def initialize_velocity(self, f_count):
120+
self.velocity = np.random.uniform(low=-1, high=1, size=f_count).tolist()
121+
122+
def drop_columns(self, X):
123+
124+
for iteration, value in enumerate(self.position):
125+
if value == 0 :
126+
X_1 = X.drop(X.columns[iteration], axis = 1)
127+
return X_1
128+
129+
def classification_accuracy(self,y_actual, y_hat):
130+
TP = 0
131+
FP = 0
132+
TN = 0
133+
FN = 0
134+
135+
for i in range(len(y_hat)):
136+
if y_actual[i]==y_hat[i]==1:
137+
TP += 1
138+
if y_hat[i]==1 and y_actual[i]!=y_hat[i]:
139+
FP += 1
140+
if y_actual[i]==y_hat[i]==0:
141+
TN += 1
142+
if y_hat[i]==0 and y_actual[i]!=y_hat[i]:
143+
FN += 1
144+
145+
class_acc = float((TP+TN)) / float((TP+FP+TN+FN))
146+
147+
if TP == 0 and FN == 0 :
148+
recall = 0
149+
else:
150+
recall = float(TP) / float(TP + FN)
151+
if TP == 0 and FP == 0:
152+
precision = 0
153+
else:
154+
precision = float(TP) / float( TP + FP )
155+
156+
return (class_acc, recall, precision)
157+
158+
def process_data(self):
159+
160+
# Separate labels and features
161+
X = self.df.drop(columns=['diagnosis'])
162+
y = self.df['diagnosis']
163+
164+
X = self.drop_columns(X)
165+
166+
# Convert the M to 1 and B to 0
167+
label = LabelEncoder()
168+
y = label.fit_transform(y)
169+
y[:20]
170+
171+
# Spilt the train and test data
172+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
173+
# we used 30% test data
174+
# check the size before beginning
175+
X_train.shape, X_test.shape, y_train.shape, y_test.shape
176+
177+
# Logistic Regression
178+
LR = LogisticRegression()
179+
LR.fit(X_train, y_train)
180+
LR.score(X_train, y_train)
181+
y_pred = LR.predict(X_test)
182+
y_pred_train = LR.predict(X_train)
183+
184+
# find accuracy
185+
ac = accuracy_score(y_test, y_pred)
186+
ac_train = accuracy_score(y_train, y_pred_train)
187+
# Code for ROC_AUC curve
188+
rc = roc_auc_score(y_test, y_pred)
189+
190+
class_acc = self.classification_accuracy(y_test, y_pred)
191+
192+
self.y_actual = y_test
193+
self.y_predict = y_pred
194+
195+
return class_acc
196+
197+
# fitness check, checks accuarcy and precision and accurarcy
198+
def fitness_check(self,fitness, fit_best):
199+
is_fitness = False
200+
201+
if fitness[0] > fit_best[0] or fit_best[0] == -1:
202+
if fitness[1] >= fit_best[1] and fitness[2] >= fit_best[2]:
203+
is_fitness = True
204+
205+
return is_fitness
206+
207+
def evaluate_fitness(self):
208+
self.fitness = self.process_data()
209+
210+
if self.fitness_check(self.fitness, self.fit_best):
211+
self.pos_best = self.position.copy()
212+
self.fit_best = self.fitness
213+
214+
def update_velocity(self, pos_best_global):
215+
c1 = 1
216+
c2 = 2
217+
w = 0.5
218+
219+
for i in range(0, self.f_count):
220+
r1 = np.random.uniform(low=-1, high=1, size=1)[0]
221+
r2 = np.random.uniform(low=-1, high=1, size=1)[0]
222+
velocity_cog = c1*r1*(self.pos_best[i]-self.position[i])
223+
velocity_soc = c2*r2*(pos_best_global[i]-self.position[i])
224+
225+
self.velocity[i]=w*self.velocity[i]+velocity_cog+velocity_soc
226+
227+
def update_position(self):
228+
229+
for i in range(0, self.f_count):
230+
self.pos_act[i] = self.pos_act[i] + self.velocity[i]
231+
232+
#adjust max value
233+
234+
if self.pos_act[i] > 1 :
235+
self.pos_act[i] = 0.9
236+
237+
if self.pos_act[i] < 0 :
238+
self.pos_act[i] = 0.0
239+
240+
self.position[i] = 1 if self.pos_act[i] > 0.5 else 0
241+
242+
def print_position(self):
243+
print(self.position)
244+
245+
def print_velocity(self):
246+
print(self.velocity)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /