JingweiToo / Wrapper-Feature-Selection-Toolbox-Python

This toolbox offers 13 wrapper feature selection methods (PSO, GA, GWO, HHO, BA, WOA, and etc.) with examples. It is simple and easy to implement.
BSD 3-Clause "New" or "Revised" License
244 stars 69 forks source link

A huge bug in `error_rate` function, the evaluation is wrong. #9

Closed lichunown closed 3 years ago

lichunown commented 3 years ago

the origin code is:

    # Number of instances
    num_train = np.size(xt, 0)
    num_valid = np.size(xv, 0)
    # Define selected features
    xtrain  = xt[:, x == 1]
    ytrain  = yt.reshape(num_train)  # Solve bug
    xvalid  = xv[:, x == 1]
    yvalid  = yv.reshape(num_valid)  # Solve bug   

However, the code such as xt[:, x == 1] will not select the features. The solve bug codes are still wrong.

In face, the code should be:

    xtrain  = xt[:, np.where(x == 1)[0]]
    xvalid  = xv[:, np.where(x == 1)[0]]

    # Training
    mdl     = KNeighborsClassifier(n_neighbors = k)
    mdl.fit(xtrain, yt)
    # Prediction
    ypred   = mdl.predict(xvalid)
    acc     = np.sum(yv == ypred) / len(yv)
JingweiToo commented 3 years ago

what do you mean by "will not select features"? The #solve bug is for solving the reshape label issue but not selected features.

Your code is working well. However, I had also tested my code and it can select the features. Maybe you can show the issue in detail?

lichunown commented 3 years ago

Sorry, I‘m misunderstood. In this scene, if x.shape is [n], the code xt[:, x == 1] performed correctly.

However, if x.shape is [1, n], numpy will raise IndexError. In addition, if the x is a tensor, the mechanism seems to change. I had experienced similar bugs. So I still suggest to use np.where instead of matlib-like coding.

JingweiToo commented 3 years ago

lichunown, thank you for your advice and suggestion.