本文整理汇总了Python中scipy.sparse.dok_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.dok_matrix方法的具体用法?Python sparse.dok_matrix怎么用?Python sparse.dok_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.dok_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_dtm
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def generate_dtm(self, corpus, tfidf=False):
""" Generate the inside document-term matrix and other peripherical information
objects. This is run when the class is instantiated.
:param corpus: corpus.
:param tfidf: whether to weigh using tf-idf. (Default: False)
:return: None
:type corpus: list
:type tfidf: bool
"""
self.dictionary = Dictionary(corpus)
self.dtm = dok_matrix((len(corpus), len(self.dictionary)), dtype=np.float)
bow_corpus = [self.dictionary.doc2bow(doctokens) for doctokens in corpus]
if tfidf:
weighted_model = TfidfModel(bow_corpus)
bow_corpus = weighted_model[bow_corpus]
for docid in self.docids:
for tokenid, count in bow_corpus[self.docid_dict[docid]]:
self.dtm[self.docid_dict[docid], tokenid] = count
示例2: shorttext_to_vec
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def shorttext_to_vec(self, shorttext):
""" Convert the shorttext into a sparse vector given the dictionary.
According to the dictionary (gensim.corpora.Dictionary), convert the given text
into a vector representation, according to the occurence of tokens.
This function is deprecated and no longer used because it is too slow to run in a loop.
But this is used while doing prediction.
:param shorttext: short text to be converted.
:return: sparse vector of the vector representation
:type shorttext: str
:rtype: scipy.sparse.dok_matrix
"""
# too slow, deprecated
tokens = tokenize(self.preprocessor(shorttext))
vec = dok_matrix((1, len(self.dictionary)))
for token in tokens:
if token in self.dictionary.token2id:
vec[0, self.dictionary.token2id[token]] = 1.0
return vec[0, :]
示例3: convert_classdict_to_XY
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def convert_classdict_to_XY(self, classdict):
""" Convert the training data into sparse matrices for training.
:param classdict: training data
:return: a tuple, consisting of sparse matrices for X (training data) and y (the labels of the training data)
:type classdict: dict
:rtype: tuple
"""
nb_data = sum([len(classdict[k]) for k in classdict])
X = dok_matrix((nb_data, len(self.dictionary)))
y = dok_matrix((nb_data, len(self.labels2idx)))
rowid = 0
for label in classdict:
if label in self.labels2idx.keys():
for shorttext in classdict[label]:
tokens = tokenize(self.preprocessor(shorttext))
for token in tokens:
X[rowid, self.dictionary.token2id[token]] += 1.0
y[rowid, self.labels2idx[label]] = 1.
rowid += 1
return X, y
示例4: load_rating_file_as_matrix
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def load_rating_file_as_matrix(self, filename):
# get numbers of users and items
num_users, num_items = 0, 0
with open(filename, 'r') as f:
line = f.readline()
while(line != None and line != ''):
arr = line.split('\t')
user, item = int(arr[0]), int(arr[1])
num_users = max(num_users, user)
num_items = max(num_items, item)
line = f.readline()
# contruct matrix
mat = sp.dok_matrix((num_users+1, num_items+1), dtype=np.float32)
with open(filename, 'r') as f:
line = f.readline()
while(line != None and line != ''):
arr = line.split('\t')
user, item, rating = int(arr[0]), int(arr[1]), float(arr[2])
if(rating > 0):
mat[user, item] = 1.0
line = f.readline()
return mat
示例5: __init__
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def __init__(self, quadratic_program: Any,
coefficients: Union[ndarray, spmatrix, List[List[float]],
Dict[Tuple[Union[int, str], Union[int, str]], float]]) -> None:
"""Creates a new quadratic expression.
The quadratic expression can be defined via an array, a list, a sparse matrix, or a
dictionary that uses variable names or indices as keys and stores the values internally as a
dok_matrix. We stores values in a compressed way, i.e., values at symmetric positions are
summed up in the upper triangle. For example, {(0, 1): 1, (1, 0): 2} -> {(0, 1): 3}.
Args:
quadratic_program: The parent QuadraticProgram.
coefficients: The (sparse) representation of the coefficients.
"""
super().__init__(quadratic_program)
self.coefficients = coefficients
示例6: __init__
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def __init__(self, quadratic_program: Any,
coefficients: Union[ndarray, spmatrix, List[float],
Dict[Union[int, str], float]]) -> None:
"""Creates a new linear expression.
The linear expression can be defined via an array, a list, a sparse matrix, or a dictionary
that uses variable names or indices as keys and stores the values internally as a
dok_matrix.
Args:
quadratic_program: The parent QuadraticProgram.
coefficients: The (sparse) representation of the coefficients.
"""
super().__init__(quadratic_program)
self.coefficients = coefficients
示例7: evaluate
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def evaluate(self, x: Union[ndarray, List, Dict[Union[int, str], float]]) -> float:
"""Evaluate the linear expression for given variables.
Args:
x: The values of the variables to be evaluated.
Returns:
The value of the linear expression given the variable values.
"""
# cast input to dok_matrix if it is a dictionary
x = self._coeffs_to_dok_matrix(x)
# compute the dot-product of the input and the linear coefficients
val = (x @ self.coefficients.transpose())[0, 0]
# return the result
return val
示例8: test_init
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def test_init(self):
""" test init. """
quadratic_program = QuadraticProgram()
for _ in range(5):
quadratic_program.continuous_var()
coefficients_list = list(range(5))
coefficients_array = np.array(coefficients_list)
coefficients_dok = dok_matrix([coefficients_list])
coefficients_dict_int = {i: i for i in range(1, 5)}
coefficients_dict_str = {'x{}'.format(i): i for i in range(1, 5)}
for coeffs in [coefficients_list,
coefficients_array,
coefficients_dok,
coefficients_dict_int,
coefficients_dict_str]:
linear = LinearExpression(quadratic_program, coeffs)
self.assertEqual((linear.coefficients != coefficients_dok).nnz, 0)
self.assertTrue((linear.to_array() == coefficients_list).all())
self.assertDictEqual(linear.to_dict(use_name=False), coefficients_dict_int)
self.assertDictEqual(linear.to_dict(use_name=True), coefficients_dict_str)
示例9: boundary_operator
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def boundary_operator(self, i):
source_simplices = self.n_faces(i)
target_simplices = self.n_faces(i-1)
if len(target_simplices) == 0:
S = dok_matrix((1, len(source_simplices)), dtype=np.float64)
S[0, 0:len(source_simplices)] = 1
else:
source_simplices_dict = {source_simplices[j]:
j for j in range(len(source_simplices))}
target_simplices_dict = {target_simplices[i]:
i for i in range(len(target_simplices))}
S = dok_matrix((len(target_simplices),
len(source_simplices)),
dtype=np.float64)
for source_simplex in source_simplices:
for a in range(len(source_simplex)):
target_simplex = source_simplex[:a]+source_simplex[(a+1):]
i = target_simplices_dict[target_simplex]
j = source_simplices_dict[source_simplex]
S[i, j] = -1 if a % 2 == 1 else 1 # S[i, j] = (-1)**a
return S
示例10: getTransitionAndRewardArrays
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def getTransitionAndRewardArrays():
""""""
P = [dok_matrix((STATES, STATES)) for a in range(ACTIONS)]
#R = spdok((STATES, ACTIONS))
R = np.zeros((STATES, ACTIONS))
# Naive approach, iterate through all possible combinations
for a in range(ACTIONS):
for s in range(STATES):
state = convertIndexToTuple(s)
if not isValid(state):
# There are no defined moves from an invalid state, so
# transition probabilities cannot be calculated. However,
# P must be a square stochastic matrix, so assign a
# probability of one to the invalid state transitioning
# back to itself.
P[a][s, s] = 1
# Reward is 0
else:
s1, p, r = getTransitionProbabilities(state, a)
P[a][s, s1] = p
R[s, a] = r
P[a] = P[a].tocsr()
#R = R.tolil()
return(P, R)
示例11: _compute_theta
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def _compute_theta(alpha):
"""Calculates the rotation angles from the alpha vector.
Args:
alpha (array[float]): alpha parameters
Returns:
(array[float]): rotation angles theta
"""
k = np.log2(alpha.shape[0])
factor = 2 ** (-k)
theta = sparse.dok_matrix(alpha.shape, dtype=np.float64) # type: sparse.dok_matrix
for row in range(alpha.shape[0]):
# Use transpose of M:
entry = sum([_matrix_M_entry(col, row) * a for (col, _), a in alpha.items()])
entry *= factor
if abs(entry) > 1e-6:
theta[row, 0] = entry
return theta
示例12: _get_alpha_z
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def _get_alpha_z(omega, n, k):
r"""Computes the rotation angles alpha for the Z rotations.
Args:
omega (float): phase of the input
n (int): total number of qubits
k (int): index of current qubit
Returns:
scipy.sparse.dok_matrix[np.float64]: a sparse vector representing :math:`\alpha^z_k`
"""
alpha_z_k = sparse.dok_matrix((2 ** (n - k), 1), dtype=np.float64)
for (i, _), om in omega.items():
i += 1
j = int(np.ceil(i * 2 ** (-k)))
s_condition = 2 ** (k - 1) * (2 * j - 1)
s_i = 1.0 if i > s_condition else -1.0
alpha_z_k[j - 1, 0] = alpha_z_k[j - 1, 0] + s_i * om / 2 ** (k - 1)
return alpha_z_k
示例13: predict
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def predict(self, X):
predictions = dok_matrix((X.shape[0], self.y.shape[1]), dtype=np.int)
distances = self.base_classifier.predict_proba(X)
topNIndices, topNDistances = self._get_top_labels(distances)
for entry, (label_list, dist_list) in enumerate(zip(topNIndices, topNDistances)):
for rank, label in enumerate(label_list):
if not self.dependencies:
training_sample = [[rank, dist_list[rank]]]
else:
training_sample = [distances[entry, :]]
if label in self.meta_classifiers:
prediction = self.meta_classifiers[label].predict(training_sample)[0]
if prediction == 1:
predictions[entry, label] = 1
return csr_matrix(predictions)
示例14: _a
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def _a(self, neighbor_ids):
result = sp.csr_matrix((0, self.y.shape[1]))
for ns in neighbor_ids:
neighbor_labels = self.y[ns]
# By squeezing we support matrix output from scipy.sparse.sum and 1D array from np.sum
labels_sum = np.squeeze(np.array(neighbor_labels.sum(0)))
predicted_labels = sp.csr_matrix([np.floor(np.divide(labels_sum, len(ns)) + (1 - self.threshold))])
# If there are no labels, we take the most frequent label.
if predicted_labels.sum() == 0:
divide = np.divide(labels_sum, len(ns))
max_label = divide.argmax()
predicted_labels = sp.dok_matrix((1, predicted_labels.shape[1]))
predicted_labels[0, max_label] = 1
predicted_labels = sp.csr_matrix(predicted_labels)
result = sp.vstack((result, predicted_labels))
return result
示例15: _b
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import dok_matrix [as 别名]
def _b(self, neighbor_ids):
result = sp.csr_matrix((0, self.y.shape[1]))
for ns in neighbor_ids:
average_label_nums = int(np.floor(np.mean([self.y[n].sum() for n in ns])))
neighbor_labels = self.y[ns]
labels_sum = np.array(neighbor_labels.sum(0))
# By squeezing we support matrix output from scipy.sparse.sum and 1D array from np.sum
divide = np.squeeze(np.divide(labels_sum, len(ns)))
predicted_indices = np.argsort(divide)[-average_label_nums:]
predicted_labels = sp.dok_matrix((1, len(divide)))
# noinspection PyTypeChecker
for index in predicted_indices:
predicted_labels[0, index] = 1
predicted_labels = sp.csr_matrix(predicted_labels)
result = sp.vstack((result, predicted_labels))
return result