当前位置: 首页>>代码示例>>Python>>正文


Python GridSearchCV.score方法代码示例

本文整理汇总了Python中sklearn.model_selection.GridSearchCV.score方法的典型用法代码示例。如果您正苦于以下问题:Python GridSearchCV.score方法的具体用法?Python GridSearchCV.score怎么用?Python GridSearchCV.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.model_selection.GridSearchCV的用法示例。


在下文中一共展示了GridSearchCV.score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_grid_search_score_method

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def test_grid_search_score_method():
    X, y = make_classification(n_samples=100, n_classes=2, flip_y=.2,
                               random_state=0)
    clf = LinearSVC(random_state=0)
    grid = {'C': [.1]}

    search_no_scoring = GridSearchCV(clf, grid, scoring=None).fit(X, y)
    search_accuracy = GridSearchCV(clf, grid, scoring='accuracy').fit(X, y)
    search_no_score_method_auc = GridSearchCV(LinearSVCNoScore(), grid,
                                              scoring='roc_auc').fit(X, y)
    search_auc = GridSearchCV(clf, grid, scoring='roc_auc').fit(X, y)

    # Check warning only occurs in situation where behavior changed:
    # estimator requires score method to compete with scoring parameter
    score_no_scoring = search_no_scoring.score(X, y)
    score_accuracy = search_accuracy.score(X, y)
    score_no_score_auc = search_no_score_method_auc.score(X, y)
    score_auc = search_auc.score(X, y)

    # ensure the test is sane
    assert_true(score_auc < 1.0)
    assert_true(score_accuracy < 1.0)
    assert_not_equal(score_auc, score_accuracy)

    assert_almost_equal(score_accuracy, score_no_scoring)
    assert_almost_equal(score_auc, score_no_score_auc)
开发者ID:YinongLong,项目名称:scikit-learn,代码行数:28,代码来源:test_search.py

示例2: score_nestedCV

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
    def score_nestedCV(self, G1, model, param_grid, effect, nested):
        k_fold = model_selection.KFold(n_splits=self.n_folds).split(range(self.Y.shape[0]))
        i_fold=0
        scores = sp.zeros(self.n_folds)
        params = list()

        for train, test in k_fold:
            (trainData, trainY) = self._packData(G1, train, effect)
            (testData, testY) = self._packData(G1, test, effect)

            if nested:
                clf = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs = self.n_jobs_grid,
                                   cv=self.n_folds_params, scoring=self.scoring, verbose=self.verbose)

                clf.fit(trainData, trainY.flatten())

                params.append(clf.best_params_)

                scores[i_fold] = clf.score(testData, testY.flatten(), method_scorer=False)
            else:

                model.fit(trainData, trainY.flatten())
                scores[i_fold] = SCORERS[self.scoring](model, testData, testY.flatten())
            i_fold+=1

        return scores,params
开发者ID:MicrosoftGenomics,项目名称:FaST-LMM,代码行数:28,代码来源:testCV.py

示例3: validate_model

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def validate_model(model, parameter_set, train_data, test_data):
    clf = GridSearchCV(estimator=model, param_grid=parameter_set, n_jobs=3, cv=10)

    clf.fit(train_data[0], train_data[1])

    grid_scores = clf.grid_scores_
    best_score = clf.best_score_
    best_params = clf.best_params_
    test_score = clf.score(test_data[0], test_data[1])
    
    return grid_scores, best_score, best_params, test_score
开发者ID:TaihuLight,项目名称:wisconsin-breast-cancer,代码行数:13,代码来源:grid_search.py

示例4: _build_model_compute_statistics

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def _build_model_compute_statistics(fset_path, model_type, model_params,
                                    params_to_optimize, model_path):
    '''Build model and return summary statistics.

    Parameters
    ----------
    fset_path : str
        Path to feature set .npz file.
    model_type : str
        Type of model to be built, e.g. 'RandomForestClassifier'.
    model_params : dict
        Dictionary with hyperparameter values to be used in model building.
        Keys are parameter names, values are the associated parameter values.
        These hyperparameters will be passed to the model constructor as-is
        (for hyperparameter optimization, see `params_to_optimize`).
    params_to_optimize : dict or list of dict
        During hyperparameter optimization, various model parameters
        are adjusted to give an optimal fit. This dictionary gives the
        different values that should be explored for each parameter. E.g.,
        `{'alpha': [1, 2], 'beta': [4, 5, 6]}` would fit models on all
        6 combinations of alpha and beta and compare the resulting models'
        goodness-of-fit. If None, only those hyperparameters specified in
        `model_parameters` will be used (passed to model constructor as-is).
    model_path : str
        Path indicating where serialized model will be saved.

    Returns
    -------
    score : float
        The model's training score.
    best_params : dict
        Dictionary of best hyperparameter values (keys are parameter names,
        values are the corresponding best values) determined by `scikit-learn`'s
        `GridSearchCV`. If no hyperparameter optimization is performed (i.e.
        `params_to_optimize` is None or is an empty dict, this will be an empty
        dict.
    '''
    fset, data = featurize.load_featureset(fset_path)
    if len(data['labels']) != len(fset):
        raise ValueError("Cannot build model for unlabeled feature set.")
    n_jobs = (model_params.pop('n_jobs') if 'n_jobs' in model_params
              and params_to_optimize else -1)
    model = MODELS_TYPE_DICT[model_type](**model_params)
    if params_to_optimize:
        model = GridSearchCV(model, params_to_optimize,
                             n_jobs=n_jobs)
    model.fit(fset, data['labels'])
    score = model.score(fset, data['labels'])
    best_params = model.best_params_ if params_to_optimize else {}
    joblib.dump(model, model_path)

    return score, best_params
开发者ID:cesium-ml,项目名称:cesium_web,代码行数:54,代码来源:model.py

示例5: svr_cv

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def svr_cv(args, X, y):
  testing = args.get('testing')
  alg = args.get('algorithm')
  cvs = args.get('cross-validations')

  scaler = preprocessing.StandardScaler()

  scores = ['precision', 'recall' ]
  score = scores[0]
  #svr = LinearSVR(C=1.0, dual=True, epsilon=0.0, fit_intercept=True,
  #   intercept_scaling=1.0, loss='epsilon_insensitive', max_iter=1000,
  #   random_state=0, tol=0.0001, verbose=0)
  svr = LinearSVR(random_state=0)
  
  C = [1] 
  eps = [1e-6]
  max_iter = [1e6]
  tol = [1e-5]

  hyperparameters = { 'linearsvr__C': C, 'linearsvr__epsilon' : eps, 'linearsvr__max_iter' : max_iter, 'linearsvr__tol' : tol }
  
  pipeline = make_pipeline(scaler, svr)

  print "cross-validating"
  t0 = timestamp()
  clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, refit=True)
  log_time(t0,"initialize GridSearchCV")
  #print clf
  t0 = timestamp()
  clf.fit(X, y)
  log_time(t0,"fit to GridSearchCV")

  log_write('clf:' + str(clf))
  #log_time(t0, 'preprocess data')
  print "Best parameters found:"
  print clf.best_params_

  t0 = timestamp()
  score_train = clf.score(X, y)
  log_time(t0,"compute training score")
  #score_test = clf.score(X_test, y_test)
  #svr_params = clf.get_params()

  print 'score_train: ' + str(score_train)
  #print 'score_test: ' + str(score_test)
  #print 'svr_params: ' + str(svr_params)

  #score = cross_val_score()
  #print 'training neg_mean_sq_error:' + str(cross_val_score(clf, X, y, scoring='neg_mean_squared_error'))

  result = { 'clf': clf, 'params':clf.best_params_, 'score_train':score_train }
  return result
开发者ID:PositronicsLab,项目名称:wild-robot,代码行数:54,代码来源:wb_linearsvr.py

示例6: fit_model

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
    def fit_model(self, model_name, classifier, parameters):
        """
        Fit a given classifier using sklearn grid_search and a cv of 5

        Returns: [model_name, best_score, test_score]
        """
        clf = GridSearchCV(classifier, parameters, cv=5)
        clf.fit(self.X_train[['x1', 'x2']], self.y_train)

        best_score = max(clf.cv_results_['mean_train_score'])
        test_score = clf.score(self.X_test, self.y_test)

        return [model_name, best_score, test_score]
开发者ID:tylergeery,项目名称:coursera,代码行数:15,代码来源:problem3_3.py

示例7: test_grid_search

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def test_grid_search():
    # Test that the best estimator contains the right value for foo_param
    clf = MockClassifier()
    grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, verbose=3)
    # make sure it selects the smallest parameter in case of ties
    old_stdout = sys.stdout
    sys.stdout = StringIO()
    grid_search.fit(X, y)
    sys.stdout = old_stdout
    assert_equal(grid_search.best_estimator_.foo_param, 2)

    assert_array_equal(grid_search.results_["param_foo_param"].data, [1, 2, 3])

    # Smoke test the score etc:
    grid_search.score(X, y)
    grid_search.predict_proba(X)
    grid_search.decision_function(X)
    grid_search.transform(X)

    # Test exception handling on scoring
    grid_search.scoring = 'sklearn'
    assert_raises(ValueError, grid_search.fit, X, y)
开发者ID:1992huanghai,项目名称:scikit-learn,代码行数:24,代码来源:test_search.py

示例8: test_grid_search_no_score

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def test_grid_search_no_score():
    # Test grid-search on classifier that has no score function.
    clf = LinearSVC(random_state=0)
    X, y = make_blobs(random_state=0, centers=2)
    Cs = [.1, 1, 10]
    clf_no_score = LinearSVCNoScore(random_state=0)
    grid_search = GridSearchCV(clf, {'C': Cs}, scoring='accuracy')
    grid_search.fit(X, y)

    grid_search_no_score = GridSearchCV(clf_no_score, {'C': Cs},
                                        scoring='accuracy')
    # smoketest grid search
    grid_search_no_score.fit(X, y)

    # check that best params are equal
    assert_equal(grid_search_no_score.best_params_, grid_search.best_params_)
    # check that we can call score and that it gives the correct result
    assert_equal(grid_search.score(X, y), grid_search_no_score.score(X, y))

    # giving no scoring function raises an error
    grid_search_no_score = GridSearchCV(clf_no_score, {'C': Cs})
    assert_raise_message(TypeError, "no scoring", grid_search_no_score.fit,
                         [[1]])
开发者ID:YinongLong,项目名称:scikit-learn,代码行数:25,代码来源:test_search.py

示例9: main

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def main():
    train_data=load_data('../input/train.csv')
    test_data=load_data('../input/test.csv')

    train_data=preprocess_data(train_data)

    PassengerId=train_data['PassengerId']
    train_data=train_data.drop('PassengerId', axis=1)

    train_target=train_data['Survived']
    train_data=train_data.drop('Survived', axis=1)


    Xtrain, Xtest, ytrain, ytest = train_test_split(train_data, train_target, test_size=0.20, random_state=36)

    print (Xtrain.shape)
    print (Xtest.shape)

    clf = svm.SVC()
    clf.fit(Xtrain, ytrain)
    print ('svm:', clf.score(Xtest, ytest))
    svm_scores = cross_val_score(clf, train_data, train_target, cv=5)
    print ('svm cross score:', svm_scores, svm_scores.mean())

    dt=tree.DecisionTreeClassifier()
    dt.fit(Xtrain, ytrain)
    print ('decision tree:', dt.score(Xtest, ytest))
    dt_scores = cross_val_score(dt, train_data, train_target, cv=5)
    print ('dt cross score:', dt_scores, dt_scores.mean())

    rf = RandomForestClassifier(n_estimators=100,min_samples_split=5)
    rf.fit(Xtrain, ytrain)
    print ('random forest:', rf.score(Xtest, ytest))
    rf_scores = cross_val_score(rf, train_data, train_target, cv=5)
    print ('rf cross score:', rf_scores, rf_scores.mean())

    knnClf=KNeighborsClassifier(weights='uniform')
    parameters = {'n_neighbors':[3,4,5], 'p':[1,2]}
    model = GridSearchCV(knnClf, param_grid=parameters)
    model.fit(Xtrain,ytrain) 
    print ('knn:', model.score(Xtest, ytest))  
    knnClf_scores = cross_val_score(model, train_data, train_target, cv=5)
    print ('knn cross score:', knnClf_scores, knnClf_scores.mean())
开发者ID:fzhurd,项目名称:fzwork,代码行数:45,代码来源:titanic_v11h2.py

示例10: load_digits

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.svm import SVC

digits = load_digits()
X_digits, y_digits = digits.data, digits.target
X_digits_train, X_digits_test, y_digits_train, y_digits_test = train_test_split(X_digits, y_digits, random_state=1)

param_grid = {'C': [0.001, 0.01, 0.1, 1, 10],
              'gamma': [0.01, 0.1, 1, 10, 100]}

grid = GridSearchCV(SVC(), param_grid=param_grid, cv=5, verbose=3)
grid.fit(X_digits_train, y_digits_train)
print('Best score for SVC: {}'.format(grid.score(X_digits_test, y_digits_test)))
print('Best parameters for SVC: {}'.format(grid.best_params_))
开发者ID:Knowa42,项目名称:Classes,代码行数:17,代码来源:18_svc_grid.py

示例11: GridSearchCV

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
# The sklearn provides an object that, given data, computes the score during the fit of an estimator
# on a parameter grid and chooses the parameters to maximize the cross-validation score. This object
# takes an estimator during the construction and exposes an estimator API:
Cs = np.logspace(-6, -1, 10)
clf = GridSearchCV(estimator=svc, param_grid=dict(C=Cs),
                   n_jobs=-1)
print(clf.fit(X_digits[:1000], y_digits[:1000]))

print(clf.best_score_)

print(clf.best_estimator_.C)


# Prediction performance on test set is not as good as on train set
print(clf.score(X_digits[1000:], y_digits[1000:]))

# By default, the GridSearchCV uses a 3-fold cross-validation.
# However, if it detects that a classifier is passed, rather than a regressor, it uses a stratified 3-fold.
lasso = linear_model.LassoCV()
diabetes = datasets.load_diabetes()
X_diabetes = diabetes.data
y_diabetes = diabetes.target
print(lasso.fit(X_diabetes, y_diabetes))

# The estimator chose automatically its lambda:
print(lasso.alpha_)

'''
Unsupervised learning: seeking representations of the data
http://scikit-learn.org/stable/tutorial/statistical_inference/unsupervised_learning.html
开发者ID:klAndersen,项目名称:scikit-learn_tutorials,代码行数:32,代码来源:statistical_learning.py

示例12: PCA

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
# construct a pipeline to to select the best params
pca = PCA()
svc = SVC()
ncomponents = [30, 60, 90, 120]
Cs = np.logspace(-4,4,5)
whiten = [True, False]
pipe = Pipeline(steps=[('pca', pca), ('svm', svc)])
params = {
    'pca__n_components': ncomponents,
    'pca__whiten': whiten,
    'svm__C': Cs,
}

# train the grid search
estimator = GridSearchCV(pipe, params, n_jobs=-1, verbose=0)
print "begin training the grid search "
t0 = time()
estimator.fit(train_data, train_label)
print "fitting done with {} seconds".format(time()-t0)
print "grid search with best score {}".format(estimator.best_score_)
best_params = estimator.best_params_
for param_name in sorted(params):
    print "{} : {}".format(param_name, best_params[param_name])

# give the actually test score
score = estimator.score(val_data, val_label)
print "With score {}".format(score)

# do the prediction and save to file
prediction = estimator.predict(test_X)
dh.write_data(prediction, '../dataset/svm_out.csv')
开发者ID:JiangQH,项目名称:kaggle,代码行数:33,代码来源:grid_svm.py

示例13: print

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
gamma), and the values are the parameter settings we want to try out. Trying the values
0.001, 0.01, 0.1, 1, 10, and 100 for C and gamma translates to the following
dictionary
"""
param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100],'gamma': [0.001, 0.01, 0.1, 1, 10, 100]}
print("Parameter grid:\n{}".format(param_grid))

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
grid_search = GridSearchCV(SVC(), param_grid, cv=5)

X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0)

grid_search.fit(X_train, y_train)

print("Test set score: {:.2f}".format(grid_search.score(X_test, y_test)))

print("Best parameters: {}".format(grid_search.best_params_))
print("Best cross-validation score: {:.2f}".format(grid_search.best_score_))

print("Best estimator:\n{}".format(grid_search.best_estimator_))

# Analyzing the result of cross-validation
# convert to DataFrame
results = pd.DataFrame(grid_search.cv_results_)
# show the first 5 rows
results.head()

scores = np.array(results.mean_test_score).reshape(6, 6)
# plot the mean cross-validation scores
mglearn.tools.heatmap(scores, xlabel='gamma', xticklabels=param_grid['gamma'],
开发者ID:hitesh789,项目名称:datasharing,代码行数:33,代码来源:Chp-5+Model+Evaluation+and+Improvement.py

示例14: svr_preprocess

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def svr_preprocess(args, X_raw, y_raw):
  alg = args.get('algorithm')
  split_sz = args.get('split-size')
  #cvs = args.get('cross-validations')
  threads = args.get('threads')

  ## preprocessing
  print 'preprocessing'
  t0 = timestamp()
  X_train, X_test, y_train, y_test = train_test_split(X_raw, y_raw, 
                                                      test_size=split_sz,
                                                      shuffle=True, 
                                                      stratify=y_raw)
   
  #svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
  #svr_lin = SVR(kernel='linear', C=1e3, epsilon=0.2)
  #svr_poly = SVR(kernel='poly', C=1e3, degree=2)
  #y_rbf = svr_rbf.fit(X_train_scaled, y).predict(X_test_scaled)
  #y_lin = svr_lin.fit(X_train_scaled, y).predict(X_test_scaled)
  #y_poly = svr_poly.fit(X_train_scaled, y).predict(X_test_scaled)
  #svr = SVR()
  #print svr.get_params()
 
  ## NOTE: May need to change scaler 
  scaler = preprocessing.StandardScaler()

  #hyperparameters = [ { 'kernel': ['rbf'], 'gamma': [1e-3,1e-2,1e-1], 'C': [1,1e1,1e2,1e3] } ,{ 'kernel': ['linear'], 'C': [1,1e1,1e2,1e3] } ]

  scores = ['precision', 'recall' ]
  score = scores[0]
  if alg == 'rbf':
    C = [1e-4, 1e-3, 1e-2, 1e-1, 1] 
    eps = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1] 
    gamma = [1e-3, 1e-2, 1e-1, 1, 1e1]

    ## two hours
    #C = [1e-2, 1e-1, 1, 1e1, 1e2, 1e3]  # 1e-1
    #eps = [1e-3, 1e-4, 1e-5, 1e-6]      # 1e-3, 1e-4, 1e-5
    #gamma = [1e-3, 1e-2, 1e-1, 1]       # 1e-1

    ## best - 1/2 hour
    #C = [1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3] 
    #eps = [1e-1, 1e-2, 1e-3] 
    #gamma = [1e-1, 1, 1e1]

    ## fast - 10 min
    #C = [1e-3, 1e-2, 1e-1, 1] 
    #eps = [1e-1, 1e-2, 1e-3] 
    ##gamma = ['auto']
    #gamma = [1e-1, 1, 1e1]

    svr = SVR(kernel='rbf')
    hyperparameters = { 'svr__C': C, 'svr__epsilon' : eps, 'svr__gamma' : gamma }
  elif alg == 'linear':
    svr = SVR(kernel='linear', max_iter=1e6)
    hyperparameters = { 'svr__C': [1, 1e-1, 1e-2, 1e-3], 'svr__epsilon' : [1e-1, 1e-2, 1e-3]}
  elif alg == 'poly':
    degree = [1,2,3] 

    svr = SVR(kernel='poly', max_iter=1e6)
    hyperparameters = { 'svr__C': [1, 1e-1, 1e-2, 1e-3], 'svr__epsilon' : [1e-1, 1e-2, 1e-3], 'svr__degree' : [1,2,3]}
  else:
    sys.exit(1)

  #hyperparameters = { 'svr__degree' : [2, 3, 4], 'svr__C': [1e3, 1e5, 1e7], 'svr__epsilon' : [0.1], 'svr__max_iter' : [1e4, 1e5, 1e6], 'svr__tol' : [0.001, 0.0001]}
  
  pipeline = make_pipeline(scaler, svr)

  print "cross-validating"
  #clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, scoring='%s_macro' % score)
  #clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, scoring='%s_macro' % score)
  clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads)
  #clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, refit=True)
  #print clf
  clf.fit(X_train, y_train)
  log_write('clf:' + str(clf))
  log_time(t0, 'preprocess data')
  print "Best parameters found:"
  print clf.best_params_

  score_train = clf.score(X_train, y_train)
  score_test = clf.score(X_test, y_test)
  #svr_params = clf.get_params()

  print 'score_train: ' + str(score_train)
  print 'score_test: ' + str(score_test)
  #print 'svr_params: ' + str(svr_params)

  #score = cross_val_score()
  
  ## fit the test set to the regression model
  print 'fitting test data'
  y_pred = clf.predict(X_test)
  #print y_pred
  r2 = r2_score(y_test, y_pred)
  mean_sq = mean_squared_error(y_test, y_pred)
  #err = y_pred - y_test
  #mu = np.mean(err)
  #err2 = err * err
  #mu2 = np.mean(err2)
#.........这里部分代码省略.........
开发者ID:PositronicsLab,项目名称:wild-robot,代码行数:103,代码来源:wb_svr.py

示例15: svr_cv

# 需要导入模块: from sklearn.model_selection import GridSearchCV [as 别名]
# 或者: from sklearn.model_selection.GridSearchCV import score [as 别名]
def svr_cv(args, X, y):
  testing = args.get('testing')
  alg = args.get('algorithm')
  cvs = args.get('cross-validations')

  #svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
  #svr_lin = SVR(kernel='linear', C=1e3, epsilon=0.2)
  #svr_poly = SVR(kernel='poly', C=1e3, degree=2)
  #y_rbf = svr_rbf.fit(X_train_scaled, y).predict(X_test_scaled)
  #y_lin = svr_lin.fit(X_train_scaled, y).predict(X_test_scaled)
  #y_poly = svr_poly.fit(X_train_scaled, y).predict(X_test_scaled)
  #svr = SVR()
  #print svr.get_params()
 
  ## NOTE: May need to change scaler 
  scaler = preprocessing.StandardScaler()

  #hyperparameters = [ { 'kernel': ['rbf'], 'gamma': [1e-3,1e-2,1e-1], 'C': [1,1e1,1e2,1e3] } ,{ 'kernel': ['linear'], 'C': [1,1e1,1e2,1e3] } ]

  scores = ['precision', 'recall' ]
  score = scores[0]
  if alg == 'rbf':
    if testing:
      ## fast - 10 min
      C = [1e-3, 1e-2, 1e-1, 1] 
      eps = [1e-1, 1e-2, 1e-3] 
      ##gamma = ['auto']
      gamma = [1e-1, 1, 1e1]
    else:
      C = [1e-1, 1] 
      eps = [1e-6] 
      gamma = [1e-1]

      # training error < 5%
      #C = [1e-1, 1, 1e1] 
      #eps = [1e-5, 1e-4, 1e-3] 
      #gamma = [1e-1, 1, 1e1]

      #C = [1e-4, 1e-3, 1e-2, 1e-1, 1] 
      #eps = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1] 
      #gamma = [1e-3, 1e-2, 1e-1, 1, 1e1]

    svr = SVR(kernel='rbf')
    hyperparameters = { 'svr__C': C, 'svr__epsilon' : eps, 'svr__gamma' : gamma }
  elif alg == 'linear':
    svr = SVR(kernel='linear', max_iter=1e6)
    hyperparameters = { 'svr__C': [1, 1e-1, 1e-2, 1e-3], 'svr__epsilon' : [1e-1, 1e-2, 1e-3]}
  elif alg == 'poly':
    degree = [1,2,3] 

    svr = SVR(kernel='poly', max_iter=1e6)
    hyperparameters = { 'svr__C': [1, 1e-1, 1e-2, 1e-3], 'svr__epsilon' : [1e-1, 1e-2, 1e-3], 'svr__degree' : [1,2,3]}
  else:
    sys.exit(1)

  #hyperparameters = { 'svr__degree' : [2, 3, 4], 'svr__C': [1e3, 1e5, 1e7], 'svr__epsilon' : [0.1], 'svr__max_iter' : [1e4, 1e5, 1e6], 'svr__tol' : [0.001, 0.0001]}
  
  pipeline = make_pipeline(scaler, svr)

  print "cross-validating"
  #clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, scoring='%s_macro' % score)
  #clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, scoring='%s_macro' % score)
  clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads)
  #clf = GridSearchCV(pipeline, hyperparameters, cv=cvs, n_jobs=threads, refit=True)
  #print clf
  clf.fit(X, y)
  log_write('clf:' + str(clf))
  #log_time(t0, 'preprocess data')
  print "Best parameters found:"
  print clf.best_params_

  score_train = clf.score(X, y)
  #score_test = clf.score(X_test, y_test)
  #svr_params = clf.get_params()

  print 'score_train: ' + str(score_train)
  #print 'score_test: ' + str(score_test)
  #print 'svr_params: ' + str(svr_params)

  #score = cross_val_score()
  #print 'training neg_mean_sq_error:' + str(cross_val_score(clf, X, y, scoring='neg_mean_squared_error'))

  result = { 'clf': clf, 'params':str(clf.best_params_), 'score_train':score_train }
  return result
开发者ID:PositronicsLab,项目名称:wild-robot,代码行数:86,代码来源:wb_svr.py


注:本文中的sklearn.model_selection.GridSearchCV.score方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。