本文整理汇总了Python中sklearn.datasets.load_diabetes方法的典型用法代码示例。如果您正苦于以下问题:Python datasets.load_diabetes方法的具体用法?Python datasets.load_diabetes怎么用?Python datasets.load_diabetes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.datasets
的用法示例。
在下文中一共展示了datasets.load_diabetes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_lasso_cv_with_some_model_selection
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_lasso_cv_with_some_model_selection():
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedKFold
from sklearn import datasets
from sklearn.linear_model import LassoCV
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
pipe = make_pipeline(
StandardScaler(),
LassoCV(cv=StratifiedKFold(n_splits=5))
)
pipe.fit(X, y)
示例2: test_lasso_path
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_lasso_path(self):
diabetes = datasets.load_diabetes()
df = pdml.ModelFrame(diabetes)
result = df.linear_model.lasso_path()
expected = lm.lasso_path(diabetes.data, diabetes.target)
self.assertEqual(len(result), 3)
tm.assert_numpy_array_equal(result[0], expected[0])
self.assertIsInstance(result[1], pdml.ModelFrame)
tm.assert_index_equal(result[1].index, df.data.columns)
self.assert_numpy_array_almost_equal(result[1].values, expected[1])
self.assert_numpy_array_almost_equal(result[2], expected[2])
result = df.linear_model.lasso_path(return_models=True)
expected = lm.lasso_path(diabetes.data, diabetes.target, return_models=True)
self.assertEqual(len(result), len(expected))
self.assertIsInstance(result, tuple)
tm.assert_numpy_array_equal(result[0], result[0])
tm.assert_numpy_array_equal(result[1], result[1])
tm.assert_numpy_array_equal(result[2], result[2])
示例3: test_LassoCV
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_LassoCV(self, criterion):
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
X = pp.normalize(X)
df = pdml.ModelFrame(diabetes)
df.data = df.data.pp.normalize()
mod1 = lm.LassoLarsIC(criterion=criterion)
mod1.fit(X, y)
mod2 = df.lm.LassoLarsIC(criterion=criterion)
df.fit(mod2)
self.assertAlmostEqual(mod1.alpha_, mod2.alpha_)
expected = mod1.predict(X)
predicted = df.predict(mod2)
self.assertIsInstance(predicted, pdml.ModelSeries)
self.assert_numpy_array_almost_equal(predicted.values, expected)
示例4: test_MixedLM
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_MixedLM(self):
import statsmodels.regression.mixed_linear_model as mlm
diabetes = datasets.load_diabetes()
models = ['MixedLM']
data = diabetes.data[:100, :]
target = diabetes.target[:100]
groups = np.array([0] * 50 + [1] * 50)
for model in models:
klass = getattr(sm, model)
estimator = base.StatsModelsRegressor(klass, groups=groups)
fitted = estimator.fit(data, target)
# result = estimator.predict(diabetes.data)
# NotImplementedError
self.assertIsInstance(fitted, mlm.MixedLMResultsWrapper)
# expected = klass(target, data, groups=groups).fit().predict(diabetes.data)
# self.assert_numpy_array_almost_equal(result, expected)
示例5: test_pipeline
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_pipeline(self):
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression
from sklearn.pipeline import Pipeline
diabetes = datasets.load_diabetes()
models = ['OLS', 'GLS', 'WLS', 'GLSAR', 'QuantReg', 'GLM', 'RLM']
for model in models:
klass = getattr(sm, model)
selector = SelectKBest(f_regression, k=5)
estimator = Pipeline([('selector', selector),
('reg', base.StatsModelsRegressor(klass))])
estimator.fit(diabetes.data, diabetes.target)
result = estimator.predict(diabetes.data)
data = SelectKBest(f_regression, k=5).fit_transform(diabetes.data, diabetes.target)
expected = klass(diabetes.target, data).fit().predict(data)
self.assert_numpy_array_almost_equal(result, expected)
示例6: _timeseries_generated_data
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def _timeseries_generated_data(self):
# Load diabetes data and convert to data frame
x, y = datasets.load_diabetes(return_X_y=True)
nrows, ncols = x.shape
column_names = [str(i) for i in range(ncols)]
X = pd.DataFrame(x, columns=column_names)
# Add an arbitrary time axis
time_column_name = "Date" + str(uuid.uuid4())
dates = pd.date_range('1980-01-01', periods=nrows, freq='MS')
X[time_column_name] = dates
index_keys = [time_column_name]
X.set_index(index_keys, inplace=True)
# Split into train and test sets
test_frac = 0.2
cutoff_index = int(np.floor((1.0 - test_frac) * nrows))
X_train = X.iloc[:cutoff_index]
y_train = y[:cutoff_index]
X_test = X.iloc[cutoff_index:]
y_test = y[cutoff_index:]
return X_train, X_test, y_train, y_test, time_column_name
示例7: main
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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))
示例8: test_svr
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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)))
示例9: test_bayesian_on_diabetes
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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)
示例10: test_xgb_regressor
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_xgb_regressor(self):
iris = load_diabetes()
x = iris.data
y = iris.target
x_train, x_test, y_train, _ = train_test_split(x, y, test_size=0.5,
random_state=42)
xgb = XGBRegressor()
xgb.fit(x_train, y_train)
conv_model = convert_xgboost(
xgb, initial_types=[('input', FloatTensorType(shape=['None', 'None']))])
self.assertTrue(conv_model is not None)
dump_data_and_model(
x_test.astype("float32"),
xgb,
conv_model,
basename="SklearnXGBRegressor-Dec3",
allow_failure="StrictVersion("
"onnx.__version__)"
"< StrictVersion('1.3.0')",
)
示例11: test_h2o_regressor
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
def test_h2o_regressor(self):
diabetes = load_diabetes()
train, test = _train_test_split_as_frames(diabetes.data, diabetes.target)
dists = ["auto", "gaussian", "huber", "laplace", "quantile"]
for d in dists:
gbm = H2OGradientBoostingEstimator(ntrees=7, max_depth=5, distribution=d)
mojo_path = _make_mojo(gbm, train)
onnx_model = _convert_mojo(mojo_path)
self.assertIsNot(onnx_model, None)
dump_data_and_model(
test,
H2OMojoWrapper(mojo_path),
onnx_model,
basename="H2OReg-Dec4",
allow_failure="StrictVersion("
"onnx.__version__)"
"< StrictVersion('1.3.0')",
)
示例12: get_sample_dataset
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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
示例13: test_regression_scorers
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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)
示例14: test_svr
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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(gamma='scale').fit(diabetes.data, np.ones(len(diabetes.data)))
svm.LinearSVR().fit(diabetes.data, np.ones(len(diabetes.data)))
示例15: test_linearsvr
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import load_diabetes [as 别名]
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_allclose(np.linalg.norm(lsvr.coef_),
np.linalg.norm(svr.coef_), 1, 0.0001)
assert_almost_equal(score1, score2, 2)