def test_split(nr_sites, local_dim, rank, rgen):
if nr_sites < 2:
return
mpa = factory.random_mpa(nr_sites, local_dim, rank, randstate=rgen)
for pos in range(nr_sites - 1):
mpa_l, mpa_r = mpa.split(pos)
assert len(mpa_l) == pos + 1
assert len(mpa_l) + len(mpa_r) == nr_sites
assert_correct_normalization(mpa_l)
assert_correct_normalization(mpa_r)
recons = np.tensordot(mpa_l.to_array(), mpa_r.to_array(), axes=(-1, 0))
assert_array_almost_equal(mpa.to_array(), recons)
for (lnorm, rnorm) in it.product(range(nr_sites - 1), range(1, nr_sites)):
mpa_l, mpa_r = mpa.split(nr_sites // 2 - 1)
assert_correct_normalization(mpa_l)
assert_correct_normalization(mpa_r)
python类product()的实例源码
def test_povm_ic_mpa(nr_sites, local_dim, rank, rgen):
# Check that the tensor product of the PauliGen POVM is IC.
paulis = povm.pauli_povm(local_dim)
inv_map = mp_from_array_repeat(paulis.linear_inversion_map, nr_sites)
probab_map = mp_from_array_repeat(paulis.probability_map, nr_sites)
reconstruction_map = mp.dot(inv_map, probab_map)
eye = factory.eye(nr_sites, local_dim**2)
assert mp.norm(reconstruction_map - eye) < 1e-5
# Check linear inversion for a particular example MPA.
# Linear inversion works for arbitrary matrices, not only for states,
# so we test it for an arbitrary MPA.
# Normalize, otherwise the absolute error check below will not work.
mpa = factory.random_mpa(nr_sites, local_dim**2, rank,
dtype=np.complex_, randstate=rgen, normalized=True)
probabs = mp.dot(probab_map, mpa)
recons = mp.dot(inv_map, probabs)
assert mp.norm(recons - mpa) < 1e-6
def _words_plus_punc(self):
"""
Returns mapping of form:
{
'cat,': 'cat',
',cat': 'cat',
}
"""
no_punc_text = REGEX_REMOVE_PUNCTUATION.sub('', self.text)
# removes punctuation (but loses emoticons & contractions)
words_only = no_punc_text.split()
# remove singletons
words_only = set( w for w in words_only if len(w) > 1 )
# the product gives ('cat', ',') and (',', 'cat')
punc_before = {''.join(p): p[1] for p in product(PUNC_LIST, words_only)}
punc_after = {''.join(p): p[0] for p in product(words_only, PUNC_LIST)}
words_punc_dict = punc_before
words_punc_dict.update(punc_after)
return words_punc_dict
def _gen_combinator(variants, _merge=True):
if not hasattr(variants, '__iter__'):
return [variants] if variants is not None else []
res = []
need_product = False
for var in variants:
if isinstance(var, list):
sol = _gen_combinator(var, _merge=False)
res.append(sol)
need_product = True
elif var is not None:
res.append(var)
if need_product:
producted = itertools.product(*res)
if _merge:
# TODO(buglloc): ??!
return list(six.moves.map(_merge_variants, producted))
return producted
elif _merge:
return list(six.moves.map(_merge_variants, [res]))
return res
def build_grid(self, grid_sizes):
grid_dict = {}
for param_name, param in self.param_dict.items():
if param.param_type == 'continuous':
grid_dict[param_name] = np.linspace(param.lower, param.upper, grid_sizes[param_name])
elif param.param_type == 'integer':
step_size = int(round((param.upper - param.lower)/float(grid_sizes[param_name])))
grid_dict[param_name] = np.concatenate([np.arange(param.lower, param.upper, step_size), [param.upper]])
elif param.param_type == 'categorical':
grid_dict[param_name] = param.possible_values
elif param.param_type == 'boolean':
grid_dict[param_name] = [True, False]
# now build the grid as a list with all possible combinations i.e. the cartesian product
grid = []
for params in list(itertools.product(*[[(k,v) for v in vals] for k, vals in grid_dict.items()])):
grid.append(dict(params))
return grid
def assertIsOrdered(self, order, x, mxx, ixx, task):
SIZE = 4
if order == 'descending':
check_order = lambda a, b: a >= b
elif order == 'ascending':
check_order = lambda a, b: a <= b
else:
error('unknown order "{}", must be "ascending" or "descending"'.format(order))
are_ordered = True
for j, k in product(range(SIZE), range(1, SIZE)):
self.assertTrue(check_order(mxx[j][k-1], mxx[j][k]),
'torch.sort ({}) values unordered for {}'.format(order, task))
seen = set()
indicesCorrect = True
size = x.size(x.dim()-1)
for k in range(size):
seen.clear()
for j in range(size):
self.assertEqual(x[k][ixx[k][j]], mxx[k][j],
'torch.sort ({}) indices wrong for {}'.format(order, task))
seen.add(ixx[k][j])
self.assertEqual(len(seen), size)
def process_declarations(self, declarations):
new_options = []
for declaration in declarations:
for option in declaration['options']:
optional_args = []
for i, arg in enumerate(option['arguments']):
if 'default' in arg:
optional_args.append(i)
for permutation in product((True, False), repeat=len(optional_args)):
option_copy = deepcopy(option)
for i, bit in zip(optional_args, permutation):
arg = option_copy['arguments'][i]
if not bit:
arg['type'] = 'CONSTANT'
arg['ignore_check'] = True
# PyYAML interprets NULL as None...
arg['name'] = 'NULL' if arg['default'] is None else arg['default']
new_options.append(option_copy)
declaration['options'] = self.filter_unique_options(declaration['options'] + new_options)
return declarations
def make_stateless(self, declaration):
declaration['name'] = 'THPTensor_stateless_({})'.format(declaration['name'])
new_options = []
for option in declaration['options']:
option['cname'] = 'THTensor_({})'.format(option['cname'])
allocated = []
for i, arg in enumerate(option['arguments']):
if 'allocate' in arg and arg['allocate']:
arg['ignore_check'] = True
allocated.append(i)
if arg['name'] == 'self':
arg['name'] = 'source'
for permutation in product((True, False), repeat=len(allocated)):
option_copy = deepcopy(option)
for i, bit in zip(allocated, permutation):
arg = option_copy['arguments'][i]
# By default everything is allocated, so we don't have to do anything
if not bit:
del arg['allocate']
del arg['ignore_check']
new_options.append(option_copy)
declaration['options'] = self.filter_unique_options(declaration['options'] + new_options)
return declaration
def _compute_rarefaction_data(feature_table, min_depth, max_depth, steps,
iterations, phylogeny, metrics):
depth_range = np.linspace(min_depth, max_depth, num=steps, dtype=int)
iter_range = range(1, iterations + 1)
rows = feature_table.ids(axis='sample')
cols = pd.MultiIndex.from_product([list(depth_range), list(iter_range)],
names=['depth', 'iter'])
data = {k: pd.DataFrame(np.NaN, index=rows, columns=cols)
for k in metrics}
for d, i in itertools.product(depth_range, iter_range):
rt = rarefy(feature_table, d)
for m in metrics:
if m in phylogenetic_metrics():
vector = alpha_phylogenetic(table=rt, metric=m,
phylogeny=phylogeny)
else:
vector = alpha(table=rt, metric=m)
data[m][(d, i)] = vector
return data
def set_variant_attributes(variant, product_class):
attr_dict = {}
existing_variants = variant.product.variants.values_list('attributes',
flat=True)
existing_variant_attributes = defaultdict(list)
for variant_attrs in existing_variants:
for attr_id, value_id in variant_attrs.items():
existing_variant_attributes[attr_id].append(value_id)
for product_attribute in product_class.variant_attributes.all():
available_values = product_attribute.values.exclude(
pk__in=[int(pk) for pk
in existing_variant_attributes[str(product_attribute.pk)]])
if not available_values:
return
value = random.choice(available_values)
attr_dict[str(product_attribute.pk)] = str(value.pk)
variant.attributes = attr_dict
variant.save(update_fields=['attributes'])
def test_large_power(cls):
"""Test power for a 9 factor model."""
factor_count = 9
factor_data = []
# generate a 2^9 factorial
for run in itertools.product([-1, 1], repeat=factor_count):
factor_data.append(list(run))
factor_data = pd.DataFrame(factor_data, columns=design.get_factor_names(factor_count))
model = "(X1+X2+X3+X4+X5+X6+X7+X8+X9)**4" # will generate a 4fi model
power_result = power.f_power(model, factor_data, 0.2, 0.05)
answer = np.ndarray(256)
answer.fill(0.61574355066172015)
answer[0] = 0.99459040972676238
np.testing.assert_allclose(power_result, answer, rtol=1e-4)
def reordered_digit_map(exponents, base=2):
"""Construct a mapping which answers the question:
If a base's exponents are applied to a number's digits in arbitrary
order (rather than the conventional greatest-to-least/"big-endian"
ordering), what will its conventionally-calculated value be?
Since every possible value will be included in this mapping, it is
implemented as an indexable tuple rather than a dict.
>>> reordered_digit_map([1, 0])
(0, 1, 2, 3)
>>> reordered_digit_map([0, 1])
(0, 2, 1, 3)
"""
assert sorted(exponents) == list(range(len(exponents)))
digit_values = range(base)
return tuple(
sum(digit * (base ** exponent)
for digit, exponent in zip(digits, exponents))
for digits in product(digit_values, repeat=len(exponents))
)
def test_spectroscopy(self):
ureg = self.ureg
eq = (532. * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV)
with ureg.context('sp'):
from pint.util import find_shortest_path
for a, b in itertools.product(eq, eq):
for x in range(2):
if x == 1:
a = a.to_base_units()
b = b.to_base_units()
da, db = Context.__keytransform__(a.dimensionality,
b.dimensionality)
p = find_shortest_path(ureg._active_ctx.graph, da, db)
self.assertTrue(p)
msg = '{0} <-> {1}'.format(a, b)
# assertAlmostEqualRelError converts second to first
self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg)
for a, b in itertools.product(eq, eq):
self.assertQuantityAlmostEqual(a.to(b.units, 'sp'), b, rtol=0.01)
def test_inputs(self):
V = 'km/hour'
T = 'ms'
L = 'cm'
f1 = lambda x: x
f2 = lambda x: self.Q_(1, x)
f3 = lambda x: self.Q_(1, x).units
f4 = lambda x: self.Q_(1, x).dimensionality
fs = f1, f2, f3, f4
for fv, ft, fl in itertools.product(fs, fs, fs):
qv = fv(V)
qt = ft(T)
ql = ft(L)
self.assertEqual(self.ureg.pi_theorem({'V': qv, 'T': qt, 'L': ql}),
[{'V': 1.0, 'T': 1.0, 'L': -1.0}])
def parse_unit_name(self, unit_name, case_sensitive=True):
"""Parse a unit to identify prefix, unit name and suffix
by walking the list of prefix and suffix.
:rtype: (str, str, str)
"""
stw = unit_name.startswith
edw = unit_name.endswith
for suffix, prefix in itertools.product(self._suffixes, self._prefixes):
if stw(prefix) and edw(suffix):
name = unit_name[len(prefix):]
if suffix:
name = name[:-len(suffix)]
if len(name) == 1:
continue
if case_sensitive:
if name in self._units:
yield (self._prefixes[prefix].name,
self._units[name].name,
self._suffixes[suffix])
else:
for real_name in self._units_casei.get(name.lower(), ()):
yield (self._prefixes[prefix].name,
self._units[real_name].name,
self._suffixes[suffix])
def rectangle_to_rectangle_distance(ca, cb, wa, wb, ha, hb):
a1 = ca + np.array([wa/2.0, ha/2.0])
a2 = ca + np.array([wa/2.0, -ha/2.0])
a3 = ca + np.array([-wa/2.0, -ha/2.0])
a4 = ca + np.array([-wa/2.0, ha/2.0])
b1 = cb + np.array([wb/2.0, hb/2.0])
b2 = cb + np.array([wb/2.0, -hb/2.0])
b3 = cb + np.array([-wb/2.0, -hb/2.0])
b4 = cb + np.array([-wb/2.0, hb/2.0])
for e1, e2 in product(rectangle_edges(a1,a2,a3,a4), rectangle_edges(b1,b2,b3,b4)):
if segments_intersect(e1[0], e1[1], e2[0], e2[1]):
return 0.0
da1 = point_to_rectangle_distance(a1, cb, wb, hb)
da2 = point_to_rectangle_distance(a2, cb, wb, hb)
da3 = point_to_rectangle_distance(a3, cb, wb, hb)
da4 = point_to_rectangle_distance(a4, cb, wb, hb)
db1 = point_to_rectangle_distance(b1, ca, wa, ha)
db2 = point_to_rectangle_distance(b2, ca, wa, ha)
db3 = point_to_rectangle_distance(b3, ca, wa, ha)
db4 = point_to_rectangle_distance(b4, ca, wa, ha)
return min([da1, da2, da3, da4, db1, db2, db3, db4])
def traverse(self):
return ((self.contents[pos[0]][pos[1]], pos) for pos in itertools.product(range(self.rows), range(self.cols)))
def _make_one_hot(self, word_inds, vec_size):
onehot = numpy.zeros((word_inds.shape + (vec_size,)))
for inds in itertools.product(*[numpy.arange(s) for s in word_inds.shape]):
onehot[inds+(word_inds[inds],)] = 1
return onehot
linearRegression_lassoRegularization.py 文件源码
项目:HousePricePredictionKaggle
作者: Nuwantha
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def poly(X):
areas = ['LotArea', 'TotalBsmtSF', 'GrLivArea', 'GarageArea', 'BsmtUnfSF']
# t = [s for s in X.axes[1].get_values() if s not in areas]
t = chain(qu_list.axes[1].get_values(),
['OverallQual', 'OverallCond', 'ExterQual', 'ExterCond', 'BsmtCond', 'GarageQual', 'GarageCond',
'KitchenQual', 'HeatingQC', 'bad_heating', 'MasVnrType_Any', 'SaleCondition_PriceDown', 'Reconstruct',
'ReconstructAfterBuy', 'Build.eq.Buy'])
for a, t in product(areas, t):
x = X.loc[:, [a, t]].prod(1)
x.name = a + '_' + t
yield x
def generate_parses(causal_tree):
node_type = causal_tree["node_type"]
if "children" not in causal_tree:
return (causal_tree,)
partial_causal_parses = []
# make a copy of the current node, minus the children (so we're keeping symbol_type, symbol, energy, node_type, etc)
current_node = causal_tree.copy()
current_node.pop("children")
if node_type in ("or","root",):
for child_node in causal_tree["children"]:
for parse in generate_parses(child_node):
current_node["children"] = (parse,)
partial_causal_parses.append(current_node.copy())
elif node_type in ("and",):
# generate causal parses on each tree
# build all cartesian products of those causal parses;
# each cartesian product is a set of children for the and node, a separate partial parse graph to return
child_parses = []
for child_node in causal_tree["children"]:
child_parses.append(generate_parses(child_node),)
for product in itertools.product(*child_parses):
current_node["children"] = product
partial_causal_parses.append(current_node.copy())
else:
raise Exception("UNKNOWN NODE TYPE: {}".format(node_type))
return partial_causal_parses