python-3.x 如何丢弃系数值最小的特征?

szqfcxe2  于 2022-12-27  发布在  Python
关注(0)|答案(1)|浏览(73)
while True and nFeatures >=3 :
    #Print co-efficients of features
    for i in range(0, nFeatures):
        print (samples.columns[i],":", coef[0][i])
    
    #Find the minimum weight among features and eliminate the feature with the smallest weight
    min = coef[0][0]
    index = 0
    for i in range(0, rfeIndex):
        if min > coef[0][i]:
            index = index + 1
            min = coef[0][i]
    if len(samples.columns) == 1:
        print ("After recursive elimination we have the", samples.columns[index], "feature with a score of:", min)
        accuracy(model, x_test, y_test)
        break
    else:
        print ("Lowest feature weight is for", samples.columns[index], "with a value of:", min)
        print ("Dropping feature", samples.columns[index])

        #Drop the feature in the 'samples' dataframe based on the lowest feature index
        samples.drop(samples.columns[index], axis = 1, inplace = True)
        y_pred = model.predict(x_test)
        accuracy(model, x_test, y_test)
        print ("\n")
        rfeIndex = rfeIndex - 1
        nFeatures = nFeatures - 1

其结果是:x1c 0d1x我不知道为什么它调用了错误的索引,但值是正确的。结果必须是PAY_AMT5,因为它的值是-1.593807954123289。但在我的程序中它被称为年龄有人请帮助我

pcww981p

pcww981p1#

在我看来,这句话中有一个错误:

index = index + 1

因为"index"变量只有在找到新的最小值时才加1。与其给"index"加1,不如将"indexn"变量等同于变量"i"。只需将增加"index"变量的行更改为下一行:

index = i

还有一件事在python中,不欢迎创建名称与某些函数匹配的变量,例如"min",因为这会导致调用此函数时出错。2因此,值得将变量"min"的名称更改为,例如"min_coeff"。

相关问题