【numpy】TypeError: only size-1 arrays can be converted to Python scalars
问题描述
编写了sigmoid函数,
import mathdef sigmoid(x):sig = 1 / (1 + math.exp(-x))return sig
其中使用math.exp()函数进行np.array矩阵x计算时出错:
TypeError: only size-1 arrays can be converted to Python scalars
分析解决
这是由于math.exp()函数不支持矩阵变量操作,可以使用numpy.exp()替代,代码更新为:
import numpy as npdef sigmoid(x):sig = 1 / (1 + np.exp(-x))return sig
需要注意的是:
numpy.exp()可用作函数使用
输入可以为数组,返还可以表示函数值,可用于图像绘制
math.exp()输入必须为固定的数或者其他数据类型(数组),
必须固定,不能是变量,不能用于画图
输出为固定数值在数值计算时使用
Thanks to: python报错:only size-1 arrays can be converted to Python scalars 解决方案_lith__romantic的博客-CSDN博客
【numpy】TypeError: only size-1 arrays can be converted to Python scalars相关推荐
- TypeError: only size-1 arrays can be converted to Python scalars
正在愉快写代码的我 ??????????? 源代码也很简单 之后可以解决,比如 再比如: 但是那样究竟为什么不对呢? plt.bar(x = x,height=data,width=0.8,align ...
- 自学Python问题记录3:only size-1 arrays can be converted to Python scalars
报错情况 only size-1 arrays can be converted to Python scalars/仅一维数据可以转换为Python标量 原因及解决方法 ax.bar(theta, ...
- [python]TypeError: only size-1 arrays can be converted to Python scalars
当在代码中同时import numpy库和math库时,在进行sin.exp等运算时需要使用 np.exp()或者 np.sin(),否则就会报这个错误. # -*- coding: utf-8 -* ...
- TypeError系列之:TypeError: only size-1 arrays can be converted to Python scalars
这样一个错误是一个类型错误,翻译过来就是, 只有维度是1的数组可以被转换成Python标量. 出现这种问题的原因会很多,非常具体,但根本原因是维度错误!! 所以各位看官要查询出错位置,看看赋值的数据( ...
- 使用math.sin时报错only size-1 arrays can be converted to Python scalar
代码: import numpy as np x = np.linspace(0,1,6) def f(x):return math.sin(x) y = f(x) 报错: only size-1 a ...
- 【Webpack】TypeError: Cannot read property ‘tap‘ of undefined at HtmlWebpackPlugin.
[Webpack]TypeError: Cannot read property 'tap' of undefined at HtmlWebpackPlugin. 报错如下: 解决办法: 下载安装指定 ...
- 成功解决TypeError: only integer scalar arrays can be converted to a scalar index
成功解决TypeError: only integer scalar arrays can be converted to a scalar index 目录 解决问题 解决思路 解决方法 解决问题 ...
- TypeError: only integer scalar arrays can be converted to a scalar index一例
问题重现: # -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') # @Author: appl ...
- TypeError: only integer scalar arrays can be converted to a scalar index
在取某些索引下的值时,报错TypeError: only integer scalar arrays can be converted to a scalar index data = data[in ...
- 【语音识别】之梅尔频率倒谱系数(mfcc)及Python实现
[语音识别]之梅尔频率倒谱系数(mfcc)及Python实现 一.mel滤波器 二.mfcc特征 Python实现 语音识别系统的第一步是进行特征提取,mfcc是描述短时功率谱包络的一种特征,在语音识 ...
最新文章
- 美陆军将在2020年军演中测试人工智能新应用
- matlab-代数方程
- BZOJ 1412 狼和羊的故事
- python爬虫,爬取糗事百科并保存到文件中
- 获取所有栈的信息,只有最上面的和最下面的,但是不能获取栈中间的activity信息...
- 卷积神经网络(卷积层,激活函数Relu,池化层,计算公式及API解释)
- OpenCASCADE绘制测试线束:数据交换命令之XDE 图层命令
- web页面优化之动态加载js和文件
- CentOS 7配置静态IP地址 解决了IP失效问题
- 两个数组合成一个json对象_两个jsonarray合并
- Redis使用过程出现类型转换异常问题- 20190220
- vue :style 设置背景图片 backgroundImage
- 认识Blend 3工作区
- 重拾Javascript(五)--优化字符串操作
- crossplaform---Nodejs in Visual Studio Code 04.Swig模版
- 视频教程-毕业设计精品课之基于ASP.NET旅游网站源码实战讲解(带房屋互租模块)-.NET
- 金士顿u盘分区工具_U盘PE安装ESD格式系统 图文教程
- Java-Specification
- 2G/3G/4G/5G
- r语言中的或怎么表示什么不同_R语言学习笔记(一)