本文整理汇总了Python中nolearn.dbn.DBN.predict方法的典型用法代码示例。如果您正苦于以下问题:Python DBN.predict方法的具体用法?Python DBN.predict怎么用?Python DBN.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nolearn.dbn.DBN
的用法示例。
在下文中一共展示了DBN.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: benchmark
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def benchmark(k, epochs):
print("*" * 80)
print("k: %d, epochs: %d\n" % (k, epochs))
#select = SelectKBest(score_func=chi2, k=k)
select = TruncatedSVD(n_components=k)
X_train_trunc = select.fit_transform(X_train, Y_train)
X_test_trunc = select.transform(X_test)
print('done truncating')
clf = DBN([X_train_trunc.shape[1], k, 4], learn_rates=0.3, learn_rate_decays=0.9, epochs=epochs, verbose=1)
clf.fit(X_train_trunc, Y_train)
pred = clf.predict(X_test_trunc)
if CREATE_SUBMISSION:
X_submit_trunc = select.transform(X_submit)
pred_submit = clf.predict(X_submit_trunc)
dump_csv(pred_submit, k, epochs)
score = metrics.f1_score(Y_test, pred)
print("f1-score: %0.3f" % score)
print("classification report:")
print(metrics.classification_report(Y_test, pred))
print("confusion matrix:")
print(metrics.confusion_matrix(Y_test, pred))
示例2: train_model
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def train_model(data_set_path='/home/devin.fisher/Kingdoms/treadstone/_samples/still_data/still_training_data.pkl'):
# data_set = None
with open(data_set_path, 'rb') as f:
data_set = pickle.load(f)
# with open('/home/devin.fisher/Kingdoms/lol/still_training_data2.pkl', 'rb') as f:
# data_set = pickle.load(f)
# (train_x, test_x, train_y, test_y) = train_test_split(data_set['data'], data_set['target'], test_size=0.1)
train_x = data_set['data']
test_x = data_set['data']
train_y = data_set['target']
test_y = data_set['target']
dbn = DBN(
[-1, 300, -1],
learn_rates=0.3,
learn_rate_decays=0.9,
epochs=60,
verbose=1)
dbn.fit(train_x, train_y)
joblib.dump(dbn, 'digit_model.pkl', compress=9)
# dbn = joblib.load('digit_model.pkl')
# compute the predictions for the test data and show a classification report
preds = dbn.predict(test_x)
print classification_report(test_y, preds)
示例3: main
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def main():
data_id = 'B'
data_path = '/broad/compbio/maxwshen/data/1-MAKETRAINTEST/complete/golf/'
print 'train...', datetime.datetime.now()
train_set = readin(data_id, 'train', data_path)
print 'valid...', datetime.datetime.now()
valid_set = readin(data_id, 'valid', data_path)
print 'test...', datetime.datetime.now()
test_set = readin(data_id, 'test', data_path)
# Input to 300 node RBM to 2 node output
dbn = DBN( \
[xtrain.shape[1], 300, 2], \
learn_rates = 5, \
learn_rate_decays = 0.9, \
epochs = 31, \
verbose = 1)
dbn.fit(dat_train, y_train)
preds = dbn.predict(dat_test)
print classification_report(y_test, preds)
out_fn = 'dbn.pickle'
with open(out_fn, 'w') as f:
pickle.dump(dbn, out_fn)
return
示例4: run
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def run():
X_train, Y_train = load_training_data()
X_train, Y_train = rotate_dataset(X_train, Y_train, 8)
X_train, Y_train = nudge_dataset(X_train, Y_train)
n_features = X_train.shape[1]
n_classes = 10
classifier = DBN([n_features, 8000, n_classes],
learn_rates=0.4, learn_rate_decays=0.9 ,epochs=75, verbose=1)
classifier.fit(X_train, Y_train)
test_data = get_test_data_set()
predictions = classifier.predict(test_data)
write_predictions_to_csv(predictions)
示例5: DigitProphet
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
class DigitProphet(object):
def __init__(self):
# load train.csv
# train = pd.read_csv("data/train.csv")
# data_train=train.as_matrix()
# values_train=data_train[:,0]
# images_train=data_train[:,1:]
# trainX, _trainX, trainY, _trainY = train_test_split(images_train/255.,values_train,test_size=0.5)
# #load test.csv
# test = pd.read_csv("data/test.csv")
# data_test=test.as_matrix()
# testX, _testX = train_test_split(data_test/255.,test_size=0.99)
# Random Forest
# self.clf = RandomForestClassifier()
# Stochastic Gradient Descent
# self.clf = SGDClassifier()
# Support Vector Machine
# self.clf = LinearSVC()
# Nearest Neighbors
# self.clf = KNeighborsClassifier(n_neighbors=13)
train = pd.read_csv("data/train.csv")
data_train=train.as_matrix()
values_train=data_train[:,0]
images_train=data_train[:,1:]
trainX, _trainX, trainY, _trainY = train_test_split(images_train/255.,values_train,test_size=0.995)
# Neural Network
self.clf = DBN([trainX.shape[1], 300, 10],learn_rates=0.3,learn_rate_decays=0.9,epochs=10,verbose = 1)
#Training
self.clf.fit(trainX, trainY)
pass
def predictImage(self,array):
image=np.atleast_2d(array)
return self.clf.predict(image)[0]
示例6: train_dbn_dataset
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def train_dbn_dataset(dataset, x_test, y_test, alpha, nhidden, epochs, batch_size, noises=[]):
from nolearn.dbn import DBN
num_classes = len(set(y_test))
print "Number of classes", num_classes
x_train, y_train = dataset
dbn_model = DBN([x_train.shape[1], nhidden, num_classes],
learn_rates = alpha,
learn_rate_decays = 0.9,
epochs = epochs,
verbose = 1,
nesterov=False,
minibatch_size=batch_size,
noises = noises)
dbn_model.fit(x_train, y_train)
from sklearn.metrics import classification_report, accuracy_score
y_true, y_pred = y_test, dbn_model.predict(x_test) # Get our predictions
print(classification_report(y_true, y_pred)) # Classification on each digit
print(roc_auc_score(y_true, y_pred)) # Classification on each digit
return y_pred, roc_auc_score(y_true, y_pred)
示例7: test
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def test(self):
#iris = datasets.load_iris()
#X, y = iris.data, iris.target
X, y = self.dataMat,self.labelMat
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.6, random_state=12)
#clf = RandomForestClassifier(max_depth=6,min_samples_split=9,min_samples_leaf=15,n_estimators=5)
#clf = DBN([X.shape[1], 24, 2],scales=0.5,learn_rates=0.02,learn_rate_decays = 0.95, learn_rate_minimums =0.001,epochs=500,l2_costs = 0.02*0.031, dropouts=0.2,verbose=0)
#cvnum = ShuffleSplit(2013,n_iter=10,test_size=0.6,train_size=0.4,random_state=0)
for scal in arange(4.5, 5.0, 0.5):
print "**************************************************************"
print "DBN scal=",scal
clf = DBN([X.shape[1], 24,48, 2],scales=0.5,learn_rates=0.01,learn_rate_decays = 0.95, learn_rate_minimums =0.001,epochs=50,l2_costs = 0.02*0.001, dropouts=0.0,verbose=0)
clf.fit(X_train, y_train);
scores = cross_val_score(clf,X,y,cv=3,scoring='roc_auc')
y_pred = clf.predict(X_test);
y_predprob = clf.predict_proba(X_test);
prf=precision_recall_fscore_support(y_test, y_pred, average='binary')
print ("Accuracy: %0.5f (+/- %0.5f)" % (scores.mean(), scores.std() * 2))
print classification_report(y_test,y_pred)
print 'The accuracy is: ', accuracy_score(y_test,y_pred)
print 'The log loss is:', log_loss(y_test, y_predprob)
print 'The ROC score is:', roc_auc_score(y_test,y_predprob[:,1])
示例8: main
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def main():
data_fn = "/home/ec2-user/Kellis/data/bravo.formatted/dat.all.txt"
blacklist_fn = "/home/ec2-user/Kellis/data/bravo.formatted/dat.blacklist.txt"
y_fn = "/home/ec2-user/Kellis/data/bravo.formatted/dat.y.txt"
data = read_delimited_txt(data_fn, "\t")
blacklist = read_delimited_txt(blacklist_fn, "\t")
y = read_delimited_txt(y_fn, "\t")
# Get names and remove the first element of each row which is the row number
names = data[0]
data = data[1:]
for i in range(len(data)):
data[i] = data[i][1:]
y = y[1:]
for i in range(len(y)):
y[i] = y[i][-1]
y = convert_y_binary(y)
# Normalizes column-wise so all values are between 0 and 1
data = normalize_0_1(data)
# Split into training, testing
xtrain, xtest, ytrain, ytest = train_test_split(data, y, test_size=0.2, random_state=1)
# Input to 300 node RBM to 2 node output
dbn = DBN([xtrain.shape[1], 300, 2], learn_rates=5, learn_rate_decays=0.9, epochs=501, verbose=1)
dbn.fit(xtrain, ytrain)
preds = dbn.predict(xtest)
print classification_report(ytest, preds)
out_fn = "dbn.pickle"
with open(out_fn, "w") as f:
pickle.dump(dbn, out_fn)
return
示例9: runOfflineML
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def runOfflineML(y, X, classifiers, savemodel=False):
X_train, X_test, y_train, y_test = train_test_split(X, y.astype("int0"), test_size=0.20, random_state=0)
data = dict(x_train=X_train, x_test=X_test, y_train=y_train, y_test=y_test)
cls_stats = initClsStats(classifiers)
for cls_name, cls in classifiers.items():
cls_stats[cls_name]["n_train"] = data["x_train"].shape[0]
cls_stats[cls_name]["n_test"] = data["x_test"].shape[0]
cls_stats[cls_name]["n_features"] = data["x_train"].shape[1]
tick = time.time()
if cls_name == "DBN":
data = dataNormalise(data)
clf = DBN([data["x_train"].shape[1], 300, 2], learn_rates=0.3, learn_rate_decays=0.9, epochs=10, verbose=1)
clf.fit(data["x_train"], data["y_train"])
else:
clf = classifiers[cls_name].fit(data["x_train"], data["y_train"])
if savemodel:
pickle.dump(clf, open(cls_name + ".dat", "w"))
clf = pickle.load(open(cls_name + ".dat", "r"))
cls_stats[cls_name]["training_time"] += time.time() - tick
# check the accuracy on the training set
tick = time.time()
predicted = clf.predict(data["x_test"])
cls_stats[cls_name]["testing_time"] += time.time() - tick
acc = metrics.accuracy_score(data["y_test"], predicted)
cls_stats[cls_name]["accuracy"] = acc
print cls_name, "accuracy is: " + str(acc)
# auc = metrics.roc_auc_score(data['y_test'], probs[:, 1])
conf_matrix = metrics.confusion_matrix(data["y_test"], predicted)
cls_stats[cls_name]["conf_matrix"] = conf_matrix
# print conf_matrix
precision, recall, fscore, support = metrics.precision_recall_fscore_support(data["y_test"], predicted)
cls_stats[cls_name]["precision"] = precision
cls_stats[cls_name]["recall"] = recall
cls_stats[cls_name]["fscore"] = fscore
cls_stats[cls_name]["support"] = support
return cls_stats
示例10: DBN
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
dbn = DBN(
[300,1024,120000],
learn_rates = 0.025,
learn_rate_decays = 0.98,
l2_costs = 0.0001,
minibatch_size=256,
epochs=5,
momentum = 0.9,
#dropouts=0.22,
verbose = 2)
dbn.fit(train_x, train_y)
print 'validation score is:' ,dbn.score(vali_x,vali_y)
result = dbn.predict(test_x)
with open('data/result','w') as f:
for el in result:
f.write(el+'\n')
#predicted_y_proba = dbn.predict_proba(test_x)
#if __name__ == "__main__":
#p_proba_str = cPickle.dumps(predicted_y_proba)
'''import sys
file_name = sys.argv[1]
with open(file_name, 'w') as a:
a.write(p_proba_str)'''
示例11:
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
if dst == "q":
break
else:
try:
img=cv2.imread(dst)
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
if img.shape != (32,20):
img=cv2.resize(img,(20,32))
_,img=cv2.threshold(img,100,255,cv2.THRESH_BINARY)
img=img/255.0
print img.shape
img=img.reshape(1,s)
img=img.astype(np.float32)
#prediction:
pred=dbn.predict(img)
print pred
except:
print "error reading image.."
示例12: fetch_mldata
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata("MNIST original")
X_train, X_test, y_train, y_test = train_test_split(mnist.data / 255.0, mnist.target)
from nolearn.dbn import DBN
clf = DBN([X_train.shape[1], 300, 10], learn_rates=0.3, learn_rate_decays=0.9, epochs=10, verbose=1)
clf.fit(X_train, y_train)
from sklearn.metrics import classification_report
from sklearn.metrics import zero_one_loss
y_pred = clf.predict(X_test)
print "Accuracy:", 1 - zero_one_loss(y_test, y_pred)
print "Classification report:"
print classification_report(y_test, y_pred)
示例13: do_operation_
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
def do_operation_(X_train,X_test,y_train,y_test,l_r,d_r):
clf = DBN([np.shape(X_train)[1],300,10],learn_rates = l_r,learn_rate_decays = d_r,epochs = 30,verbose = 1 )
clf.fit(X_train,y_train)
y_test,y_pred = y_test, clf.predict(X_test)
result = np.sum(y_test == y_pred)
return (result,l_r,d_r)
示例14: range
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
Y = np.concatenate([Y for _ in range(5)], axis=0)
return X, Y
def rotate_dataset(X):
XX = np.zeros(X.shape)
for index in range(X.shape[0]):
angle = np.random.randint(-7,7)
XX[index,:] = nd.rotate(np.reshape(X[index,:],((28,28))),angle,reshape=False).ravel()
return XX
# Load Data
mnist = pd.read_csv("data_5k.csv")
#mnist = mnist[:50]
y_train = mnist['label'].values
X_train = mnist.loc[:,'pixel0':].values
X_test = pd.read_csv("test.csv").values
X_test = X_test[:2000]
X_train = np.asarray(X_train / 255.0, 'float32')
X_test = np.asarray(X_test / 255.0, 'float32')
#X_train, y_train = nudge_dataset(X_train, y_train)
#X_train = rotate_dataset(X_train)
clf = DBN([X_train.shape[1], 350, 10],\
learn_rates=0.3,\
learn_rate_decays=0.95,\
learn_rates_pretrain=0.005,\
epochs=120,\
verbose=1)
clf.fit(X_train, y_train)
subm = pd.read_csv("rf_benchmark.csv")
subm.Label = clf.predict(X_test)
subm.to_csv("result.csv", index_label='ImageId', col=['Label'], index=False)
示例15: DBN
# 需要导入模块: from nolearn.dbn import DBN [as 别名]
# 或者: from nolearn.dbn.DBN import predict [as 别名]
data_train = data_train.astype('float') / 255.
labels_train = labels_train
data_test = test_batch['data'].astype('float') / 255.
labels_test = np.array(test_batch['labels'])
n_feat = data_train.shape[1]
n_targets = labels_train.max() + 1
net = DBN(
[n_feat, n_feat / 3, n_targets],
epochs=100,
learn_rates=0.01,
learn_rate_decays=0.99,
learn_rate_minimums=0.005,
verbose=1,
)
net.fit(data_train, labels_train)
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
expected = labels_test
predicted = net.predict(data_test)
print "Classification report for classifier %s:\n%s\n" % (
net, classification_report(expected, predicted))
print "Confusion matrix:\n%s" % confusion_matrix(expected, predicted)
print "prediction over expected" % predicted/expected
joblib.dump(net, 'nl_dbn.pkl', compress=9)
#nl_clone = joblib.load('nl_dbn.pkl')