本文整理汇总了Python中sklearn.ensemble.ExtraTreesRegressor.fit方法的典型用法代码示例。如果您正苦于以下问题:Python ExtraTreesRegressor.fit方法的具体用法?Python ExtraTreesRegressor.fit怎么用?Python ExtraTreesRegressor.fit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.ExtraTreesRegressor
的用法示例。
在下文中一共展示了ExtraTreesRegressor.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def fit(self, X, y, **kwargs):
for key, value in kwargs.iteritems():
if key in self.INITPARAMS.keys():
self.INITPARAMS[key] = value
model = ExtraTreesRegressor(**self.INITPARAMS)
model.fit(X, y)
self.model = model
示例2: do_etrees
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def do_etrees(filename):
df, Y = create_merged_dataset(filename)
etree = ExtraTreesRegressor(n_estimators=200, n_jobs=-1, min_samples_leaf=5, random_state=SEED)
X = df.drop(['driver', 'trip'], 1)
etree.fit(X, Y)
probs = etree.predict(X[:200])
return pd.DataFrame({'driver': df['driver'][:200], 'trip': df['trip'][:200], 'probs': probs})
示例3: fit
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def fit(self, X, y, weights = None, **kwargs):
if weights is None: weights = np.ones(y.shape[0])
data = np.hstack((y.reshape(y.shape[0],1),X))
S = wcov(data, weights)
corr = wcorr(data, weights)
wsd = np.sqrt(S.diagonal())
ExtraTrees = ExtraTreesRegressor(**kwargs)
ExtraTrees.fit(X,y, sample_weight=weights)
Rsquare = ( S[0,1:].dot(np.linalg.inv(S[1:,1:]).dot(S[1:,0])) )/S[0,0]
# assign proportion of Rsquare to each covariate dep. on importance
self.importances = ExtraTrees.feature_importances_ * Rsquare
model = self.constrained_optimization( corr )
if self.fit_intercept:
w = np.diagflat( weights/np.sum(weights),k=0)
wmean = np.sum(w.dot(data), axis=0)
self.intercept_ = wmean[0] - wsd[0]*np.sum(wmean[1:]*model.x/wsd[1:])
self.coef_ = wsd[0]*model.x/wsd[1:]
return self
示例4: build_models
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def build_models(self):
self.remove_columns(
[
"institute_latitude",
"institute_longitude",
"institute_state",
"institute_country",
"var10",
"var11",
"var12",
"var13",
"var14",
"var15",
"instructor_past_performance",
"instructor_association_industry_expert",
"secondary_area",
"var24",
]
)
model1 = GradientBoostingRegressor(learning_rate=0.1, n_estimators=200, subsample=0.8)
model2 = RandomForestRegressor(n_estimators=50)
model3 = ExtraTreesRegressor(n_estimators=50)
model1.fit(self.X, self.y)
model2.fit(self.X, self.y)
model3.fit(self.X, self.y)
return [model1, model2, model3]
示例5: cal_important_features
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def cal_important_features(batch=10, threshold=1e-4):
X_samples, Y_samples, scaler = dat.data_prepare('ocpm', 'lifetime_ecpm', outlier=0.05)
tot_goot_atrs = {}
for a in ATRS[5:]: tot_goot_atrs[a] = {}
for i in np.arange(1,batch+1):
Ts = timeit.default_timer()
model = ExtraTreesRegressor(n_jobs=6)
model.fit(X_samples, Y_samples)
print "Totally %i features." % len(model.feature_importances_)
print "[Labels] %i categories, %i interests, %i client_names, %i auto_tags" % (num.categories_len, num.interests_len, num.client_names_len, num.auto_tags_len)
good_atrs = show_important_features(model.feature_importances_, threshold)
for a in reversed(ATRS[5:]):
for b in good_atrs[a]:
if b in tot_goot_atrs[a]:
tot_goot_atrs[a][b] += 1
else:
tot_goot_atrs[a][b] = 1
print "%i batch finished in %.1f secs." % (i, (timeit.default_timer() - Ts))
print "------------------------------------------------"
# show performances
for atr in reversed(ATRS[5:]):
print "-------[%s]-----------------------" % atr
for j in np.arange(1,batch+1):
good_keys = [k for k,v in tot_goot_atrs[atr].items() if (v >= j)]
print "%i keys occurs > %i times." % (len(good_keys), j)
return tot_goot_atrs
示例6: predict_with_one
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def predict_with_one(X, out_file_name):
n_samples, n_features = X.shape
iter_num = 3
div = ShuffleSplit(n_samples, n_iter=iter_num, test_size=0.2, random_state=0)
model = ExtraTreesRegressor(n_estimators=5)
score_matrix = np.zeros((n_features, n_features))
t = time()
round_num = 0
for train, test in div:
round_num += 1
train_samples = X[np.array(train)]
test_samples = X[np.array(test)]
for i in range(n_features):
for j in range(n_features):
X_train = train_samples[:, i:i+1]
X_test = test_samples[:, i:i+1]
y_train = train_samples[:, j]
y_test = test_samples[:, j]
# for i in range(len(fl)):
# for j in range(len(fl)):
# if fl[j][1]-fl[j][0] != 1:
# continue
# X_train = train_samples[:, fl[i][0]:fl[i][1]]
# X_test = test_samples[:, fl[i][0]:fl[i][1]]
# y_train = train_samples[:, fl[j][0]]
# y_test = test_samples[:, fl[j][0]]
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
score_matrix[i, j] += mae
print('Round', round_num, '|', i, j, mae, time()-t)
np.savetxt(os.path.join(CODE_PATH, out_file_name),
score_matrix/iter_num, fmt='%.3f', delimiter=',')
示例7: mul_dtree
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def mul_dtree(X, Y2):
forest = ExtraTreesRegressor(n_estimators=5,
compute_importances=True,
random_state=0)
forest.fit(X[:200], Y2[:200])
forest.predict(X[200:])
print Y2[200:]
示例8: classify
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def classify(self):
"""Perform classification"""
clf = ETRegressor(n_estimators=500, min_samples_split=5, min_samples_leaf=2)
#pca = PCA(n_components = 400)
#self._ClassifyDriver__traindata = pca.fit_transform(self._ClassifyDriver__traindata)
#self._ClassifyDriver__testdata = pca.transform(self._ClassifyDriver__testdata)
#print self._ClassifyDriver__traindata.shape
clf.fit(self._ClassifyDriver__traindata, self._ClassifyDriver__trainlabels)
self._ClassifyDriver__y = clf.predict(self._ClassifyDriver__testdata)
示例9: build_extra_tree_regressor
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def build_extra_tree_regressor(X_test, X_train_full, y_train_full):
print "Building ExtraTrees regressor..."
etr = ExtraTreesRegressor(n_estimators=500)
etr.fit(X_train_full, y_train_full)
etr_predict = etr.predict(X_test)
return etr_predict
示例10: extra_trees_regressor
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def extra_trees_regressor(x, y, n_estimators, max_depth):
kf = KFold(len(x), n_folds=3)
scores = []
for train_index, test_index in kf:
X_train, X_test = x[train_index], x[test_index]
y_train, y_test = y[train_index], y[test_index]
clf = ExtraTreesRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=0)
clf.fit(X_train, y_train)
scores.append(mean_squared_error(clf.predict(X_test), y_test) ** 0.5)
return np.mean(scores)
示例11: reg_skl_etr
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def reg_skl_etr(param, data):
[X_tr, X_cv, y_class_tr, y_class_cv, y_reg_tr, y_reg_cv] = data
etr = ExtraTreesRegressor(n_estimators=param['n_estimators'],
max_features=param['max_features'],
n_jobs=param['n_jobs'],
random_state=param['random_state'])
etr.fit(X_tr, y_reg_tr)
pred = etr.predict(X_cv)
RMSEScore = getscoreRMSE(y_reg_cv, pred)
return RMSEScore, pred
示例12: MyExtraTreeReg
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
class MyExtraTreeReg(MyRegressor):
def __init__(self, params=dict()):
self._params = params
self._extree = ExtraTreesRegressor(**(self._params))
def update_params(self, updates):
self._params.update(updates)
self._extree = ExtraTreesRegressor(**(self._params))
def fit(self, Xtrain, ytrain):
self._extree.fit(Xtrain, ytrain)
def predict(self, Xtest, option = None):
return self._extree.predict(Xtest)
def plt_feature_importance(self, fname_list, f_range = list()):
importances = self._extree.feature_importances_
std = np.std([tree.feature_importances_ for tree in self._extree.estimators_], axis=0)
indices = np.argsort(importances)[::-1]
fname_array = np.array(fname_list)
if not f_range:
f_range = range(indices.shape[0])
n_f = len(f_range)
plt.figure()
plt.title("Extra Tree Feature importances")
plt.barh(range(n_f), importances[indices[f_range]],
color="b", xerr=std[indices[f_range]], ecolor='k',align="center")
plt.yticks(range(n_f), fname_array[indices[f_range]])
plt.ylim([-1, n_f])
plt.show()
def list_feature_importance(self, fname_list, f_range = list(), return_list = False):
importances = self._extree.feature_importances_
indices = np.argsort(importances)[::-1]
print 'Extra tree feature ranking:'
if not f_range :
f_range = range(indices.shape[0])
n_f = len(f_range)
for i in range(n_f):
f = f_range[i]
print '{0:d}. feature[{1:d}] {2:s} ({3:f})'.format(f + 1, indices[f], fname_list[indices[f]], importances[indices[f]])
if return_list:
return [indices[f_range[i]] for i in range(n_f)]
示例13: algorithm_ExtraTrees
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def algorithm_ExtraTrees(X_train,Y_train,X_validation,Y_validation, seed=7):
# 训练模型
scaler = StandardScaler().fit(X_train)
rescaledX = scaler.transform(X_train)
gbr = ExtraTreesRegressor(n_estimators=80)
gbr.fit(X=rescaledX, y=Y_train)
# 评估算法模型
rescaledX_validation = scaler.transform(X_validation)
predictions = gbr.predict(rescaledX_validation)
print(mean_squared_error(Y_validation, predictions))
示例14: estimate
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def estimate():
from loadData import loadSets
from helper import splitDataset, separateTargetFromTrain
from sklearn.ensemble import ExtraTreesRegressor
import numpy as np
import math
best_rmsle = 2
best_i = 0
trainingSet, testingSet = loadSets()
testingSet = None
trainingData, testingData = splitDataset(trainingSet, 0.6)
testingData, validationData = splitDataset(testingData, 0.5)
trainingSet = None
trainingTarget, trainingFeatures = separateTargetFromTrain(trainingData)
testingTarget, testingFeatures = separateTargetFromTrain(testingData)
validationTarget, validationFeatures = separateTargetFromTrain(validationData)
testingTarget = testingTarget.values
validationTarget = validationTarget.values
trainingData = None
testingData = None
validationData = None
for i in range(2000, 3001, 1000):
model = ExtraTreesRegressor(n_estimators = i, n_jobs = -1)
model.fit(trainingFeatures, trainingTarget)
predictions = model.predict(testingFeatures)
cost = pow(np.log(predictions + 1) - np.log(testingTarget + 1), 2)
rmsle = math.sqrt(np.mean(cost))
print i, " estimators: ", rmsle
if rmsle < best_rmsle:
best_rmsle = rmsle
best_i = i
print "Best: ", best_i, " estimators with rmsle: ", best_rmsle
model = ExtraTreesRegressor(n_estimators = best_i, n_jobs = -1)
model.fit(trainingFeatures, trainingTarget)
predictions = model.predict(validationFeatures)
cost = pow(np.log(predictions + 1) - np.log(validationTarget + 1), 2)
rmsle = math.sqrt(np.mean(cost))
print "Final model cost: ", rmsle
示例15: dummie_columns_extra_trees
# 需要导入模块: from sklearn.ensemble import ExtraTreesRegressor [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesRegressor import fit [as 别名]
def dummie_columns_extra_trees(train, test):
from sklearn.ensemble import ExtraTreesRegressor
print "-- {} --".format("Extremely Randomized Trees Regression using all but remarks")
predicting_columns = list(train._get_numeric_data().columns.values)
predicting_columns.remove("LISTPRICE")
predicting_columns.remove("SOLDPRICE")
rf = ExtraTreesRegressor(
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 "Accuracy: {}\n".format(score)
return score, predictions