本文整理汇总了Python中sklearn.ensemble.RandomForestClassifier.predict_log_proba方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestClassifier.predict_log_proba方法的具体用法?Python RandomForestClassifier.predict_log_proba怎么用?Python RandomForestClassifier.predict_log_proba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomForestClassifier
的用法示例。
在下文中一共展示了RandomForestClassifier.predict_log_proba方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
def main():
#Loading the training set and test set
path1 = "C:\Python32\A2PW1.csv"
path2 = "C:\Python32\A2PW3.csv"
train = read_csv(path1, has_header = True)
target = [x[0] for x in train]
train = [x[1:] for x in train]
test = read_csv(path2, has_header = True)
test = [x[1:] for x in test]
print('The training set is:')
print(train)
print('The test set is:')
print(test)
#create the model
rf = RandomForestClassifier(n_estimators = 100)
#throw the data into model
rf.fit(train, target)
predicted_probs = rf.predict_log_proba(test)
print(predicted_probs)
output_file_path = "C:\Python32\pythontoday.txt"
numpy.savetxt(output_file_path, predicted_probs,delimiter=',',fmt='%1.4e')
newArr = rf.fit_transform(test,target)
print('newArr becomes: ',newArr)
示例2: test_drf_classifier_backupsklearn
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
def test_drf_classifier_backupsklearn(backend='auto'):
df = pd.read_csv("./open_data/creditcard.csv")
X = np.array(df.iloc[:, :df.shape[1] - 1], dtype='float32', order='C')
y = np.array(df.iloc[:, df.shape[1] - 1], dtype='float32', order='C')
import h2o4gpu
Solver = h2o4gpu.RandomForestClassifier
#Run h2o4gpu version of RandomForest Regression
drf = Solver(backend=backend, random_state=1234, oob_score=True)
print("h2o4gpu fit()")
drf.fit(X, y)
#Run Sklearn version of RandomForest Regression
from sklearn.ensemble import RandomForestClassifier
drf_sk = RandomForestClassifier(random_state=1234, oob_score=True, max_depth=3)
print("Scikit fit()")
drf_sk.fit(X, y)
if backend == "sklearn":
assert (drf.predict(X) == drf_sk.predict(X)).all() == True
assert (drf.predict_log_proba(X) == drf_sk.predict_log_proba(X)).all() == True
assert (drf.predict_proba(X) == drf_sk.predict_proba(X)).all() == True
assert (drf.score(X, y) == drf_sk.score(X, y)).all() == True
assert (drf.decision_path(X)[1] == drf_sk.decision_path(X)[1]).all() == True
assert (drf.apply(X) == drf_sk.apply(X)).all() == True
print("Estimators")
print(drf.estimators_)
print(drf_sk.estimators_)
print("n_features")
print(drf.n_features_)
print(drf_sk.n_features_)
assert drf.n_features_ == drf_sk.n_features_
print("n_classes_")
print(drf.n_classes_)
print(drf_sk.n_classes_)
assert drf.n_classes_ == drf_sk.n_classes_
print("n_features")
print(drf.classes_)
print(drf_sk.classes_)
assert (drf.classes_ == drf_sk.classes_).all() == True
print("n_outputs")
print(drf.n_outputs_)
print(drf_sk.n_outputs_)
assert drf.n_outputs_ == drf_sk.n_outputs_
print("Feature importance")
print(drf.feature_importances_)
print(drf_sk.feature_importances_)
assert (drf.feature_importances_ == drf_sk.feature_importances_).all() == True
print("oob_score")
print(drf.oob_score_)
print(drf_sk.oob_score_)
assert drf.oob_score_ == drf_sk.oob_score_
示例3: test_probability
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
def test_probability():
"""Predict probabilities."""
# Random forest
clf = RandomForestClassifier(n_estimators=10, random_state=1)
clf.fit(iris.data, iris.target)
assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1),
np.ones(iris.data.shape[0]))
assert_array_almost_equal(clf.predict_proba(iris.data),
np.exp(clf.predict_log_proba(iris.data)))
# Extra-trees
clf = ExtraTreesClassifier(n_estimators=10, random_state=1)
clf.fit(iris.data, iris.target)
assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1),
np.ones(iris.data.shape[0]))
assert_array_almost_equal(clf.predict_proba(iris.data),
np.exp(clf.predict_log_proba(iris.data)))
示例4: test_probability
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
def test_probability():
"""Predict probabilities."""
olderr = np.seterr(divide="ignore")
# Random forest
clf = RandomForestClassifier(n_estimators=10, random_state=1, max_features=1, max_depth=1)
clf.fit(iris.data, iris.target)
assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1), np.ones(iris.data.shape[0]))
assert_array_almost_equal(clf.predict_proba(iris.data), np.exp(clf.predict_log_proba(iris.data)))
# Extra-trees
clf = ExtraTreesClassifier(n_estimators=10, random_state=1, max_features=1, max_depth=1)
clf.fit(iris.data, iris.target)
assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1), np.ones(iris.data.shape[0]))
assert_array_almost_equal(clf.predict_proba(iris.data), np.exp(clf.predict_log_proba(iris.data)))
np.seterr(**olderr)
示例5: enumerate
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
# prepare where to store the ratios
ratios_all_lstm = np.empty(len(labels_all))
# split indices into folds
predict_idx_list = np.array_split(range(nsamples), nfolds)
# run CV
for fid, predict_idx in enumerate(predict_idx_list):
print "Current fold is %d" % fid
train_idx = list(set(range(nsamples)) - set(predict_idx))
# extract predictions using RF on static
print " Extracting predictions on static data with RF..."
rf = RandomForestClassifier(n_estimators=nestimators)
rf.fit(static_all[train_idx], labels_all[train_idx])
predictions_all_rf[predict_idx] = rf.predict_log_proba(static_all[predict_idx])
predictions_all_rf[predictions_all_rf == -inf] = np.min(predictions_all_rf[predictions_all_rf != -inf])
# extract predictions using LSTM on dynamic
print " Extracting predictions on dynamic data with LSTM..."
lstmcl = LSTMClassifier(lstmsize, lstmdropout, lstmoptim, lstmnepochs, lstmbatchsize)
model_pos, model_neg = lstmcl.train(dynamic_all[train_idx], labels_all[train_idx])
mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, dynamic_all[predict_idx])
predictions_all_lstm[predict_idx] = np.vstack((mse_pos, mse_neg)).T
ratios_all_lstm[predict_idx] = lstmcl.pos_neg_ratios(model_pos, model_neg, dynamic_all[predict_idx])
#
# Prepare combined datasets for the future experiments
#
开发者ID:annitrolla,项目名称:Generative-Models-in-Classification,代码行数:32,代码来源:eleven_on_inforegister_gpu.py
示例6: RandomForestClassifier
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
trainB_static = train_static[train_half:]
trainA_dynamic = train_dynamic[:train_half]
trainB_dynamic = train_dynamic[train_half:]
trainA_labels = train_labels[:train_half]
trainB_labels = train_labels[train_half:]
#
# Train enrichment models on trainA
#
print 'Training enrichment models...'
# extract predictions using RF on static
rf = RandomForestClassifier(n_estimators=nestimators)
rf.fit(trainA_static, trainA_labels)
predictions_trainB_rf = rf.predict_log_proba(trainB_static)
predictions_trainB_rf[predictions_trainB_rf == -inf] = np.min(predictions_trainB_rf[predictions_trainB_rf != -inf])
predictions_test_rf = rf.predict_log_proba(test_static)
predictions_test_rf[predictions_test_rf == -inf] = np.min(predictions_test_rf[predictions_test_rf != -inf])
# extract predictions using HMM on dynamic
hmmcl = HMMClassifier()
model_pos, model_neg = hmmcl.train(nhmmstates, nhmmiter, hmmcovtype, trainA_dynamic, trainA_labels)
predictions_trainB_hmm = hmmcl.predict_log_proba(model_pos, model_neg, trainB_dynamic)
ratios_trainB_hmm = hmmcl.pos_neg_ratios(model_pos, model_neg, trainB_dynamic)
predictions_test_hmm = hmmcl.predict_log_proba(model_pos, model_neg, test_dynamic)
ratios_test_hmm = hmmcl.pos_neg_ratios(model_pos, model_neg, test_dynamic)
# extract predictions using LSTM on dynamic
lstmcl = LSTMClassifier(lstmsize, lstmdropout, lstmoptim, lstmnepochs, lstmbatchsize)
model_pos, model_neg = lstmcl.train(trainA_dynamic, trainA_labels)
示例7: pjoin
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import predict_log_proba [as 别名]
#fraw = pjoin(folder, filename+'.nii.gz')
#存储数据
#np.save('/home/gongyilong/brain-pca/mat_save/whole_brain_Xlearning',X_learning)
#np.save('/home/gongyilong/brain-pca/mat_save/whole_brain_Xreduced',X_reduced)
#joblib.dump(bayes_estimator, os.getcwd()+'/modelsave/whole_bayes_estimator.pkl')
#bayes_estimator = joblib.load(os.getcwd()+'/modelsave/whole_bayes_estimator.pkl')
print 'random_forest'
random_forest = RandomForestClassifier(n_estimators=8000)
ranfortitle = 'Random Forest(1000) model CV(10) fitting whole_brain PCA feature vector after feature selection(30000)'
plot_learning_curve(random_forest,ranfortitle,X_reduced,Y_learning, cv=10)
random_forest = random_forest.fit(X_reduced,Y_learning)
pred_y = random_forest.predict(X_reduced)
prob_log = random_forest.predict_log_proba(X_reduced)
prob = random_forest.predict_proba(X_reduced)
#np.savetxt(os.getcwd()+'/mat_save/pred_y.txt',pred_y)
#np.savetxt(os.getcwd()+'/mat_save/prob_log.txt',prob_log)
#np.savetxt(os.getcwd()+'/mat_save/prob.txt',prob)
print 'bayes_estimator'
bayes_estimator = GaussianNB()
bayes_estimator.fit(X_reduced,Y_learning)
bayestitle = 'Naive Bayes model CV(10) fitting whole_brain PCA feature vector after feature selection(30000)'
plot_learning_curve(bayes_estimator, bayestitle,X_reduced,Y_learning, cv=10)
#plt.savefig('figure_pdf/whole_bayes_reduced.pdf')
#plt.savefig('figure_pdf/whole_bayes_reduced.eps')
"""
coef = bayes_estimator.theta_
# reverse feature selection