I want to classify my data with SVM with optimized features using PSO. can any one help me to define costfunction(fitness function or optimize function) for feature selection. Currently i have written following cost function.
1. i am confused what will be argument of cost function. My problem is to minimize number of features for svm classification .
def fitness_function(new_matrix):
svc_linear = svm.SVC(kernel='linear', C=1)
svc_linear.fit(new_matrix,y_train)
predicted= svc_linear.predict(X_test)
cnf_matrix = confusion_matrix(y_test, predicted)
FP=cnf_matrix.sum(axis=0)-np.diag(cnf_matrix)
FN=cnf_matrix.sum(axis=1)-np.diag(cnf_matrix)
TP=np.diag(cnf_matrix)
TN=cnf_matrix.sum()-(FP+FN+TP)
Error_rate=(FP+FN)/(FP+FN+TP+TN)
fitness=Error_rate
return(fitness)