【问题标题】:How can I solve the error "TypeError: can only concatenate str (not "numpy.float64") to str" trying to output the correlation如何解决错误 \"TypeError: can only concatenate str (not \"numpy.float64\") to str\" 试图输出相关性
【发布时间】:2022-09-27 17:05:29
【问题描述】:

我从 Python 开始,我试图在 QPlainTextEdit 中输出 Pearson、Spearman 和 Kendall 的相关性,使用函数 \"print_correlation_results\"。但是当我尝试这样做时,我得到了这个错误,我该如何解决?

相关性

def correlation_files(self):
    ...       
    
    if widgets.optLoadFiles.isChecked():
        ...
        if not result_file.error:
            if len(parameters_list)==2:
                ...
                
                widgets.txtParametersResult.setPlainText(widgets.txtParametersResult.toPlainText()+\"\\n\"+self.print_correlation_results())        #Here I call the function
                
            ...
        
    
def print_correlation_results(self):
    
    parameters = widgets.cmbParametersFile.currentText()
    parameters_list = parameters.split(\", \")
    FileName = widgets.txtDataFile.text()
    result_file = ResultFile(FileName)
    if not result_file.error:
        if len(parameters_list)==2:
            measurements = result_file.get_params(parameters_list)
            data1 = measurements[parameters_list[0]][\"measure\"]
            data2 = measurements[parameters_list[1]][\"measure\"]
    
    # calculate correlation
    corr, pvalue = pearsonr(data1, data2) # Pearson\'s r, valor p
    corr2, pvalue2 = spearmanr(data1, data2) # Spearman\'s rho, valor p
    corr3, pvalue3 = kendalltau(data1, data2) # Kendall\'s tau, valor p
    
    print_correlation_results =\" - Pearsons correlation:\\t \" + corr + pvalue + \"\\n\"
    print_correlation_results +=\" - Spearmanr correlation:\\t\" + corr2 + pvalue2 + \"\\n\"
    print_correlation_results +=\" - kendalltau correlation:\\t\" + corr3 + pvalue3 + \"\\n\"

标签: python correlation pyqt6


【解决方案1】:

正是错误所说的。首先,您需要告诉我们它在哪一行发现了错误,因为我现在要进行猜测。我认为这条线是这样的:

 print_correlation_results =" - Pearsons correlation:\t " + corr + pvalue + "\n"

就好像你会说:

"asdf" + 5

你不能那样做。在这种情况下,它是一个 numpy 浮点数,而不是整数。但这是同一回事。 corr、pvalue 或两者都不是字符串。因此,在将它们与“ - Pearsons correlation:\t ”连接之前,您需要将它们变成一个字符串

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 2023-02-25
    • 2021-08-18
    • 2022-11-01
    • 2021-08-31
    • 2021-12-18
    • 2020-08-29
    • 1970-01-01
    • 2019-02-02
    相关资源
    最近更新 更多