当前位置: 首页>>代码示例>>Python>>正文


Python Vectorizer._convert_dict_to_sparse_matrix方法代码示例

本文整理汇总了Python中eden.graph.Vectorizer._convert_dict_to_sparse_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python Vectorizer._convert_dict_to_sparse_matrix方法的具体用法?Python Vectorizer._convert_dict_to_sparse_matrix怎么用?Python Vectorizer._convert_dict_to_sparse_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在eden.graph.Vectorizer的用法示例。


在下文中一共展示了Vectorizer._convert_dict_to_sparse_matrix方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ListVectorizer

# 需要导入模块: from eden.graph import Vectorizer [as 别名]
# 或者: from eden.graph.Vectorizer import _convert_dict_to_sparse_matrix [as 别名]

#.........这里部分代码省略.........
        return self.transform(graphs_iterators_list_transf)

    def transform(self, graphs_iterators_list, weights=list()):
        """
        Transforms a list of networkx graphs into a Numpy csr sparse matrix
        ( Compressed Sparse Row matrix ).

        Arguments:

        graphs_iterators_list : list of iterators over networkx graphs.
          The data.

        weights : list of positive real values.
          Weights for the linear combination of sparse vectors obtained on each iterated tuple of graphs.
        """
        # if no weights are provided then assume unitary weight
        if len(weights) == 0:
            weights = [1] * len(graphs_iterators_list)
        assert(len(graphs_iterators_list) == len(weights)), 'ERROR: weights size is different than iterators size.'
        assert(len(filter(lambda x: x < 0, weights)) == 0), 'ERROR: weight list contains negative values.'
        for i, graphs in enumerate(graphs_iterators_list):
            if len(self.vectorizers) == 0:
                data_matrix_curr = self.vectorizer.transform(graphs)
            else:
                data_matrix_curr = self.vectorizers[i].transform(graphs)
            if i == 0:
                data_matrix = data_matrix_curr * weights[i]
            else:
                data_matrix = data_matrix + data_matrix_curr * weights[i]
        return data_matrix

    def similarity(self, graphs_iterators_list, ref_instance=None, weights=list()):
        """
        This is a generator.
        """
        self._reference_vec = self._convert_dict_to_sparse_matrix(
            self._transform(0, ref_instance))

        # if no weights are provided then assume unitary weight
        if len(weights) == 0:
            weights = [1] * len(graphs_iterators_list)
        assert(len(graphs_iterators_list) == len(weights)
               ), 'ERROR: weights count is different than iterators count.'
        assert(len(filter(lambda x: x < 0, weights)) ==
               0), 'ERROR: weight list contains negative values.'
        try:
            while True:
                graphs = [G_iterator.next() for G_iterator in graphs_iterators_list]
                yield self._similarity(graphs, weights)
        except StopIteration:
            return

    def _similarity(self, graphs, weights=list()):
        # extract feature vector
        for i, graph in enumerate(graphs):
            x_curr = self.vectorizer._convert_dict_to_sparse_matrix(
                self.vectorizer._transform(0, graph))
            if i == 0:
                x = x_curr * weights[i]
            else:
                x = x + x_curr * weights[i]
        res = self._reference_vec.dot(x.T).todense()
        prediction = res[0, 0]
        return prediction

    def predict(self, graphs_iterators_list, estimator=SGDClassifier(), weights=list()):
        """
        Purpose:
        ----------
        It outputs the estimator prediction of the vectorized graph.

        Arguments:

        estimator : scikit-learn predictor trained on data sampled from the same distribution.
          If None the vertex weigths are by default 1.
        """
        self.estimator = estimator
        # if no weights are provided then assume unitary weight
        if len(weights) == 0:
            weights = [1] * len(graphs_iterators_list)
        assert(len(graphs_iterators_list) == len(weights)), 'ERROR: weights count is different than iterators count.'
        assert(len(filter(lambda x: x < 0, weights)) == 0), 'ERROR: weight list contains negative values.'
        try:
            while True:
                graphs = [G_iterator.next() for G_iterator in graphs_iterators_list]
                yield self._predict(graphs, weights)
        except StopIteration:
            return

    def _predict(self, graphs, weights=list()):
        # extract feature vector
        for i, graph in enumerate(graphs):
            x_curr = self.vectorizer._convert_dict_to_sparse_matrix(self.vectorizer._transform(0, graph))
            if i == 0:
                x = x_curr * weights[i]
            else:
                x = x + x_curr * weights[i]
        margins = self.estimator.decision_function(x)
        prediction = margins[0]
        return prediction
开发者ID:gianlucacorrado,项目名称:EDeN,代码行数:104,代码来源:multi_graph.py


注:本文中的eden.graph.Vectorizer._convert_dict_to_sparse_matrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。