Python 如何将字符向量与平方矩阵连接起来
我有一个距离矩阵,其形式为:Python 如何将字符向量与平方矩阵连接起来,python,vector,concatenation,Python,Vector,Concatenation,我有一个距离矩阵,其形式为: 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 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)
到目前为止,您尝试了什么代码?您的数据是如何准确地存储在脚本中的?在脚本内部还是文件中?这些是numpy向量和矩阵还是vanilla python?如果您只需要进行处理,并且以后不会更新,那么您可以
zip(向量,矩阵)
获得您想要的结果。已经解决了!