我有一个名为 network 的列表,需要使用函数进行修改。该列表包含 100 个元素,其中每个元素随机 +1 或 -1。必须以某种方式修改此列表,以便每次函数运行时,它都会修改网络副本中的一个元素。然后我需要在图表上绘制这些列表。
我的代码如下:
import math
import random
import numpy as np
#Generate network elements
network=[]
for x in range(0,100):
network.append(random.choice([-1,1]))
#Generate Matrix
matrix=np.ones((100,100))
p=np.random.permutation(100)
matrix[p[:10]]=1
matrix[p[:10]]=-1
#Set diagonal values to zero
matrix[np.diag_indices_from(matrix)]=0
#THIS IS THE FUNCTION THAT WILL BE MODIFYING AN ELEMENT, i, IN NETWORK.
def new_sum(i):
hi=np.sum(np.dot(matrix[i][:100],network[:100]))
if hi>0:
hi==1
if hi<0:
hi==-1
return hi
"""HERE IS WHERE I NEED HELP. I NEED TO HAVE THIS FUNCTION RUN AT RANDOM
ON ANY ELEMENT IN NETWORK. WHEN IT RUNS, I NEED TO COPY NETWORK AS A NEW
LIST AND HAVE hi REPLACE THE CORRESPONDING INDEX, i, IN THE COPIED LIST. THE
PROBLEM IS THAT I NEED TO HAVE THIS FUNCTION RUN 300 TIMES, AND EVERYTIME
IT RUNS, I NEED TO COPY NETWORK AS A NEW LIST AND HAVE hi REPLACE THE
CORRESPONDING INDEX, i, IN THE COPIED LIST. HERE IS CODE: """
def new_function(value):
for i in range(0,value):
new_list=network[:]
new_sum(i)= x
new_list[i]=x
return new_list
def plot_data(ax, data, xval):
N:len(data)
uniqdata, idx=np.unique(data, return_inverse=True)
ax.scatter(np.ones(N)*xval,np.arange(1,N+1),s=50,c=cmap[idx])
cmap = np.array([(1,0,0),(0,1,0)])
fig, ax=plt.subplots
然后,我必须绘制需要创建的 300 个列表,其中每个列表都是data
函数中的参数 , plot_data
。我该怎么做呢?