机器学习实战的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. 遇到的问题之“解决tomcat中文乱码问题”

    方案1:在server.xml中添加了 URIEncoding="UTF-8" 属性 路径:C:\stop\apache-tomcat-8.5.69\conf 修改内容 如若方案1 ...

  2. 顺利通过EMC实验(13)

  3. 隐藏IE10默认在input框输入内容后显示“X”按钮

    ::-ms-clear{display: none;} ::-ms-reveal{display: none;}

  4. python-人物风云榜(实现排名)

    Description 又到了云之国一年一度的任务风云榜更新的大日子了.给出每个人风云力数值,需要你给出每个人的排名.注意,排名存在并列的情况. Input 一共有 22 行.第一行一个整数 n ,表 ...

  5. JDBC/Mybatis连接数据库报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.

    造成这个的原因是maven导入MyBatis的时候会自动导入最新版本的8.0.11,然后8.0.11采用了新驱动,之前版本会报错. 当我们使用高版本的MySQL驱动时可以在获取数据库的连接getCon ...

  6. An=n的前n项和的前n项和

    #include<iostream> using namespace std; int main() { int n,a=0,b=0; cin>>n; for(int i=1; ...

  7. windows上用命令行我们查看机器cpu信息(使用计算器-程序员模式-四字时,查看系统类型)

    查看系统是64位还是32位 C:\Users\qingshuic>wmic os get osarchitecture OSArchitecture 64-bitC:\Users\qingshu ...

  8. 前端NEXT实践系列:(一)ECMAScript 6.0技术栈

    随着ECMAScript 6.0(ES6)是JavaScript 语言的下一代标准的普及,各个大公司和大的厂商都推出了自己的前端开发框架,如Angular,React,Vue 等,微软更是锦上添花,开 ...

  9. Mysql学习day2随笔

    --jion on 连接查询 --jion where 等值查询 建议先用jion on再用where过滤 --inner jion 返回交集 --left join 无论右表是否匹配,都会从左表返回 ...

  10. 【课程汇总】OpenHarmony 成长计划知识赋能第二期课程(附链接)

    OpenHarmony 开源开发者成长计划第二期知识赋能直播课程以入门为主,共设置 8 节课,覆盖了应用开发.设备开发.内核驱动等多个技术领域.带领开发者快速了解如何玩转 OpenHarmony.如何 ...