def classifyNB(vec2Classify, n_name): trainMatrix, trainCategory, allIds = getModelByGroup( n_name, "dispersed", trainModel, originModel["ids"], groupNum, testPercentage) model = tree.DecisionTreeClassifier() model.fit(trainMatrix, trainCategory) predicted = model.predict(vec2Classify) return predicted[0]
def classifyNB(vec2Classify, n_name): dating_data_mat_linear, testCategory, allIds = getModelByGroup( n_name, "dispersed", trainModel, originModel["ids"], groupNum, testPercentage) norm_mat_linear = z_score_norm(dating_data_mat_linear) knn_classifier = KNeighborsClassifier() knn_classifier.fit(norm_mat_linear, testCategory) predicted = knn_classifier.predict(vec2Classify) return predicted[0]
def dating_class_test(n_name): print("\ngroup name is " + n_name) testMatrix, testCategory, allIds = getModelByGroup(n_name, "test", trainModel, originModel["ids"], groupNum, testPercentage) right_count = 0.0 for key, line in enumerate(testMatrix): cls = classifyNB([line], n_name) actual = testCategory[key] notice = "" if (cls == actual): right_count += 1.0 if (cls != actual): notice = "fail" cha = cls - actual too_many = "" pid = 0 if len(allIds) > 0: pid = allIds[key] if cha >= errorMoney or cha <= -errorMoney: too_many = "many" print( "pid is %s, the classifier came back with: %s, the real answer is: %s %s %s" % (pid, cls, actual, notice, too_many)) #print key,line,cls rate = right_count / len(testCategory) print("the total right rate is: %f" % (rate)) print("error num is: %f" % int((len(testCategory) - right_count))) print("right num is: %f" % int(right_count)) return rate