本文整理汇总了Python中classifier.Classifier.test方法的典型用法代码示例。如果您正苦于以下问题:Python Classifier.test方法的具体用法?Python Classifier.test怎么用?Python Classifier.test使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类classifier.Classifier
的用法示例。
在下文中一共展示了Classifier.test方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def run(self):
"""
Function: Run
-------------
This function will evaluate your solution! You do not need to
write any code in this file, however you SHOULD understand this
function!
"""
print "Running the full pipeline!"
K=25
trainImages = util.loadTrainImages()[:1000]
testImages = util.loadTestImages()
classifier = Classifier()
print 'Training..........'
classifier.train(trainImages, K)
trainPredictions = classifier.test(trainImages)
trainAccuracy = self.evaluate(trainPredictions, trainImages)
print 'Testing...........'
testPredictions = classifier.test(testImages)
testAccuracy = self.evaluate(testPredictions, testImages)
print 'All done. Here is your summary:'
self.reportAccuracy(trainAccuracy, 'Train Accuracy')
self.reportAccuracy(testAccuracy, 'Test Accuracy')
示例2: runNeuralNetwork
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def runNeuralNetwork(train, test, batchSize, classNum, hLayer=None, mode=None, momentumFactor=0.0):
"""
A function that call the the classifier to train a learning model.
Args:
train: training examples (numpy)
test: testing examples (numpy)
batchSize: the number of training example for each iteration
classNum: the number of classes
hLayer: number of the hidden layer nodes (list)
mode: weight initializing mode
momentumFactor: momentum factor
"""
print ""
print "Neural Network =============================="
print " - number of hidden layer nodes:",
if hLayer is not None:
print hLayer
else:
print " default (one hidden layer with node number = 2 * feature number)"
print " - weight initialization mode:",
if mode is not None:
print mode
else:
print "default"
print " - momentum factor", momentumFactor
nn = Classifier("neural_network", hidden_layer=hLayer, weightInitMode=mode, momentumFactor=momentumFactor)
nn.train(train, test, classNum, batchSize)
nn.test(test, "test")
示例3: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def main():
parser = argparse.ArgumentParser(description='Clasificador de musica.\nToma los datos de entrenamiento de un archivo y utiliza algoritmos evolutivos para crear y mejorar las reglas de clasificación.')
parser.add_argument('-d', '--data', help='Archivo donde se encuentra la información fuente para el clasificador.')
args = vars(parser.parse_args())
"""
Los valores default son:
tamaño discretizacion - 100
poblacion de generacion - 10
min fitness para terminar - 0.9
numero a seleccionar - 4
porcentaje de mutacion - 0.05
maximo de generaciones - 10000
tipo de seleccion - ROULETTE_WHEEL_SELECTION
"""
defaults = [100, 10, 0.9, 4, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION]
classifier = Classifier(args['data'], discrete_intervals=defaults[0], size_rule_generation=defaults[1], filter_list=["skewness", "spectral_rolloff", "energy", "sv", "spread", "centroid", "obsi", "kurtosis"], log_results=True)
start = time.clock()
best_results = classifier.train(req_min_fitness=defaults[2], gen_select=defaults[3], mutation_prob=defaults[4], limit_generations=defaults[5])
duration = (time.clock() - start)*1000
print "Duration\t", duration, "ms"
print "Training endend."
print "Best results:", ', '.join([str(key) + " fitness: " + str(value['fitness']) for key, value in best_results.items()])
print "Testing:"
classifier.test()
print "Testing ended."
示例4: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def main(c = "decision_tree", option = "IG", dataset = "iris", ratio = 0.8):
classifier_types = {0: "decision_tree", 1: "naive_bayes", 2: "neural_net"}
options = {0:["IG", "IGR"], 1:["normal"], 2:["shallow", "medium"]}
ratio = float(ratio)
if dataset == "monks":
(training, test) = load_data.load_monks(ratio)
elif dataset == "congress":
(training, test) = load_data.load_congress_data(ratio)
elif dataset == "iris":
(training, test) = load_data.load_iris(ratio)
else:
print "Error: Cannot find dataset name."
return
print "Training... Please hold."
# classifier_types = {0: "decision_tree", 2: "neural_net"}
# options = {0:["IG", "IGR"], 2:["shallow", "medium"]}
# (training, test) = load_data.load_iris(0.8)
# nn_classifier = Classifier(classifier_type="neural_net", option = "medium")
# nn_classifier.train(training)
# nn_classifier.test(test)
# print test
# (training, test) = load_data.load_congress_data(0.8)
# print test
# (training, test) = load_data.load_monks(1)
# print test
# (training, test) = load_data.load_iris(0.8)
# print training
# "option = IG/IGR"
# dt_classifier = Classifier(classifier_type="decision_tree", weights=[], option="IG")
# dt_classifier.train(training)
# dt_classifier.test(test)
# for i, c in classifier_types.iteritems():
# for option in options[i]:
print " "
print "================================================================="
print "Dataset = ", dataset
print "Classifier = ", c
print "Option = ", option
classifier = Classifier(classifier_type=c, weights = [], option = option)
classifier.train(training)
classifier.test(test)
print "================================================================="
print " "
# option value could be either shallow(3 layers) or medium(5)
# nn_classifier = Classifier(classifier_type="neural_net", option = "medium")
# nn_classifier.train(training)
# nn_classifier.test(test)
return
开发者ID:lb5160482,项目名称:Machine-Learning-Classifier-Artificial-Intelligence,代码行数:56,代码来源:train_and_test.py
示例5: trainNtest
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def trainNtest(args):
classifierType = ["decision_tree", "naive_bayes", "neural_network"]
data_set = ["congress", "monk", "iris"]
data = ""
if len(args) == 4:
if args[0][3:] == "congress":
data = ld.load_congress_data(int(args[1][3:]) / 100.0)
num_input = 16
num_output = 2
elif args[0][3:] == "monk":
data = ld.load_monks(int(args[1]))
num_input = 6
num_output = 2
elif args[0][3:] == "iris":
data = ld.load_iris(int(args[1][3:]) / 100.0)
num_input = 4
num_output = 3
else:
print "INVALID DATA NAME"
return
method_num = int(args[2][3])
kwargs = {}
if method_num == 0 or method_num == 2:
kwargs[1] = args[2][5]
kwargs[2] = args[2][7]
classifier = Classifier(classifierType[int(args[2][3])], one=args[2][5], two=args[2][7], num_input=num_input, num_output=num_output)
else:
classifier = Classifier(classifierType[int(args[2][3])])
else:
print "ERROR: NEED 4 PARAMETERS"
return
#pdb.set_trace()
#nb = Naive_Bayes("naive_bayes")
#classifier = Classifier(classifierType[1])
#data = ld.load_congress_data(.85)
#data = ld.load_iris(.70)
#pdb.set_trace()
classifier.train(data[0])
if args[3] == "-test":
classifier.test(data[1])
else:
classifier.test(data[0])
示例6: test
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def test(args):
test_performance(args, 5)
return
#return
options = [
[100, 10, 0.9, 4, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION], # discrete_intervals, size_rule_generation, req_min_fitness, gen_select, limit_generations
[1000, 10, 0.9, 4, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 20, 0.9, 4, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 5, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 10, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 10, 0.9, 6, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[200, 50, 0.9, 10, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[300, 10, 0.9, 4, 0.1, 10000, selection.ROULETTE_WHEEL_SELECTION],
[500, 15, 0.9, 2, 0.005, 10000, selection.ROULETTE_WHEEL_SELECTION],
[50, 20, 0.9, 4, 0.1, 10000, selection.ROULETTE_WHEEL_SELECTION]
]
#prueba Tamaño de población
options = [
[100, 5, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 10, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 15, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 20, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 30, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 50, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION]
]
#prueba Proceso de seleccion
options = [
[100, 10, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
[100, 10, 0.9, 2, 0.05, 10000, selection.RANK_SELECTION],
[100, 10, 0.9, 2, 0.05, 10000, selection.TOURNAMENT_SELECTION]
]
options = [
[100, 10, 0.9, 2, 0.05, 10000, selection.ROULETTE_WHEEL_SELECTION],
]
average_multiple_runs(30, options, args)
test_combinations(args)
for num, option in enumerate(options):
print "Option num:", num, ", val:", option
classifier = Classifier(args['data'], discrete_intervals=option[0], size_rule_generation=option[1], filter_list=["skewness", "spectral_rolloff", "energy", "sv", "spread", "centroid", "obsi", "kurtosis"], log_results=True)
best_results = classifier.train(req_min_fitness=option[2], gen_select=option[3], mutation_prob=option[4], limit_generations=option[5])
print "Testing"
classifier.test()
# classifier.guess_genre([7.53659769442,1389.49121537,0.0166588959174,0.355062895642,1480.75635175,769.172547276,3.47303203307,69.8220939453])
print "Training ended\nFinal fitness:", best_results
示例7: average_multiple_runs
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def average_multiple_runs(num_runs, options, args):
for num, option in enumerate(options):
print "Running", num_runs, "iterations with options:", option
list_best_results = []
list_test_results = []
list_correct_results = []
for i in range(num_runs):
print "Running #" + str(i + 1)
classifier = Classifier(args['data'], discrete_intervals=option[0], size_rule_generation=option[1], filter_list=["skewness", "spectral_rolloff", "energy", "sv", "spread", "centroid", "obsi", "kurtosis"], log_results=False)
best_results = classifier.train(req_min_fitness=option[2], gen_select=option[3], mutation_prob=option[4], limit_generations=option[5], selection_type=option[6])
test_results, correct_results = classifier.test()
list_best_results.append(best_results)
list_test_results.append(test_results)
list_correct_results.append(correct_results)
print "Results for option: ", option
print "run\ttype\tgen\tfitness"
for i, results in enumerate(list_best_results):
for rule, result in results.items():
print str(i + 1) + "\t" + rule[:7] + "\t" + str(result["generation"]) + "\t" + str(result["fitness"])
print "run\ttype\tavg correct rules"
for i, results in enumerate(list_test_results):
for avg_map in results:
print str(i + 1) + "\t" + avg_map.keys()[0][:7] + "\t" + str(avg_map[avg_map.keys()[0]])
print "run\ttype\tavg correct results"
for i, results in enumerate(list_correct_results):
for avg_map in results:
print str(i + 1) + "\t" + avg_map.keys()[0][:7] + "\t" + str(avg_map[avg_map.keys()[0]])
示例8: run
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def run(self):
print "Running the full pipeline!"
K=25
trainImages = util.loadTrainImages()[:1000]
testImages = util.loadTestImages()
classifier = Classifier()
print 'Training..........'
classifier.train(trainImages, K)
trainPredictions = classifier.test(trainImages)
trainAccuracy = self.evaluate(trainPredictions, trainImages)
print 'Testing...........'
testPredictions = classifier.test(testImages)
testAccuracy = self.evaluate(testPredictions, testImages)
print 'All done. Here is your summary:'
self.reportAccuracy(trainAccuracy, 'Train Accuracy')
self.reportAccuracy(testAccuracy, 'Test Accuracy')
示例9: runDev
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def runDev(self):
print "Running in development mode"
K=5
trainImages = util.loadTrainImages()[:100]
testImages = util.loadTestImages()[:100]
classifier = Classifier()
print 'Training..........'
classifier.train(trainImages, K)
trainPredictions = classifier.test(trainImages)
trainAccuracy = self.evaluate(trainPredictions, trainImages)
print 'All done. Here is your summary:'
self.reportAccuracy(trainAccuracy, 'Train Accuracy')
示例10: runDev
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def runDev(self):
"""
Function: runDev
-------------
This function will run the full pipeline in development mode.
I.e. it will use only 10 centroids and 100 images.
"""
print "Running in development mode"
K=5
trainImages = util.loadTrainImages()[:100]
testImages = util.loadTestImages()[:100]
classifier = Classifier()
print 'Training..........'
classifier.train(trainImages, K)
trainPredictions = classifier.test(trainImages)
trainAccuracy = self.evaluate(trainPredictions, trainImages)
print 'All done. Here is your summary:'
self.reportAccuracy(trainAccuracy, 'Train Accuracy')
示例11: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
def main(is_interactive=True, k=64, des_option=constants.ORB_FEAT_OPTION, svm_kernel=cv2.SVM_LINEAR):
if not is_interactive:
experiment_start = time.time()
# Check for the dataset of images
if not os.path.exists(constants.DATASET_PATH):
print("Dataset not found, please copy one.")
return
dataset = Dataset(constants.DATASET_PATH)
dataset.generate_sets()
# Check for the directory where stores generated files
if not os.path.exists(constants.FILES_DIR_NAME):
os.makedirs(constants.FILES_DIR_NAME)
if is_interactive:
des_option = input("Enter [1] for using ORB features or [2] to use SIFT features.\n")
k = input("Enter the number of cluster centers you want for the codebook.\n")
svm_option = input("Enter [1] for using SVM kernel Linear or [2] to use RBF.\n")
svm_kernel = cv2.SVM_LINEAR if svm_option == 1 else cv2.SVM_RBF
des_name = constants.ORB_FEAT_NAME if des_option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME
log = Log(k, des_name, svm_kernel)
codebook_filename = filenames.codebook(k, des_name)
if is_interactive:
codebook_option = input("Enter [1] for generating a new codebook or [2] to load one.\n")
else:
codebook_option = constants.GENERATE_OPTION
if codebook_option == constants.GENERATE_OPTION:
# Calculate all the training descriptors to generate the codebook
start = time.time()
des = descriptors.all_descriptors(dataset, dataset.get_train_set(), des_option)
end = time.time()
log.train_des_time(end - start)
# Generates the codebook using K Means
print("Generating a codebook using K-Means with k={0}".format(k))
start = time.time()
codebook = descriptors.gen_codebook(dataset, des, k)
end = time.time()
log.codebook_time(end - start)
# Stores the codebook in a file
utils.save(codebook_filename, codebook)
print("Codebook saved in {0}".format(codebook_filename))
else:
# Load a codebook from a file
print("Loading codebook ...")
codebook = utils.load(codebook_filename)
print("Codebook with shape = {0} loaded.".format(codebook.shape))
# Train and test the dataset
classifier = Classifier(dataset, log)
svm = classifier.train(svm_kernel, codebook, des_option=des_option, is_interactive=is_interactive)
print("Training ready. Now beginning with testing")
result, labels = classifier.test(codebook, svm, des_option=des_option, is_interactive=is_interactive)
# Store the results from the test
classes = dataset.get_classes()
log.classes(classes)
log.classes_counts(dataset.get_classes_counts())
result_filename = filenames.result(k, des_name, svm_kernel)
test_count = len(dataset.get_test_set()[0])
result_matrix = np.reshape(result, (len(classes), test_count))
utils.save_csv(result_filename, result_matrix)
# Create a confusion matrix
confusion_matrix = np.zeros((len(classes), len(classes)), dtype=np.uint32)
for i in range(len(result)):
predicted_id = int(result[i])
real_id = int(labels[i])
confusion_matrix[real_id][predicted_id] += 1
print("Confusion Matrix =\n{0}".format(confusion_matrix))
log.confusion_matrix(confusion_matrix)
log.save()
print("Log saved on {0}.".format(filenames.log(k, des_name, svm_kernel)))
if not is_interactive:
experiment_end = time.time()
elapsed_time = utils.humanize_time(experiment_end - experiment_start)
print("Total time during the experiment was {0}".format(elapsed_time))
else:
# Show a plot of the confusion matrix on interactive mode
utils.show_conf_mat(confusion_matrix)
raw_input("Press [Enter] to exit ...")
示例12: Classifier
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import test [as 别名]
# classifier = Classifier(c, feature_set)
# classifier.plot_performance()
# classifier.plot_performnace_ngram(limit=6)
# classifier.train()
# classifier.test()
i = 0.2
accuracies = []
fscores = []
cs = []
while i <= 5:
c = SklearnClassifier(Pipeline([('clf', LinearSVC(C=i))]))
classifier = Classifier(c, feature_set)
classifier.train()
accuracy, fscore = classifier.test()
accuracies.append(accuracy)
fscores.append(fscore)
cs.append(i)
i += 0.2
print i
plt.plot(cs, accuracies, label='Accuracy', linewidth=2)
plt.plot(cs, fscores, label='F1-score', linewidth=2)
plt.xlabel('C')
plt.legend(loc='lower right')
plt.show()
t = 'a'
while t != '':
t = raw_input('>')