本文整理汇总了Python中holoviews.Dataset.sample方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.sample方法的具体用法?Python Dataset.sample怎么用?Python Dataset.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类holoviews.Dataset
的用法示例。
在下文中一共展示了Dataset.sample方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HoloMapTest
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
class HoloMapTest(ComparisonTestCase):
def setUp(self):
self.xs = range(11)
self.y_ints = [i*2 for i in range(11)]
self.ys = np.linspace(0, 1, 11)
self.columns = Dataset(np.column_stack([self.xs, self.y_ints]),
kdims=['x'], vdims=['y'])
def test_holomap_redim(self):
hmap = HoloMap({i: Dataset({'x':self.xs, 'y': self.ys * i},
kdims=['x'], vdims=['y'])
for i in range(10)}, kdims=['z'])
redimmed = hmap.redim(x='Time')
self.assertEqual(redimmed.dimensions('all', True),
['z', 'Time', 'y'])
def test_holomap_redim_nested(self):
hmap = HoloMap({i: Dataset({'x':self.xs, 'y': self.ys * i},
kdims=['x'], vdims=['y'])
for i in range(10)}, kdims=['z'])
redimmed = hmap.redim(x='Time', z='Magnitude')
self.assertEqual(redimmed.dimensions('all', True),
['Magnitude', 'Time', 'y'])
def test_columns_collapse_heterogeneous(self):
collapsed = HoloMap({i: Dataset({'x':self.xs, 'y': self.ys * i},
kdims=['x'], vdims=['y'])
for i in range(10)}, kdims=['z']).collapse('z', np.mean)
expected = Dataset({'x':self.xs, 'y': self.ys * 4.5}, kdims=['x'], vdims=['y'])
self.compare_dataset(collapsed, expected)
def test_columns_sample_homogeneous(self):
samples = self.columns.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 10, 20]))
def test_holomap_map_with_none(self):
hmap = HoloMap({i: Dataset({'x':self.xs, 'y': self.ys * i},
kdims=['x'], vdims=['y'])
for i in range(10)}, kdims=['z'])
mapped = hmap.map(lambda x: x if x.range(1)[1] > 0 else None, Dataset)
self.assertEqual(hmap[1:10], mapped)
def test_holomap_hist_two_dims(self):
hmap = HoloMap({i: Dataset({'x':self.xs, 'y': self.ys * i},
kdims=['x'], vdims=['y'])
for i in range(10)}, kdims=['z'])
hists = hmap.hist(dimension=['x', 'y'])
self.assertEqual(hists['right'].last.kdims, ['y'])
self.assertEqual(hists['top'].last.kdims, ['x'])
示例2: HoloMapTest
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
class HoloMapTest(ComparisonTestCase):
def setUp(self):
self.xs = range(11)
self.y_ints = [i*2 for i in range(11)]
self.ys = np.linspace(0, 1, 11)
self.columns = Dataset(np.column_stack([self.xs, self.y_ints]),
kdims=['x'], vdims=['y'])
def test_columns_collapse_heterogeneous(self):
collapsed = HoloMap({i: Dataset({'x':self.xs, 'y': self.ys * i},
kdims=['x'], vdims=['y'])
for i in range(10)}, kdims=['z']).collapse('z', np.mean)
expected = Dataset({'x':self.xs, 'y': self.ys * 4.5}, kdims=['x'], vdims=['y'])
self.compare_dataset(collapsed, expected)
def test_columns_sample_homogeneous(self):
samples = self.columns.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 10, 20]))
示例3: test_dataset_scalar_sample
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
def test_dataset_scalar_sample(self):
ds = Dataset({'A': 1, 'B': np.arange(10)}, kdims=['A', 'B'])
self.assertEqual(ds.sample([(1,)]).dimension_values('B'), np.arange(10))
示例4: HomogeneousColumnTests
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
#.........这里部分代码省略.........
dataset_sorted = Dataset(np.column_stack([self.xs[::-1], -xs_2[::-1]]),
kdims=[('x', 'X-label')], vdims=[('y', 'Y-label')])
self.assertEqual(dataset.sort('y'), dataset_sorted)
self.assertEqual(dataset.sort('Y-label'), dataset_sorted)
def test_dataset_redim_hm_kdim(self):
redimmed = self.dataset_hm.redim(x='Time')
self.assertEqual(redimmed.dimension_values('Time'),
self.dataset_hm.dimension_values('x'))
def test_dataset_redim_hm_kdim_range_aux(self):
redimmed = self.dataset_hm.redim.range(x=(-100,3))
self.assertEqual(redimmed.kdims[0].range, (-100,3))
def test_dataset_redim_hm_kdim_soft_range_aux(self):
redimmed = self.dataset_hm.redim.soft_range(x=(-100,30))
self.assertEqual(redimmed.kdims[0].soft_range, (-100,30))
def test_dataset_redim_hm_kdim_alias(self):
redimmed = self.dataset_hm_alias.redim(x='Time')
self.assertEqual(redimmed.dimension_values('Time'),
self.dataset_hm_alias.dimension_values('x'))
def test_dataset_redim_hm_vdim(self):
redimmed = self.dataset_hm.redim(y='Value')
self.assertEqual(redimmed.dimension_values('Value'),
self.dataset_hm.dimension_values('y'))
def test_dataset_redim_hm_vdim_alias(self):
redimmed = self.dataset_hm_alias.redim(y=Dimension(('val', 'Value')))
self.assertEqual(redimmed.dimension_values('Value'),
self.dataset_hm_alias.dimension_values('y'))
def test_dataset_sample_hm(self):
samples = self.dataset_hm.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 10, 20]))
def test_dataset_sample_hm_alias(self):
samples = self.dataset_hm_alias.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 10, 20]))
def test_dataset_array_hm(self):
self.assertEqual(self.dataset_hm.array(),
np.column_stack([self.xs, self.y_ints]))
def test_dataset_array_hm_alias(self):
self.assertEqual(self.dataset_hm_alias.array(),
np.column_stack([self.xs, self.y_ints]))
def test_dataset_add_dimensions_value_hm(self):
table = self.dataset_hm.add_dimension('z', 1, 0)
self.assertEqual(table.kdims[1], 'z')
self.compare_arrays(table.dimension_values('z'), np.zeros(table.shape[0]))
def test_dataset_add_dimensions_values_hm(self):
table = self.dataset_hm.add_dimension('z', 1, range(1,12))
self.assertEqual(table.kdims[1], 'z')
self.compare_arrays(table.dimension_values('z'), np.array(list(range(1,12))))
def test_dataset_slice_hm(self):
dataset_slice = Dataset({'x':range(5, 9), 'y':[2 * i for i in range(5, 9)]},
kdims=['x'], vdims=['y'])
self.assertEqual(self.dataset_hm[5:9], dataset_slice)
def test_dataset_slice_hm_alias(self):
dataset_slice = Dataset({'x':range(5, 9), 'y':[2 * i for i in range(5, 9)]},
示例5: HeterogeneousColumnTests
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
#.........这里部分代码省略.........
def test_dataset_range_with_dimension_range(self):
dt64 = np.array([np.datetime64(datetime.datetime(2017, 1, i)) for i in range(1, 4)])
ds = Dataset(dt64, [Dimension('Date', range=(dt64[0], dt64[-1]))])
self.assertEqual(ds.range('Date'), (dt64[0], dt64[-1]))
# Operations
@pd_skip
def test_dataset_redim_with_alias_dframe(self):
test_df = pd.DataFrame({'x': range(10), 'y': range(0,20,2)})
dataset = Dataset(test_df, kdims=[('x', 'X-label')], vdims=['y'])
redim_df = pd.DataFrame({'X': range(10), 'y': range(0,20,2)})
dataset_redim = Dataset(redim_df, kdims=['X'], vdims=['y'])
self.assertEqual(dataset.redim(**{'X-label':'X'}), dataset_redim)
self.assertEqual(dataset.redim(**{'x':'X'}), dataset_redim)
def test_dataset_mixed_type_range(self):
ds = Dataset((['A', 'B', 'C', None],), 'A')
self.assertEqual(ds.range(0), ('A', 'C'))
def test_dataset_sort_vdim_ht(self):
dataset = Dataset({'x':self.xs, 'y':-self.ys},
kdims=['x'], vdims=['y'])
dataset_sorted = Dataset({'x': self.xs[::-1], 'y':-self.ys[::-1]},
kdims=['x'], vdims=['y'])
self.assertEqual(dataset.sort('y'), dataset_sorted)
def test_dataset_sort_string_ht(self):
dataset_sorted = Dataset({'Gender':['F', 'M', 'M'], 'Age':[12, 10, 16],
'Weight':[10,15,18], 'Height':[0.8,0.8,0.6]},
kdims=self.kdims, vdims=self.vdims)
self.assertEqual(self.table.sort(), dataset_sorted)
def test_dataset_sample_ht(self):
samples = self.dataset_ht.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 0.5, 1]))
def test_dataset_reduce_ht(self):
reduced = Dataset({'Age':self.age, 'Weight':self.weight, 'Height':self.height},
kdims=self.kdims[1:], vdims=self.vdims)
self.assertEqual(self.table.reduce(['Gender'], np.mean), reduced)
def test_dataset_1D_reduce_ht(self):
self.assertEqual(self.dataset_ht.reduce('x', np.mean), np.float64(0.5))
def test_dataset_2D_reduce_ht(self):
reduced = Dataset({'Weight':[14.333333333333334], 'Height':[0.73333333333333339]},
kdims=[], vdims=self.vdims)
self.assertEqual(self.table.reduce(function=np.mean), reduced)
def test_dataset_2D_partial_reduce_ht(self):
dataset = Dataset({'x':self.xs, 'y':self.ys, 'z':self.zs},
kdims=['x', 'y'], vdims=['z'])
reduced = Dataset({'x':self.xs, 'z':self.zs},
kdims=['x'], vdims=['z'])
self.assertEqual(dataset.reduce(['y'], np.mean), reduced)
def test_dataset_2D_aggregate_spread_fn_with_duplicates(self):
dataset = Dataset({'x': np.array([0, 0, 1, 1]), 'y': np.array([0, 1, 2, 3]),
'z': np.array([1, 2, 3, 4])},
kdims=['x', 'y'], vdims=['z'])
agg = dataset.aggregate('x', function=np.mean, spreadfn=np.var)
self.assertEqual(agg, Dataset({'x': np.array([0, 1]), 'z': np.array([1.5, 3.5]),
'z_var': np.array([0.25, 0.25])},
kdims=['x'], vdims=['z', 'z_var']))
示例6: HomogeneousColumnTypes
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
class HomogeneousColumnTypes(object):
"""
Tests for data formats that require all dataset to have the same
type (e.g numpy arrays)
"""
def setUp(self):
self.restore_datatype = Dataset.datatype
self.data_instance_type = None
def init_data(self):
self.xs = range(11)
self.xs_2 = [el**2 for el in self.xs]
self.y_ints = [i*2 for i in range(11)]
self.dataset_hm = Dataset((self.xs, self.y_ints),
kdims=['x'], vdims=['y'])
def tearDown(self):
Dataset.datatype = self.restore_datatype
# Test the array constructor (homogenous data) to be supported by
# all interfaces.
def test_dataset_array_init_hm(self):
"Tests support for arrays (homogeneous)"
dataset = Dataset(np.column_stack([self.xs, self.xs_2]),
kdims=['x'], vdims=['x2'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_ndelement_init_hm(self):
"Tests support for homogeneous NdElement (backwards compatibility)"
dataset = Dataset(NdElement(zip(self.xs, self.xs_2),
kdims=['x'], vdims=['x2']))
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_dataframe_init_hm(self):
"Tests support for homogeneous DataFrames"
if pd is None:
raise SkipTest("Pandas not available")
dataset = Dataset(pd.DataFrame({'x':self.xs, 'x2':self.xs_2}),
kdims=['x'], vdims=[ 'x2'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
# Properties and information
def test_dataset_shape(self):
self.assertEqual(self.dataset_hm.shape, (11, 2))
def test_dataset_range(self):
self.assertEqual(self.dataset_hm.range('y'), (0, 20))
def test_dataset_closest(self):
closest = self.dataset_hm.closest([0.51, 1, 9.9])
self.assertEqual(closest, [1., 1., 10.])
# Operations
def test_dataset_sort_vdim_hm(self):
xs_2 = np.array(self.xs_2)
dataset = Dataset(np.column_stack([self.xs, -xs_2]),
kdims=['x'], vdims=['y'])
dataset_sorted = Dataset(np.column_stack([self.xs[::-1], -xs_2[::-1]]),
kdims=['x'], vdims=['y'])
self.assertEqual(dataset.sort('y'), dataset_sorted)
def test_dataset_sample_hm(self):
samples = self.dataset_hm.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 10, 20]))
def test_dataset_array_hm(self):
self.assertEqual(self.dataset_hm.array(),
np.column_stack([self.xs, self.y_ints]))
def test_dataset_add_dimensions_value_hm(self):
table = self.dataset_hm.add_dimension('z', 1, 0)
self.assertEqual(table.kdims[1], 'z')
self.compare_arrays(table.dimension_values('z'), np.zeros(len(table)))
def test_dataset_add_dimensions_values_hm(self):
table = self.dataset_hm.add_dimension('z', 1, range(1,12))
self.assertEqual(table.kdims[1], 'z')
self.compare_arrays(table.dimension_values('z'), np.array(list(range(1,12))))
def test_dataset_slice_hm(self):
dataset_slice = Dataset({'x':range(5, 9), 'y':[2 * i for i in range(5, 9)]},
kdims=['x'], vdims=['y'])
self.assertEqual(self.dataset_hm[5:9], dataset_slice)
def test_dataset_slice_fn_hm(self):
dataset_slice = Dataset({'x':range(5, 9), 'y':[2 * i for i in range(5, 9)]},
kdims=['x'], vdims=['y'])
self.assertEqual(self.dataset_hm[lambda x: (x >= 5) & (x < 9)], dataset_slice)
def test_dataset_1D_reduce_hm(self):
dataset = Dataset({'x':self.xs, 'y':self.y_ints}, kdims=['x'], vdims=['y'])
self.assertEqual(dataset.reduce('x', np.mean), 10)
def test_dataset_2D_reduce_hm(self):
dataset = Dataset({'x':self.xs, 'y':self.y_ints, 'z':[el ** 2 for el in self.y_ints]},
#.........这里部分代码省略.........
示例7: HeterogeneousColumnTypes
# 需要导入模块: from holoviews import Dataset [as 别名]
# 或者: from holoviews.Dataset import sample [as 别名]
class HeterogeneousColumnTypes(HomogeneousColumnTypes):
"""
Tests for data formats that all dataset to have varied types
"""
def init_data(self):
self.kdims = ['Gender', 'Age']
self.vdims = ['Weight', 'Height']
self.gender, self.age = ['M','M','F'], [10,16,12]
self.weight, self.height = [15,18,10], [0.8,0.6,0.8]
self.table = Dataset({'Gender':self.gender, 'Age':self.age,
'Weight':self.weight, 'Height':self.height},
kdims=self.kdims, vdims=self.vdims)
super(HeterogeneousColumnTypes, self).init_data()
self.ys = np.linspace(0, 1, 11)
self.zs = np.sin(self.xs)
self.dataset_ht = Dataset({'x':self.xs, 'y':self.ys},
kdims=['x'], vdims=['y'])
# Test the constructor to be supported by all interfaces supporting
# heterogeneous column types.
def test_dataset_ndelement_init_ht(self):
"Tests support for heterogeneous NdElement (backwards compatibility)"
dataset = Dataset(NdElement(zip(self.xs, self.ys), kdims=['x'], vdims=['y']))
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_dataframe_init_ht(self):
"Tests support for heterogeneous DataFrames"
if pd is None:
raise SkipTest("Pandas not available")
dataset = Dataset(pd.DataFrame({'x':self.xs, 'y':self.ys}), kdims=['x'], vdims=['y'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
# Test literal formats
def test_dataset_expanded_dimvals_ht(self):
self.assertEqual(self.table.dimension_values('Gender', expanded=False),
np.array(['M', 'F']))
def test_dataset_implicit_indexing_init(self):
dataset = Dataset(self.ys, kdims=['x'], vdims=['y'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_tuple_init(self):
dataset = Dataset((self.xs, self.ys), kdims=['x'], vdims=['y'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_simple_zip_init(self):
dataset = Dataset(zip(self.xs, self.ys), kdims=['x'], vdims=['y'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_zip_init(self):
dataset = Dataset(zip(self.gender, self.age,
self.weight, self.height),
kdims=self.kdims, vdims=self.vdims)
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_odict_init(self):
dataset = Dataset(OrderedDict(zip(self.xs, self.ys)), kdims=['A'], vdims=['B'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
def test_dataset_dict_init(self):
dataset = Dataset(dict(zip(self.xs, self.ys)), kdims=['A'], vdims=['B'])
self.assertTrue(isinstance(dataset.data, self.data_instance_type))
# Operations
def test_dataset_sort_vdim_ht(self):
dataset = Dataset({'x':self.xs, 'y':-self.ys},
kdims=['x'], vdims=['y'])
dataset_sorted = Dataset({'x': self.xs[::-1], 'y':-self.ys[::-1]},
kdims=['x'], vdims=['y'])
self.assertEqual(dataset.sort('y'), dataset_sorted)
def test_dataset_sort_string_ht(self):
dataset_sorted = Dataset({'Gender':['F', 'M', 'M'], 'Age':[12, 10, 16],
'Weight':[10,15,18], 'Height':[0.8,0.8,0.6]},
kdims=self.kdims, vdims=self.vdims)
self.assertEqual(self.table.sort(), dataset_sorted)
def test_dataset_sample_ht(self):
samples = self.dataset_ht.sample([0, 5, 10]).dimension_values('y')
self.assertEqual(samples, np.array([0, 0.5, 1]))
def test_dataset_reduce_ht(self):
reduced = Dataset({'Age':self.age, 'Weight':self.weight, 'Height':self.height},
kdims=self.kdims[1:], vdims=self.vdims)
self.assertEqual(self.table.reduce(['Gender'], np.mean), reduced)
def test_dataset_1D_reduce_ht(self):
self.assertEqual(self.dataset_ht.reduce('x', np.mean), np.float64(0.5))
def test_dataset_2D_reduce_ht(self):
reduced = Dataset({'Weight':[14.333333333333334], 'Height':[0.73333333333333339]},
kdims=[], vdims=self.vdims)
self.assertEqual(self.table.reduce(function=np.mean), reduced)
def test_dataset_2D_partial_reduce_ht(self):
#.........这里部分代码省略.........