本文整理汇总了Python中sklearn.datasets方法的典型用法代码示例。如果您正苦于以下问题:Python sklearn.datasets方法的具体用法?Python sklearn.datasets怎么用?Python sklearn.datasets使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn
的用法示例。
在下文中一共展示了sklearn.datasets方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ShapLinearExplainer
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def test_ShapLinearExplainer(self):
corpus, y = shap.datasets.imdb()
corpus_train, corpus_test, y_train, y_test = train_test_split(corpus, y, test_size=0.2, random_state=7)
vectorizer = TfidfVectorizer(min_df=10)
X_train = vectorizer.fit_transform(corpus_train)
X_test = vectorizer.transform(corpus_test)
model = sklearn.linear_model.LogisticRegression(penalty="l1", C=0.1, solver='liblinear')
model.fit(X_train, y_train)
shapexplainer = LinearExplainer(model, X_train, feature_dependence="independent")
shap_values = shapexplainer.explain_instance(X_test)
print("Invoked Shap LinearExplainer")
# comment this test as travis runs out of resources
示例2: test_ShapGradientExplainer
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def test_ShapGradientExplainer(self):
# model = VGG16(weights='imagenet', include_top=True)
# X, y = shap.datasets.imagenet50()
# to_explain = X[[39, 41]]
#
# url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
# fname = shap.datasets.cache(url)
# with open(fname) as f:
# class_names = json.load(f)
#
# def map2layer(x, layer):
# feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
# return K.get_session().run(model.layers[layer].input, feed_dict)
#
# e = GradientExplainer((model.layers[7].input, model.layers[-1].output),
# map2layer(preprocess_input(X.copy()), 7))
# shap_values, indexes = e.explain_instance(map2layer(to_explain, 7), ranked_outputs=2)
#
print("Skipped Shap GradientExplainer")
示例3: test_download
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def test_download(tmpdata):
"""Test that fetch_mldata is able to download and cache a data set."""
_urlopen_ref = datasets.mldata.urlopen
datasets.mldata.urlopen = mock_mldata_urlopen({
'mock': {
'label': sp.ones((150,)),
'data': sp.ones((150, 4)),
},
})
try:
mock = assert_warns(DeprecationWarning, fetch_mldata,
'mock', data_home=tmpdata)
for n in ["COL_NAMES", "DESCR", "target", "data"]:
assert_in(n, mock)
assert_equal(mock.target.shape, (150,))
assert_equal(mock.data.shape, (150, 4))
assert_raises(datasets.mldata.HTTPError,
assert_warns, DeprecationWarning,
fetch_mldata, 'not_existing_name')
finally:
datasets.mldata.urlopen = _urlopen_ref
示例4: test_fetch_one_column
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def test_fetch_one_column(tmpdata):
_urlopen_ref = datasets.mldata.urlopen
try:
dataname = 'onecol'
# create fake data set in cache
x = sp.arange(6).reshape(2, 3)
datasets.mldata.urlopen = mock_mldata_urlopen({dataname: {'x': x}})
dset = fetch_mldata(dataname, data_home=tmpdata)
for n in ["COL_NAMES", "DESCR", "data"]:
assert_in(n, dset)
assert_not_in("target", dset)
assert_equal(dset.data.shape, (2, 3))
assert_array_equal(dset.data, x)
# transposing the data array
dset = fetch_mldata(dataname, transpose_data=False, data_home=tmpdata)
assert_equal(dset.data.shape, (3, 2))
finally:
datasets.mldata.urlopen = _urlopen_ref
示例5: test_retry_with_clean_cache
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def test_retry_with_clean_cache(tmpdir):
data_id = 61
openml_path = sklearn.datasets.openml._DATA_FILE.format(data_id)
cache_directory = str(tmpdir.mkdir('scikit_learn_data'))
location = _get_local_path(openml_path, cache_directory)
os.makedirs(os.path.dirname(location))
with open(location, 'w') as f:
f.write("")
@_retry_with_clean_cache(openml_path, cache_directory)
def _load_data():
# The first call will raise an error since location exists
if os.path.exists(location):
raise Exception("File exist!")
return 1
warn_msg = "Invalid cache, redownloading file"
with pytest.warns(RuntimeWarning, match=warn_msg):
result = _load_data()
assert result == 1
示例6: test_fetch_openml_cache
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def test_fetch_openml_cache(monkeypatch, gzip_response, tmpdir):
def _mock_urlopen_raise(request):
raise ValueError('This mechanism intends to test correct cache'
'handling. As such, urlopen should never be '
'accessed. URL: %s' % request.get_full_url())
data_id = 2
cache_directory = str(tmpdir.mkdir('scikit_learn_data'))
_monkey_patch_webbased_functions(
monkeypatch, data_id, gzip_response)
X_fetched, y_fetched = fetch_openml(data_id=data_id, cache=True,
data_home=cache_directory,
return_X_y=True)
monkeypatch.setattr(sklearn.datasets.openml, 'urlopen',
_mock_urlopen_raise)
X_cached, y_cached = fetch_openml(data_id=data_id, cache=True,
data_home=cache_directory,
return_X_y=True)
np.testing.assert_array_equal(X_fetched, X_cached)
np.testing.assert_array_equal(y_fetched, y_cached)
示例7: setup
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def setup(pblm):
import sklearn.datasets
iris = sklearn.datasets.load_iris()
pblm.primary_task_key = 'iris'
pblm.default_data_key = 'learn(all)'
pblm.default_clf_key = 'RF'
X_df = pd.DataFrame(iris.data, columns=iris.feature_names)
samples = MultiTaskSamples(X_df.index)
samples.apply_indicators(
{'iris': {name: iris.target == idx
for idx, name in enumerate(iris.target_names)}})
samples.X_dict = {'learn(all)': X_df}
pblm.samples = samples
pblm.xval_kw['type'] = 'StratifiedKFold'
示例8: burczynski06
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def burczynski06() -> AnnData:
"""\
Bulk data with conditions ulcerative colitis (UC) and Crohn's disease (CD).
The study assesses transcriptional profiles in peripheral blood mononuclear
cells from 42 healthy individuals, 59 CD patients, and 26 UC patients by
hybridization to microarrays interrogating more than 22,000 sequences.
Reference
---------
Burczynski et al., "Molecular classification of Crohn's disease and
ulcerative colitis patients using transcriptional profiles in peripheral
blood mononuclear cells"
J Mol Diagn 8, 51 (2006). PMID:16436634.
"""
filename = settings.datasetdir / 'burczynski06/GDS1615_full.soft.gz'
url = 'ftp://ftp.ncbi.nlm.nih.gov/geo/datasets/GDS1nnn/GDS1615/soft/GDS1615_full.soft.gz'
adata = read(filename, backup_url=url)
return adata
示例9: pbmc68k_reduced
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def pbmc68k_reduced() -> AnnData:
"""\
Subsampled and processed 68k PBMCs.
10x PBMC 68k dataset from
https://support.10xgenomics.com/single-cell-gene-expression/datasets
The original PBMC 68k dataset was preprocessed using scanpy and was saved
keeping only 724 cells and 221 highly variable genes.
The saved file contains the annotation of cell types (key: `'bulk_labels'`),
UMAP coordinates, louvain clustering and gene rankings based on the
`bulk_labels`.
Returns
-------
Annotated data matrix.
"""
filename = HERE / '10x_pbmc68k_reduced.h5ad'
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning, module="anndata")
return read(filename)
示例10: __init__
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def __init__(self, subset, shuffle=True, random_state=42):
if subset == "all":
shuffle = False # chronological split violated if shuffled
else:
shuffle = shuffle
dataset = sklearn.datasets.fetch_rcv1(subset=subset, shuffle=shuffle, random_state=random_state)
self.data = dataset.data
self.labels = dataset.target
self.class_names = dataset.target_names
assert len(self.class_names) == 103 # 103 categories according to LYRL2004
N, C = self.labels.shape
assert C == len(self.class_names)
N, V = self.data.shape
self.vocab = np.zeros(V) # hacky workaround to create placeholder value
self.orig_vocab_size = V
示例11: _bunch_to_df
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def _bunch_to_df(bunch, schema_X, schema_y, test_size=0.2, random_state=42):
train_X_arr, test_X_arr, train_y_arr, test_y_arr = train_test_split(
bunch.data, bunch.target,
test_size=test_size, random_state=random_state)
feature_schemas = schema_X['items']['items']
if isinstance(feature_schemas, list):
feature_names = [f['description'] for f in feature_schemas]
else:
feature_names = [f'x{i}' for i in range(schema_X['items']['maxItems'])]
train_X_df = pd.DataFrame(train_X_arr, columns=feature_names)
test_X_df = pd.DataFrame(test_X_arr, columns=feature_names)
train_y_df = pd.Series(train_y_arr, name='target')
test_y_df = pd.Series(test_y_arr, name='target')
train_nrows, test_nrows = train_X_df.shape[0], test_X_df.shape[0]
train_X = lale.datasets.data_schemas.add_schema(train_X_df, {
**schema_X, 'minItems': train_nrows, 'maxItems': train_nrows })
test_X = lale.datasets.data_schemas.add_schema(test_X_df, {
**schema_X, 'minItems': test_nrows, 'maxItems': test_nrows })
train_y = lale.datasets.data_schemas.add_schema(train_y_df, {
**schema_y, 'minItems': train_nrows, 'maxItems': train_nrows })
test_y = lale.datasets.data_schemas.add_schema(test_y_df, {
**schema_y, 'minItems': test_nrows, 'maxItems': test_nrows })
return (train_X, train_y), (test_X, test_y)
示例12: load_iris_df
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def load_iris_df(test_size=0.2):
iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target
target_name = 'target'
X, y = shuffle(iris.data, iris.target, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42)
X_train_df = pd.DataFrame(X_train, columns = iris.feature_names)
y_train_df = pd.Series(y_train, name = target_name)
X_test_df = pd.DataFrame(X_test, columns = iris.feature_names)
y_test_df = pd.Series(y_test, name = target_name)
return (X_train_df, y_train_df), (X_test_df, y_test_df)
示例13: digits_df
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def digits_df(test_size=0.2, random_state=42):
digits = sklearn.datasets.load_digits()
ncols = digits.data.shape[1]
schema_X = {
'description': 'Features of digits dataset (classification).',
'documentation_url': 'https://scikit-learn.org/0.20/datasets/index.html#optical-recognition-of-handwritten-digits-dataset',
'type': 'array',
'items': {
'type': 'array',
'minItems': ncols, 'maxItems': ncols,
'items': {
'type': 'number', 'minimum': 0, 'maximum': 16}}}
schema_y = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'array',
'items': {
'type': 'integer', 'minimum': 0, 'maximum': 9}}
(train_X, train_y), (test_X, test_y) = _bunch_to_df(
digits, schema_X, schema_y, test_size, random_state)
return (train_X, train_y), (test_X, test_y)
示例14: synthesize_data
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def synthesize_data(n_samples, n_features, n_targets):
rnd = await mpc.transfer(random.randrange(2**31), senders=0)
X, Y = sklearn.datasets.make_regression(n_samples=n_samples,
n_features=n_features,
n_informative=max(1, n_features - 5),
n_targets=n_targets, bias=42,
effective_rank=max(1, n_features - 3),
tail_strength=0.5, noise=1.2,
random_state=rnd) # all parties use same rnd
if n_targets == 1:
Y = np.transpose([Y])
X = np.concatenate((X, Y), axis=1)
b_m = np.min(X, axis=0)
b_M = np.max(X, axis=0)
coef_add = [-(m + M) / 2 for m, M in zip(b_m, b_M)]
coef_mul = [2 / (M - m) for m, M in zip(b_m, b_M)]
for xi in X:
for j in range(len(xi)):
# map to [-1,1] range
xi[j] = (xi[j] + coef_add[j]) * coef_mul[j]
return X
示例15: mnist
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import datasets [as 别名]
def mnist(random_state=42):
"""
x is in [0, 1] with shape (b, 1, 28, 28) and dtype floatX
y is an int32 vector in range(10)
"""
raw = sklearn.datasets.fetch_mldata('MNIST original')
# rescaling to [0, 1] instead of [0, 255]
x = raw['data'].reshape(-1, 1, 28, 28).astype(fX) / 255.0
y = raw['target'].astype("int32")
# NOTE: train data is initially in order of 0 through 9
x1, x2, y1, y2 = sklearn.cross_validation.train_test_split(
x[:60000],
y[:60000],
random_state=random_state,
test_size=10000)
train = {"x": x1, "y": y1}
valid = {"x": x2, "y": y2}
# NOTE: test data is in order of 0 through 9
test = {"x": x[60000:], "y": y[60000:]}
return train, valid, test