在《机器学习实战》的第9章树回归中的源代码错误:

既是:

TypeError: unhashable type: 'matrix'

matrix类型不能被hash

解决过程:

In [22]: testMat
Out[22]:
matrix([[ 1.,  0.,  0.,  0.],[ 0.,  1.,  0.,  0.],[ 0.,  0.,  1.,  0.],[ 0.,  0.,  0.,  1.]])In [23]: testMat[:,1]
Out[23]:
matrix([[ 0.],[ 1.],[ 0.],[ 0.]])In [24]: set(testMat[:,1])
Traceback (most recent call last):File "<ipython-input-24-34e910607d48>", line 1, in <module>set(testMat[:,1])TypeError: unhashable type: 'matrix'In [25]: testMat[:,1].T.tolist()
Out[25]: [[0.0, 1.0, 0.0, 0.0]]In [26]: testMat[:,1].T.A.tolist()
Out[26]: [[0.0, 1.0, 0.0, 0.0]]In [27]: testMat[:,1].T.A.tolist()[0]
Out[27]: [0.0, 1.0, 0.0, 0.0]In [28]: testMat[:,1].T.tolist()[0]
Out[28]: [0.0, 1.0, 0.0, 0.0]In [29]: set(testMat[:,1].T.tolist()[0])
Out[29]: {0.0, 1.0}

即必须转换为行向量,然后再转换为列表,最后集合处理得到所有不同的元素值。

我这里用的是windows下的python 2.7 版本,其它的系统和版本暂不清楚。

机器学习实战:TypeError: unhashable type: 'matrix'相关推荐

  1. TypeError: unhashable type: 'dict'

    TypeError: unhashable type: 'dict' 错误代码: def update_data(uri_path,value):updateBody = {{"query& ...

  2. 成功解决TypeError: unhashable type: 'numpy.ndarray'

    成功解决TypeError: unhashable type: 'numpy.ndarray' 目录 解决问题 解决思路 解决方法 解决问题 TypeError: unhashable type: ' ...

  3. typeError: unhashable type: 'list’问题分析

    list常用去重方法 在python中使用list存储数据时有时候需要list中的数据不能重复,这样就需要用到去重操作,因为set是会自动把重复的内容去掉的,所以常用的简单去重方法是先将list转为s ...

  4. TypeError: unhashable type: 'list'

    在使用list创建set的时候,一直出现TypeError: unhashable type: 'list',这种错误,检查代码,并没有出错,只可能是有些未知的错误,之前可能没有遇到过,百度后发现,原 ...

  5. Python 2.x vs Python 3.x(四)—— TypeError: unhashable type

    list 是可变类型,无法进行 hash,或者说凡可变类型都无法进行 hash: 1. list 作为字典(dict)的 key 注意字典,无论是 Python 2.x 还是 Python 3.x 都 ...

  6. Python debug——TypeError: unhashable type(list/set/dict)

    正如错误提示,list/set/dict 均不可被哈希. 这一异常通常出现在,调用 set(-) 来构造一个 set (集合类型)时,set() 需要传递进来可哈希的元素(hashable items ...

  7. 【解决报错原因分析】画图plt.contourf(X,Y,Z)报错TypeError: unhashable type: ‘numpy.ndarray‘(含详细示例讲解)

    今天简化画图代码的时候发现了很奇怪的报错现象,经过一系列尝试找到了根源,希望帮助后来人,主要问题出现在如下语句中(为了体现问题.方便比对,特意在这改变了x为xx,如果你不想看这冗长的示例,可以直接按照 ...

  8. TypeError: unhashable type

    TypeError: unhashable type TypeError: unhashable type: 'list' 通常意味着您正在尝试使用列表作为哈希参数.这意味着当您尝试散列一个 不可散列 ...

  9. list转set遇坑:TypeError: unhashable type: ‘list‘

    由list组成的list(list套list)是不能转set的,形如: datalist = [[a],[b]] 转set时报错:TypeError: unhashable type: 'list' ...

最新文章

  1. AppManager
  2. python deque的内在实现 本质上就是双向链表所以用于stack、队列非常方便
  3. Spring和shiro整合 logout 配置方式
  4. 数据结构与算法(3)-----队列和栈
  5. 图书销售统计程序c语言,图书销售管理系统C语言程序报告.pdf
  6. C++多态,虚函数,纯虚函数
  7. HDU 4635(强连通分量分解
  8. linux如何运行qt源码,Linux/Ubuntu下编译Qt4.8.2源码
  9. python tar 多目录压缩脚本,使用tarfile(Python)只压缩给定目录中的文件
  10. 32岁已育国企技术女,上升无望,下一步如何做?
  11. for循环的几种写法(变化)
  12. 免费学python的网课-学习python的时候观看网课学习还是买书学习效率高?
  13. cocos-2d iphone入门(二) cocos2d源代码生成查询文档
  14. 【整理】牛客网编程题前端篇(较难难度)
  15. 什么是IP防护等级,又该如何区分
  16. c语言指针实验报告总结,指针(C语言实验报告).doc
  17. 计算机主要应用领域八大智能,八大智能(八大智能与五大领域图)
  18. 高德地图 调用天地图 WMTS 服务
  19. 大神李沐被曝离职,投身大模型创业!网友:希望不要耽误他解读论文
  20. C语言基础 输出月份名 问题

热门文章

  1. OpenCV在浏览器中运行深度网络
  2. OpenCV模板匹配Template Matching
  3. QDoc特殊内容special content
  4. QDoc标记命令Markup Commands
  5. OpenGL Fur Rendering毛发渲染的实例
  6. OpenGL延迟着色之三
  7. C++中int id[sizeof(unsigned long)]的语句,正确吗?
  8. c++预处理命令 #include用法
  9. 「C++」C++ Primer Plus 笔记:第一章 预备知识
  10. 1.socket编程:socket编程,网络字节序,函数介绍,IP地址转换函数,sockaddr数据结构,网络套接字函数,socket相关函数,TCP server和client