# Python numpy.Infinity() 使用实例

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
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
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
:type df: pandas.DataFrame
:type groupby: Array
:return: k-anonymity
:rtype: int
"""
df = _remove_unknown(df, groupby, unknown)
size_group = df.groupby(groupby).size()
if len(size_group) == 0:
return np.Infinity
return min(size_group) ```

Example 6

```def sample_cut(lX, uX, birth_time):
rate = np.sum(uX - lX)
if rate > 0:
E = np.random.exponential(scale=1.0/rate)
cut_time = birth_time + E
dim = sample_discrete(uX - lX)
loc = lX[dim] + (uX[dim] - lX[dim]) * np.random.rand()
return cut_time, dim, loc
else:
return np.Infinity, None, None

# FOURIER FEATURES ```

Example 7

```def bellman_ford(self, source):
'''
Returns Labels-algorithm's shortest paths from source to all other
nodes, if the (directed) graph doesn't contains cycles
'''
if self.oriented is False:
print 'cannot apply bellman_ford, graph is not oriented'
return
dist = np.array([np.Infinity for x in range(self.n)], dtype=np.float32)
pred = np.empty((self.n), dtype=np.int)
pred[source] = source
dist[source] = 0

for i in np.arange(1, self.n):
for e in range(len(self.edges)):
if dist[self.edges[e][0]] + self.costs[e] < dist[self.edges[e][1]]:
dist[self.edges[e][1]] = dist[
self.edges[e][0]] + self.costs[e]
pred[self.edges[e][1]] = self.edges[e][0]

for e in range(len(self.edges)):
if dist[self.edges[e][1]] > dist[self.edges[e][0]] + self.costs[e]:
print 'Error, Graph contains a negative-weight cycle'
break

edges = np.array([], dtype=object)
for v in range(len(pred)):
edges = np.append(edges, None)
edges[-1] = [pred[v], v]

return edges  # , prev, dist ```

Example 8

```def floyd_warshall(self, source):
'''
Returns floyd_warshall's shortest paths from source to all other
nodes, if the (directed) graph doesn't contains negative cycles
'''
print '''warning! apply this algorithm only if constricted, it takes\\
O(n^3)!'''
print 'O(n^3) = O(', self.n**3, ')'
dist = np.empty((self.n, self.n), dtype=np.float32)
pred = np.zeros((self.n, self.n), dtype=np.int)
dist.fill(np.Infinity)
for v in range(self.n):
dist[v][v] = .0
for e in range(len(self.edges)):
u = self.edges[e][0]
v = self.edges[e][1]
dist[u][v] = self.costs[e]
pred[u][v] = v
for h in range(1, self.n):
for i in range(1, self.n):
for j in range(self.n):
if dist[i][h] + dist[h][j] < dist[i][j]:
dist[i][j] = dist[i][h] + dist[h][j]
pred[i][j] = pred[h][j]
for i in range(1, self.n):
if dist[i][i] < 0:
print 'Error! found negative cycle, thus the problem is inferiorly unlinmited'
return
edges = np.array([], dtype=object)
for v in range(len(pred)):
edges = np.append(edges, None)
edges[-1] = [pred[source][v], v]

return edges  # , prev, dist ```

Example 9

```def bellman_ford(self, source):
'''
Returns Labels-algorithm's shortest paths from source to all other
nodes, if the (directed) graph doesn't contains cycles
'''
if self.oriented is False:
print 'cannot apply bellman_ford, graph is not oriented'
return
dist = np.array([np.Infinity for x in range(self.n)], dtype=np.float32)
pred = np.empty((self.n), dtype=np.int)
pred[source] = source
dist[source] = 0

for i in np.arange(1, self.n):
for e in range(len(self.edges)):
if dist[self.edges[e][0]] + self.costs[e] < dist[self.edges[e][1]]:
dist[self.edges[e][1]] = dist[
self.edges[e][0]] + self.costs[e]
pred[self.edges[e][1]] = self.edges[e][0]

for e in range(len(self.edges)):
if dist[self.edges[e][1]] > dist[self.edges[e][0]] + self.costs[e]:
print 'Error, Graph contains a negative-weight cycle'
break

edges = np.array([], dtype=object)
for v in range(len(pred)):
edges = np.append(edges, None)
edges[-1] = [pred[v], v]

return edges  # , prev, dist ```

Example 10

```def floyd_warshall(self, source):
'''
Returns floyd_warshall's shortest paths from source to all other
nodes, if the (directed) graph doesn't contains negative cycles
'''
print '''warning! apply this algorithm only if constricted, it takes\\
O(n^3)!'''
print 'O(n^3) = O(', self.n**3, ')'
dist = np.empty((self.n, self.n), dtype=np.float32)
pred = np.zeros((self.n, self.n), dtype=np.int)
dist.fill(np.Infinity)
for v in range(self.n):
dist[v][v] = .0
for e in range(len(self.edges)):
u = self.edges[e][0]
v = self.edges[e][1]
dist[u][v] = self.costs[e]
pred[u][v] = v
for h in range(1, self.n):
for i in range(1, self.n):
for j in range(self.n):
if dist[i][h] + dist[h][j] < dist[i][j]:
dist[i][j] = dist[i][h] + dist[h][j]
pred[i][j] = pred[h][j]
for i in range(1, self.n):
if dist[i][i] < 0:
print 'Error! found negative cycle, thus the problem is inferiorly unlinmited'
return
edges = np.array([], dtype=object)
for v in range(len(pred)):
edges = np.append(edges, None)
edges[-1] = [pred[source][v], v]

return edges  # , prev, dist ```

Example 11

```def predict(self, user, loc):
"""Predict the probability about user will checkin in loc.
prob = Pr[l|Li] = IIPr[l, li] (li in Li)
see: Exploiting Geographical Influence for Collaborative
Point-of-Interest Recommendation
"""
if user in self._cache:
if loc not in self._cache[user]:
return 0.0
return self._cache[user][loc]

self._cache[user] = {}
max_y = -np.Infinity
for l in xrange(self.num_items):
if l in self.checkins[user]:
continue
y = 0.0
for li in self.checkins[user]:
d = distance(self.locations[l], self.locations[li]) / 1000.0
y += np.log(self.prob(d))
if y > max_y:
max_y = y
self._cache[user][l] = y
for l, y in self._cache[user].items():
self._cache[user][l] = math.e ** (y - max_y)

if loc not in self._cache[user]:
return 0.0
return self._cache[user][loc] ```

Example 12

```def __init__(self, x=None, fitness=np.Infinity):
"""
Create object instance.

:param x: Position in the search-space aka. candidate solution.
:param fitness: Associated fitness of the position in the search-space.
:return: Object instance.
"""

# Copy arguments to instance variables.
self.x = x
self.fitness = fitness

######################################################################## ```

Example 13

```def fitness(self, x, limit=np.Infinity):
return np.sum(x ** 2) ```

Example 14

```def fitness(self, x, limit=np.Infinity):
return np.sum(100.0 * (x[1:] - x[:-1] ** 2) ** 2 + (1.0 - x[:-1]) ** 2) ```

Example 15

```def fitness(self, x, limit=np.Infinity):
return np.sum(x ** 2 + 10.0 - 10.0 * np.cos(2 * np.pi * x)) ```

Example 16

```def fitness(self, x, limit=np.Infinity):
return np.sum([np.sum(x[:i])**2 for i in range(self.dim)]) ```

Example 17

```def fitness(self, x, limit=np.Infinity):
return np.max(np.abs(x)) ```

Example 18

```def fitness(self, x, limit=np.Infinity):
absx = np.abs(x)
fitness = np.sum(absx) + np.prod(absx)

# For large dim, np.prod(absx) often overflows to infinity.
# This means the optimizers cannot work properly.
# So we limit the fitness to 1e20.
if fitness == np.Infinity or fitness > 1e20:
fitness = 1e20

return fitness ```

Example 19

```def fitness(self, x, limit=np.Infinity):
return np.sum(np.floor(x + 0.5) ** 2) ```

Example 20

```def fitness(self, x, limit=np.Infinity):
value = np.e + 20.0 - 20.0 * np.exp(-0.2 * np.sqrt(np.sum(x ** 2) / self.dim)) \
- np.exp(np.sum(np.cos(2 * np.pi * x)) / self.dim)

# Due to rounding errors, the value is sometimes slightly negative.
# This gives a warning in the MetaFitness-class.
if value < 0.0:
value = 0.0

return value ```

Example 21

```def fitness(self, x, limit=np.Infinity):
# Split the calculation into smaller formulas.
y = 1.0 + (x + 1.0) / 4.0
a = 10.0 * np.sin(np.pi * y[0]) ** 2
b = np.sum(((y[:-1] - 1.0) ** 2) * (1.0 + 10.0 * (np.sin(np.pi * y[1:]) ** 2)))
c = (y[-1] - 1.0) ** 2

# Penalty term.
penalty = _penalty(x, 10.0, 100.0, 4.0)

return np.pi * (a + b + c) / self.dim + penalty ```

Example 22

```def fitness(self, x, limit=np.Infinity):
# Split the calculation into smaller formulas.
a = np.sin(3 * np.pi * x[0]) ** 2
b = np.sum(((x[:-1] - 1.0) ** 2) * (1.0 + np.sin(3 * np.pi * x[1:]) ** 2))
c = (x[-1] - 1.0) ** 2 * (1.0 + np.sin(2 * np.pi * x[-1])**2)

# Penalty term.
penalty = _penalty(x, 5.0, 100.0, 4.0)

return 0.1 * (a + b + c) / self.dim + penalty

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

# List of all of the above classes for benchmark problems. ```

Example 23

```def sphere_func(x, limit=np.Infinity):
return np.sum(x*x)

######################################################################## ```

Example 24

```def mu_fullsum_func(
mu,
node_vec,
eventmemes,
etimes,
T,
gamma,
alpha,
omega,
W,
beta,
kernel_evaluate,
K_evaluate,
):
'''
'''

#     return -evaluate_nonapproximated_loglikelihood_wordsbyusers(node_vec, eventmemes, etimes,
#                                                                 T, mu, gamma, alpha, omega, W, beta,
#                                                                 kernel_evaluate, K_evaluate)

ll = 0

##     alpha_summedrows=np.sum(alpha, axis=1)

for (eventidx, (infected_u, eventmeme, etime1)) in \
enumerate(izip(node_vec, eventmemes, etimes)):

#        ll+=gamma[eventmeme]*mu[infected_u]

ll += event_nonapproximated_logintensity(  #                                                etimes, node_vec, eventmemes,
infected_u,
eventmeme,
etime1,
T,
etimes[:eventidx],
node_vec[:eventidx],
eventmemes[:eventidx],
mu,
gamma,
omega,
alpha,
kernel_evaluate,
)

##         ll-=alpha_summedrows[int(infected_u)]*K_evaluate(etime1, T, omega)
##         if -ll==np.Infinity:
##             event_nonapproximated_logintensity(infected_u, eventmeme, etime1, T, etimes[:eventidx], node_vec[:eventidx],
##                                                eventmemes[:eventidx], mu, gamma, omega, alpha, kernel_evaluate)

ll -= T * np.sum(np.outer(mu, gamma))
return -ll

# ===== ```

Example 25

```def event_approximated_logintensity(
infected_u,
eventmeme,
etime1,
T,
etimes,
node_vec,
eventmemes,
mu,
gamma,
omega,
alpha,
kernel_evaluate,
print_summed_elements=False,
):

components = []
ll = 0
if mu[int(infected_u)] * gamma[int(eventmeme)] > 0:
ll += np.log(mu[int(infected_u)] * gamma[int(eventmeme)])
components.append(np.log(mu[int(infected_u)]
* gamma[int(eventmeme)]))

for (eventidx, (etime2, node2, eventmeme2)) in \
enumerate(izip(etimes, node_vec, eventmemes)):
if etime2 < etime1:
if eventmeme2 == eventmeme:
intensity_val = alpha[int(node2), int(infected_u)] \
* kernel_evaluate(etime1, etime2, omega)
if intensity_val > 0:
ll += np.log(intensity_val)
components.append(np.log(intensity_val))

# If there are other components then 0, we can ignore zeros; however,
# if there are no components whatsoever, then we get minus infinity

ll = -np.Infinity
if print_summed_elements:
+ '+ '.join(map(lambda x: '%10.6f' % x, components))
return ll ```

Example 26

```def evaluate_loglikelihood_nowords(
node_vec,
eventmemes,
etimes,
T,
mu,
gamma,
alpha,
omega,
kernel_evaluate,
K_evaluate,
):

ll = 0
alpha_summedrows = np.sum(alpha, axis=1)
for (eventidx, (infected_u, eventmeme, etime1)) in \
enumerate(izip(node_vec, eventmemes, etimes)):
infected_u,
eventmeme,
etime1,
T,
etimes[:eventidx],
node_vec[:eventidx],
eventmemes[:eventidx],
mu,
gamma,
omega,
alpha,
kernel_evaluate,
)
ll -= alpha_summedrows[int(infected_u)] * K_evaluate(etime1, T,
omega)
if -ll == np.Infinity:
infected_u,
eventmeme,
etime1,
T,
etimes[:eventidx],
node_vec[:eventidx],
eventmemes[:eventidx],
mu,
gamma,
omega,
alpha,
kernel_evaluate,
)

ll -= T * np.sum(np.outer(mu, gamma))
return ll

# TODO: need to write a test for this! ```

Example 27

```def alpha_fullsum_func(
alpha,
node_vec,
eventmemes,
etimes,
T,
mu,
gamma,
omega,
W,
beta,
kernel_evaluate,
K_evaluate,
):
'''
'''

#     res=0
#    for alphaij_sub in alphaij:
#    alpha[indexes[0]][indexes[1]]=alphaij_sub
#     res=evaluate_nonapproximated_loglikelihood_wordsbyusers(node_vec, eventmemes, etimes,
#                                                             T, mu, gamma, alpha, omega, W, beta,
#                                                             kernel_evaluate, K_evaluate)

ll = 0

alpha_summedrows = np.sum(alpha, axis=1)
for (eventidx, (infected_u, eventmeme, etime1)) in \
enumerate(izip(node_vec, eventmemes, etimes)):
infected_u,
eventmeme,
etime1,
T,
etimes[:eventidx],
node_vec[:eventidx],
eventmemes[:eventidx],
mu,
gamma,
omega,
alpha,
kernel_evaluate,
)
ll -= alpha_summedrows[int(infected_u)] * K_evaluate(etime1, T,
omega)

#         if -ll==np.Infinity: