當前位置: 首頁>>代碼示例>>Python>>正文


Python base.spmatrix方法代碼示例

本文整理匯總了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 
開發者ID:ANSSI-FR,項目名稱:SecuML,代碼行數:21,代碼來源:plots.py

示例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) 
開發者ID:ANSSI-FR,項目名稱:SecuML,代碼行數:33,代碼來源:scores.py

示例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) 
開發者ID:ANSSI-FR,項目名稱:SecuML,代碼行數:30,代碼來源:density.py

示例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) 
開發者ID:ANSSI-FR,項目名稱:SecuML,代碼行數:10,代碼來源:dataset.py

示例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)) 
開發者ID:CrawlScript,項目名稱:TF-GNN,代碼行數:57,代碼來源:gcn.py


注:本文中的scipy.sparse.base.spmatrix方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。