本文整理汇总了Python中scipy.sparse.base.spmatrix方法的典型用法代码示例。如果您正苦于以下问题:Python base.spmatrix方法的具体用法?Python base.spmatrix怎么用?Python base.spmatrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse.base
的用法示例。
在下文中一共展示了base.spmatrix方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _gen_label_plot_dataset
# 需要导入模块: from scipy.sparse import base [as 别名]
# 或者: from scipy.sparse.base import spmatrix [as 别名]
def _gen_label_plot_dataset(self, instances, label=None, family=None,
color=None):
if label is not None:
if label != 'unlabeled':
instances = instances.get_annotated_instances(label=label)
else:
instances = instances.get_unlabeled_instances()
else:
instances = instances.get_annotated_instances(family=family)
values = instances.features.get_values_from_index(self.feature_index)
if isinstance(values, spmatrix):
values = values.toarray()
plot_label = label if label is not None else family
plot_color = color
if plot_color is None:
plot_color = get_label_color(plot_label)
dataset = PlotDataset(values, plot_label)
dataset.set_color(plot_color)
self.plot_datasets[plot_label] = dataset
示例2: compute_scoring_func
# 需要导入模块: from scipy.sparse import base [as 别名]
# 或者: from scipy.sparse.base import spmatrix [as 别名]
def compute_scoring_func(self, func):
if func == 'variance':
features = self.instances.features.get_values()
annotations = self.instances.annotations.get_labels()
if isinstance(features, spmatrix):
variance = mean_variance_axis(features, axis=0)[1]
else:
variance = features.var(axis=0)
return variance, None
features = self.annotated_instances.features.get_values()
annotations = self.annotated_instances.annotations.get_supervision(
self.multiclass)
if func == 'f_classif':
return f_classif(features, annotations)
elif func == 'mutual_info_classif':
if isinstance(features, spmatrix):
discrete_indexes = True
else:
features_types = self.instances.features.info.types
discrete_indexes = [i for i, t in enumerate(features_types)
if t == FeatureType.binary]
if not discrete_indexes:
discrete_indexes = False
return (mutual_info_classif(features, annotations,
discrete_features=discrete_indexes),
None)
elif func == 'chi2':
return chi2(features, annotations)
else:
assert(False)
示例3: _display_dataset
# 需要导入模块: from scipy.sparse import base [as 别名]
# 或者: from scipy.sparse.base import spmatrix [as 别名]
def _display_dataset(self, dataset):
eps = 0.00001
linewidth = dataset.linewidth
delta = self.max_value - self.min_value
density_delta = 1.2 * delta
if delta > 0:
x = np.arange(self.min_value - 0.1*delta,
self.max_value + 0.1*delta,
density_delta / self.num_points)
else:
x = np.array([self.min_value - 2*eps, self.max_value + 2*eps])
if isinstance(dataset.values, spmatrix):
variance = mean_variance_axis(dataset.values, axis=0)[1]
else:
variance = np.var(dataset.values)
if variance < eps:
linewidth += 2
mean = np.mean(dataset.values)
x = np.sort(np.append(x, [mean, mean - eps, mean + eps]))
density = [1 if v == mean else 0 for v in x]
else:
self.kde.fit(dataset.values)
x_density = [[y] for y in x]
# kde.score_samples returns the 'log' of the density
log_density = self.kde.score_samples(x_density).tolist()
density = list(map(math.exp, log_density))
self.ax.plot(x, density, label=dataset.label, color=dataset.color,
linewidth=linewidth, linestyle=dataset.linestyle)
示例4: _set_values
# 需要导入模块: from scipy.sparse import base [as 别名]
# 或者: from scipy.sparse.base import spmatrix [as 别名]
def _set_values(self, values):
self.values = values
if len(self.values.shape) == 1:
new_shape = (self.values.shape[0], 1)
if isinstance(self.values, spmatrix):
self.values = self.values.reshape(new_shape)
else:
self.values = np.reshape(self.values, new_shape)
示例5: train
# 需要导入模块: from scipy.sparse import base [as 别名]
# 或者: from scipy.sparse.base import spmatrix [as 别名]
def train(self,
adj,
feature_matrix,
labels,
train_masks,
test_masks,
steps=1000,
learning_rate=1e-3,
l2_coe=1e-3,
drop_rate=1e-3,
show_interval=20,
eval_interval=20):
if test_masks is None:
test_masks = 1 - np.array(train_masks)
A = GCN.gcn_kernal_tensor(adj, sparse=True)
num_classes = self.model.num_units_list[-1]
one_hot_labels = tf.one_hot(labels, num_classes)
optimizer = tf.train.AdamOptimizer(learning_rate)
if feature_matrix is None:
feature_matrix = sp.diags(range(adj.shape[0]))
if isinstance(feature_matrix, spmatrix):
coo_feature_matrix = feature_matrix.tocoo().astype(np.float32)
x = tf.SparseTensor(indices=np.stack((coo_feature_matrix.row, coo_feature_matrix.col), axis=1),
values=coo_feature_matrix.data, dense_shape=coo_feature_matrix.shape)
else:
x = tf.Variable(feature_matrix, trainable=False)
num_masked = tf.cast(tf.reduce_sum(train_masks), tf.float32)
for step in range(steps):
with tf.GradientTape() as tape:
logits = self.model([A, x], training=True)
losses = tf.nn.softmax_cross_entropy_with_logits(
logits=logits,
labels=one_hot_labels
)
losses *= train_masks
mean_loss = tf.reduce_sum(losses) / num_masked
loss = mean_loss + self.model.l2_loss() * l2_coe
watched_vars = tape.watched_variables()
grads = tape.gradient(loss, watched_vars)
optimizer.apply_gradients(zip(grads, watched_vars))
if step % show_interval == 0:
print("step = {}\tloss = {}".format(step, loss))
if step % eval_interval == 0:
preds = self.model([A, x])
preds = tf.argmax(preds, axis=-1).numpy()
accuracy, macro_f1, micro_f1 = evaluate(preds, labels, test_masks)
print("step = {}\taccuracy = {}\tmacro_f1 = {}\tmicro_f1 = {}".format(step, accuracy, macro_f1, micro_f1))