numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U1'), dtype('float64')) -> None
在机器学习实战的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的更多相关文章
- ImportError: numpy.core.multiarray failed to import
1. ImportError: numpy.core.multiarray failed to import pip install -U numpy http://stackoverflow.com ...
- 异常 No module named 'numpy.core._multiarray_umath
No module named 'numpy.core._multiarray_umath 解决方法: 1. 可能是由于模型保存时出错,导致模型没有保存成功,此时删掉保存的模型即可 2. numpy版 ...
- ‘No module named 'numpy.core._multiarray_umath’ 或者‘no module named numpy’
在import TensorFlow时,如果遇到‘No module named 'numpy.core._multiarray_umath’ 或者‘no module named numpy’,大多 ...
- ImportError: DLL load failed: 找不到指定的模块;ImportError: numpy.core.multiarray failed to import 报错解决
python程序运行出错,出错的两行主要信息如下: ImportError: DLL load failed: 找不到指定的模块 ImportError: numpy.core.multiarray ...
- No module named 'numpy.core._multiarray_umath'
问题:基于anaconda prompt 安装好TensorFlow框架以后,引入的时候(import tensorflow as tf)报如下图片的错误: 回答:网上好多人说是需要升级numpy,我 ...
- 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 ...
- pyinstaller打包出错numpy.core.multiarray failed to import
py原文件运行时正常,但用pyinstaller打包为exe后,在运行则报错: 这是因为cv2要求的numpy版本与你装的numpy版本不一样,导致冲突:网上很多说升级numpy,但你把numpy升的 ...
- python3.6安装jpype1后引入jpype报“ImportError: numpy.core.multiarray failed to import”问题
jpype是调用java接口的第三方库,通过该库,python可以运行java程序,从而解决一些调用java的问题,比如:java开发的接口,测试时, 有java的加密算法就不用python写一遍重复 ...
- 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> ...
随机推荐
- zookeeper 负载均衡和 nginx 负载均衡区别?
zk 的负载均衡是可以调控,nginx 只是能调权重,其他需要可控的都需要自己写插件:但是 nginx 的吞吐量比 zk 大很多,应该说按业务选择用哪种方式.
- carsim笔记——道路设置
第一步: 进入道路轨迹设置 道路情况设置举例 第二步:设置道路3D的显示效果 对上面的解释举例说明
- jQ模拟打字效果插件typetype
typetype是一个jquery插件,可以模拟人类的打字效果. 效果图如下所示: 查看演示 http://weber.pub/demo/160828/jQuery.Type/jQuery.type. ...
- 【Android开发】URL 转义与反转义
1,转义 @org.junit.Test public void testEncode(){ String url="http://192.168.0.19:8888/cas/login&q ...
- Python Turtle库绘制蟒蛇
使用Python Turtle库来绘制蟒蛇 import turtle引入了海龟绘图体系 使用setup函数,设定了一个宽650像素和高350像素的窗体,其位置左上角坐标是200,200 说明位置在距 ...
- Python输出数字金字塔
使用Python输出一个数字金字塔 运行结果: 源代码: ''' Python输出数字金字塔 ''' for x in range(1,10): print(' '*(15-x),end='') n= ...
- 谈谈我认识的js原型
众所周知,JavaScript中是没有传统类的概念的,js通过原型链的方式实现继承.原型是js学习中的一大重点知识,在ES6出来之前,因为js不像php.java一样拥有类的写法,所以继承方式也就不像 ...
- flex布局图片和文字同级,文字过多导致图片变形问题
图片增加css样式即可 flex-grow: 0;flex-shrink: 0;
- 为Anaconda python3安装gi模块
项目开发中需要使用到gi模块,Ubuntu自带的Python3.5可以正常使用gi.项目解释环境是Anaconda python3.5,提示ImportError: No module named ' ...
- Python中用类实现对象和封装
""" 用类实现对象和封装 对象:对应客观世界的事物,将描述事物的一组数据和与这组数据有关的操作封装在一起, 形成一个实体,这个实体就是对象 类:具有相同或相似性质的对象 ...