本文整理汇总了Python中sklearn.model_selection.cross_val_predict函数的典型用法代码示例。如果您正苦于以下问题:Python cross_val_predict函数的具体用法?Python cross_val_predict怎么用?Python cross_val_predict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cross_val_predict函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Random_forest
def Random_forest(features,target,test_size_percent=0.2,cv_split=3):
X_array = features.as_matrix()
y_array = target.as_matrix()
model_rdf = RandomForestRegressor()
X_train, X_test, y_train, y_test = train_test_split(X_array, y_array.T.squeeze(), test_size=test_size_percent, random_state=4)
model_rdf.fit(X_train,y_train)
test_prediction = model_rdf.predict(X_test)
tscv = TimeSeriesSplit(cv_split)
training_score = cross_val_score(model_rdf,X_train,y_train,cv=tscv.n_splits)
testing_score = cross_val_score(model_rdf,X_test,y_test,cv=tscv.n_splits)
print"Cross-val Training score:", training_score.mean()
# print"Cross-val Testing score:", testing_score.mean()
training_predictions = cross_val_predict(model_rdf,X_train,y_train,cv=tscv.n_splits)
testing_predictions = cross_val_predict(model_rdf,X_test,y_test,cv=tscv.n_splits)
training_accuracy = metrics.r2_score(y_train,training_predictions)
# test_accuracy_model = metrics.r2_score(y_test,test_prediction_model)
test_accuracy = metrics.r2_score(y_test,testing_predictions)
# print"Cross-val predicted accuracy:", training_accuracy
print"Test-predictions accuracy:",test_accuracy
plot_model(target,y_train,y_test,training_predictions,testing_predictions)
return model_rdf
开发者ID:SOLIMAN68,项目名称:Data-driven_Building_simulation_Polimi_EETBS,代码行数:25,代码来源:master_1_4_eachBuilding_allModels.py
示例2: svm_regressor
def svm_regressor(features,target,test_size_percent=0.2,cv_split=5):
scale=preprocessing.MinMaxScaler()
X_array = scale.fit_transform(features)
y_array = scale.fit_transform(target)
X_train, X_test, y_train, y_test = train_test_split(X_array, y_array.T.squeeze(), test_size=test_size_percent, random_state=4)
svr = SVR(kernel='rbf',C=10,gamma=1)
svr.fit(X_train,y_train.ravel())
test_prediction = svr.predict(X_test)
tscv = TimeSeriesSplit(cv_split)
training_score = cross_val_score(svr,X_train,y_train,cv=tscv.n_splits)
testing_score = cross_val_score(svr,X_test,y_test,cv=tscv.n_splits)
print"Cross-val Training score:", training_score.mean()
# print"Cross-val Testing score:", testing_score.mean()
training_predictions = cross_val_predict(svr,X_train,y_train,cv=tscv.n_splits)
testing_predictions = cross_val_predict(svr,X_test,y_test,cv=tscv.n_splits)
training_accuracy = metrics.r2_score(y_train,training_predictions)
# test_accuracy_model = metrics.r2_score(y_test,test_prediction_model)
test_accuracy = metrics.r2_score(y_test,testing_predictions)
# print"Cross-val predicted accuracy:", training_accuracy
print"Test-predictions accuracy:",test_accuracy
return svr
开发者ID:SOLIMAN68,项目名称:Data-driven_Building_simulation_Polimi_EETBS,代码行数:25,代码来源:master_1_4_eachBuilding_allModels.py
示例3: test_cross_val_predict_with_method
def test_cross_val_predict_with_method():
iris = load_iris()
X, y = iris.data, iris.target
X, y = shuffle(X, y, random_state=0)
classes = len(set(y))
kfold = KFold(len(iris.target))
methods = ['decision_function', 'predict_proba', 'predict_log_proba']
for method in methods:
est = LogisticRegression()
predictions = cross_val_predict(est, X, y, method=method)
assert_equal(len(predictions), len(y))
expected_predictions = np.zeros([len(y), classes])
func = getattr(est, method)
# Naive loop (should be same as cross_val_predict):
for train, test in kfold.split(X, y):
est.fit(X[train], y[train])
expected_predictions[test] = func(X[test])
predictions = cross_val_predict(est, X, y, method=method,
cv=kfold)
assert_array_almost_equal(expected_predictions, predictions)
示例4: linear_regression
def linear_regression(features,target,test_size_percent=0.2,cv_split=5):
''' Features -> Pandas Dataframe with attributes as columns
target -> Pandas Dataframe with target column for prediction
Test_size_percent -> Percentage of data point to be used for testing'''
X_array = features.as_matrix()
y_array = target.as_matrix()
ols = linear_model.LinearRegression()
X_train, X_test, y_train, y_test = train_test_split(X_array, y_array.T.squeeze(), test_size=test_size_percent, random_state=4)
# model = ols.fit(X_train, y_train)
ols.fit(X_train, y_train)
# test_prediction_model = ols.predict(X_test)
tscv = TimeSeriesSplit(cv_split)
training_score = cross_val_score(ols,X_train,y_train,cv=tscv.n_splits)
testing_score = cross_val_score(ols,X_test,y_test,cv=tscv.n_splits)
print"Cross-val Training score:", training_score.mean()
# print"Cross-val Testing score:", testing_score.mean()
training_predictions = cross_val_predict(ols,X_train,y_train,cv=tscv.n_splits)
testing_predictions = cross_val_predict(ols,X_test,y_test,cv=tscv.n_splits)
training_accuracy = metrics.r2_score(y_train,training_predictions)
# test_accuracy_model = metrics.r2_score(y_test,test_prediction_model)
test_accuracy = metrics.r2_score(y_test,testing_predictions)
# print"Cross-val predicted accuracy:", training_accuracy
print"Test-predictions accuracy:",test_accuracy
plot_model(target,y_train,y_test,training_predictions,testing_predictions)
return ols
开发者ID:SOLIMAN68,项目名称:Data-driven_Building_simulation_Polimi_EETBS,代码行数:29,代码来源:master_1_4_eachBuilding_allModels.py
示例5: neural_net
def neural_net(features,target,test_size_percent=0.2,cv_split=3,n_iter=100,learning_rate=0.01):
'''Features -> Pandas Dataframe with attributes as columns
target -> Pandas Dataframe with target column for prediction
Test_size_percent -> Percentage of data point to be used for testing'''
scale=preprocessing.MinMaxScaler()
X_array = scale.fit_transform(features)
y_array = scale.fit_transform(target)
mlp = Regressor(layers=[Layer("Rectifier",units=5), # Hidden Layer1
Layer("Rectifier",units=3) # Hidden Layer2
,Layer("Linear")], # Output Layer
n_iter = n_iter, learning_rate=0.01)
X_train, X_test, y_train, y_test = train_test_split(X_array, y_array.T.squeeze(), test_size=test_size_percent, random_state=4)
mlp.fit(X_train,y_train)
test_prediction = mlp.predict(X_test)
tscv = TimeSeriesSplit(cv_split)
training_score = cross_val_score(mlp,X_train,y_train,cv=tscv.n_splits)
testing_score = cross_val_score(mlp,X_test,y_test,cv=tscv.n_splits)
print"Cross-val Training score:", training_score.mean()
# print"Cross-val Testing score:", testing_score.mean()
training_predictions = cross_val_predict(mlp,X_train,y_train,cv=tscv.n_splits)
testing_predictions = cross_val_predict(mlp,X_test,y_test,cv=tscv.n_splits)
training_accuracy = metrics.r2_score(y_train,training_predictions)
# test_accuracy_model = metrics.r2_score(y_test,test_prediction_model)
test_accuracy = metrics.r2_score(y_test,testing_predictions)
# print"Cross-val predicted accuracy:", training_accuracy
print"Test-predictions accuracy:",test_accuracy
plot_model(target,y_train,y_test,training_predictions,testing_predictions)
return mlp
开发者ID:SOLIMAN68,项目名称:Data-driven_Building_simulation_Polimi_EETBS,代码行数:32,代码来源:master_1_4_eachBuilding_allModels.py
示例6: scan2D
def scan2D(X, y, window=(10, 10), estimator_params=dict(n_jobs=-1), cv=3):
"2D scanning"
inputs, labels, instances = [], [], []
instance_count = 0
for sample, label in zip(X, y):
sample_shape = sample.shape
for s1 in range(sample.shape[0]-window[0]):
for s2 in range(sample.shape[1]-window[1]):
part = sample[s1:s1+window[0], s2:s2+window[1]]
inputs.append(part.flatten())
labels.append(label)
instances.append(instance_count)
instance_count += 1
rf = RandomForestClassifier(**estimator_params)
estimator_params.update({'max_features': 1})
cf = RandomForestClassifier(**estimator_params)
probas1 = cross_val_predict(rf, inputs, labels, cv=cv, method='predict_proba')
probas2 = cross_val_predict(cf, inputs, labels, cv=cv, method='predict_proba')
probas = []
for instance in set(instances):
mask = [i == instance for i in instances]
p1 = probas1[mask]
p2 = probas2[mask]
p = np.concatenate([p1.flatten(), p2.flatten()], axis=0)
probas.append(p)
return probas
示例7: fit
def fit(self, X, y):
# Check data
X, y = np.array(X), np.array(y)
X, y = check_X_y(X, y)
# Split to grow cascade and validate
mask = np.random.random(y.shape[0]) < self.validation_fraction
X_tr, X_vl = X[mask], X[~mask]
y_tr, y_vl = y[mask], y[~mask]
self.classes_ = unique_labels(y)
self.layers_, inp_tr, inp_vl = [], X_tr, X_vl
self.scores_ = []
# First layer
forests = [RandomForestClassifier(max_features=1, n_estimators=self.n_estimators, min_samples_split=10, criterion='gini', n_jobs=-1), # Complete random
RandomForestClassifier(max_features=1, n_estimators=self.n_estimators, min_samples_split=10, criterion='gini', n_jobs=-1), # Complete random
RandomForestClassifier(n_estimators=self.n_estimators, n_jobs=-1),
RandomForestClassifier(n_estimators=self.n_estimators, n_jobs=-1)]
_ = [f.fit(inp_tr, y_tr) for f in forests]
p_vl = [f.predict_proba(inp_vl) for f in forests]
labels = [self.classes_[i] for i in np.argmax(np.array(p_vl).mean(axis=0), axis=1)]
score = self.scoring(y_vl, labels)
self.layers_.append(forests)
self.scores_.append(score)
p_tr = [cross_val_predict(f, inp_tr, y_tr, cv=self.cv, method='predict_proba') for f in forests]
# Fit other layers
last_score = score
inp_tr, inp_vl = np.concatenate([X_tr]+p_tr, axis=1), np.concatenate([X_vl]+p_vl, axis=1)
while True: # Grow cascade
forests = [RandomForestClassifier(max_features=1, n_estimators=self.n_estimators, min_samples_split=10, criterion='gini', n_jobs=-1), # Complete random
RandomForestClassifier(max_features=1, n_estimators=self.n_estimators, min_samples_split=10, criterion='gini', n_jobs=-1), # Complete random
RandomForestClassifier(n_estimators=self.n_estimators, n_jobs=-1),
RandomForestClassifier(n_estimators=self.n_estimators, n_jobs=-1)]
_ = [forest.fit(inp_tr, y_tr) for forest in forests] # Fit the forest
p_vl = [forest.predict_proba(inp_vl) for forest in forests]
labels = [self.classes_[i] for i in np.argmax(np.array(p_vl).mean(axis=0), axis=1)]
score = self.scoring(y_vl, labels)
if score - last_score > self.tolerance:
self.layers_.append(forests)
p_tr = [cross_val_predict(f, inp_tr, y_tr, cv=self.cv, method='predict_proba') for f in forests]
inp_tr, inp_vl = np.concatenate([X_tr]+p_tr, axis=1), np.concatenate([X_vl]+p_vl, axis=1)
self.scores_.append(score)
last_score = score
print(self.scores_)
else:
break
# Retrain on entire dataset
inp_ = X
for forests in self.layers_:
_ = [f.fit(inp_, y) for f in forests]
p = [cross_val_predict(f, inp_, y, cv=self.cv, method='predict_proba') for f in forests]
inp_ = np.concatenate([X]+p, axis=1)
return self
示例8: test_cross_val_predict_sparse_prediction
def test_cross_val_predict_sparse_prediction():
# check that cross_val_predict gives same result for sparse and dense input
X, y = make_multilabel_classification(n_classes=2, n_labels=1,
allow_unlabeled=False,
return_indicator=True,
random_state=1)
X_sparse = csr_matrix(X)
y_sparse = csr_matrix(y)
classif = OneVsRestClassifier(SVC(kernel='linear'))
preds = cross_val_predict(classif, X, y, cv=10)
preds_sparse = cross_val_predict(classif, X_sparse, y_sparse, cv=10)
preds_sparse = preds_sparse.toarray()
assert_array_almost_equal(preds_sparse, preds)
示例9: test_cross_val_predict_pandas
def test_cross_val_predict_pandas():
# check cross_val_score doesn't destroy pandas dataframe
types = [(MockDataFrame, MockDataFrame)]
try:
from pandas import Series, DataFrame
types.append((Series, DataFrame))
except ImportError:
pass
for TargetType, InputFeatureType in types:
# X dataframe, y series
X_df, y_ser = InputFeatureType(X), TargetType(y2)
check_df = lambda x: isinstance(x, InputFeatureType)
check_series = lambda x: isinstance(x, TargetType)
clf = CheckingClassifier(check_X=check_df, check_y=check_series)
cross_val_predict(clf, X_df, y_ser)
示例10: fit
def fit(self, x, y, **params):
""" fit training data """
preds = []
for i, clf in enumerate(self.clfs):
log.info("fit %s"%i)
if "Keras" in str(clf) and "verbose" in params:
params["fit_params"] = dict(verbose=params["verbose"])
# save out-of-fold predictions to fit metaclf
if clf.hasattr("predict_proba"):
method = "predict_proba"
else:
method = "predict"
pred = cross_val_predict(clf, x, y,
cv=self.cv, verbose=0,
method=method,
**params)
preds.append(pred)
# fully fitted to predict test data
clf.fit(x, y, verbose=0)
# fit metaclf on out-of-fold predictions
log.info("fit metaclf")
self.metaclf.fit(np.hstack(preds), y)
return self
示例11: crossval
def crossval(self, verbose=0, seed=0, method="predict", **params):
""" returns crossval score
sets self.preds
"""
# track time spent per run
starttime = time()
np.random.seed(seed)
# useful for keras but throws exception for others
if "Keras" in get_clfname(self.clf):
self.clf.set_params(verbose=verbose)
self.clf.set_params(**params)
self.preds = cross_val_predict(self.clf, self.xtrain, self.ytrain,
method=method)
score = self.scorer._score_func(self.ytrain, self.preds) \
* self.scorer._sign
# log results
params.update(clf=get_clfname(self.clf),
name=self.name,
score=score,
elapsed=time()-starttime)
if self.runs:
self.runs.append(params, self.preds)
return score
示例12: test_cross_val_predict_input_types
def test_cross_val_predict_input_types():
clf = Ridge()
# Smoke test
predictions = cross_val_predict(clf, X, y)
assert_equal(predictions.shape, (10,))
# test with multioutput y
predictions = cross_val_predict(clf, X_sparse, X)
assert_equal(predictions.shape, (10, 2))
predictions = cross_val_predict(clf, X_sparse, y)
assert_array_equal(predictions.shape, (10,))
# test with multioutput y
predictions = cross_val_predict(clf, X_sparse, X)
assert_array_equal(predictions.shape, (10, 2))
# test with X and y as list
list_check = lambda x: isinstance(x, list)
clf = CheckingClassifier(check_X=list_check)
predictions = cross_val_predict(clf, X.tolist(), y.tolist())
clf = CheckingClassifier(check_y=list_check)
predictions = cross_val_predict(clf, X, y.tolist())
# test with 3d X and
X_3d = X[:, :, np.newaxis]
check_3d = lambda x: x.ndim == 3
clf = CheckingClassifier(check_X=check_3d)
predictions = cross_val_predict(clf, X_3d, y)
assert_array_equal(predictions.shape, (10,))
示例13: crossVertifyTestData
def crossVertifyTestData(self):
"""
交叉验证Test数据并返回结果
:param self: 类变量本身
:returns: 返回真正的y和预测的y,真正的y在前面
"""
# 进行交叉验证
predict_y = cross_val_predict(self.model, self.test_X, cv=10)
return self.test_y, predict_y
示例14: _get_estimator_mse
def _get_estimator_mse(self, x, y, estimator):
"""Return the RMSE for *estimator*.
Use GroupKFold where a group is a combination of input size and number
of workers. The prediction of a group is done when it is out of the
training set.
"""
groups = self._groups.loc[x.index]
cv = GroupKFold(n_splits=3)
prediction = cross_val_predict(estimator, x, y, groups, cv)
return metrics.mean_squared_error(y, prediction)
示例15: save_fit_plot
def save_fit_plot(x, y, fit, name, folder):
predicted = cross_val_predict(fit, x, y, cv=10)
linfit = np.polyfit(y, predicted, 1)
fig, ax = plt.subplots()
ax.scatter(y, predicted, s=1, alpha=0.1)
ax.plot([y.min(), y.max()], [y.min(), y.max()], "k--", lw=2)
ax.plot(y, np.poly1d(linfit)(y), "g--", lw=2)
ax.set_xlabel("Measured")
ax.set_ylabel("Predicted")
f_name = timed_filename(name, "pdf")
plt.savefig(os.path.join(folder, f_name))