Ejemplo n.º 1
0
def lenseTest():
    fr = open("lenses.txt")
    lenses = [inst.strip().split("\t") for inst in fr.readlines() ]
    lenseLabels = ['age', 'prescript', 'astigmatic', 'tearRate']
    lensesTree = createTree(lenses, lenseLabels[:])
    print lensesTree

    import treeplotter
    treeplotter.createPlot(lensesTree)
Ejemplo n.º 2
0
                nazwaKlasy = klasyfikuj(drugi[key], etykiety, test)
            else:
                nazwaKlasy = drugi[key]
    return nazwaKlasy


def zapiszDrzewo(wejscie, nazwaPliku):
    import pickle
    fw = open(nazwaPliku, 'w')
    pickle.dump(wejscie, fw)  #zapis do pliku
    fw.close()


def otworzDrzewo(nazwaPliku):
    import pickle
    fr = open(nazwaPliku)
    return pickle.load(fr)  #odczyt z pliku


myDat = []
myDat, etykiety = utworzZbiorDanych()
print(myDat)
print(liczenieEntropii(myDat))
print(wyborNajlepszejCechy(myDat))
myTree = stworzDrzewo(myDat, etykiety)
print(myTree)
import treeplotter
print(treeplotter.getNumLeafs(myTree))
print(treeplotter.getTreeDepth(myTree))
treeplotter.createPlot(myTree)
Ejemplo n.º 3
0
Returns:
    lensesTree: 预测隐形眼镜决策树
'''


def contactLensesType():
    #fr = open('lenses.txt')
    with open('lenses.txt') as fr:
        lenses = [inst.strip().split('\t') for inst in fr.readlines()]
        lensesLabels = ['age', 'prescript', 'astigmatic', 'teatRate']
        lensesTree = createTree(lenses, lensesLabels)
    return lensesTree


if __name__ == '__main__':
    myDat, labels = createDataSet()
    print(calcShannonEnt(myDat))
    print(splitDataSet(myDat, 0, 1))
    print(chooseBestFeatureToSplit(myDat))
    print(createTree(myDat, labels))

    import treeplotter
    myTree = treeplotter.retrieveTree(0)
    print(classify(myTree, labels, [1, 1]))

    storeTree(myTree, 'classifierStorge.txt')
    print(grabTree('classifierStorge.txt'))

    print(contactLensesType())
    treeplotter.createPlot(contactLensesType())
Ejemplo n.º 4
0
def classify(inputTree, featLabels, testVec):
    firstStr = inputTree.keys()[0]
    secondDict = inputTree[firstStr]
    featIndex = featLabels.index(firstStr)
    key = testVec[featIndex]
    valueOfFeat = secondDict[key]
    if isinstance(valueOfFeat, dict):
        classLabel = classify(valueOfFeat, featLabels, testVec)
    else:
        classLabel = valueOfFeat
    return classLabel


def storeTree(inputTree, filename):
    import pickle
    fw = open(filename, 'w')
    pickle.dump(inputTree, fw)
    fw.close()


def grabTree(filename):
    import pickle
    fr = open(filename)
    return pickle.load(fr)


ds, l = createDataSet()
tree = createTree(ds, l)
treeplotter.createPlot(tree)
Ejemplo n.º 5
0
def grabTree(filename):
    import pickle
    fr = open(filename)
    return pickle.load(fr)


# # yun xing
# mydata,mylabels=creatDataSet()
# print mydata,mylabels
# mydata[0][-1]='maybe'
# result=calcShannonEnt(mydata)
# result=spiltDataSet(mydata,0,1)
# mytree=createTree(mydata,mylabels)
# print mytree
# mytree=treeplotter.retrieveTree(0)
# result=classify(mytree,mylabels,[1,0])
# print result
# def add(a,b):
#     a+=1
#     b+=2
#     return a,b
# print add(1,2)

# storeTree(mytree,'classifierStorage.txt')
# print grabTree('classifierStorage.txt')
fr = open('lenses.txt')
lenses = [inst.strip().split('\t') for inst in fr.readlines()]
lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate']
lensesTree = createTree(lenses, lensesLabels)
treeplotter.createPlot(lensesTree)
Ejemplo n.º 6
0
    myTree = {}
    myTree = {bestFeatLabel: {}}
    del (labels[bestFeat])
    featValues = [example[bestFeat] for example in dataset]
    uniqueVals = set(featValues)
    for value in uniqueVals:
        subLabels = labels[:]
        myTree[bestFeatLabel][value] = createTree(split_dataset.splitDataSet(dataset, bestFeat, value), subLabels)

    return myTree


if __name__ == "__main__":
    dataset = [
        ["omer", "demircan", "kocaeli"],
        ["eray", "arslan", "sakarya"],
        ["ss", "dd", "izmir"],
        ["yunus", "kocyigit", "kocaeli"],
        ["omer", "demircan", "kocaeli"],
        ["eray", "arslan", "sakarya"],
        ["ss", "dd", "izmir"],
        ["yunus", "kocyigit", "kocaeli"],
        ["omer", "demircan", "kocaeli"],
        ["eray", "arslan", "sakarya"],
        ["ss", "dd", "izmir"],
        ["yunus", "kocyigit", "kocaeli"],
    ]
    tree = createTree(dataset, ["ad", "soyad", "sehir"])
    myTree = treeplotter.retrieveTree(0)
    treeplotter.createPlot(myTree)