python unpack infinity_Python numpy.Infinity() 使用实例

这是一个关于如何在Python中使用numpy库的Infinity特性的代码示例集合,包括在Prim算法、最小生成树计算、优化问题求解、数据处理等场景下的应用。
摘要由CSDN通过智能技术生成

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def prim(self):

'''

Returns Prim's minimum spanninng tree

'''

big_f = set([])

costs = np.empty((self.n), dtype=object)

costs[:] = np.max(self.costs) + 1

big_e = np.empty((self.n), dtype=object)

big_q = set(range(self.n))

tree_edges = np.array([], dtype=object)

while len(big_q) > 0:

v = np.argmin(costs)

big_q.remove(v)

costs[v] = np.Infinity

big_f.add(v)

if big_e[v] is not None:

tree_edges = np.append(tree_edges, None)

tree_edges[-1] = (big_e[v], v)

for i, w in zip(range(len(self.FSs[v])), self.FSs[v]):

if w in big_q and self.FS_costs[v][i] < costs[w]:

costs[w] = self.FS_costs[v][i]

big_e[w] = v

return tree_edges

Example 2

def prim(self):

'''

Returns Prim's minimum spanninng tree

'''

big_f = set([])

costs = np.empty((self.n), dtype=object)

costs[:] = np.max(self.costs) + 1

big_e = np.empty((self.n), dtype=object)

big_q = set(range(self.n))

tree_edges = np.array([], dtype=object)

while len(big_q) > 0:

v = np.argmin(costs)

big_q.remove(v)

costs[v] = np.Infinity

big_f.add(v)

if big_e[v] is not None:

tree_edges = np.append(tree_edges, None)

tree_edges[-1] = (big_e[v], v)

for i, w in zip(range(len(self.FSs[v])), self.FSs[v]):

if w in big_q and self.FS_costs[v][i] < costs[w]:

costs[w] = self.FS_costs[v][i]

big_e[w] = v

return tree_edges

Example 3

def fitness(self, x, limit=np.Infinity):

"""

Wraps the fitness-function of the actual problem, whose results

are logged if the fitness is an improvement.

"""

# Calculate fitness of the actual problem.

new_fitness = self.problem.fitness(x=x, limit=limit)

# If log is desired.

if self.capacity > 0:

# If the new fitness is an improvement over the worst-known fitness in the log.

if new_fitness < self.solutions[-1].fitness:

# Update the worst solution in the log with the new solution.

self.solutions[-1] = _LogElement(x=x, fitness=new_fitness)

# Sort the logged solutions.

self.solutions = sorted(self.solutions, key=lambda solution: solution.fitness)

return new_fitness

########################################################################

Example 4

def fitness(self, x, limit=np.Infinity):

"""

This is the fitness function that must be implemented for an

optimization problem.

It is also sometimes called the cost- or error-function.

:param x:

Calculate the fitness value for these parameters in the search-space.

:param limit:

Calculation of the fitness can be aborted if the value is greater than this limit.

This is used for so-called Pre-Emptive Fitness Evaluation in the MetaFitness-class.

You can ignore this value.

:return:

Fitness-value of x.

"""

# Raise an exception if the child-class has not implemented this function.

raise NotImplementedError

########################################################################

Example 5

def get_k(df, groupby, unknown=None):

"""

Return the k-anonymity level of a df, grouped by the specified columns.

:param df: The dataframe to get k from

:param groupby: The columns to group by

:typ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值