本文整理汇总了Python中sklearn.ensemble.RandomForestClassifier.get_params方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestClassifier.get_params方法的具体用法?Python RandomForestClassifier.get_params怎么用?Python RandomForestClassifier.get_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomForestClassifier
的用法示例。
在下文中一共展示了RandomForestClassifier.get_params方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: onescore
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def onescore(X, Y, Xtest):
clf = RandomForestClassifier(oob_score=True, n_jobs=-1, n_estimators=1000, max_features=300, random_state=0)
clf.fit(X, Y)
print "oob_score = ", clf.oob_score_
print clf.get_params()
ytest = clf.predict(Xtest)
output(ytest, "try_004.csv")
示例2: test_set_params
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def test_set_params():
"""set_params should be able to set estimators"""
clf1 = LogisticRegression(random_state=123, C=1.0)
clf2 = RandomForestClassifier(random_state=123, max_depth=None)
clf3 = GaussianNB()
eclf1 = VotingClassifier([('lr', clf1), ('rf', clf2)], voting='soft',
weights=[1, 2])
assert_true('lr' in eclf1.named_estimators)
assert_true(eclf1.named_estimators.lr is eclf1.estimators[0][1])
assert_true(eclf1.named_estimators.lr is eclf1.named_estimators['lr'])
eclf1.fit(X, y)
assert_true('lr' in eclf1.named_estimators_)
assert_true(eclf1.named_estimators_.lr is eclf1.estimators_[0])
assert_true(eclf1.named_estimators_.lr is eclf1.named_estimators_['lr'])
eclf2 = VotingClassifier([('lr', clf1), ('nb', clf3)], voting='soft',
weights=[1, 2])
eclf2.set_params(nb=clf2).fit(X, y)
assert_false(hasattr(eclf2, 'nb'))
assert_array_equal(eclf1.predict(X), eclf2.predict(X))
assert_array_almost_equal(eclf1.predict_proba(X), eclf2.predict_proba(X))
assert_equal(eclf2.estimators[0][1].get_params(), clf1.get_params())
assert_equal(eclf2.estimators[1][1].get_params(), clf2.get_params())
eclf1.set_params(lr__C=10.0)
eclf2.set_params(nb__max_depth=5)
assert_true(eclf1.estimators[0][1].get_params()['C'] == 10.0)
assert_true(eclf2.estimators[1][1].get_params()['max_depth'] == 5)
assert_equal(eclf1.get_params()["lr__C"],
eclf1.get_params()["lr"].get_params()['C'])
示例3: training_and_test
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def training_and_test(token, train_data, test_data, num_classes, result):
"""Train and test
Args:
token (:obj:`str`): token representing this run
train_data (:obj:`tuple` of :obj:`numpy.array`): Tuple of training feature and label
test_data (:obj:`tuple` of :obj:`numpy.array`): Tuple of testing feature and label
num_classes (:obj:`int`): Number of classes
result (:obj:`pyActLearn.performance.record.LearningResult`): LearningResult object to hold learning result
"""
model = RandomForestClassifier(n_estimators=20, criterion="entropy")
model.fit(train_data[0], train_data[1].flatten())
# Test
predicted_y = model.predict(test_data[0])
predicted_proba = model.predict_proba(test_data[0])
# Evaluate the Test and Store Result
confusion_matrix = get_confusion_matrix(num_classes=num_classes,
label=test_data[1].flatten(), predicted=predicted_y)
result.add_record(model.get_params(), key=token, confusion_matrix=confusion_matrix)
# In case any label is missing, populate it
if predicted_proba.shape[1] != num_classes:
temp_array = np.zeros((predicted_proba.shape[0], num_classes), np.float32)
for i in range(len(model.classes_)):
temp_array[:, model.classes_[i]] = predicted_proba[:, i]
predicted_proba = temp_array
return predicted_y, predicted_proba
示例4: cross_validation
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def cross_validation(X, y):
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
assert(len(y) == len(X))
# Split the dataset in two equal parts
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.8, random_state=42)
depth = [8, 16, 32, 64]
split = [1, 2, 4, 8, 16, 32, 64]
best_score = 0
best_train_score = 0
best_param = None
for d in depth:
for s in split:
model = RandomForestClassifier(n_estimators=500, criterion="entropy", max_features="sqrt", max_depth=d, min_samples_split=s, n_jobs=-1)
model = model.fit(X_train, y_train)
print "Depth: %d split: %d" % (d, s)
print "Model trainning score:"
score_train = model.score(X_train, y_train)
print score_train
#ax.scatter(d, s, score_train, c='b', marker='o')
print "Model test score:"
score_test = model.score(X_test, y_test)
print score_test
#ax.scatter(d, s, score_test, c='r', marker='^')
if score_test > best_score:
best_score = score_test
best_train_score = score_train
best_param = model.get_params()
print "=================="
print best_train_score
print best_score
print best_param
return best_param
示例5: fit
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def fit(self,train_X,train_Y):
#split set into ones and zeros
zeros = train_X[train_Y == 0,:]
ones = train_X[train_Y == 1,:]
num_ones = ones.shape[0]
# compute number of chunks to split
num_chunks = int(zeros.shape[0]/num_ones)
chunks = np.array_split(zeros,num_chunks)
#train rfs
i = 0
for chunk in chunks:
print('training random forest %s of %s' %(i,num_chunks))
chunk_rf = RandomForestClassifier(n_estimators = 1000, n_jobs = -1)
print(chunk_rf.get_params())
chunk_train_X = np.concatenate([chunk,ones])
chunk_train_Y = np.concatenate([np.zeros([chunk.shape[0],1]),np.ones([num_ones,1])]).ravel()
#cross_validation
if self.weights is not None:
print('cross_validation')
scores = cross_validation.cross_val_score(chunk_rf, chunk_train_X, chunk_train_Y, cv = 10, n_jobs = -1)
print(scores.mean())
self.weights.append(scores.mean())
#train
chunk_rf.fit(chunk_train_X,chunk_train_Y)
self.rfs.append(chunk_rf)
i+=1
示例6: train_model_03
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def train_model_03(dataset_id):
# Random Forest
X, Y, test = prepare_data_for_training(dataset_id)
clf = RandomForestClassifier(n_estimators=300, min_samples_split=150,
bootstrap=False, criterion="gini",
max_depth=117, min_samples_leaf=3, n_jobs=-1)
train_and_make_predictions(clf, X, Y, test,
"RandomForest %s" % clf.get_params())
示例7: tuning_randomforest
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
def tuning_randomforest(X, y):
clf = RandomForestClassifier(n_estimators=10000, criterion='entropy', max_depth=6,
min_samples_split=2, min_samples_leaf=1,
min_weight_fraction_leaf=0,
max_features=0.2, n_jobs=-1, class_weight='balanced_subsample',
verbose=0)
print 'parameters:', clf.get_params()
skf = StratifiedShuffleSplit(y, n_iter=1, test_size=0.25, random_state=0)
for train_index, val_index in skf:
X_train, X_val = X[train_index], X[val_index]
y_train, y_val = y[train_index], y[val_index]
clf.fit(X_train, y_train)
print 'train accuracy', clf.score(X_train, y_train)
y_val_pred = clf.predict(X_val)
print 'val auc:', roc_auc_score(y_val, y_val_pred)
示例8: train_test_split
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
trainingSet = np.vstack((trainingSetEllipticals, trainingSetSpirals)) #using only elliptical and spiral for training
np.random.shuffle(trainingSet)
trainingSetLabels = trainingSet[:,12] #putting labels in separate array
trainingSetLabels[trainingSetLabels == 0] = -1 #replacing all 0 with -1 to match sklearn format
trainingSet = trainingSet[:, 1:11] #removing label cols from actual inputs
trainingSet, testingSet, trainingSetLabels, testingSetLabels = train_test_split(trainingSet, trainingSetLabels, test_size = 0.6, random_state = 0) #fixes random_state so results reproducible
startTime = time.time()
print "Time before training = ", startTime
clf = RandomForestClassifier() #No max depth initial, tweak as necessary later
clf = clf.fit(trainingSet, trainingSetLabels)
print "Params after training:"
print clf.get_params()
trainingAccuracy = clf.score(trainingSet, trainingSetLabels)
print "Training accuracy = ", trainingAccuracy
testingAccuracy = clf.score(testingSet, testingSetLabels)
print "Testing accuracy = ", testingAccuracy
print "Done training and testing! Time = ", time.time() - startTime, "seconds"
示例9: float
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
print "PLS Training error " , float(error)/yp_t.shape[0]
yp_new = pls.predict(Xp_v, copy=True)
yp_pred = (yp_new[:,0] > yp_new[:,1]).astype(int)
#print y_new, y_pred, y_v
#print ((y_v - y_pred) ** 2).sum(), y_v.shape[0]
error = ((yp_v - yp_pred) ** 2).sum()
print "PLS Validation error " , float(error)/yp_v.shape[0]
X_new = pls.transform(X)
rf = RandomForestClassifier(n_estimators=500, max_depth=None, max_features=int(math.sqrt(n_components)), min_samples_split=100, random_state=144, n_jobs=4)
#print "shapes ", X_new.shape, y.shape
#print X_new,y
X_t, X_v, y_t, y_v = tts(X_new,yd,train_size=0.85)
rf.fit(X_t, y_t)
print "Random Forest Classifier: ", rf.get_params()
print "Covariance Classifier Training score: ", rf.score(X_t, y_t)
print "Covariance Classifier Validation score: ", rf.score(X_v, y_v)
#print "Class prob: ", zip(rf.predict_proba(X_v), y_v)
sample_weights = rf.predict_proba(pls.transform(Xp_t))[:,1]
print sample_weights.shape
sample_weights = abs(sample_weights-0.5)
for a in [.01, .1, .3, 1, 3, 10, 20, 30, 40, 50, 100]:
clf = SGDClassifier(alpha=a,loss=algo,n_iter=20)
clf.fit(Xp_t,yp_t,sample_weight=sample_weights)
clf2 = SGDClassifier(alpha=a,loss=algo,n_iter=20)
clf2.fit(Xp_t,yp_t)
print "alpha: ", a
print "Target score with weights: ", clf.score(Xt,yt)
示例10: with
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
features_list = df.columns.values[1::]
# Fit a random forest with (mostly) default parameters to determine feature importance
forest = RandomForestClassifier(oob_score=True, n_estimators=10000)
forest.fit(X, y)
feature_importance = forest.feature_importances_
# make importances relative to max importance
feature_importance = 100.0 * (feature_importance / feature_importance.max())
# Get the indexes of all features over the importance threshold
important_idx = np.where(feature_importance)[0]
# Get the sorted indexes of important features
sorted_idx = np.argsort(feature_importance[important_idx])[::-1]
print "\nFeatures sorted by importance (DESC):\n", important_idx[sorted_idx]
# Adapted from http://scikit-learn.org/stable/auto_examples/ensemble/plot_gradient_boosting_regression.html
pos = np.arange(sorted_idx.shape[0]) + .5
plt.subplot(1, 2, 2)
plt.barh(pos, feature_importance[important_idx][sorted_idx[::-1]], align='center')
plt.yticks(pos, important_idx[sorted_idx[:-1]])
plt.xlabel('Relative Importance')
plt.title('Variable Importance')
plt.show()
sorted_idx
feature_importance
forest.get_params()
df.filter(regex='Survived|Age_sc|SibSp|Parch|Fare_[0, 7.896]|Fare_[7.896, 14.454]|Fare_[14.454, 31.275]|Fare_[31.275, 512.329]|Sex|Pclass|Child|FamilySize|Family|Title_id')
示例11: RandomForestClassifier
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
X_test=np.loadtxt("X_test.gz",delimiter=",")
####################################################################################
####################################################################################
####################################################################################
#classifier
RFmodel = RandomForestClassifier(
n_estimators=10, #number of trees to generate
max_features='auto', #consider sqrt of number of features when splitting
n_jobs=1, #run in parallel on all cores
criterion="entropy"
)
#train
RFmodel = RFmodel.fit(X_train, Y_train)
#get parameters
params=RFmodel.get_params()
#score on training set
acc_rate=RFmodel.score(X_train,Y_train)
print acc_rate
#feature importances
feat_imp=RFmodel.feature_importances_
#predict probabilities
test_probs=RFmodel.predict_proba(X_test)
#output test set probabilities to csv file
columns=['ARSON', 'ASSAULT', 'BAD CHECKS', 'BRIBERY',
'BURGLARY', 'DISORDERLY CONDUCT',
'DRIVING UNDER THE INFLUENCE', 'DRUG/NARCOTIC',
'DRUNKENNESS', 'EMBEZZLEMENT', 'EXTORTION',
'FAMILY OFFENSES', 'FORGERY/COUNTERFEITING', 'FRAUD',
'GAMBLING', 'KIDNAPPING', 'LARCENY/THEFT',
示例12: ExtraTreesClassifier
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
clf_etree = ExtraTreesClassifier(n_estimators=1000, max_depth=None, max_features=int(math.sqrt(n_features)), min_samples_split=100, random_state=144, n_jobs=4);
clf_etree.fit(X_train, y_train)
print "Validation set score: ERF " , clf_etree.score(X_val, y_val)
clf_boost = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1),algorithm="SAMME", n_estimators=500, random_state=74494, learning_rate=0.8)
clf_boost.fit(X_train, y_train)
print "Validation set score: ABOOST " , clf_boost.score(X_val, y_val)
#clf_gboost = GradientBoostingClassifier(n_estimators=int(reg), random_state=74494, learning_rate=0.2)
#clf_gboost.fit(X_train, y_train)
#print "Validation set score:LR " , clf_gboost.score(X_val, y_val)
print "Classifier:"
print clf, clf.get_params()
print clf_etree, clf_etree.get_params()
print clf_boost, clf_boost.get_params()
if(fe==1): #L1 norm based feature elimination
clf_fe = LogisticRegression(C=1000,penalty='l1',random_state=0)
clf_fe.fit(X_train, y_train)
X_train = X_train[:,clf_fe.coef_.ravel()!=0]
print "Xtrain.shape: ", X_train.shape
X_val = X_val[:,clf_fe.coef_.ravel()!=0]
clf2_l = svm.SVC(kernel='linear', C=reg)
clf2_l.fit(X_train, y_train)
print "Lasso Validation set score filtered coeff linear: " , clf2_l.score(X_val, y_val)
clf2 = svm.SVC(kernel='rbf', C=reg, gamma=g)
示例13: RandomForestClassifier
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
from sklearn.ensemble import RandomForestClassifier
if '--example' in sys.argv:
trainingdata = [[1, 1], [2, 0.5], [-1, -1], [-2, -2]]
traininglabel = [1, 1, -1, -1]
testdata = [[1, 3], [-3, -3]]
model = RandomForestClassifier()
model.fit(trainingdata, traininglabel)
output = model.predict(testdata)
for label in output:
print label
probas = model.predict_proba(testdata)
for label in probas:
print label
for weights in model.get_params():
print weights
for i, gini_imp in enumerate(model.feature_importances_):
print "gini係数 index = ", i, gini_imp
if '--learn' in sys.argv:
import json
anses = []
traings = []
for line in open('./learning.json').read().split('\n'):
if line.strip() == "" : continue
ans_label, data = json.loads(line.strip())
anses.append(ans_label)
traings.append(data)
model = RandomForestClassifier()
示例14: RandomForestClassifier
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
from sklearn.ensemble import RandomForestClassifier
# train the model
wqp_rf = RandomForestClassifier()
wqp_rf.fit(wqp_train_SX, wqp_train_y)
# predict and evaluate performance
wqp_rf_predictions = wqp_rf.predict(wqp_test_SX)
meu.display_model_performance_metrics(true_labels=wqp_test_y, predicted_labels=wqp_rf_predictions,
classes=wqp_label_names)
# ## Hyperparameter tuning with Grid Search & Cross Validation
# In[23]:
print(wqp_rf.get_params())
# ### Get the best hyperparameter values
# In[24]:
from sklearn.model_selection import GridSearchCV
param_grid = {
'n_estimators': [100, 200, 300, 500],
'max_features': ['auto', None, 'log2']
}
wqp_clf = GridSearchCV(RandomForestClassifier(random_state=42), param_grid, cv=5,
scoring='accuracy')
示例15:
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import get_params [as 别名]
nthread = 4,
min_child_weight = 1,
subsample= 0.8,
seed = 1337,
objective= 'multi:softprob',
max_depth = 7,
gamma= .2)
# use the xgb interface
xgb_param = clf.get_xgb_params()
xgb_param['num_class'] = 5
xgb_param['eval_metric'] = 'mlogloss'
Xg_train = xgb.DMatrix(X_train, label=y_train, missing=np.nan)
cvresult = xgb.cv(xgb_param,
Xg_train,
num_boost_round = clf.get_params()['n_estimators'],
nfold = 5,
show_progress = True,
early_stopping_rounds = 100)
clf.set_params(n_estimators=cvresult.shape[0])
clf.fit(X_train, y_train)
best_outcome_params = clf.get_params()
best_outcome_score = cvresult.min()
try:
# predict the outcome probabilities
y_pred = grid.predict_proba(X_test)
except:
# predict the outcome probabilities
y_pred = clf.predict_proba(X_test)