机器学习实战:逻辑回归(3)-Sklearn实现病马死亡率预测

from sklearn.linear_model import LogisticRegression

"""
函数说明:使用Sklearn构建Logistic回归分类器
Parameters:
    无
Returns:
    无
"""
def colicSklearn():
    frTrain = open('horseColicTraining.txt','r')
    frTest = open('horseColicTest.txt','r')
    trainingSet = []; trainingLabels = []
    testSet = []; testLabels = []
    for line in frTrain.readlines():
        currentLine = line.strip().split('\t')
        lineArray = []
        for i in range(len(currentLine)-1):
            lineArray.append(float(currentLine[i]))
        trainingSet.append(lineArray)
        trainingLabels.append(float(currentLine[-1]))

    for line in frTest.readlines():
        currentLine = line.strip().split('\t')
        lineArray = []
        for i in range(len(currentLine)-1):
            lineArray.append(float(currentLine[i]))
        testSet.append(lineArray)
        testLabels.append(float(currentLine[-1]))
    classifier = LogisticRegression(solver='liblinear',max_iter=10).fit(trainingSet,trainingLabels)
    test_accuarcy = classifier.score(testSet,testLabels)*100
    print('正确率:%f%%'%test_accuarcy)
colicSklearn()

你可能感兴趣的:(机器学习实战,sklearn,机器学习,逻辑回归)