问题描述

编写了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相关推荐

  1. TypeError: only size-1 arrays can be converted to Python scalars

    正在愉快写代码的我 ??????????? 源代码也很简单 之后可以解决,比如 再比如: 但是那样究竟为什么不对呢? plt.bar(x = x,height=data,width=0.8,align ...

  2. 自学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, ...

  3. [python]TypeError: only size-1 arrays can be converted to Python scalars

    当在代码中同时import numpy库和math库时,在进行sin.exp等运算时需要使用 np.exp()或者 np.sin(),否则就会报这个错误. # -*- coding: utf-8 -* ...

  4. TypeError系列之:TypeError: only size-1 arrays can be converted to Python scalars

    这样一个错误是一个类型错误,翻译过来就是, 只有维度是1的数组可以被转换成Python标量. 出现这种问题的原因会很多,非常具体,但根本原因是维度错误!! 所以各位看官要查询出错位置,看看赋值的数据( ...

  5. 使用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 ...

  6. 【Webpack】TypeError: Cannot read property ‘tap‘ of undefined at HtmlWebpackPlugin.

    [Webpack]TypeError: Cannot read property 'tap' of undefined at HtmlWebpackPlugin. 报错如下: 解决办法: 下载安装指定 ...

  7. 成功解决TypeError: only integer scalar arrays can be converted to a scalar index

    成功解决TypeError: only integer scalar arrays can be converted to a scalar index 目录 解决问题 解决思路 解决方法 解决问题 ...

  8. 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 ...

  9. 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 ...

  10. 【语音识别】之梅尔频率倒谱系数(mfcc)及Python实现

    [语音识别]之梅尔频率倒谱系数(mfcc)及Python实现 一.mel滤波器 二.mfcc特征 Python实现 语音识别系统的第一步是进行特征提取,mfcc是描述短时功率谱包络的一种特征,在语音识 ...

最新文章

  1. 美陆军将在2020年军演中测试人工智能新应用
  2. matlab-代数方程
  3. BZOJ 1412 狼和羊的故事
  4. python爬虫,爬取糗事百科并保存到文件中
  5. 获取所有栈的信息,只有最上面的和最下面的,但是不能获取栈中间的activity信息...
  6. 卷积神经网络(卷积层,激活函数Relu,池化层,计算公式及API解释)
  7. OpenCASCADE绘制测试线束:数据交换命令之XDE 图层命令
  8. web页面优化之动态加载js和文件
  9. CentOS 7配置静态IP地址 解决了IP失效问题
  10. 两个数组合成一个json对象_两个jsonarray合并
  11. Redis使用过程出现类型转换异常问题- 20190220
  12. vue :style 设置背景图片 backgroundImage
  13. 认识Blend 3工作区
  14. 重拾Javascript(五)--优化字符串操作
  15. crossplaform---Nodejs in Visual Studio Code 04.Swig模版
  16. 视频教程-毕业设计精品课之基于ASP.NET旅游网站源码实战讲解(带房屋互租模块)-.NET
  17. 金士顿u盘分区工具_U盘PE安装ESD格式系统 图文教程
  18. Java-Specification
  19. 2G/3G/4G/5G
  20. r语言中的或怎么表示什么不同_R语言学习笔记(一)

热门文章

  1. 【51单片机】OOK无线通讯在无线门磁报警中的应用
  2. iOS开发之—— XCODE真机调试设备连接一直忙碌如何处理!(真机调试各种错误提示解决)...
  3. js 对象和回调函数
  4. 高等数学:极限与连续
  5. python使用虚拟内存_深入浅出虚拟内存(一)
  6. ASP.NET复习资料
  7. 街头篮球服务器维护,《街头篮球》4月23日新版活动抢先看
  8. 走迷宫小游戏课设(C语言)
  9. 你的CRM系统为什么用不起来?
  10. AT学习报告一 软件的初步使用