Beispiel #1
0
class ImageFeederTree:
    '''
	This class is used to feed the images of faces to the classifier,
	and getting back the predictions.
	This should be used after the classifier has been trained.
	'''
    def __init__(self):
        self.tree_classifier = tree.DecisionTreeClassifier()
        self.tree_obj = TreeClassifier()
        self.predicted_results = []
        self.array_of_images = []

    def convertRawDataToTestData(self, list_of_images):
        '''
		This module is used to convert the list of images,
		into a format that can be accepted by the classifier.
		This should be called before getPrediction().

		#Parameters: 'list_of_images' is the List of images for which predictions are needed.
		#Return: None

		'''
        length_of_list = len(list_of_images)
        for i in range(length_of_list):
            img = list_of_images[i]
            img = np.asarray(img)
            img = img.flatten()
            self.array_of_images.append(img)
        self.array_of_images = np.asarray(self.array_of_images)

    def getPrediction(self, pickle_name):
        '''
		This module loads the classifier and,
		predicts the label of the images passed to it.

		#Parameters: 'pickle_name' is the pickle file to be loaded
		#Return: Array of predictions
		'''
        self.tree_classifier = self.tree_obj.loadClassifier(pickle_name)
        self.predicted_results = self.tree_classifier.predict(
            self.array_of_images)
        return self.predicted_results
def testKNNClassifier(trainingSet_data, trainingSet_target, testSet_data, testSet_target):
    classChoice = int(input("1. Knn Class\n 2. Decision Tree \n >"))

    if classChoice == 2:
        myClassifyer = TreeClassifier()
    else:
        myClassifyer = Classifier()


    myClassifyer.train(trainingSet_data, trainingSet_target)

    # predictions of classifier
    predictionSet = myClassifyer.predict(testSet_data) # check

    classifier = KNeighborsClassifier(n_neighbors=3)
    classifier.fit(trainingSet_data, trainingSet_target)
    predictions = classifier.predict(testSet_data)

    # calculate percent correct
    numCorrect = 0
    numGivenCorrect = 0
    for x in range(0, len(testSet_target)):
        if predictionSet[x] == testSet_target[x]:
            numCorrect += 1
        if predictions[x] == testSet_target[x]:
            numGivenCorrect += 1

    percentCorrect = numCorrect / float(len(predictionSet))
    percentGivenCorrect = numGivenCorrect / float(len(predictions))

    print ("Using my kNN percent correct is :")
    print("%.2f " %percentCorrect)
    print ("or ", numCorrect, " /", len(predictionSet))

    # for given classifier
    print ("Given kNN percent correct is :")
    print ("%.2f " %percentGivenCorrect)
    print ("or ", numGivenCorrect, " /", len(predictions))

    return
Beispiel #3
0
 def __init__(self):
     self.tree_classifier = tree.DecisionTreeClassifier()
     self.tree_obj = TreeClassifier()
     self.predicted_results = []
     self.array_of_images = []
def runTreeTest():
    data = []
    targets = []
    labels = []
    choice = int(input("Choose Set: \n 1. Votes \n 2. Lenses \n 3. Iris \n> "))
    if (choice == 3):
        iris = datasets.load_iris()
        data = iris.data
        targets = iris.target
        data = convertToNom(data)
        labels = ["SepalL", "SepalW", "PetalL", "PetalW"]
    elif (choice == 2):
        lensesData = readLensData("lenses.csv")
        np_data = np.array(lensesData)
        data = np_data[:, :(len(lensesData[0]) - 1)]
        targets = np_data[:, (len(lensesData[0]) - 1)] # last column
        labels = ["Age", "Precription", "Astigmatic", "TearProduction"]
    else:
        votesData = readVotesData("votes.csv")
        np_data = np.array(votesData)
        data = np_data[:, -(len(votesData[0]) - 1):] #5 digits from the left
        targets = np_data[:, 0] # first column republican1 or democrate 0
        labels = ["handicapped-infants", "water-project-cost-sharing", "adoption-of-the-budget-resolution",
                    "physician-fee-freeze", "el-salvador-aid", "religious-groups-in-schools", "anti-satellite-test-ban",
                    "aid-to-nicaraguan-contras","mx-missile","immigration","synfuels-corporation-cutback","education-spending",
                    "superfund-right-to-sue","crime","duty-free-exports","export-administration-act-south-africa"]


     # randomize
    np.unique(targets)
    np.random.seed(0)

    # 3 randomize the index
    indices = np.random.permutation(len(data))

    testNum = int(len(data) * 0.3)  # 70% train, 30% test

    trainingSet_data = data[indices[:-int(testNum)]]
    trainingSet_target = targets[indices[:-int(testNum)]]

    testSet_data = data[indices[-int(testNum):]]
    testSet_target = targets[indices[-int(testNum):]]

    train_classes = []
    test_classes = []
    # convert to arrays
    for t in trainingSet_target:
        train_classes.append(t)
    for t in testSet_target:
        test_classes.append(t)

    #print("Train Data:\n", trainingSet_data, train_classes)
    #print("Test Data:\n", testSet_data, test_classes)

    classifier = TreeClassifier()
    train_labels = []
    train_labels = list(labels)
    classifier.train(trainingSet_data, train_classes, labels)

    myPredictions = classifier.predict(testSet_data, train_labels)

    # calculate percent correct
    numCorrect = 0
    numGivenCorrect = 0
    for x in range(0, len(testSet_target)):
        if myPredictions[x] == test_classes[x]:
            numCorrect += 1
        #if predictionSet[x] == testSet_target[x]:
         #   numGivenCorrect += 1

    percentCorrect = numCorrect / float(len(test_classes))
    #percentGivenCorrect = numGivenCorrect / float(len(predictions))

    print ("Using my decision tree :")
    print("%.2f " %percentCorrect)
    print ("or ", numCorrect, " /", len(test_classes))

    return