Answer the question
In order to leave comments, you need to log in
How can I make the function iterate over only integer x values?
There is a function that is calculated by the formula.
I then pass the result of the function to the differential_evolution function.
But the search includes values like 3.23343343.
How can I make sure that only integer values get into the differential_evolution function?
from scipy.optimize import differential_evolution
import numpy as np
def function2(x):
res = x[0] * x[1]
if res < 100:
with open("генетика.csv", "a") as f:
f.write(str(x))
f.write('\t')
f.write(str(res))
f.write('\n')
return res
bounds = [(-5, 5),(-10, 10)]
result = differential_evolution(function2, bounds)
result.x, result.fun
print(result.x, result.fun)
def is_int(x):
temp = str(x) # конвертируем в str для проверок
i = 0 # счетчик
while i < len(temp):
if temp[i] == '.': # проверяем является ли целым / узнаем индекс нуля
while i + 1 < len(temp): # пробегаемся по индексам после "."
if temp[i + 1] != '0': # если после "." не ноль - не Int
return False
i += 1
else:
return True
i += 1
else:
return True # если "." нет - следовательно Int
Answer the question
In order to leave comments, you need to log in
For cases like x = 1.0:
if int(x) == x:
...
if type(x) is int:
...
data = [1.0, 1.23, 2.0, 2.71, 3.0, 3.14]
for x in filter(float.is_integer, data):
print(x)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question