我目前正在使用numpy矩阵进行数学算法。每个矩阵的元素是一个离散数,除以一个巨大的整数(100万幅)。我使用float64作为数据类型,但我很难理解为什么我在某些元素的微积分中得到NaN。在哪些情况下可以在python中引发NaN?
P.S。:上面提到的每个分数的所有分子都大于0。
感谢您的帮助
谢谢!
在我加载和处理每个矩阵元素的要点之下:
将值加载到每个矩阵的元素X [i]:
def run(self):
for index in xrange(self.range_nodes[0], self.range_nodes[1]):
print "Getting k predecessors of " + str(index)
list_dict_adjacencies = self.graph.predecessors(self.nodes_list[index])
print "Got k predecessors of " + str(index)
value = 0.0
for node in list_dict_adjacencies:
node_index = self.nodes_list.index(node)
value += numpy.float64((1.0/len(list_dict_adjacencies)) * self.X_last[node_index, 0])
self.X[index] = value
初始化所有矩阵(X_last,X,X_last_top和X_top):
def gauss_jacobi_method(graph, min_quadratic_error):
nodes_list = graph.nodes()
nodes_dict_degree = dict()
for node in nodes_list:
nodes_dict_degree[node] = len(graph.predecessors(node))
top_nodes = map(lambda y: y[0], sorted(nodes_dict_degree.items(), key=lambda x: x[1], reverse=True)[:1000])
X_vector = numpy.empty((len(graph.nodes()), 1))
sum_degree = 0
for node in nodes_list:
sum_degree += len(graph.predecessors(node))
for index in xrange(len(nodes_list)):
X_vector[index, 0] = numpy.float64(len(graph.predecessors(nodes_list[index])))/sum_degree
X_last = numpy.asmatrix(X_vector)
X_last_top = numpy.asmatrix(numpy.empty((1000, 1)))
for index in xrange(len(top_nodes)):
X_last_top[index] = X_last[nodes_list.index(top_nodes[index]), 0]
X = gauss_jacobi_iterate_threaded(graph, nodes_list, X_last)
X_top = numpy.asmatrix(numpy.empty((1000, 1)))
for index in xrange(len(top_nodes)):
X_top[index] = X[nodes_list.index(top_nodes[index]), 0]
logging.debug(time.strftime("%H:%M:%S") + ' ZERO ITERATION')
logging.debug(time.strftime("%H:%M:%S") + ' ERROR: %s' % str(numpy.linalg.norm(X - X_last)))
logging.debug(time.strftime("%H:%M:%S") + 'VECTOR: \n' + '\n'.join([str(X.item(index, 0)) + ' ' + nodes_list[index] for index in xrange(X.shape[0])]))