TypeError:不支持的操作数类型'_io.TextIOWrapper'和'浮动'

2024-06-28 00:15:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到这个错误


类型错误

Traceback (most recent call last)
<ipython-input-7-995aa3052f74> in <module>()
      A,B,C,D = 200, 48.475, 0.0026, 400
--->  y_true = sigmoide(x, A, B, C, D)
<ipython-input-7-995aa3052f74> in sigmoide(x, A, B, C, D)
          """4PL sigmoide equation."""
--->      return A*erf((x-B)/C*sqrt(2)) + D
TypeError: unsupported operand type(s) for -: '_io.TextIOWrapper' and 'float'

当我尝试运行此代码时:

^{pr2}$

请帮忙解决这个错误吗?谢谢!在


Tags: intrue类型mostinputreturn错误ipython
2条回答

x定义为打开的文件:

x = open("C:\\Users\Anaconda3\Lib\site-packages\data\data1.txt", "r")

但不是从文件中读取数据,而是调用sigmoide函数并将打开的文件作为第一个参数传递!在

^{pr2}$

在函数内部,继续尝试从打开的文件中计算减法!在

return A*erf((x-B)/C*sqrt(2)) + D

这当然行不通——Python不知道如何从打开的文件中减去一个数字!然后得到unsupported operand type(s)错误。在

非常感谢大家!在

@Const:你刚才提到的错误是因为我打开x和y的meas文件时,它们是脚本而不是float。以下是我解决错误的代码:

x = open("C:\\Users\Anaconda3\Lib\site-packages\data\data1.txt", "r")

f1=np.array(x.read().split("\n")).astype(float)

print(f1)


y_meas = open("C:\\Users\Anaconda3\Lib\site-packages\data\data2.txt", "r")

f2=np.array(y_meas.read().split("\n")).astype(float)

print(f2)

相关问题 更多 >