如何用平方矩阵连接字符向量

时间:2018-08-09 22:20:22

标签: 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.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)

0 个答案:

没有答案