So I am making a genetic algorithm in python, and what it does is it forms optim
ID: 3578416 • Letter: S
Question
So I am making a genetic algorithm in python, and what it does is it forms optimal clusters. I'll skip right to the computer science part that I'm having trouble with. Say I have a matrix d:
[[ 0 2 6 4 3 2 1 4 5]
[ 2 0 3 4 5 10 2 3 1]
[ 6 3 0 2 3 10 2 4 8]
[ 4 4 2 0 12 7 3 2 6]
[ 3 5 3 12 0 2 8 4 1]
[ 2 10 10 7 2 0 9 2 5]
[ 1 2 2 3 8 9 0 10 2]
[ 4 3 4 2 4 2 10 0 4]
[ 5 1 8 6 1 5 2 4 0]]
And I have an organism, whose 1 cluster covers nodes: [1010111], (the other cluster covers nodes [0101000], but I want to do is create a new d matrix for each of these clusters (the d matrix is the distance matrix). For example, for [1010111], it covers nodes 1,3,5,6,7, so it should delete 0,2,4,5,6 rows and columns of matrix d. I am having problems looping this because the np.delete creates a new matrix each time and it ultimately does not delete the right row/column. This is the loop I have tried:
for l in range(7):
if generation[l]==0:
d2 = np.delete(d1,(l),axis =1)
d1=d2
Thanks! Oh and, for this part of the genetic algorithm, it is the fitness testing, so I want to find the minimum distance (the sum of total distance for each cluster), if you have a better idea of how to do that, pleaselet me know!
Explanation / Answer
import pandas as pd
import numpy as np
x = np.array([ [ 0 ,2, 6, 4, 3, 2, 1, 4, 5],
[ 2, 0, 3, 4, 5, 10, 2, 3, 1],
[ 6, 3, 0, 2, 3, 10, 2, 4, 8],
[ 4, 4, 2, 0, 12, 7, 3, 2, 6],
[ 3, 5, 3, 12, 0, 2, 8, 4, 1],
[ 2, 10, 10, 7, 2, 0, 9, 2, 5],
[ 1, 2, 2, 3, 8, 9, 0, 10, 2],
[ 4, 3, 4, 2, 4, 2, 10, 0, 4],
[ 5, 1, 8, 6, 1, 5, 2, 4, 0] ])
cluster=input("Enter the cluster:")
li=[]
for i in range(len(cluster)):
if int(cluster[i])==0:
li.append(i)
print(' ')
print('New matrix')
df=pd.DataFrame(x)
#print(df)
df1=df
df1.drop(df1.index[li])
print(' ')
print(df1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.