本文整理汇总了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