python的set处理二维数组转一维数组的方法示例
作者:下士闻道 时间:2022-03-30 12:37:02
for splitValue in set(dataset[:, featureIndex].tolist()):
首先set是一个无序,无重复的数据结构,所以很多时候使用它来进行去重;但是set接收的函数是原生array,这个怎么办?我现在有的numpy里面的matrix;于是我查了一下,直接使用matrix.tolist(),但是发现返回值有问题:
import os
os.chdir("D:\\galaxy\\aliyunsvn\\code\\MLInAction\\dataset")
dataArr = loadDataSet("ex00.txt")
dataMat = mat(dataArr)
dataMat[:, 0].tolist()
输出为:
[[0.036098], [0.727098],..
并不是我想要的,因为如果此时将其放于set中将会报错:
set(dataMat[:, 0].tolist())
>>TypeError: unhashable type: 'list'
于是查了一下,做了调整:
import os
os.chdir("D:\\galaxy\\aliyunsvn\\code\\MLInAction\\dataset")
dataArr = loadDataSet("ex00.txt")
dataMat = mat(dataArr)
dataMat[:, 0].A.flatten().tolist()
>> [0.036098, 0.712386, ....
很好就是要这种一维的数组,然后再使用数组作为参数放入set中,完美。
set(dataMat[:, 0].A.flatten().tolist())
来源:https://www.cnblogs.com/xiashiwendao/p/10507230.html
标签:python,二维数组,一维数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
windows 2000 IIS下配置Php+Mysql+zend的图文教程(完整版)
2007-06-15 10:51:00
![](https://img.aspxhome.com/file/uploadpic/20076/15/200761515925137.gif)
Python自动化办公之Word文档的创建与生成
2022-06-16 17:49:43
![](https://img.aspxhome.com/file/2023/8/75908_0s.jpg)
详解Python3的TFTP文件传输
2023-06-01 22:29:17
![](https://img.aspxhome.com/file/2023/6/87896_0s.png)
Python的Django中将文件上传至七牛云存储的代码分享
2023-11-28 14:00:24
Session.TimeOut的最大取值是1440,超出会报错
2011-03-31 11:19:00
oracle 查询表名以及表的列名
2009-07-26 09:33:00
python GUI库图形界面开发之PyQt5中QMainWindow, QWidget以及QDialog的区别和选择
2022-04-22 01:39:50
mysql中普通索引和唯一索引的效率对比
2010-12-08 16:03:00
![](https://img.aspxhome.com/file/UploadPic/201012/8/2010128165343945s.jpg)
2008年10佳改版网站
2008-09-22 20:15:00
![](https://img.aspxhome.com/file/UploadPic/20089/22/2008922202035116s.jpg)
python脚本实现验证码识别
2022-02-03 09:41:30
![](https://img.aspxhome.com/file/2023/4/79664_0s.jpg)
FCKeditor新版本发布,并更名为CKeditor
2009-09-08 13:09:00
![](https://img.aspxhome.com/file/UploadPic/20099/8/ckeditor-60s.jpg)
100%点击区的滑动门
2007-06-14 22:00:00
在Python中操作列表之List.pop()方法的使用
2021-06-20 12:02:17
在VS2008中编译MYSQL5.1.48的方法
2023-07-12 00:42:46
python 函数、变量中单下划线和双下划线的区别详解
2021-06-29 11:32:33
文字链接,怎么办?
2008-08-04 13:18:00
![](https://img.aspxhome.com/file/UploadPic/20088/4/20088413222403s.gif)
python 按不同维度求和,最值,均值的实例
2023-06-12 15:08:23
![](https://img.aspxhome.com/file/2023/9/59479_0s.jpg)
php反序列化之魔术方法超详细讲解
2023-11-16 07:24:59
![](https://img.aspxhome.com/file/2023/1/55621_0s.png)
python实现Decorator模式实例代码
2022-05-10 06:04:56
在python中使用[[v]*n]*n遇到的坑及解决
2022-02-03 10:31:35
![](https://img.aspxhome.com/file/2023/5/89315_0s.png)