本文整理汇总了Python中crosscat.cython_code.State.p_State方法的典型用法代码示例。如果您正苦于以下问题:Python State.p_State方法的具体用法?Python State.p_State怎么用?Python State.p_State使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类crosscat.cython_code.State
的用法示例。
在下文中一共展示了State.p_State方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_X_L_and_X_D
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def generate_X_L_and_X_D(T, M_c, cols_to_views, row_to_clusters, seed=0):
state = State.p_State(M_c, T, SEED=seed)
X_L = state.get_X_L()
# insert assigment into X_L (this is not a valid X_L because the counts and
# suffstats will be wrong)
X_L['column_partition']['assignments'] = cols_to_views
state = State.p_State(M_c, T, X_L=X_L, X_D=row_to_clusters, SEED=seed)
X_L = state.get_X_L()
X_D = state.get_X_D()
return X_L, X_D
示例2: GenerateRandomState
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def GenerateRandomState(n_rows, n_cols, seed, mean_gen=0.0, std_gen=1.0, std_data=0.1, alpha_col=1.0, alpha_rows=1.0):
# check the inputs
assert(type(n_rows) is int)
assert(type(n_cols) is int)
assert(type(mean_gen) is float)
assert(type(std_gen) is float)
assert(type(std_data) is float)
assert(type(alpha_col) is float)
assert(type(alpha_rows) is float)
assert(n_rows > 0)
assert(n_cols > 0)
assert(std_gen > 0.0)
assert(std_data > 0.0)
assert(alpha_col > 0.0)
assert(alpha_rows > 0.0)
rng = np.random.RandomState(seed)
# generate the partitioning
part = GenerateRandomPartition(n_rows, n_cols, alpha_col, alpha_rows, seed=seed)
# fill it with data
T, M_r, M_c = GenDataFromPartitions(part['col_parts'], part['row_parts'], mean_gen, std_gen, std_data)
# this part is kind of hacky:
# generate a state from the prior
state = State.p_State(M_c, T, N_GRID=100)
# get the X_L and X_D and implant part['col_parts'], part['row_parts'], then
# create a new state with the new X_L and X_D defined
X_L = state.get_X_L()
X_D = state.get_X_D()
# this should be all we need to change for
# State.transform_latent_state_to_constructor_args(X_L, X_D) to be able
# to construct the arguments to intialize a state
X_L['column_partition']['assignments'] = part['col_parts'].tolist()
X_D = part['row_parts'].tolist()
# hack in the alpha values supplied (or not) by the user
X_L['column_partition']['hypers']['alpha'] = alpha_col
for i in range(len(X_L['view_state'])):
X_L['view_state'][i]['row_partition_model']['hypers']['alpha'] = alpha_col
for i in range(n_cols):
X_L['column_hypers'][i]['alpha'] = alpha_rows
# create a new state with the updated X_D and X_L
state = State.p_State(M_c, T, X_L=X_L, X_D=X_D, N_GRID=100)
return state, T, M_r, M_c
示例3: get_generative_clustering
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def get_generative_clustering(M_c, M_r, T,
data_inverse_permutation_indices,
num_clusters, num_views):
# NOTE: this function only works because State.p_State doesn't use
# column_component_suffstats
num_rows = len(T)
num_cols = len(T[0])
X_D_helper = numpy.repeat(range(num_clusters), (num_rows / num_clusters))
gen_X_D = [
X_D_helper[numpy.argsort(data_inverse_permutation_index)]
for data_inverse_permutation_index in data_inverse_permutation_indices
]
gen_X_L_assignments = numpy.repeat(range(num_views), (num_cols / num_views))
# initialize to generate an X_L to manipulate
local_engine = LE.LocalEngine()
bad_X_L, bad_X_D = local_engine.initialize(M_c, M_r, T,
initialization='apart')
bad_X_L['column_partition']['assignments'] = gen_X_L_assignments
# manually constrcut state in in generative configuration
state = State.p_State(M_c, T, bad_X_L, gen_X_D)
gen_X_L = state.get_X_L()
gen_X_D = state.get_X_D()
# run inference on hyperparameters to leave them in a reasonable state
kernel_list = (
'row_partition_hyperparameters',
'column_hyperparameters',
'column_partition_hyperparameter',
)
gen_X_L, gen_X_D = local_engine.analyze(M_c, T, gen_X_L, gen_X_D, n_steps=1,
kernel_list=kernel_list)
#
return gen_X_L, gen_X_D
示例4: _do_analyze_with_diagnostic
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_analyze_with_diagnostic(
SEED, X_L, X_D, M_c, T, kernel_list, n_steps, c, r, max_iterations,
max_time, diagnostic_func_dict, every_N, ROW_CRP_ALPHA_GRID,
COLUMN_CRP_ALPHA_GRID, S_GRID, MU_GRID, N_GRID, do_timing, CT_KERNEL,
progress,):
diagnostics_dict = collections.defaultdict(list)
if diagnostic_func_dict is None:
diagnostic_func_dict = dict()
every_N = None
p_State = State.p_State(
M_c, T, X_L, X_D, SEED=SEED, ROW_CRP_ALPHA_GRID=ROW_CRP_ALPHA_GRID,
COLUMN_CRP_ALPHA_GRID=COLUMN_CRP_ALPHA_GRID, S_GRID=S_GRID,
MU_GRID=MU_GRID, N_GRID=N_GRID, CT_KERNEL=CT_KERNEL)
with gu.Timer('all transitions', verbose=False) as timer:
p_State.transition(
kernel_list, n_steps, c, r, max_iterations, max_time,
progress=progress,
diagnostic_func_dict=diagnostic_func_dict,
diagnostics_dict=diagnostics_dict,
diagnostics_every_N=every_N)
X_L_prime = p_State.get_X_L()
X_D_prime = p_State.get_X_D()
if do_timing:
# Diagnostics and timing are exclusive.
diagnostics_dict = timer.elapsed_secs
return X_L_prime, X_D_prime, diagnostics_dict
示例5: _do_analyze2
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_analyze2((M_c, T, X_L, X_D, kernel_list, n_steps, c, r,
max_iterations, max_time, SEED)):
p_State = State.p_State(M_c, T, X_L, X_D, SEED=SEED)
p_State.transition(kernel_list, n_steps, c, r,
max_iterations, max_time)
X_L_prime = p_State.get_X_L()
X_D_prime = p_State.get_X_D()
return X_L_prime, X_D_prime
示例6: GenerateStateFromPartitions
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def GenerateStateFromPartitions(col_parts, row_parts, mean_gen=0.0, std_gen=1.0, std_data=0.1):
T, M_r, M_c = GenDataFromPartitions(col_parts, row_parts, mean_gen=mean_gen, std_gen=std_gen, std_data=std_data)
state = State.p_State(M_c, T, N_GRID=100)
X_L = state.get_X_L()
X_D = state.get_X_D()
if type(col_parts) is not list:
X_L['column_partition']['assignments'] = col_parts.tolist()
if type(row_parts) is not list:
X_D = row_parts.tolist()
# create a new state with the updated X_D and X_L
state = State.p_State(M_c, T, X_L=X_L, X_D=X_D, N_GRID=100)
return state, T, M_c, M_r, X_L, X_D
示例7: _sample_and_insert
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _sample_and_insert(self, M_c, T, X_L, X_D, matching_row_indices):
p_State = State.p_State(M_c, T, X_L, X_D)
draws = []
for matching_row_idx in matching_row_indices:
random_seed = self.get_next_seed()
draw = p_State.get_draw(matching_row_idx, random_seed)
p_State.insert_row(draw, matching_row_idx)
draws.append(draw)
T.append(draw)
X_L, X_D = p_State.get_X_L(), p_State.get_X_D()
return draws, T, X_L, X_D
示例8: _do_initialize
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_initialize(
SEED, M_c, M_r, T, initialization, row_initialization,
ROW_CRP_ALPHA_GRID, COLUMN_CRP_ALPHA_GRID, S_GRID, MU_GRID, N_GRID,):
p_State = State.p_State(
M_c, T, initialization=initialization,
row_initialization=row_initialization, SEED=SEED,
ROW_CRP_ALPHA_GRID=ROW_CRP_ALPHA_GRID,
COLUMN_CRP_ALPHA_GRID=COLUMN_CRP_ALPHA_GRID, S_GRID=S_GRID,
MU_GRID=MU_GRID, N_GRID=N_GRID,)
X_L = p_State.get_X_L()
X_D = p_State.get_X_D()
return X_L, X_D
示例9: _do_insert
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_insert(M_c, T, X_L, X_D, new_rows, N_GRID, CT_KERNEL):
p_State = State.p_State(M_c, T, X_L=X_L, X_D=X_D,
N_GRID=N_GRID,
CT_KERNEL=CT_KERNEL)
row_idx = len(T)
for row_data in new_rows:
p_State.insert_row(row_data, row_idx)
p_State.transition(which_transitions=['row_partition_assignments'], r=[row_idx])
row_idx += 1
X_L_prime = p_State.get_X_L()
X_D_prime = p_State.get_X_D()
return X_L_prime, X_D_prime
示例10: setUp
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def setUp(self):
# generate a crosscat state and pull the metadata
gen_seed = 0
num_clusters = 2
self.num_rows = 10
self.num_cols = 2
num_splits = 1
self.T, self.M_r, self.M_c = du.gen_factorial_data_objects(gen_seed,
num_clusters, self.num_cols,
self.num_rows, num_splits)
state = State.p_State(self.M_c, self.T)
self.X_L = state.get_X_L()
self.X_D = state.get_X_D()
示例11: _do_analyze
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_analyze(
SEED, X_L, X_D, M_c, T, kernel_list, n_steps, c, r,
max_iterations, max_time, ROW_CRP_ALPHA_GRID, COLUMN_CRP_ALPHA_GRID,
S_GRID, MU_GRID, N_GRID, CT_KERNEL, progress):
p_State = State.p_State(
M_c, T, X_L, X_D, SEED=SEED, ROW_CRP_ALPHA_GRID=ROW_CRP_ALPHA_GRID,
COLUMN_CRP_ALPHA_GRID=COLUMN_CRP_ALPHA_GRID, S_GRID=S_GRID,
MU_GRID=MU_GRID, N_GRID=N_GRID, CT_KERNEL=CT_KERNEL)
p_State.transition(
kernel_list, n_steps, c, r, max_iterations, max_time, progress)
X_L_prime = p_State.get_X_L()
X_D_prime = p_State.get_X_D()
return X_L_prime, X_D_prime
示例12: _do_analyze_with_diagnostic
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_analyze_with_diagnostic(SEED, X_L, X_D, M_c, T, kernel_list, n_steps, c, r,
max_iterations, max_time, diagnostic_func_dict, every_N,
ROW_CRP_ALPHA_GRID, COLUMN_CRP_ALPHA_GRID,
S_GRID, MU_GRID,
N_GRID,
do_timing,
CT_KERNEL,
):
diagnostics_dict = collections.defaultdict(list)
if diagnostic_func_dict is None:
diagnostic_func_dict = dict()
every_N = None
child_n_steps_list = get_child_n_steps_list(n_steps, every_N)
# import ipdb; ipdb.set_trace()
p_State = State.p_State(M_c, T, X_L, X_D, SEED=SEED,
ROW_CRP_ALPHA_GRID=ROW_CRP_ALPHA_GRID,
COLUMN_CRP_ALPHA_GRID=COLUMN_CRP_ALPHA_GRID,
S_GRID=S_GRID,
MU_GRID=MU_GRID,
N_GRID=N_GRID,
CT_KERNEL=CT_KERNEL,
)
with gu.Timer('all transitions', verbose=False) as timer:
for child_n_steps in child_n_steps_list:
p_State.transition(kernel_list, child_n_steps, c, r,
max_iterations, max_time)
for diagnostic_name, diagnostic_func in six.iteritems(diagnostic_func_dict):
diagnostic_value = diagnostic_func(p_State)
diagnostics_dict[diagnostic_name].append(diagnostic_value)
pass
pass
pass
X_L_prime = p_State.get_X_L()
X_D_prime = p_State.get_X_D()
#
if do_timing:
# diagnostics and timing are exclusive
diagnostics_dict = timer.elapsed_secs
pass
return X_L_prime, X_D_prime, diagnostics_dict
示例13: calc_mean_test_log_likelihood
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def calc_mean_test_log_likelihood(M_c, T, X_L, X_D, T_test):
state = State.p_State(M_c, T, X_L, X_D)
test_log_likelihoods = map(state.calc_row_predictive_logp, T_test)
mean_test_log_likelihood = numpy.mean(test_log_likelihoods)
return mean_test_log_likelihood
示例14: check_one_feature_sampler
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def check_one_feature_sampler(component_model_type, show_plot=False):
"""
Tests the ability of component model of component_model_type to capture the
distribution of the data.
1. Draws 100 random points from a standard normal distribution
2. Initializes a component model with that data (and random hyperparameters)
3. Draws data from that component model
4. Initialize a crosscat state with that data
5. Get one sample after 100 transitions
6. Draw predictive samples
7. Caluclates the 95 precent support of the continuous distribution or the
entire support of the discrete distribution
8. Calculate the true pdf for each point in the support
9. Calculate the predictive probability given the sample for each point in
the support
10. (OPTIONAL) Plot the original data, predictive samples, pdf, and
predictive probabilities
11. Calculate goodness of fit stats (returns p value)
"""
N = 250
get_next_seed = lambda : random.randrange(2147483647)
data_params = default_data_parameters[component_model_type.model_type]
X = component_model_type.generate_data_from_parameters(data_params, N, gen_seed=get_next_seed())
hyperparameters = component_model_type.draw_hyperparameters(X, gen_seed=get_next_seed())[0]
component_model = component_model_type.from_data(X, hyperparameters)
model_parameters = component_model.sample_parameters_given_hyper()
# generate data from the parameters
T = component_model_type.generate_data_from_parameters(model_parameters, N, gen_seed=get_next_seed())
# create a crosscat state
M_c = du.gen_M_c_from_T(T, cctypes=[component_model_type.cctype])
state = State.p_State(M_c, T)
# transitions
n_transitions = 100
state.transition(n_steps=n_transitions)
# get the sample
X_L = state.get_X_L()
X_D = state.get_X_D()
# generate samples
# kstest has doesn't compute the same answer with row and column vectors
# so we flatten this column vector into a row vector.
predictive_samples = numpy.array(su.simple_predictive_sample(M_c, X_L, X_D, [], [(N,0)], get_next_seed, n=N)).flatten(1)
# get support
discrete_support = component_model_type.generate_discrete_support(model_parameters)
# calculate simple predictive probability for each point
Q = [(N,0,x) for x in discrete_support]
probabilities = su.simple_predictive_probability(M_c, X_L, X_D, []*len(Q), Q,)
T = numpy.array(T)
# get histogram. Different behavior for discrete and continuous types. For some reason
# the normed property isn't normalizing the multinomial histogram to 1.
if is_discrete[component_model_type.model_type]:
T_hist, edges = numpy.histogram(T, bins=len(discrete_support))
S_hist, _ = numpy.histogram(predictive_samples, bins=edges)
T_hist = T_hist/float(numpy.sum(T_hist))
S_hist = S_hist/float(numpy.sum(S_hist))
edges = numpy.array(discrete_support,dtype=float)
else:
T_hist, edges = numpy.histogram(T, bins=min(20,len(discrete_support)), normed=True)
S_hist, _ = numpy.histogram(predictive_samples, bins=edges, normed=True)
edges = edges[0:-1]
# Goodness-of-fit-tests
if not is_discrete[component_model_type.model_type]:
# do a KS tests if the distribution in continuous
# cdf = lambda x: component_model_type.cdf(x, model_parameters)
# stat, p = stats.kstest(predictive_samples, cdf) # 1-sample test
stat, p = stats.ks_2samp(predictive_samples, T[:,0]) # 2-sample test
test_str = "KS"
else:
# Cressie-Read power divergence statistic and goodness of fit test.
# This function gives a lot of flexibility in the method <lambda_> used.
freq_obs = S_hist*N
freq_exp = numpy.exp(probabilities)*N
stat, p = stats.power_divergence(freq_obs, freq_exp, lambda_='pearson')
test_str = "Chi-square"
if show_plot:
pylab.clf()
pylab.axes([0.1, 0.1, .8, .7])
# bin widths
width = (numpy.max(edges)-numpy.min(edges))/len(edges)
pylab.bar(edges, T_hist, color='blue', alpha=.5, width=width, label='Original data')
pylab.bar(edges, S_hist, color='red', alpha=.5, width=width, label='Predictive samples')
#.........这里部分代码省略.........
示例15: _do_initialize2
# 需要导入模块: from crosscat.cython_code import State [as 别名]
# 或者: from crosscat.cython_code.State import p_State [as 别名]
def _do_initialize2((M_c, M_r, T, initialization, SEED)):
p_State = State.p_State(M_c, T, initialization=initialization, SEED=SEED)
X_L = p_State.get_X_L()
X_D = p_State.get_X_D()
return X_L, X_D