a = sess.run(y) a = round(a[0][0], 1) # 对浮点数进行四舍五入 a = '{:.2f}'.format(a) # 将结果输出为字符串格式Traceback (most recent call last): File "C:/Users/DELL/Desktop/python_flask-dc/python_flask/app.py", line 43, in <module> from python_flask.bpnn_tf import predicttem File "C:\Users\DELL\Desktop\python_flask-dc\python_flask\bpnn_tf.py", line 131, in <module> a = round(a[0][0], 1) # 对浮点数进行四舍五入 TypeError: type numpy.ndarray doesn't define round method
时间: 2024-03-01 22:54:05 浏览: 17
这个错误提示是因为在使用内置的 `round()` 函数时,传入了一个 numpy 的数组,而 numpy 数组不支持 Python 内置函数的操作。你可以使用 numpy 中的 `around()` 函数对数组进行四舍五入操作。修改代码如下:
```
a = sess.run(y)
a = np.around(a, 1) # 对浮点数进行四舍五入
a = a[0][0]
```
这样就可以避免这个错误了。
相关问题
a = sess.run(y) a = np.round(a[0][0], 1) # 对浮点数进行四舍五入 a = '{:.2f}'.format(a) # 将结果输出为字符串格式 File "C:\Users\DELL\Desktop\python_flask-dc\python_flask\bpnn_tf.py", line 131, in <module> a = '{:.2f}'.format(a) # 将结果输出为字符串格式 TypeError: unsupported format string passed to numpy.ndarray.__format__
这个错误提示是因为在使用格式化字符串时传入了不支持的格式化字符,而这个格式化字符是传给了一个 numpy 的数组。你可以尝试将这个数组转换为 Python 的列表或元组,然后再进行格式化输出。在 Python 中,可以使用 `tolist()` 方法将 numpy 数组转换为 Python 列表。修改代码如下:
```
a = sess.run(y)
a = np.round(a[0][0], 1)
a = '{:.2f}'.format(float(a)) # 将结果输出为字符串格式
```
这样就可以避免这个错误了。
action = self.sess.run(self.actor_net.output, state)[0] TypeError: input must be a dictionary
这个错误通常是由于代码中使用了一个字典作为输入,但是这个字典的类型不正确导致的。具体来说,错误提示 "input must be a dictionary" 表示传入的输入必须是一个字典类型的数据,否则会出现这个错误。
在这段代码中,state 可能被误认为是一个字典类型的数据,而不是一个 numpy 数组。这可能是因为在传递 state 参数时,没有将其包装成一个字典,而是直接将其作为一个参数进行传递。解决这个问题的方法是将 state 包装成一个字典,然后将其传递给 sess.run() 函数。具体来说,可以使用以下代码来解决这个问题:
```
action = self.sess.run(self.actor_net.output, feed_dict={self.state_ph: state})[0]
```
这里将 state 包装成了一个字典,并将其作为 feed_dict 参数传递给 sess.run() 函数。这样就可以避免 "input must be a dictionary" 的 TypeError 错误。