本文整理汇总了Python中sklearn.tree.DecisionTreeRegressor方法的典型用法代码示例。如果您正苦于以下问题:Python tree.DecisionTreeRegressor方法的具体用法?Python tree.DecisionTreeRegressor怎么用?Python tree.DecisionTreeRegressor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.tree
的用法示例。
在下文中一共展示了tree.DecisionTreeRegressor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_regression
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_regression():
# Check regression for various parameter settings.
rng = check_random_state(0)
X_train, X_test, y_train, y_test = train_test_split(boston.data[:50],
boston.target[:50],
random_state=rng)
grid = ParameterGrid({"max_samples": [0.5, 1.0],
"max_features": [0.5, 1.0],
"bootstrap": [True, False],
"bootstrap_features": [True, False]})
for base_estimator in [None,
DummyRegressor(),
DecisionTreeRegressor(),
KNeighborsRegressor(),
SVR(gamma='scale')]:
for params in grid:
BaggingRegressor(base_estimator=base_estimator,
random_state=rng,
**params).fit(X_train, y_train).predict(X_test)
示例2: fit
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def fit(self, X, y):
"""
Method:
1) Create n_estimator tree estimators with greedy splitting
2) For each estimator i, sample (X, y) randomly N times with replacement
and train the estimator on this sampled dataset (X_i, y_i)
3) Predict by taking the mean predictions of each estimator
"""
self.models = []
N = len(X)
for i in range(self.n_estimators):
# Create tree with greedy splits
model = DecisionTreeRegressor(max_depth=self.max_depth)
# Bagging procedure
idx_sample = np.random.choice(N, N)
model.fit(X[idx_sample], y[idx_sample])
self.models.append(model)
示例3: fit
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def fit(self, X, y):
"""
Method:
1) Create n_estimator tree estimators with random splitting
on d/3 max features
2) For each estimator i, sample (X, y) randomly N times with replacement
and train the estimator on this sampled dataset (X_i, y_i)
3) Predict by taking the mean predictions of each estimator
"""
self.models = []
N, d = X.shape
for i in range(self.n_estimators):
# Create tree with random splitting on d/3 max features
model = DecisionTreeRegressor(max_depth=self.max_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
splitter="random",
max_features=d/3)
# Bagging procedure
idx_sample = np.random.choice(N, N) # random sampling of length N
model.fit(X[idx_sample], y[idx_sample]) # fit on random sampling
self.models.append(model)
示例4: Train
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def Train(data, modelcount, censhu, yanzhgdata):
model = AdaBoostRegressor(DecisionTreeRegressor(max_depth=censhu),
n_estimators=modelcount, learning_rate=0.8)
model.fit(data[:, :-1], data[:, -1])
# 给出训练数据的预测值
train_out = model.predict(data[:, :-1])
# 计算MSE
train_mse = mse(data[:, -1], train_out)
# 给出验证数据的预测值
add_yan = model.predict(yanzhgdata[:, :-1])
# 计算MSE
add_mse = mse(yanzhgdata[:, -1], add_yan)
print(train_mse, add_mse)
return train_mse, add_mse
# 最终确定组合的函数
示例5: regression_cart
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def regression_cart(x,y):
'''
Estimate a CART regressor
'''
# create the regressor object
cart = sk.DecisionTreeRegressor(min_samples_split=80,
max_features="auto", random_state=66666,
max_depth=5)
# estimate the model
cart.fit(x,y)
# return the object
return cart
# the file name of the dataset
示例6: test_ransac_custom_base_estimator
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_ransac_custom_base_estimator():
base_estimator = DecisionTreeRegressor()
estimator = linear_model.RANSACRegressor(
base_estimator=base_estimator,
random_state=1)
estimator.fit([[1], [2], [3]], [1, 2, 3])
assembler = assemblers.RANSACModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(2.5),
ast.CompOpType.LTE),
ast.NumVal(2.0),
ast.NumVal(3.0))
assert utils.cmp_exprs(actual, expected)
示例7: test_single_condition
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_single_condition():
estimator = tree.DecisionTreeRegressor()
estimator.fit([[1], [2]], [1, 2])
assembler = assemblers.TreeModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(1.5),
ast.CompOpType.LTE),
ast.NumVal(1.0),
ast.NumVal(2.0))
assert utils.cmp_exprs(actual, expected)
示例8: test_two_conditions
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_two_conditions():
estimator = tree.DecisionTreeRegressor()
estimator.fit([[1], [2], [3]], [1, 2, 3])
assembler = assemblers.TreeModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(1.5),
ast.CompOpType.LTE),
ast.NumVal(1.0),
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(2.5),
ast.CompOpType.LTE),
ast.NumVal(2.0),
ast.NumVal(3.0)))
assert utils.cmp_exprs(actual, expected)
示例9: test_bootstrap_features
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_bootstrap_features():
# Test that bootstrapping features may generate duplicate features.
rng = check_random_state(0)
X_train, X_test, y_train, y_test = train_test_split(boston.data,
boston.target,
random_state=rng)
ensemble = BaggingRegressor(base_estimator=DecisionTreeRegressor(),
max_features=1.0,
bootstrap_features=False,
random_state=rng).fit(X_train, y_train)
for features in ensemble.estimators_features_:
assert_equal(boston.data.shape[1], np.unique(features).shape[0])
ensemble = BaggingRegressor(base_estimator=DecisionTreeRegressor(),
max_features=1.0,
bootstrap_features=True,
random_state=rng).fit(X_train, y_train)
for features in ensemble.estimators_features_:
assert_greater(boston.data.shape[1], np.unique(features).shape[0])
示例10: test_parallel_regression
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_parallel_regression():
# Check parallel regression.
rng = check_random_state(0)
X_train, X_test, y_train, y_test = train_test_split(boston.data,
boston.target,
random_state=rng)
ensemble = BaggingRegressor(DecisionTreeRegressor(),
n_jobs=3,
random_state=0).fit(X_train, y_train)
ensemble.set_params(n_jobs=1)
y1 = ensemble.predict(X_test)
ensemble.set_params(n_jobs=2)
y2 = ensemble.predict(X_test)
assert_array_almost_equal(y1, y2)
ensemble = BaggingRegressor(DecisionTreeRegressor(),
n_jobs=1,
random_state=0).fit(X_train, y_train)
y3 = ensemble.predict(X_test)
assert_array_almost_equal(y1, y3)
示例11: test_gridsearch
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_gridsearch():
# Check that base trees can be grid-searched.
# AdaBoost classification
boost = AdaBoostClassifier(base_estimator=DecisionTreeClassifier())
parameters = {'n_estimators': (1, 2),
'base_estimator__max_depth': (1, 2),
'algorithm': ('SAMME', 'SAMME.R')}
clf = GridSearchCV(boost, parameters)
clf.fit(iris.data, iris.target)
# AdaBoost regression
boost = AdaBoostRegressor(base_estimator=DecisionTreeRegressor(),
random_state=0)
parameters = {'n_estimators': (1, 2),
'base_estimator__max_depth': (1, 2)}
clf = GridSearchCV(boost, parameters)
clf.fit(boston.data, boston.target)
示例12: test_importances_gini_equal_mse
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_importances_gini_equal_mse():
# Check that gini is equivalent to mse for binary output variable
X, y = datasets.make_classification(n_samples=2000,
n_features=10,
n_informative=3,
n_redundant=0,
n_repeated=0,
shuffle=False,
random_state=0)
# The gini index and the mean square error (variance) might differ due
# to numerical instability. Since those instabilities mainly occurs at
# high tree depth, we restrict this maximal depth.
clf = DecisionTreeClassifier(criterion="gini", max_depth=5,
random_state=0).fit(X, y)
reg = DecisionTreeRegressor(criterion="mse", max_depth=5,
random_state=0).fit(X, y)
assert_almost_equal(clf.feature_importances_, reg.feature_importances_)
assert_array_equal(clf.tree_.feature, reg.tree_.feature)
assert_array_equal(clf.tree_.children_left, reg.tree_.children_left)
assert_array_equal(clf.tree_.children_right, reg.tree_.children_right)
assert_array_equal(clf.tree_.n_node_samples, reg.tree_.n_node_samples)
示例13: test_imputation_pipeline_grid_search
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_imputation_pipeline_grid_search():
# Test imputation within a pipeline + gridsearch.
X = sparse_random_matrix(100, 100, density=0.10)
missing_values = X.data[0]
pipeline = Pipeline([('imputer',
SimpleImputer(missing_values=missing_values)),
('tree',
tree.DecisionTreeRegressor(random_state=0))])
parameters = {
'imputer__strategy': ["mean", "median", "most_frequent"]
}
Y = sparse_random_matrix(100, 1, density=0.10).toarray()
gs = GridSearchCV(pipeline, parameters)
gs.fit(X, Y)
示例14: test_ovr_ovo_regressor
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def test_ovr_ovo_regressor():
# test that ovr and ovo work on regressors which don't have a decision_
# function
ovr = OneVsRestClassifier(DecisionTreeRegressor())
pred = ovr.fit(iris.data, iris.target).predict(iris.data)
assert_equal(len(ovr.estimators_), n_classes)
assert_array_equal(np.unique(pred), [0, 1, 2])
# we are doing something sensible
assert_greater(np.mean(pred == iris.target), .9)
ovr = OneVsOneClassifier(DecisionTreeRegressor())
pred = ovr.fit(iris.data, iris.target).predict(iris.data)
assert_equal(len(ovr.estimators_), n_classes * (n_classes - 1) / 2)
assert_array_equal(np.unique(pred), [0, 1, 2])
# we are doing something sensible
assert_greater(np.mean(pred == iris.target), .9)
示例15: generate_regression_data_and_models
# 需要导入模块: from sklearn import tree [as 别名]
# 或者: from sklearn.tree import DecisionTreeRegressor [as 别名]
def generate_regression_data_and_models():
df = pd.DataFrame()
for _ in range(1000):
a = np.random.normal(0, 1)
b = np.random.normal(0, 3)
c = np.random.normal(12, 4)
target = a + b + c
df = df.append({
"A": a,
"B": b,
"C": c,
"target": target
}, ignore_index=True)
reg1 = tree.DecisionTreeRegressor()
reg2 = ensemble.RandomForestRegressor()
column_names = ["A", "B", "C"]
target_name = "target"
X = df[column_names]
reg1.fit(X, df[target_name])
reg2.fit(X, df[target_name])
return df, column_names, target_name, reg1, reg2