# 遗传算法进行特征选择,遗传算法：特征选择算法的适应度函数  I have data set n x m where there are n observations and each observation consists of m values for m attributes. Each observation has also observed result assigned to it. m is big, too big for my task. I am trying to find a best and smallest subset of m attributes that still represents the whole dataset quite well, so that I could use only these attributes for teaching a neural network.

I want to use genetic algorithm for this. The problem is the fittness function. It should tell how well the generated model (subset of attributes) still reflects the original data. And I don't know how to evaluate certain subset of attributes against the whole set.Of course I could use the neural network(that will later use this selected data anyway) for checking how good the subset is - the smaller the error, the better the subset. BUT, this takes a looot of time in my case and I do not want to use this solution. I am looking for some other way that would preferably operate only on the data set.

What I thought about was: having subset S (found by genetic algorithm), trim data set so that it contains values only for subset S and check how many observations in this data ser are no longer distinguishable (have same values for same attributes) while having different result values. The bigger the number is, the worse subset it is. But this seems to me like a bit too computationally exhausting.

Are there any other ways to evaluate how well a subset of attributes still represents the whole data set?

This cost function should do what you want: sum the factor loadings that correspond to the features comprising each subset.

The higher that sum, the greater the share of variability in the response variable that is explained with just those features. If i understand the OP, this cost function is a faithful translation of "represents the whole set quite well" from the OP.

Reducing to code is straightforward:

Calculate the covariance matrix of your dataset (first remove thecolumn that holds the response variable, i.e., probably the lastone). If your dataset is m x n (columns x rows), then thiscovariance matrix will be n x n, with "1"s down the main diagonal.

Next, perform an eigenvalue decomposition on this covariancematrix; this will give you the proportion of the total variabilityin the response variable, contributed by that eigenvalue (eacheigenvalue corresponds to a feature, or column). [Note,singular-value decomposition (SVD) is often used for this step, butit's unnecessary--an eigenvalue decomposition is much simpler, andalways does the job as long as your matrix is square, whichcovariance matrices always are].

Your genetic algorithm will, at each iteration, return a set ofcandidate solutions (features subsets, in your case). The next taskin GA, or any combinatorial optimization, is to rank those candiatesolutions by their cost function score. In your case, the costfunction is a simple summation of the eigenvalue proportion for eachfeature in that subset. (I guess you would want to scale/normalizethat calculation so that the higher numbers are the least fitthough.)

A sample calculation (using python + NumPy):

>>> # there are many ways to do an eigenvalue decomp, this is just one way>>> import numpy as NP>>> import numpy.linalg as LA>>> # calculate covariance matrix of the data set (leaving out response variable column)>>> C = NP.corrcoef(d3, rowvar=0)>>> C.shape (4, 4)>>> C array([[ 1., -0.11,0.87,0.82],[-0.11,1., -0.42, -0.36],[ 0.87, -0.42,1.,0.96],[ 0.82, -0.36,0.96,1.]])>>> # now calculate eigenvalues & eivenvectors of the covariance matrix:>>> eva, evc = LA.eig(C)>>> # now just get value proprtions of each eigenvalue:>>> # first, sort the eigenvalues, highest to lowest:>>> eva1 = NP.sort(eva)[::-1]>>> # get value proportion of each eigenvalue:>>> eva2 = NP.cumsum(eva1/NP.sum(eva1)) # "cumsum" is just cumulative sum>>> title1 = "ev value proportion">>> print( "{0}".format("-"*len(title1)) )------------------->>> for row in q :print("{0:1d} {1:3f} {2:3f}".format(int(row), row, row)) ev valueproportion 1 2.91 0.727 2 0.92 0.953 3 0.14 0.995 4 0.02 1.000

so it's the third column of values just above (one for each feature) that are summed (selectively, depending on which features are present in a given subset you are evaluating with the cost function).