Search code examples
pythonfor-loopcomparisongaussian

comparison of standard deviation between various features


hi i'm kinda new on python and i'm trying to create a code that compares standard deviation of different feature in order to extract the smallest one, i'm not sure about the code i've made

minstd=0;
for feature in range(13):
var = np.var(trainx[trainy==1,feature])
std = np.sqrt(var) # deviazione standard
a = std(feature)-std(feature+1)
if a>0
 minstd=std(feature);
else 
 minstd=std(feature+1);
minstd

i also got this error

File "<ipython-input-18-44801ce3407e>", line 6
if a>0
      ^SyntaxError: invalid syntax

can someone explain the problem?


Solution

  • Sorry, but I have to say what you need is some basic python grammar.

    1. In python, you need not add a ; at the end of the line
    2. In python, the function definition with a : behind the bracket (as well as for some control flow keywords like for, if...;
    3. In python, the layer of the code are represented by indent (rather than {} in C or C++) Your code should be like this:
    minstd=0
    for feature in range(13):
        var = np.var(trainx[trainy==1,feature])
        std = np.sqrt(var) # deviazione standard
        a = std(feature)-std(feature+1)
        if a>0:
            minstd=std(feature);
        else:
            minstd=std(feature+1);
    minstd
    ``