Python scipy.sparse.base.spmatrix() Examples

The following are 5 code examples of scipy.sparse.base.spmatrix(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.sparse.base , or try the search function .
Example #1
Source File: plots.py    From SecuML with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #2
Source File: scores.py    From SecuML with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #3
Source File: density.py    From SecuML with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #4
Source File: dataset.py    From SecuML with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #5
Source File: gcn.py    From TF-GNN with GNU General Public License v3.0 4 votes vote down vote up
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))