本文整理汇总了Python中classifier.Classifier.train方法的典型用法代码示例。如果您正苦于以下问题:Python Classifier.train方法的具体用法?Python Classifier.train怎么用?Python Classifier.train使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类classifier.Classifier
的用法示例。
在下文中一共展示了Classifier.train方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: runNeuralNetwork
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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")
示例2: run
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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')
示例3: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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
示例4: test_performance
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def test_performance(args, num_runs):
#Features:
features = ["skewness", "spectral_rolloff", "energy", "sv", "spread", "centroid", "zcr", "obsi", "kurtosis"]
option = [100, 10, 0.9, 2, 0.05, 1000, selection.ROULETTE_WHEEL_SELECTION]
for i in range(1, len(features) + 1):
print "Num of features:", i
for num_run in range(num_runs):
classifier = Classifier(args['data'], discrete_intervals=option[0], size_rule_generation=option[1], filter_list=features[:i], log_results=False)
start = time.clock()
classifier.train(req_min_fitness=option[2], gen_select=option[3], mutation_prob=option[4], limit_generations=option[5], selection_type=option[6])
duration = (time.clock() - start)*1000
print num_run, "\t", duration
示例5: trainNtest
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def main():
args = parser.parse_args()
data_json = read_dataset(args.data)
processor = TextProcessor()
classifier = Classifier(processor)
classifier.train(data_json)
serialized_classifier = classifier.dump()
ensure_directory(args.output)
with open(args.output, 'w') as f:
f.write(serialized_classifier)
f.write(os.linesep)
示例7: average_multiple_runs
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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: runDev
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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')
示例9: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [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."
示例10: _learn
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def _learn(sample):
_extname = sample.get('extname')
_filename = sample.get('filename')
_langname = sample['language']
if _extname:
if _extname not in db['extnames'][_langname]:
db['extnames'][_langname].append(_extname)
db['extnames'][_langname].sort()
if _filename:
db['filenames'][_langname].append(_filename)
db['filenames'][_langname].sort()
data = open(sample['path']).read()
Classifier.train(db, _langname, data)
示例11: main
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def main():
args = parser.parse_args()
data_json = read_dataset(args.data)
random.shuffle(data_json)
training_set_ratio = 0.7
training_set_size = int(training_set_ratio * len(data_json) + 0.5)
training_set = data_json[:training_set_size]
test_set = data_json[training_set_size:]
processor = TextProcessor()
classifier = Classifier(processor)
classifier.train(training_set)
print classifier.dump() == Classifier.load(classifier.dump(), processor).dump()
示例12: test_classify_by_randomforest
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def test_classify_by_randomforest():
stock_d = testdata()
ti = TechnicalIndicators(stock_d)
filename = 'test_N225_randomforest.pickle'
clffile = os.path.join(os.path.dirname(
os.path.abspath(__file__)),
'..', 'clf',
filename)
if os.path.exists(clffile):
os.remove(clffile)
clf = Classifier(filename)
ti.calc_ret_index()
ret = ti.stock['ret_index']
train_X, train_y = clf.train(ret, classifier="Random Forest")
eq_(filename, os.path.basename(clf.filename))
r = round(train_X[-1][-1], 5)
expected = 1.35486
eq_(r, expected)
r = round(train_X[0][0], 5)
expected = 1.08871
eq_(r, expected)
expected = 14
r = len(train_X[0])
eq_(r, expected)
expected = 120
r = len(train_X)
eq_(r, expected)
expected = [1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 1, 0, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 0, 1, 0,
1, 1, 1, 1, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1,
1, 0, 1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 0, 1, 1, 0, 1,
1, 0, 1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
1, 1, 1, 0, 1, 1, 1, 0, 0, 1,
1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
for r, e in zip(train_y, expected):
eq_(r, e)
expected = 1
test_y = clf.classify(ret)
assert(test_y[0] == 0 or test_y[0] == 1)
if os.path.exists(clffile):
os.remove(clffile)
示例13: test_combinations
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def test_combinations(args, graph=False):
py = plotly.plotly(username='vierja', key='uzkqabvlzm', verbose=False)
options = [100, 10, 0.9, 4, 0.05, 10000]
features = ["skewness", "spectral_rolloff", "energy", "sv", "spread", "centroid", "zcr", "obsi", "kurtosis"]
electronic_y = []
classical_y = []
categories = []
print '\t'.join([feature[:2] for feature in features] + ["meta", "acou", "regg", "elec", "class"])
for i in range(1, len(features) + 1):
combinations = [list(comb) for comb in itertools.combinations(features, i)]
for comb in combinations:
comb_name = ', '.join(comb)
classifier = Classifier(args['data'], discrete_intervals=options[0], size_rule_generation=options[1], filter_list=comb)
top_fitness = classifier.train(req_min_fitness=options[2], gen_select=options[3], mutation_prob=options[4], limit_generations=options[5])
for feature in features:
if feature in comb:
sys.stdout.write("X\t")
else:
sys.stdout.write("\t")
sys.stdout.write(str(top_fitness['metal']["fitness"])[:4] + "\t")
sys.stdout.write(str(top_fitness['acoustic']["fitness"])[:4] + "\t")
sys.stdout.write(str(top_fitness['reggae']["fitness"])[:4] + "\t")
sys.stdout.write(str(top_fitness['electronic']["fitness"])[:4] + "\t")
sys.stdout.write(str(top_fitness['classical']["fitness"])[:4] + "\n")
if graph:
print "Training ended\nFinal fitness:", top_fitness
electronic_y.append(top_fitness['metal'])
classical_y.append(top_fitness['classical'])
categories.append(comb_name)
if len(categories) > 20:
electronic = {
"name": "Metal",
"x": categories,
"y": electronic_y,
"type": "bar"
}
classical = {
"name": "Classical",
"x": categories,
"y": classical_y,
"type": "bar"
}
layout = {
"barmode": "group",
'xaxis': {'type': 'combination'},
'catagories': categories
}
response = py.plot([electronic, classical], layout=layout)
print response['url']
electronic_y = []
classical_y = []
categories = []
示例14: _learn
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def _learn(sample):
_extname = sample.get("extname")
_filename = sample.get("filename")
_langname = sample["language"]
if _extname:
db["extnames"][_langname] = db["extnames"].get(_langname, [])
if _extname not in db["extnames"][_langname]:
db["extnames"][_langname].append(_extname)
db["extnames"][_langname].sort()
if _filename:
db["filenames"][_langname] = db["filenames"].get(_langname, [])
db["filenames"][_langname].append(_filename)
db["filenames"][_langname].sort()
data = open(sample["path"]).read()
Classifier.train(db, _langname, data)
示例15: trainJCfromSP
# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import train [as 别名]
def trainJCfromSP():
finalList = allArrays()
def mapFunc(x):
if x[1] == ".py" or x[1] == ".sml":
listX = list(x)
listX[1] = 1
x = tuple(listX)
else:
listX = list(x)
listX[1] = -1
x = tuple(listX)
return x
dataList = []
for x in finalList:
dataList.append(mapFunc(x))
random.shuffle(dataList)
JCfromSP = Classifier(len(finalList[0][0]))
JCfromSP.train(dataList, 0.05)
return JCfromSP