本文整理汇总了Python中sklearn.ensemble.RandomForestRegressor.predict_proba方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestRegressor.predict_proba方法的具体用法?Python RandomForestRegressor.predict_proba怎么用?Python RandomForestRegressor.predict_proba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomForestRegressor
的用法示例。
在下文中一共展示了RandomForestRegressor.predict_proba方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: randomForestSecond
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import predict_proba [as 别名]
def randomForestSecond(train,
labels,
test,
prior_weight = None,
n_estimators=100,
n_jobs=1,
verbose=0):
"""
:param train: The features of training data, obtained with getFeatures
:param labels: The kaggle labels of the training data
:param test: The faetures of testing data
:param prior_weight: the normalized weights to which output will be rescaled
by default: no rescaling. If 'auto', use ratio from kaggle training data
:param n_estimators:
:param n_jobs:
:param verbose:
:return:
"""
if prior_weight == 'auto':
prior_weight = [25810/35126.0, 2443/35126.0, 5292/35126.0, 873/35126.0, 708/35126.0]
assert np.sum(prior_weight) < (1.0 + 1e-4) and np.sum(prior_weight) > (1.0 - 1e-4)
model = RandomForestRegressor(n_estimators=n_estimators,
n_jobs=n_jobs,
verbose=verbose)
print "Now training model..."
model.fit(train, labels)
print "Now predicting samples..."
predictions = model.predict_proba(test)
if prior_weight is not None:
sortedpred = np.sort(predictions)
indexratio = np.cumsum(prior_weight)
n = len(sortedpred)
indexes = [int(i * n) for i in indexratio[:-1]]
thresholds = [sortedpred[i] for i in indexes] + [sortedpred[-1]]
predictions = np.digitize(predictions, thresholds, right=True)
return predictions
示例2: print
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import predict_proba [as 别名]
score.mean_validation_score,
np.std(score.cv_validation_scores)))
print("Parameters: {0}".format(score.parameters))
print("")
return(top_scores)
scores=report(random_search.grid_scores_)
kw=scores[0].parameters
kw.update({'n_estimators':500, 'n_jobs':7, 'verbose':1})
clf_large = RandomForestRegressor(**kw)
clf_large.fit(data[features].ix[data.null_flag==0,:], data[target].ix[(data.null_flag==0) ])
# evaluate
probs=clf_large.predict_proba(test[features])
test=pd.read_csv(path+'test.csv')
test.reset_index(drop=True, inplace=True)
data.reset_index(drop=True, inplace=True)
join_cols=[c for c in cols if c not in ['Semana']]
test=pd.merge(test, data[cols+['dr1', 'dr2', 'Demanda_uni_equil']], \
how='inner', left_on=list(join_cols), right_on=list(join_cols), suffixes=('','_d'))
days=test.Semana.unique()
days=days[np.argsort(days)]
df_list=[]
for i, d in enumerate(days):
示例3: main
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import predict_proba [as 别名]
#.........这里部分代码省略.........
test["Child"] = 0
test.loc[test["Age"] < 18, "Child"] = 1
# Add in Mother column
test["Mother"] = 0
test.loc[(test["Age"] > 18) & (test["Sex"] == 1) & (test["Parch"] > 0) & (test["Title"] != 2), "Mother"] = 1
def scale(data, features):
scaled = MinMaxScaler().fit_transform(data[features])
data[features] = scaled
# Normalize data
# scale(train, ["Fare"])
# scale(test, ["Fare"])
# Discretize Age
# train.loc[train["Age"] < 18, "Age"] = 0
# train.loc[train["Age"].between(18, 60), "Age"] = 1
# train.loc[train["Age"] > 60, "Age"] = 2
# test.loc[test["Age"] < 18, "Age"] = 0
# test.loc[test["Age"].between(18, 60), "Age"] = 1
# test.loc[test["Age"] > 60, "Age"] = 2
# The columns we'll use to predict the target
predictors = ["Pclass", "Sex", "Age", "Fare", "Embarked",
"FamilySize", "Title", "FamilyId", "Deck"]
# Prepare predictors
train_predictors = train[predictors]
# Prepare target
train_target = train["Survived"]
# Create and train the random forest
# Multi-core CPUs can use: rf = RandomForestClassifier(n_estimators=100, n_jobs=2)
rf = RandomForestClassifier(n_estimators=150, min_samples_split=4, min_samples_leaf=2, oob_score=True)
# Fit the algorithm to the data
rf.fit(train_predictors, train_target)
# Perform feature selection
selector = SelectKBest(f_classif, k=5)
selector.fit(train_predictors, train_target)
# Get the raw p-values for each feature, and transform from p-values into scores
scores = -np.log10(selector.pvalues_)
print("Univariate feature selection:")
for feature, imp in zip(predictors, scores):
print(feature, imp)
print("\nRANDOM FOREST METRICS:")
# Feature importances
print("\nFeature importances:")
for feature, imp in zip(predictors, rf.feature_importances_):
print(feature, imp)
# Base estimate
print("\nBase score: ")
print(rf.score(train_predictors, train_target))
# Cross validate our RF and output the mean score
scores = cross_validation.cross_val_score(rf, train_predictors, train_target, cv=4)
print("Cross validated score: ")
print(scores.mean())
# Out of bag estimate
print("OOB score: ")
print(rf.oob_score_)
# Split the data into a training set and a test set, and train the model
X_train, X_test, y_train, y_test = cross_validation.train_test_split(train_predictors, train_target, test_size=0.25)
rf.fit(X_train, y_train)
# Output roc auc score
disbursed = rf.predict_proba(X_test)
print("Roc_auc score:")
print(roc_auc_score(y_test, disbursed[:, 1]))
# Print a confusion matrix
y_pred = rf.predict(X_test)
print("\nConfusion matrix (rows: actual, cols: prediction)")
print(confusion_matrix(y_test, y_pred))
# Ensemble
ens = ensemble(train_predictors, train_target)
# Predict
predictions = ens.fit(train_predictors, train_target).predict(test[predictors])
# Map predictions to outcomes (only possible outcomes are 1 and 0)
predictions[predictions > .5] = 1
predictions[predictions <= .5] = 0
# Create submission and output
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": predictions
})
submission.to_csv("data/kaggle.csv", index=False)