oliver
oliver

Reputation: 147

How to fix 'float' object is not iterable?

I write a function phix that is input a x and N, then get a summation x_new.

def phix(x, N):
  lam=np.floor(N**(1/3))
  x_new=0
  for i in range(0, int(lam)):
   if (i+1)*N**(-1/3)>x>i*N**(-1/3):
    x_new=x_new+(2*i+1)/(2*N**(1/3))
  return x_new

Here is a error:

N=1
x = np.random.random_sample(N)
x_new=phix(x, N)
x_val = np.array(list(Counter(x_new).keys()))

TypeError: 'float' object is not iterable

In fact, I want to input N=2 or N=3 and phix(x, N) return a vector of x_new not a value. I am not sure how to do that? I try the following code but it seems does not work.

def phix(x,N):
  lam=np.floor(N**(1/3))
  x_new=np.zeros(N)
  for i in range(0, N):
    x_newa=0
    for j in range(0, int(lam)):
      if (j+1)*N**(-1/3)>x[i]>j*N**(-1/3):
        x_newa=x_newa+(2*j+1)/(2*N**(1/3))
    x_new[i]=x_newa
  return(x_new)

I do the following example.

N=3
x = np.random.random_sample(N)
x_new=phix(x,N)
print(x_new)

But it shows all same values

[0.34668064 0.34668064 0.34668064]

Upvotes: 0

Views: 266

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54897

OK, I don't know why you have two loops in there. There only needs to be one loop, running from 0 to cube-root(N). The function should accept one value (x) and return one value. The value being added does not depend on x at all - x is only used to determine whether an element of the summation is included or not.

So, I believe this produces the result you want. For each of your random values of x, there is one result. And as I said, when N is 3, the inner loop only runs once, so the result can ONLY be 0 or 0.34668. When N=1000 there is a bit more variation, but there are still only 10! possible results.

import numpy as np

def phix(x,N):
    ncr = N**(1/3)
    lam = int(ncr)
    sumx = 0
    for i in range(lam):
        if i/ncr < x < (i+1)/ncr:
            sumx += (i+i+1) / (2*ncr)
    return sumx

N = 1000
x = np.random.random_sample(N)
for x1 in x:
    print( x1, phix(x1,N) )

Output (truncated):

0.16252465361984203 0.15000000000000002
0.6527047022599177 0.6500000000000001
0.7733129495624551 0.7500000000000001
0.03800607206261242 0.05000000000000001
0.7116353720754358 0.7500000000000001
0.01845039536391846 0.05000000000000001
0.3398936159178093 0.3500000000000001
0.44312359112375477 0.45000000000000007
0.3010799287710728 0.3500000000000001
0.37401793425303764 0.3500000000000001
0.7049621859196674 0.7500000000000001
0.5044002562214386 0.55
0.30073336035132303 0.3500000000000001
0.31630770524340746 0.3500000000000001
0.8465422342801152 0.8500000000000002
0.39679187879066746 0.3500000000000001
0.10910213537513935 0.15000000000000002
0.8932112016365839 0.8500000000000002
0.9858585971124458 0
0.49024772936880123 0.45000000000000007
(993 more)
import numpy as np

def phix(x,N):
    ncr = N**(1/3)
    lam = int(ncr)
    sumx = 0
    for i in range(lam):
        if i/ncr < x < (i+1)/ncr:
            sumx += (i+i+1) / (2*ncr)
    return sumx

N = 1000
x = np.random.random_sample(N)
for x1 in x:
    print( x1, phix(x1,N) )

Upvotes: 1

Related Questions