我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用sklearn.datasets.load_diabetes()。
def test_integration_quic_graph_lasso(self, params_in, expected): ''' Just tests inputs/outputs (not validity of result). ''' X = datasets.load_diabetes().data ic = QuicGraphLasso(**params_in) ic.fit(X) result_vec = [ np.linalg.norm(ic.covariance_), np.linalg.norm(ic.precision_), np.linalg.norm(ic.opt_), np.linalg.norm(ic.duality_gap_), ] print(result_vec) assert_allclose(expected, result_vec, atol=1e-1, rtol=1e-1)
def test_integration_quic_graph_lasso_cv(self, params_in, expected): ''' Just tests inputs/outputs (not validity of result). ''' X = datasets.load_diabetes().data ic = QuicGraphLassoCV(**params_in) ic.fit(X) result_vec = [ np.linalg.norm(ic.covariance_), np.linalg.norm(ic.precision_), np.linalg.norm(ic.opt_), np.linalg.norm(ic.duality_gap_), ] if isinstance(ic.lam_, float): result_vec.append(ic.lam_) elif isinstance(ic.lam_, np.ndarray): assert ic.lam_.shape == params_in['lam'].shape print(result_vec) assert_allclose(expected, result_vec, atol=1e-1, rtol=1e-1) assert len(ic.grid_scores) == len(ic.cv_lams_)
def test_integration_quic_graph_lasso_ebic(self, params_in, expected): ''' Just tests inputs/outputs (not validity of result). ''' X = datasets.load_diabetes().data ic = QuicGraphLassoEBIC(**params_in) ic.fit(X) result_vec = [ np.linalg.norm(ic.covariance_), np.linalg.norm(ic.precision_), ] if isinstance(ic.lam_, float): result_vec.append(ic.lam_) elif isinstance(ic.lam_, np.ndarray): assert ic.lam_.shape == params_in['lam'].shape print(result_vec) assert_allclose(expected, result_vec, atol=1e-1, rtol=1e-1)
def main(): diabetes = datasets.load_diabetes() diabetes_X = diabetes.data[:, np.newaxis, 2] diabetes_X_train = diabetes_X[:-20] diabetes_X_test = diabetes_X[-20:] diabetes_y_train = diabetes.target[:-20] diabetes_y_test = diabetes.target[-20:] regr = linear_model.LinearRegression() regr.fit(diabetes_X_train, diabetes_y_train) print('Coefficients: \n', regr.coef_) print("Mean squared error: %.2f" % np.mean((regr.predict(diabetes_X_test) - diabetes_y_test)**2)) print('Variance score: %.2f' % regr.score(diabetes_X_test, diabetes_y_test))
def test_partial_fit(): data = load_diabetes() clf = MLPRegressor(n_epochs=1) X, y = data['data'], data['target'] for _ in range(30): clf.partial_fit(X, y) y_pred = clf.predict(X) assert pearsonr(y_pred, y)[0] > 0.5
def test_embedding_default(): # Make sure the embedding works by default. data = load_diabetes() X, y = data['data'], data['target'] clf = MLPRegressor(n_epochs=1) clf.fit(X, y) assert clf.transform(X).shape[1] == 256
def test_bayesian_on_diabetes(): # Test BayesianRidge on diabetes raise SkipTest("XFailed Test") diabetes = datasets.load_diabetes() X, y = diabetes.data, diabetes.target clf = BayesianRidge(compute_score=True) # Test with more samples than features clf.fit(X, y) # Test that scores are increasing at each iteration assert_array_equal(np.diff(clf.scores_) > 0, True) # Test with more features than samples X = X[:5, :] y = y[:5] clf.fit(X, y) # Test that scores are increasing at each iteration assert_array_equal(np.diff(clf.scores_) > 0, True)
def test_svr(): # Test Support Vector Regression diabetes = datasets.load_diabetes() for clf in (svm.NuSVR(kernel='linear', nu=.4, C=1.0), svm.NuSVR(kernel='linear', nu=.4, C=10.), svm.SVR(kernel='linear', C=10.), svm.LinearSVR(C=10.), svm.LinearSVR(C=10.), ): clf.fit(diabetes.data, diabetes.target) assert_greater(clf.score(diabetes.data, diabetes.target), 0.02) # non-regression test; previously, BaseLibSVM would check that # len(np.unique(y)) < 2, which must only be done for SVC svm.SVR().fit(diabetes.data, np.ones(len(diabetes.data))) svm.LinearSVR().fit(diabetes.data, np.ones(len(diabetes.data)))
def test_integration_quic_graph_lasso_fun(self, params_in, expected): ''' Just tests inputs/outputs (not validity of result). ''' X = datasets.load_diabetes().data lam = 0.5 if 'lam' in params_in: lam = params_in['lam'] del params_in['lam'] S = np.corrcoef(X, rowvar=False) if 'init_method' in params_in: if params_in['init_method'] == 'cov': S = np.cov(X, rowvar=False) del params_in['init_method'] precision_, covariance_, opt_, cpu_time_, iters_, duality_gap_ =\ quic(S, lam, **params_in) result_vec = [ np.linalg.norm(covariance_), np.linalg.norm(precision_), np.linalg.norm(opt_), np.linalg.norm(duality_gap_), ] print(result_vec) assert_allclose(expected, result_vec, atol=1e-1, rtol=1e-1)
def test_invalid_method(self): ''' Test behavior of invalid inputs. ''' X = datasets.load_diabetes().data ic = QuicGraphLasso(method='unknownmethod') assert_raises(NotImplementedError, ic.fit, X)
def read_training(self): return datasets.load_diabetes()
def main(dataset_size, test_proportion): diabetes = load_diabetes() X = diabetes.data[:dataset_size] y = diabetes.target[:dataset_size] fig, ax_list = plt.subplots(3, 1, figsize=(8, 6)) plot_errors_by_lambda(X, y, test_proportion=test_proportion, regression_class=Ridge, ax=ax_list[0]) plot_errors_by_lambda(X, y, test_proportion=test_proportion, regression_class=Lasso, ax=ax_list[1]) plot_errors_by_lambda(X, y, test_proportion=test_proportion, regression_class=LinearRegression, ax=ax_list[2]) plt.tight_layout() plt.show()
def get_sample_dataset(dataset_properties): """Returns sample dataset Args: dataset_properties (dict): Dictionary corresponding to the properties of the dataset used to verify the estimator and metric generators. Returns: X (array-like): Features array y (array-like): Labels array splits (iterator): This is an iterator that returns train test splits for cross-validation purposes on ``X`` and ``y``. """ kwargs = dataset_properties.copy() data_type = kwargs.pop('type') if data_type == 'multiclass': try: X, y = datasets.make_classification(random_state=8, **kwargs) splits = model_selection.StratifiedKFold(n_splits=2, random_state=8).split(X, y) except Exception as e: raise exceptions.UserError(repr(e)) elif data_type == 'iris': X, y = datasets.load_iris(return_X_y=True) splits = model_selection.StratifiedKFold(n_splits=2, random_state=8).split(X, y) elif data_type == 'mnist': X, y = datasets.load_digits(return_X_y=True) splits = model_selection.StratifiedKFold(n_splits=2, random_state=8).split(X, y) elif data_type == 'breast_cancer': X, y = datasets.load_breast_cancer(return_X_y=True) splits = model_selection.StratifiedKFold(n_splits=2, random_state=8).split(X, y) elif data_type == 'boston': X, y = datasets.load_boston(return_X_y=True) splits = model_selection.KFold(n_splits=2, random_state=8).split(X) elif data_type == 'diabetes': X, y = datasets.load_diabetes(return_X_y=True) splits = model_selection.KFold(n_splits=2, random_state=8).split(X) else: raise exceptions.UserError('Unknown dataset type {}'.format(dataset_properties['type'])) return X, y, splits
def test_replicability(): """Make sure running fit twice in a row finds the same parameters.""" diabetes = load_diabetes() X_diabetes, y_diabetes = diabetes.data, diabetes.target ind = np.arange(X_diabetes.shape[0]) rng = np.random.RandomState(0) rng.shuffle(ind) X_diabetes, y_diabetes = X_diabetes[ind], y_diabetes[ind] clf = MLPRegressor(keep_prob=0.9, random_state=42, n_epochs=100) target = y_diabetes # Just predict on the training set, for simplicity. pred1 = clf.fit(X_diabetes, target).predict(X_diabetes) pred2 = clf.fit(X_diabetes, target).predict(X_diabetes) assert_array_almost_equal(pred1, pred2)
def test_embedding_no_layers(): # Make sure the embedding works with no layers. data = load_diabetes() X, y = data['data'], data['target'] clf = MLPRegressor(n_epochs=1, hidden_units=[]) clf.fit(X, y) assert clf.transform(X).shape[1] == 1
def test_embedding_specific_layer(): # Make sure the embedding works with no layers. data = load_diabetes() X, y = data['data'], data['target'] clf = MLPRegressor( n_epochs=1, hidden_units=(256, 8, 256), transform_layer_index=1) clf.fit(X, y) assert clf.transform(X).shape[1] == 8
def load_data_regression(): ''' load the date set for regression (diabetes) :return: train_data, test_data, train_value, test_value ''' diabetes = datasets.load_diabetes() return cross_validation.train_test_split(diabetes.data,diabetes.target, test_size=0.25,random_state=0)
def load_data_regression(): ''' load the diabetes data for regression :return: train_data, test_data, train_value, test_value ''' diabetes = datasets.load_diabetes() return cross_validation.train_test_split(diabetes.data,diabetes.target, test_size=0.25,random_state=0)
def load_data_regression(): ''' load the diabetes for regression :return: train_data, test_data, train_value, test_value ''' diabetes = datasets.load_diabetes() return cross_validation.train_test_split(diabetes.data,diabetes.target, test_size=0.25,random_state=0)
def load_data(): diabetes = datasets.load_diabetes() return cross_validation.train_test_split(diabetes.data,diabetes.target, test_size=0.25,random_state=0)
def load_data(): ''' load for the dataset return: 1 array for the regression problem. train_data, test_data, train_value, test_value ''' diabetes = datasets.load_diabetes() return cross_validation.train_test_split(diabetes.data,diabetes.target, test_size=0.25,random_state=0)
def load_data_regression(): ''' load dataset for regression :return: train_data,test_data, train_target, test_target ''' diabetes = datasets.load_diabetes() return cross_validation.train_test_split(diabetes.data,diabetes.target, test_size=0.25,random_state=0)
def test_regression_scorers(): # Test regression scorers. diabetes = load_diabetes() X, y = diabetes.data, diabetes.target X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) clf = Ridge() clf.fit(X_train, y_train) score1 = get_scorer('r2')(clf, X_test, y_test) score2 = r2_score(y_test, clf.predict(X_test)) assert_almost_equal(score1, score2)
def test_load_diabetes(): res = load_diabetes() assert_equal(res.data.shape, (442, 10)) assert_true(res.target.size, 442)
def test_linearsvr(): # check that SVR(kernel='linear') and LinearSVC() give # comparable results diabetes = datasets.load_diabetes() lsvr = svm.LinearSVR(C=1e3).fit(diabetes.data, diabetes.target) score1 = lsvr.score(diabetes.data, diabetes.target) svr = svm.SVR(kernel='linear', C=1e3).fit(diabetes.data, diabetes.target) score2 = svr.score(diabetes.data, diabetes.target) assert np.linalg.norm(lsvr.coef_ - svr.coef_) / np.linalg.norm(svr.coef_) < .1 assert np.abs(score1 - score2) < 0.1