简体   繁体   中英

Numpy arrays incorrectly have identical values?

I have a small program where I am playing with creating an evolutionary algorithm related to disease spread. I have run into an issue that has driven me slightly mad trying to figure out the problem.

I have two numpy arrays, "infections", which is an array where each element is binary representation of whether that individual has been exposed and "current_infections", that is only ongoing infections and is supposed to be incremented by days.

For Example:

infections = [0,0,0,1,1]
current_infections = [0,0,0,15,0]

This would represent five individuals, individual three has had the disease for 15 days and individual four has had it for long enough that they have recovered and no longer currently have it.


infections = []
current_infections = []
responsible_infections = []
spread_rate = 0.1
contagious_period = 20
initial_node_infections = 2
current_network = #this is an array of adjacency matrices for nodes in a network


#initial infections.
def initialize_infections():
  global infections, current_infections, responsible_infections
  responsible_infections = np.zeros(current_network.shape[0])
  infections = np.zeros(current_network.shape[0])
  for each in rd.sample(range(len(current_network)), k=initial_node_infections):
    infections[each] = 1
  current_infections = infections[:]

# runs a day in simulation. 
# returns 1 if there are still ongoing infections at the end of day, 0 if not
def progress_day():
  global current_infections
  print(np.sum(current_infections), np.sum(infections))  #should not be equivalent, yet they are
  for i in range(len(current_infections)):
    if current_infections[i] >= 1 and current_infections[i]<contagious_period:
      current_infections[i]+=1
    elif current_infections[i]>=contagious_period:
      #patient recovered
      current_infections[i] = 0
  for i in range(contacts_per_day):
    for j in range(len(current_infections)):
      if current_infections[j] >= 1:
        spread_infection(current_network[j], j)

  if not np.sum(current_infections):
    return 0
  else:
    return 1




#given infected node it calculates spread of disease to adjacent nodes.
def spread_infection(person, rp):
  global infections, current_infections, responsible_infections
  for x in range(len(person)):
    if person[x] == 1 and infections[x] == 0 and rd.random()<=spread_rate:
      current_infections[x] = 1
      infections[x] = 1
      responsible_infections[rp]+=1 #infections a given person is responsible for.


def main():
  global current_infections, infections
  initialize_infections()
  day = 0
  while day<100:
    if not progress_day():
      break
    day+=1

main()

For some reason changes made to an element in current_infections are also being made to that element in infections so they are both incrementing. Am i doing something incorrectly with numpy such that they are somehow the same array?

current_infections = infections[:] makes current_infections a view over the elements in infections. Use current_infections = infections.copy() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM