本文整理汇总了Python中sklearn.ensemble.RandomForestRegressor.score方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestRegressor.score方法的具体用法?Python RandomForestRegressor.score怎么用?Python RandomForestRegressor.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomForestRegressor
的用法示例。
在下文中一共展示了RandomForestRegressor.score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_boston
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def test_boston():
"""Check consistency on dataset boston house prices."""
for c in ("mse",):
# Random forest
clf = RandomForestRegressor(n_estimators=5, criterion=c,
random_state=1)
clf.fit(boston.data, boston.target)
score = clf.score(boston.data, boston.target)
assert score < 3, ("Failed with max_features=None, "
"criterion %s and score = %f" % (c, score))
clf = RandomForestRegressor(n_estimators=5, criterion=c,
max_features=6, random_state=1)
clf.fit(boston.data, boston.target)
score = clf.score(boston.data, boston.target)
assert score < 3, ("Failed with max_features=None, "
"criterion %s and score = %f" % (c, score))
# Extra-trees
clf = ExtraTreesRegressor(n_estimators=5, criterion=c, random_state=1)
clf.fit(boston.data, boston.target)
score = clf.score(boston.data, boston.target)
assert score < 3, ("Failed with max_features=None, "
"criterion %s and score = %f" % (c, score))
clf = ExtraTreesRegressor(n_estimators=5, criterion=c, max_features=6,
random_state=1)
clf.fit(boston.data, boston.target)
score = clf.score(boston.data, boston.target)
assert score < 3, ("Failed with max_features=None, "
"criterion %s and score = %f" % (c, score))
示例2: random_forest_regressor
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def random_forest_regressor(train_x, train_y, pred_x, review_id, v_curve=False, l_curve=False, get_model=True):
"""
:param train_x: train
:param train_y: text
:param pred_x: test set to predict
:param review_id: takes in a review id
:param v_curve: run the code for validation curve
:param l_curve: run the code for learning curve
:param get_model: run the code
:return:the predicted values,learning curve, validation curve
"""
rf = RandomForestRegressor(n_estimators=20,criterion='mse',max_features='auto', max_depth=10)
if get_model:
print "Fitting RF..."
rf.fit(train_x, np.log(train_y+1))
print rf.score(train_x, np.log(train_y+1))
rf_pred = np.exp(rf.predict(pred_x))-1.0
Votes = rf_pred[:,np.newaxis]
Id = np.array(review_id)[:,np.newaxis]
submission_rf = np.concatenate((Id,Votes),axis=1)
# create submission csv for Kaggle
np.savetxt("submission_rf.csv", submission_rf,header="Id,Votes", delimiter=',',fmt="%s, %0.2f", comments='')
# plot validation and learning curves
if v_curve:
train_y = np.log(train_y+1.0)
plot_validation_curve(RandomForestRegressor(), "Random Forest: Validation Curve(No: of trees)", train_x,train_y,'n_estimators',[5,10,20,50,100])
if l_curve:
train_y = np.log(train_y+1.0)
plot_learning_curve(RandomForestRegressor(), "Random Forest: Learning Curve", train_x,train_y)
示例3: dummie_columns_random_forest
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def dummie_columns_random_forest(train, test):
from sklearn.ensemble import RandomForestRegressor
print "-- {} --".format("Random Forest Regression using all but remarks")
predicting_columns = list(train._get_numeric_data().columns.values)
predicting_columns.remove("LISTPRICE")
predicting_columns.remove("SOLDPRICE")
predicting_columns.remove("SQFT")
rf = RandomForestRegressor(
n_estimators=300, n_jobs=-1)
rf.fit(train[predicting_columns], train["SOLDPRICE"])
score = rf.score(test[predicting_columns], test["SOLDPRICE"])
predictions = rf.predict(test[predicting_columns])
sample_predictions(test, predictions)
# print "-- Feature Importance --"
# for x in range(len(rf.feature_importances_)):
# print predicting_columns[x], rf.feature_importances_[x]
"""
feature_importance = rf.feature_importances_
# make importances relative to max importance
feature_importance = 100.0 * (feature_importance / feature_importance.max())
sorted_idx = np.argsort(feature_importance)
pos = np.arange(sorted_idx.shape[0]) + .5
plt.subplot(1, 2, 2)
plt.barh(pos, feature_importance[sorted_idx], align='center')
plt.yticks(pos, test[predicting_columns].columns.values[sorted_idx], fontsize=6)
plt.xlabel('Relative Importance')
plt.title('Variable Importance')
plt.show()
"""
print "Accuracy: {}\n".format(score)
return score, predictions
示例4: random_forest_regressor
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def random_forest_regressor(df):
"""
INPUT: Pandas dataframe
OUTPUT: R^2 and Mean Absolute Error performance metrics, feature importances
"""
y = df.pop("price").values
X = df.values
feature_names = df.columns
xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size=0.3, random_state=5)
clf = RandomForestRegressor()
clf.fit(xtrain, ytrain)
score = clf.score(xtest, ytest)
feat_imps = clf.feature_importances_
ypredict = clf.predict(xtest)
mae = np.mean(np.absolute(ytest - ypredict))
mae_percent = np.mean(np.absolute(ytest - ypredict) / ytest)
return (
"R^2 is ",
score,
"MAE is ",
mae,
"MAE percent is ",
mae_percent,
"Feature Importances are ",
zip(feature_names, feat_imps),
)
示例5: random_forest
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def random_forest(X_train, y_train, y_test, X_test, num_trees=100):
model = RandomForestRegressor(n_estimators=num_trees, oob_score=True)
model.fit(X_train, y_train)
prediction = model.predict(X_test)
mean_squared_error = mse(y_test, model.predict(X_test))
r2 = model.score(X_test, y_test)
return (mean_squared_error, r2)
示例6: train_model
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def train_model(X_train, X_test, y_train, y_test):
rf_model = RandomForestRegressor(max_depth=2, random_state=0)
rf_model.fit(X_train,y_train)
print (rf_model.score(X_test,y_test))
output = pd.DataFrame({'actual':y_test['lag_idle_day'],'pred':rf_model.predict(X_test)})
print (output)
return rf_model, output
示例7: cross_val
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def cross_val(seq, ft):
n_folds = 10
X, y = load_train_data(seq, ft)
print('%d-fold cross validation. Dataset: %d samples, %d features' % (n_folds, X.shape[0], X.shape[1]))
kf = KFold(len(y), n_folds=n_folds)
n_est = range(30, 110, 20)
results = []
for n_estimators in n_est:
scores = []
for i, (train, test) in enumerate(kf):
rf = RandomForestRegressor(n_estimators=n_estimators, n_jobs=mp.cpu_count())
# the (default) score for each regression tree in the ensemble is regression
# r2 determination coefficient (e.g., how much variance in y is explained
# by the model)
# https://www.khanacademy.org/math/probability/regression/regression-correlation/v/r-squared-or-coefficient-of-determination
rf.fit(X[train], y[train])
if False:
y_pred = rf.predict(X[test])
score = mean_squared_error(y_pred, y[test])
else:
score = rf.score(X[test], y[test])
scores.append(score)
scores = np.array(scores)
print("n_estimators=%d; accuracy (R^2 score): %0.2f (+/- %0.2f)" % (n_estimators, scores.mean(), scores.std() * 2))
results.append([seq, ft, X.shape[0], n_estimators, scores.mean(), scores.std()*2])
return results
示例8: rf_regressor
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def rf_regressor(self):
X = X.toarray() # Convert X from sparse to array
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
model = RandomForestRegressor(n_estimators=100, oob_score=True, random_state=42)
model.fit(X_train, y_train)
return model.score(X_test, y_test).round(2)
示例9: regression
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def regression(X_train, y_train, X_test, y_test):
"""
Train the regressor from Scikit-Learn.
"""
# Random forest regressor w/ param optimization
params = {'n_estimators':1000, 'criterion':'mse', 'max_depth':20, 'min_samples_split':1, #'estimators':400, depth:20
'min_samples_leaf':1, 'max_features':2, 'bootstrap':True, 'oob_score':False, #'max_features':'log2'
'n_jobs':32, 'random_state':0, 'verbose':0, 'min_density':None, 'max_leaf_nodes':None}
if config.DEBUG: params['verbose'] = 1
regr = RandomForestRegressor(**params)
# Train the model using the training sets
regr.fit(X_train, y_train)
return regr
# Plot the resutls
save_semeval_data.plot_results(regr, params, X_test, y_test, feature_names)
if config.DEBUG:
# Show the mean squared error
print("Residual sum of squares: %.2f" % np.mean((regr.predict(X_test) - y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(X_test, y_test))
return regr
示例10: test_run
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def test_run():
data1 = os.path.join("data", "data3.csv")
dataset1 = pd.read_csv(data1)
number = preprocessing.LabelEncoder()
dataset1.apply(number.fit_transform)
# print dataset1.ix[1:5]
dataset = dataset1.as_matrix()
x = dataset[:,1:60]
y = dataset[:,60]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=.33)
# # y = dataset[:,60]
# x_ =x[0:10000,:]
# # y = dataset[0:10000,60]
# y_ = y[0:10000]
# x_test = x[11001:12001,:]
# # y_test = dataset[1001:1201, -1].astype(int)
# y_test = y[11001:12001]
# print y_test
# create a base classifier used to evaluate a subset of attributes
print "starting"
pca = PCA()#n_components = 2)#DecisionTreeRegressor() #RandomForestClassifier() #ExtraTreesClassifier()
X_reduced = pca.fit_transform(scale(X_train))
model = RandomForestRegressor() #ExtraTreesClassifier()
model.fit(scale(X_reduced), y_train)
print (model.score(scale(X_test),y_test))
y_predict = model.predict(scale(X_test))
df = pd.DataFrame(y_predict)
path = 'data/results_RF_PCA.csv'
# print (model.explained_variance_ratio_)
print "done"
# scores = cross_val_score(model, x, y)
# print (scores.mean())
df.to_csv(path)
示例11: RandomForestModel
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def RandomForestModel(X_train,X_cv,y_train,y_cv):
n_estimators = [5,10,20,30,40,50]
best_random_forest = None
best_mse = float('inf')
best_score = -float('inf')
print "################# Performing Random Forest ####################### \n\n\n\n"
for estm in n_estimators:
random_forest = RandomForestRegressor(n_estimators=estm)
predictor = random_forest.fit(X_train,y_train)
score = random_forest.score(X_cv,y_cv)
mse = np.mean((random_forest.predict(X_cv) - y_cv) **2)
print "Number of estimators used: ",estm
print "Residual sum of squares: %.2f "%mse
print "Variance score: %.2f \n"%score
if best_score <= score:
if best_mse > mse:
best_mse = mse
best_score = score
best_random_forest = predictor
print "\nBest score: ",best_score
print "Best mse: ",best_mse
return best_random_forest
示例12: randomForestRegressorStudy
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def randomForestRegressorStudy(X,Y, setSize, comment):
#runs random forest regressor on the data to see the performance of the prediction and to determine predictive features
X_train=X[:setSize]
X_test=X[setSize:]
Y_train=Y[:setSize]
Y_test=Y[setSize:]
rf_reg=RandomForestRegressor(n_estimators=10)
rf_reg.fit(X_train, Y_train)
Y_pred=rf_reg.predict(X_train)
print "random forest regressor for "+comment, rf_reg.score(X_train, Y_train), rf_reg.score(X_test, Y_test)
print "feature importances", rf_reg.feature_importances_
scores = cross_validation.cross_val_score(rf_reg, X, Y, cv=5)
print "cross-validation"
print scores
示例13: estimators
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def estimators():
score_list = []
for x in xrange(5, 500, 5):
RFC = RandomForestRegressor(n_estimators=x)
RFC.fit(X_train, y_train)
score = RFC.score(X_test, y_test)
score_list.append(score)
return score_list
示例14: test_oob_score_regression
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def test_oob_score_regression():
"""Check that oob prediction is pessimistic estimate.
Not really a good test that prediction is independent."""
clf = RandomForestRegressor(n_estimators=50, oob_score=True, random_state=rng)
n_samples = boston.data.shape[0]
clf.fit(boston.data[: n_samples / 2, :], boston.target[: n_samples / 2])
test_score = clf.score(boston.data[n_samples / 2 :, :], boston.target[n_samples / 2 :])
assert_greater(test_score, clf.oob_score_)
assert_greater(clf.oob_score_, 0.8)
示例15: random_forest_regressor
# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import score [as 别名]
def random_forest_regressor(X, y, weight):
from sklearn.ensemble import RandomForestRegressor
from sklearn import cross_validation
X_train, X_test, y_train, y_test, weight_train, weight_test = cross_validation.train_test_split(
X, y, weight, test_size=0.4, random_state=0)
clf = RandomForestRegressor(n_estimators=20, max_features='sqrt', n_jobs=-1)
clf.fit(X_train, y_train, weight_train)
print(clf.score(X_test, y_test, weight_test))