机器学习实战的Logistic回归梯度上升优化算法中遇到了这个问题

numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U1'), dtype('float64')) -> None

代码如下

import math
import numpy as np def loadDataSet():
dataSet = []
labelSet = []
with open('testSet.txt') as fbj:
for line in fbj.readlines():
lineArr = line.strip().split()
# print(lineArr)
dataSet.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelSet.append(lineArr[2])
return dataSet, labelSet def sigmoid(inX):
result = 1/(1+np.exp(-inX))
return result def gradAscent(dataSet, labelSet):
dataMtrix = np.mat(dataSet)
labelMat = np.mat(labelSet).transpose()
m, n = np.shape(dataMtrix)
alpha = 0.001
maxCycles = 500
weights = np.ones((n, 1))
for _ in range(maxCycles):
h = sigmoid(dataMtrix * weights)
error = labelMat - h
weights = weights + alpha * dataMtrix.transpose() * error
return weights dataSet, labelSet = loadDataSet()
# print(dataSet)
# print(labelSet)
print(gradAscent(dataSet, labelSet))

这里报错说的是数据类型不符不能相减

那么分别查看一下(在jupyter调试)

labelMat.dtype

dtype('<U1')
h.dtype

dtype('float64')

那么解决办法就是将<U1类型换成float64

但是使用如下方法还是报错

labelMat.dtype = 'float64'

ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.

那么只好乖乖使用astype方法

labelMat = labelMat.astype(np.float64)

修改后的代码及结果如下

import math
import numpy as np def loadDataSet():
dataSet = []
labelSet = []
with open('testSet.txt') as fbj:
for line in fbj.readlines():
lineArr = line.strip().split()
# print(lineArr)
dataSet.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelSet.append(lineArr[2])
return dataSet, labelSet def sigmoid(inX):
result = 1/(1+np.exp(-inX))
return result def gradAscent(dataSet, labelSet):
dataMtrix = np.mat(dataSet)
labelMat = np.mat(labelSet).transpose()
labelMat = labelMat.astype(np.float64)
m, n = np.shape(dataMtrix)
alpha = 0.001
maxCycles = 500
weights = np.ones((n, 1))
for _ in range(maxCycles):
h = sigmoid(dataMtrix * weights)
error = labelMat - h
weights = weights + alpha * dataMtrix.transpose() * error
return weights dataSet, labelSet = loadDataSet()
# print(dataSet)
# print(labelSet)
print(gradAscent(dataSet, labelSet)) [[ 4.12414349]
[ 0.48007329]
[-0.6168482 ]]

numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U1'), dtype('float64')) -> None的更多相关文章

  1. ImportError: numpy.core.multiarray failed to import

    1. ImportError: numpy.core.multiarray failed to import pip install -U numpy http://stackoverflow.com ...

  2. 异常 No module named 'numpy.core._multiarray_umath

    No module named 'numpy.core._multiarray_umath 解决方法: 1. 可能是由于模型保存时出错,导致模型没有保存成功,此时删掉保存的模型即可 2. numpy版 ...

  3. ‘No module named 'numpy.core._multiarray_umath’ 或者‘no module named numpy’

    在import TensorFlow时,如果遇到‘No module named 'numpy.core._multiarray_umath’ 或者‘no module named numpy’,大多 ...

  4. ImportError: DLL load failed: 找不到指定的模块;ImportError: numpy.core.multiarray failed to import 报错解决

    python程序运行出错,出错的两行主要信息如下: ImportError: DLL load failed: 找不到指定的模块 ImportError: numpy.core.multiarray ...

  5. No module named 'numpy.core._multiarray_umath'

    问题:基于anaconda prompt 安装好TensorFlow框架以后,引入的时候(import tensorflow as tf)报如下图片的错误: 回答:网上好多人说是需要升级numpy,我 ...

  6. ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' ImportError: numpy.core.multiarray failed to import

      出现以下错误:可能是因为你的numpy版本太低 更新numpy的版本 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgra ...

  7. pyinstaller打包出错numpy.core.multiarray failed to import

    py原文件运行时正常,但用pyinstaller打包为exe后,在运行则报错: 这是因为cv2要求的numpy版本与你装的numpy版本不一样,导致冲突:网上很多说升级numpy,但你把numpy升的 ...

  8. python3.6安装jpype1后引入jpype报“ImportError: numpy.core.multiarray failed to import”问题

    jpype是调用java接口的第三方库,通过该库,python可以运行java程序,从而解决一些调用java的问题,比如:java开发的接口,测试时, 有java的加密算法就不用python写一遍重复 ...

  9. Could not find a version that satisfies the requirement numpy>=1.7.0 (from pan das==0.17.0) (from versions: ) No matching distribution found for numpy>=1.7.0 (from pandas==0.17.0)

    今天晚上一直在安装pandas,天杀的,真的是太难了.后来发现提示: Could not find a version that satisfies the requirement numpy> ...

随机推荐

  1. 哪些是重要的 bean 生命周期方法?你能重载它们吗?

    有两个重要的 bean 生命周期方法,第一个是 setup , 它是在容器加载 bean 的时候被调用.第二个方法是 teardown 它是在容器卸载类的时候被调用. The bean 标签有两个重要 ...

  2. [译] Facebook:我们是如何构建第一个跨平台的 React Native APP

    英文原文(需FQ):https://code.facebook.com/posts/1189117404435352/ 早些时候,我们介绍过iOS版的React Native. React Nativ ...

  3. 前端每日实战:133# 视频演示如何用 CSS 和 GSAP 创作有多个关键帧的连续动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eLMKJG 可交互视频 此视频是可 ...

  4. Linux 0.11源码阅读笔记-中断过程

    Linux 0.11源码阅读笔记-中断过程 是什么中断 中断发生时,计算机会停止当前运行的程序,转而执行中断处理程序,然后再返回原被中断的程序继续运行.中断包括硬件中断和软件中断,硬中断是由外设自动产 ...

  5. Java/C++实现装饰模式---模拟手机功能的升级过程

    用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人:而JarPhone除了声音还能振动:更高级的手机(ComplexPhone)除了声音.振动外,还 ...

  6. ccf颁奖晚会

    感想: 首先,十分感谢学校给我们参加比赛的机会,给予我们这次难能可贵的学习机会,第一次在这么大型的赛场中展现自己,我们也不免有些紧张.从最开始在线上进行模型训练,到我们不远千里在江苏进行场地的勘察,到 ...

  7. Node Sass version 7.0.1 is incompatible with ^4.0.0

    网上一大堆, 什么降级node版本, 升级node-sass版本 , 再或者安装nvm来管理npm版本等等, 其实很烦 这边就两步: npm uninstall node-sass npm i -D ...

  8. jquery 常用选择器和方法以及遍历(超详细)

    jQuery 常用选择器和和方法 学习总结 一.JQuery 介绍 1. 什么是 jQuery 2. jQuery 版本介绍 3. jQuery 文件介绍 二.jQuery 对象 1. jQuery ...

  9. 为Anaconda python3安装gi模块

    项目开发中需要使用到gi模块,Ubuntu自带的Python3.5可以正常使用gi.项目解释环境是Anaconda python3.5,提示ImportError: No module named ' ...

  10. partOne当堂测试代码

    /* ScoreInformation类*/public class ScoreInformation { private String stunumber;//学号 private String n ...