本文整理匯總了Python中sklearn.learning_curve.validation_curve方法的典型用法代碼示例。如果您正苦於以下問題:Python learning_curve.validation_curve方法的具體用法?Python learning_curve.validation_curve怎麽用?Python learning_curve.validation_curve使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.learning_curve
的用法示例。
在下文中一共展示了learning_curve.validation_curve方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: GenerateValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def GenerateValidationCurve(estimator, X, y):
#todo: edit parameters
#generate validation curve data
num_resources = -1 #engage all available CPUs and GPUs
alpha_range = [0.0001, 0.001, 0.01, 0.1, 1.0, 10.0]#alpha
#titrating hidden layer amount and size
num_hidden_layers = 2
hidden_units_range1 = [32]*num_hidden_layers
hidden_units_range2 = [64]*num_hidden_layers
hidden_units_range3 = [128]*num_hidden_layers
hidden_units_range4 = [256]*num_hidden_layers
hidden_units_range5 = [512]*num_hidden_layers
hidden_units_range6 = [1024]*num_hidden_layers
hidden_units_range7 = [2048]*num_hidden_layers
hidden_units_range8 = [4096]*num_hidden_layers
hidden_units_range = [hidden_units_range1, hidden_units_range2, hidden_units_range3, hidden_units_range4,
hidden_units_range5, hidden_units_range6, hidden_units_range7, hidden_units_range8]
train_scores, test_scores = validation_curve(estimator=estimator, X=X, y=y, param_name='regressor__hidden_units', param_range=hidden_units_range, cv=10, n_jobs=num_resources)
train_mean = np.mean(train_scores, axis = 1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
PlotValidationCurve(hidden_units_range, train_mean, train_std, test_mean, test_std)
示例2: drawValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
#np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(5,700)
train_scores, valid_scores = validation_curve(self.adaReg, X, y, "n_estimators",
train_sizes, cv=5, scoring='mean_squared_error')
train_scores = -1.0/5 *train_scores
valid_scores = -1.0/5 *valid_scores
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training MSE")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation MSE")
plt.legend(loc="best")
plt.xlabel('Estimators')
plt.ylabel('MSE')
plt.title('Validation Curve with AdaBoost-DecisionTree Regression\n on the parameter of Estimators when the Decsion Tree has max depth=10')
plt.grid(True)
plt.show()
示例3: drawValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
#np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(2,60)
train_scores, valid_scores = validation_curve(DecisionTreeRegressor(max_features=None), X, y, "max_depth",
train_sizes, cv=5, scoring='mean_squared_error')
train_scores = -1.0/5 *train_scores
valid_scores = -1.0/5 *valid_scores
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training MSE")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation MSE")
plt.legend(loc="best")
plt.xlabel('Max Depth')
plt.ylabel('MSE')
plt.title('Validation Curve with Decision \nTree Regression on the parameter of Max Depth')
plt.grid(True)
plt.show()
示例4: drawValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
#np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(2,100,2)
train_scores, valid_scores = validation_curve(self.regr, X, y, "n_neighbors",
train_sizes, cv=5, scoring='mean_squared_error')
train_scores = -1.0/5*train_scores
valid_scores = -1.0/5*valid_scores
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training MSE")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation MSE")
plt.legend(loc="best")
plt.xlabel('K Neighbors')
plt.ylabel('MSE')
plt.title('Validation Curve with KNN Regression on the parameter of K Neighbors')
plt.grid(True)
plt.show()
示例5: drawValidationCurve_maxdepth
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve_maxdepth(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
#np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(1,60)
train_scores, valid_scores = validation_curve(self.model, X, y, "max_depth",
train_sizes, cv=5, scoring='mean_squared_error')
train_scores = -1.0/5*train_scores
valid_scores = -1.0/5*valid_scores
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training MSE")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation MSE")
plt.legend(loc="best")
plt.xlabel('Max Depth')
plt.ylabel('MSE')
plt.title('Validation Curve with Random Forest Regression \non the parameter of Max Depth when n_estimators=32')
plt.grid(True)
plt.show()
示例6: drawValidationCurve_estimators
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve_estimators(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
#np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(1,40)
train_scores, valid_scores = validation_curve(self.model, X, y, "n_estimators",
train_sizes, cv=5, scoring='mean_squared_error')
train_scores = -1.0/5*train_scores
valid_scores = -1.0/5*valid_scores
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training MSE")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation MSE")
plt.legend(loc="best")
plt.xlabel('Estimators')
plt.ylabel('MSE')
plt.title('Validation Curve with Random Forest Regression \non the parameter of estimators when max_depth=39')
plt.grid(True)
plt.show()
示例7: drawValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(1,60)
train_scores, valid_scores = validation_curve(self.clf, X, y, "n_neighbors",
train_sizes, cv=5)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training Precision")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation Precision")
plt.legend(loc="best")
plt.xlabel('K Neighbors')
plt.ylabel('Precision')
plt.title('Validation Curve with KNN on the parameter of K')
plt.grid(True)
plt.show()
示例8: drawValidationCurve_maxdepth
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve_maxdepth(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(2,60)
train_scores, valid_scores = validation_curve(self.clf, X, y, "max_depth",
train_sizes, cv=5)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training Precision")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation Precision")
plt.legend(loc="best")
plt.xlabel('Max Depth')
plt.ylabel('Precision')
plt.title('Validation Curve with Random Forest Classification\n on the parameter of Max Depth when n_stimators=20')
plt.grid(True)
plt.show()
示例9: drawValidationCurve_estimators
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve_estimators(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(2,40)
train_scores, valid_scores = validation_curve(self.clf, X, y, "n_estimators",
train_sizes, cv=5)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training Precision")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation Precision")
plt.legend(loc="best")
plt.xlabel('Estimators')
plt.ylabel('Precision')
plt.title('Validation Curve with Random Forest Classification\n on the parameter of Estimators when Max Depth=30')
plt.grid(True)
plt.show()
示例10: drawValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(2,75)
train_scores, valid_scores = validation_curve(self.ada, X, y, "n_estimators",
train_sizes, cv=5)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training Precision")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation Precision")
plt.legend(loc="best")
plt.xlabel('Estimators')
plt.ylabel('Precision')
plt.title('Validation Curve with AdaBoost-DecisionTree on the parameter of Estimators')
plt.grid(True)
plt.show()
示例11: drawValidationCurve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def drawValidationCurve(self):
"""
To draw the validation curve
:return:NA
"""
X, y = self.X_train, self.y_train.ravel()
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X, y = X[indices], y[indices]
train_sizes = range(2,60)
train_scores, valid_scores = validation_curve(self.clf, X, y, "max_depth",
train_sizes, cv=5)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, valid_scores_mean - valid_scores_std,
valid_scores_mean + valid_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training Precision")
plt.plot(train_sizes, valid_scores_mean, '*-', color="g",
label="Cross-validation Precision")
plt.legend(loc="best")
plt.xlabel('Max Depth(log2(all features) to be considered) ')
plt.ylabel('Precision')
plt.title('Validation Curve with Decision Tree on the parameter of Max Depth')
plt.grid(True)
plt.show()
示例12: test_validation_curve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def test_validation_curve():
'''
test validation_curve with LinerSVC and different C
:return: None
'''
digits = load_digits()
X,y=digits.data,digits.target
param_name="C"
param_range = np.logspace(-2, 2)
train_scores, test_scores = validation_curve(LinearSVC(), X, y, param_name=param_name,
param_range=param_range,cv=10, scoring="accuracy")
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.semilogx(param_range, train_scores_mean, label="Training Accuracy", color="r")
ax.fill_between(param_range, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.2, color="r")
ax.semilogx(param_range, test_scores_mean, label="Testing Accuracy", color="g")
ax.fill_between(param_range, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.2, color="g")
ax.set_title("Validation Curve with LinearSVC")
ax.set_xlabel("C")
ax.set_ylabel("Score")
ax.set_ylim(0,1.1)
ax.legend(loc='best')
plt.show()
示例13: test_validation_curve
# 需要導入模塊: from sklearn import learning_curve [as 別名]
# 或者: from sklearn.learning_curve import validation_curve [as 別名]
def test_validation_curve():
X, y = make_classification(n_samples=2, n_features=1, n_informative=1,
n_redundant=0, n_classes=2,
n_clusters_per_class=1, random_state=0)
param_range = np.linspace(0, 1, 10)
with warnings.catch_warnings(record=True) as w:
train_scores, test_scores = validation_curve(
MockEstimatorWithParameter(), X, y, param_name="param",
param_range=param_range, cv=2
)
if len(w) > 0:
raise RuntimeError("Unexpected warning: %r" % w[0].message)
assert_array_almost_equal(train_scores.mean(axis=1), param_range)
assert_array_almost_equal(test_scores.mean(axis=1), 1 - param_range)