本文整理汇总了Python中sklearn.model_selection.RandomizedSearchCV.predict方法的典型用法代码示例。如果您正苦于以下问题:Python RandomizedSearchCV.predict方法的具体用法?Python RandomizedSearchCV.predict怎么用?Python RandomizedSearchCV.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.model_selection.RandomizedSearchCV
的用法示例。
在下文中一共展示了RandomizedSearchCV.predict方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_large_grid
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def test_large_grid():
"""In this test, we purposely overfit a RandomForest to completely random data
in order to assert that the test error will far supercede the train error.
"""
if not SK18:
custom_cv = KFold(n=y_train.shape[0], n_folds=3, shuffle=True, random_state=42)
else:
custom_cv = KFold(n_splits=3, shuffle=True, random_state=42)
# define the pipe
pipe = Pipeline([
('scaler', SelectiveScaler()),
('pca', SelectivePCA(weight=True)),
('rf', RandomForestClassifier(random_state=42))
])
# define hyper parameters
hp = {
'scaler__scaler': [StandardScaler(), RobustScaler(), MinMaxScaler()],
'pca__whiten': [True, False],
'pca__weight': [True, False],
'pca__n_components': uniform(0.75, 0.15),
'rf__n_estimators': randint(5, 10),
'rf__max_depth': randint(5, 15)
}
# define the grid
grid = RandomizedSearchCV(pipe, hp, n_iter=2, scoring='accuracy', n_jobs=1, cv=custom_cv, random_state=42)
# this will fail because we haven't fit yet
assert_fails(grid.score, (ValueError, AttributeError), X_train, y_train)
# fit the grid
grid.fit(X_train, y_train)
# score for coverage -- this might warn...
with warnings.catch_warnings():
warnings.simplefilter("ignore")
grid.score(X_train, y_train)
# coverage:
assert grid._estimator_type == 'classifier'
# get predictions
tr_pred, te_pred = grid.predict(X_train), grid.predict(X_test)
# evaluate score (SHOULD be better than random...)
accuracy_score(y_train, tr_pred), accuracy_score(y_test, te_pred)
# grid score reports:
# assert fails for bad percentile
assert_fails(report_grid_score_detail, ValueError, **{'random_search': grid, 'percentile': 0.0})
assert_fails(report_grid_score_detail, ValueError, **{'random_search': grid, 'percentile': 1.0})
# assert fails for bad y_axis
assert_fails(report_grid_score_detail, ValueError, **{'random_search': grid, 'y_axis': 'bad_axis'})
# assert passes otherwise
report_grid_score_detail(grid, charts=True, percentile=0.95) # just ensure percentile works
示例2: build_nn
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def build_nn(x_train, y_train, x_test, y_test, n_features):
"""
Constructing a regression neural network model from input dataframe
:param x_train: features dataframe for model training
:param y_train: target dataframe for model training
:param x_test: features dataframe for model testing
:param y_test: target dataframe for model testing
:return: None
"""
net = NeuralNet(layers=[('input', InputLayer),
('hidden0', DenseLayer),
('hidden1', DenseLayer),
('output', DenseLayer)],
input_shape=(None, x_train.shape[1]), # Number of i/p nodes = number of columns in x
hidden0_num_units=15,
hidden0_nonlinearity=lasagne.nonlinearities.softmax,
hidden1_num_units=17,
hidden1_nonlinearity=lasagne.nonlinearities.softmax,
output_num_units=1, # Number of o/p nodes = number of columns in y
output_nonlinearity=lasagne.nonlinearities.softmax,
max_epochs=100,
update_learning_rate=0.01,
regression=True,
verbose=0)
# Finding the optimal set of params for each variable in the training of the neural network
param_dist = {'hidden0_num_units':sp_randint(3, 30), 'hidden1_num_units':sp_randint(3, 30)}
clf = RandomizedSearchCV(estimator=net, param_distributions=param_dist,
n_iter=15, n_jobs=-1)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
# Mean absolute error regression loss
mean_abs = sklearn.metrics.mean_absolute_error(y_test, y_pred)
# Mean squared error regression loss
mean_sq = sklearn.metrics.mean_squared_error(y_test, y_pred)
# Median absolute error regression loss
median_abs = sklearn.metrics.median_absolute_error(y_test, y_pred)
# R^2 (coefficient of determination) regression score function
r2 = sklearn.metrics.r2_score(y_test, y_pred)
# Explained variance regression score function
exp_var_score = sklearn.metrics.explained_variance_score(y_test, y_pred)
with open('../trained_networks/nn_%d_data.pkl' % n_features, 'wb') as results:
pickle.dump(clf, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(net, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(mean_abs, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(mean_sq, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(median_abs, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(r2, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(exp_var_score, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(y_pred, results, pickle.HIGHEST_PROTOCOL)
return
示例3: test_randomgridsearch_slm
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def test_randomgridsearch_slm(make_gaus_data):
X, y, Xs, ys = make_gaus_data
slm = StandardLinearModel(LinearBasis(onescol=True))
param_dict = {
'var': [Parameter(1.0 / v, Positive()) for v in range(1, 6)]
}
estimator = RandomizedSearchCV(slm, param_dict, n_jobs=-1, n_iter=2)
estimator.fit(X, y)
Ey = estimator.predict(Xs)
assert len(ys) == len(Ey) # we just want to make sure this all runs
示例4: test_randomgridsearch_glm
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def test_randomgridsearch_glm(make_gaus_data):
X, y, Xs, ys = make_gaus_data
glm = GeneralizedLinearModel(Gaussian(), LinearBasis(onescol=True),
random_state=1, maxiter=100)
param_dict = {'batch_size': range(1, 11)}
estimator = RandomizedSearchCV(glm, param_dict, verbose=1, n_jobs=-1,
n_iter=2)
estimator.fit(X, y)
Ey = estimator.predict(Xs)
assert len(ys) == len(Ey) # we just want to make sure this all runs
示例5: test_pickle
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def test_pickle():
# Test that a fit search can be pickled
clf = MockClassifier()
grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=True)
grid_search.fit(X, y)
grid_search_pickled = pickle.loads(pickle.dumps(grid_search))
assert_array_almost_equal(grid_search.predict(X),
grid_search_pickled.predict(X))
random_search = RandomizedSearchCV(clf, {'foo_param': [1, 2, 3]},
refit=True, n_iter=3)
random_search.fit(X, y)
random_search_pickled = pickle.loads(pickle.dumps(random_search))
assert_array_almost_equal(random_search.predict(X),
random_search_pickled.predict(X))
示例6: build_lasso
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def build_lasso(x_train, y_train, x_test, y_test, n_features):
"""
Constructing a Lasso linear model with cross validation from input dataframe
:param x_train: features dataframe for model training
:param y_train: target dataframe for model training
:param x_test: features dataframe for model testing
:param y_test: target dataframe for model testing
:return: None
"""
model = Lasso(random_state=1)
# Random state has int value for non-random sampling
param_dist = {'alpha': np.arange( 0.0001, 1, 0.001 ).tolist()}
clf = RandomizedSearchCV(estimator=model, param_distributions=param_dist,
n_iter=15, n_jobs=-1)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print(clf.best_params_, clf.best_score_)
# Mean absolute error regression loss
mean_abs = sklearn.metrics.mean_absolute_error(y_test, y_pred)
# Mean squared error regression loss
mean_sq = sklearn.metrics.mean_squared_error(y_test, y_pred)
# Median absolute error regression loss
median_abs = sklearn.metrics.median_absolute_error(y_test, y_pred)
# R^2 (coefficient of determination) regression score function
r2 = sklearn.metrics.r2_score(y_test, y_pred)
# Explained variance regression score function
exp_var_score = sklearn.metrics.explained_variance_score(y_test, y_pred)
with open('../trained_networks/lasso_%d_data.pkl' % n_features, 'wb') as results:
pickle.dump(clf, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(mean_abs, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(mean_sq, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(median_abs, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(r2, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(exp_var_score, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(y_pred, results, pickle.HIGHEST_PROTOCOL)
return
示例7: build_tree
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
def build_tree(x_train, y_train, x_test, y_test, n_features):
"""
Constructing a decision trees regression model from input dataframe
:param x_train: features dataframe for model training
:param y_train: target dataframe for model training
:param x_test: features dataframe for model testing
:param y_test: target dataframe for model testing
:return: None
"""
model = DecisionTreeRegressor()
param_dist = {'max_depth': sp_randint(1, 15),
'min_samples_split': sp_randint(2, 15)}
clf = RandomizedSearchCV(estimator=model, param_distributions=param_dist,
n_iter=15, n_jobs=-1)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print(clf.best_params_, clf.best_score_)
# Mean absolute error regression loss
mean_abs = sklearn.metrics.mean_absolute_error(y_test, y_pred)
# Mean squared error regression loss
mean_sq = sklearn.metrics.mean_squared_error(y_test, y_pred)
# Median absolute error regression loss
median_abs = sklearn.metrics.median_absolute_error(y_test, y_pred)
# R^2 (coefficient of determination) regression score function
r2 = sklearn.metrics.r2_score(y_test, y_pred)
# Explained variance regression score function
exp_var_score = sklearn.metrics.explained_variance_score(y_test, y_pred)
with open('../trained_networks/dt_%d_data.pkl' % n_features, 'wb') as results:
pickle.dump(clf, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(mean_abs, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(mean_sq, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(median_abs, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(r2, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(exp_var_score, results, pickle.HIGHEST_PROTOCOL)
pickle.dump(y_pred, results, pickle.HIGHEST_PROTOCOL)
return
示例8: uniform
# 需要导入模块: from sklearn.model_selection import RandomizedSearchCV [as 别名]
# 或者: from sklearn.model_selection.RandomizedSearchCV import predict [as 别名]
return tf.maximum(alpha * z, z, name=name)
return parametrized_leaky_relu
param_distribs = {
"n_neurons": [50, 100],
"learning_rate": uniform(0.001, 0.01),
"activation": [tf.nn.elu, leaky_relu(alpha=0.01)],
}
rnd_search = RandomizedSearchCV(DNNClassifier(random_state=42), param_distribs,
n_iter=20,
fit_params={"X_valid": X_valid1, "y_valid": y_valid1, "n_epochs": 30},
random_state=42, verbose=2)
rnd_search.fit(X_train1, y_train1)
y_pred1 = rnd_search.predict(X_test1)
print(accuracy_score(y_test1, y_pred1))
rnd_search.best_estimator_.save(modeldir + '/my_best_mnist_0_4')
dnn_clf = DNNClassifier(learning_rate=0.0011596625, batch_size=200)
dnn_clf.fit(X_train1, y_train1, n_epochs=30, X_valid = X_valid1, y_valid = y_valid1)
y_pred = dnn_clf.predict(X_test1)
accuracy_score(y_test1, y_pred)
y_pred_train = dnn_clf.predict(X_train1)
accuracy_score(y_train1, y_pred_train)
dnn_clf_bn = DNNClassifier(learning_rate=0.0011596625, batch_size=200, batch_norm_momentum=0.95)
dnn_clf_bn.fit(X_train1, y_train1, n_epochs=30, X_valid = X_valid1, y_valid = y_valid1)
y_pred = dnn_clf_bn.predict(X_test1)
accuracy_score(y_test1, y_pred)