У меня есть матрица расстояний в виде:
0 0.81556 0.87214 0.6861
0.81556 0 0.17909 0.33358
0.87214 0.17909 0 0.47373
0.6861 0.33358 0.47373 0
и вектор строки:
A
B
C
D
и я хочу получить следующую конкатенацию:
A 0 0.81556 0.87214 0.6861
B 0.81556 0 0.17909 0.33358
C 0.87214 0.17909 0 0.47373
C 0.6861 0.33358 0.47373 0
Проблема здесь в том, что как только вектор строк объединяется, вся матрица преобразуется в строку, и этого не должно быть.
вот функция, которую я использую до сих пор:
"" Generates the random matrix based on the dimensions of the input matrix being compared"""
def generateMatrix(N): # N -> (x,y,z) or (x,y)
labels =[]
# creates random matrix with NxN dimmensions
rn_matrix = np.random.uniform(0.0,5.0,N)
# computes Distance matrix
adist = squareform(pdist(rn_matrix))
# Generates the labels vector for the random matrix
# it should generate the letters and insert them in the first column
for i in range(N[0]):
letter = random.choice("MLN")
labels.append(letter)
# random distance matrix with labels in it
# labels_df = pd.DataFrame({'arg1':np.array(labels)})
# adist_df = pd.DataFrame(data=adist,index=list(range(15)),columns= list(range(15)))
distance_matrix = np.c_[labels, adist]
# df = pd.concat(labels_df,adist_df)
print(distance_matrix)
# filename assigned
outfilename = "rn_distance_matrix.csv"
# stores the distance matrix in a csv format
# outfile = np.savetxt( outfilename, distance_matrix, delimiter = ",")
distance_matrix.to_csv(outfilename,index = False)
# returns the name of the file where the distance matrix is stored
return(outfilename)
Это множественные векторы и матрицы или ванильный питон?
Если вам требуется только обработка и они не будут обновляться позже, вы можете использовать zip(vector, matrix), чтобы получить желаемый результат.
уже решил!






Какой код вы пробовали до сих пор? Как именно ваши данные хранятся в вашем скрипте - внутри скрипта или в файле?