SKlearning大部分的输入数据都是M * N数组.

然而我们从数据库或文件读取得来的通常是Python内定的类型tuple或list

它们的优势就不说了,但是直接把list或tuple构成的二维数组传入scikit是会出问题的.

如:

DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)

下面贴上如何把list/tuple转为scikit使用的array

首先, 准备数据如下:

读取一行数据变为一维数组

    conn = sql.connect('result_sale.db')
conn.text_factory = str
dataSet = conn.execute('select * from sampleData')
tpRows = dataSet.fetchone()
conn.close()
print type(tpRows)
print tpRows lstRows = list(tpRows)
aryRows1 = np.array(lstRows) # 转成数组
#aryRows2 = np.array(lstRows).reshape(1, -1) # 转成1行N列 (二维数组)
#aryRows3 = np.array(lstRows).reshape(-1, 1) # 转成N行1列 (二维数组)
print lstRows
print aryRows1

输入如下: 请留意输入的不同点 :)

('00', '01', '02', '03', '04', '05', '06', '07', '08')  (tuple)
['00', '01', '02', '03', '04', '05', '06', '07', '08'] (list)
['00' '01' '02' '03' '04' '05' '06' '07' '08'] (array) Process finished with exit code 0

一次性转换整个数据集

    conn = sql.connect('result_sale.db')
conn.text_factory = str
dataSet = conn.execute('select * from sampleData')
tpRows = dataSet.fetchall()
conn.close() aryRows1 = np.array(tpRows) # 转成数组
#aryRows2 = np.array(tpRows).reshape(1, -1) # 转成1行N列 (二维数组)
#aryRows3 = np.array(tpRows).reshape(-1, 1) # 转成N行1列 (二维数组)
print aryRows1
#print aryRows2
#print aryRows3

输入如下:

[['00' '01' '02' '03' '04' '05' '06' '07' '08']
['10' '11' '12' '13' '14' '15' '16' '17' '18']
['20' '21' '22' '23' '24' '25' '26' '27' '28']
['30' '31' '32' '33' '34' '35' '36' '37' '38']
['40' '41' '42' '43' '44' '45' '46' '47' '48']
['50' '51' '52' '53' '54' '55' '56' '57' '58']
['60' '61' '62' '63' '64' '65' '66' '67' '68']
['70' '71' '72' '73' '74' '75' '76' '77' '78']
['80' '81' '82' '83' '84' '85' '86' '87' '88']] Process finished with exit code 0

逐条纪录转换, 可以用下标来引用数组

    conn = sql.connect('result_sale.db')
conn.text_factory = str
dataSet = conn.execute('select * from sampleData')
tpRows = dataSet.fetchall()
conn.close() #aryRows = np.zeros([len(tpRows), len(tpRows[0])])
aryRows = np.ones_like(tpRows) #亦可使用 empty, empty_like, zeros, zeros_like 等方法 j=0
for row in tpRows:
aryRows[j][:] = row
j += 1
print aryRows

输入如下:

[['00' '01' '02' '03' '04' '05' '06' '07' '08']
['10' '11' '12' '13' '14' '15' '16' '17' '18']
['20' '21' '22' '23' '24' '25' '26' '27' '28']
['30' '31' '32' '33' '34' '35' '36' '37' '38']
['40' '41' '42' '43' '44' '45' '46' '47' '48']
['50' '51' '52' '53' '54' '55' '56' '57' '58']
['60' '61' '62' '63' '64' '65' '66' '67' '68']
['70' '71' '72' '73' '74' '75' '76' '77' '78']
['80' '81' '82' '83' '84' '85' '86' '87' '88']] Process finished with exit code 0

List tuple 类型转成数组的更多相关文章

  1. PHP将对象转换成数组的方法(兼容多维数组类型)

    /** * @author gayayang * @date 2012-8-21 * @todo 将对象转换成数组 * @param unknown_type $obj * @return unkno ...

  2. python学习第五天 List和tuple类型介绍及其List切片

    List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...

  3. 从jquery源码中看类型判断和数组的一些操作

    在深入看jquery源码中,大家会发现源码写的相当巧妙.那我今天也通过几个源码中用到的技巧来抛砖引玉,希望大家能共同研究源码之精华,不要囫囵吞枣. 1.将类数组转化成数组 我想大家首先想到的方法是fo ...

  4. oracle根据分隔符将字符串分割成数组函数

    --创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...

  5. 黄聪:PHP字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、切割成数组等)

    一.字符串替换 str_replace("iwind", "kiki", "i love iwind, iwind said"); 将输出 ...

  6. 将COleDateTime类型数据转换成char *数据

    用OpenCV做多摄像头校准时间,在图像上显示时间信息,需求要将COleDateTime类型数据转换成char *数据 具体代码如下: 1: COleDateTime m_checkDate; 2: ...

  7. Yii Active Record 查询结果转化成数组

    使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...

  8. js 判断是否为数组的方式 及 类数组转换成数组格式

    1. 判断是否为数组的通用方式 Object.prototype.toString.call(o)=='[object Array]' 其他方式: typeof ,  instanceof,  ary ...

  9. .net 4.0 中的特性总结(四):Tuple类型

    Tuple是具有指定数量和顺序的值的一种数据结构.针对这种数据结构,.Net4.0中提供了一组Tuple类型,具体如下: Tuple   Tuple<T>   Tuple<T1, T ...

随机推荐

  1. 学习C#线程

    2016-12-17 无意间看到了关于C#线程的讲解.经过一下午的学习后,慢慢的对线程也有了一定的理解.这里讲解的是最基础的内容,包括线程的创建.睡眠.等待.终止. 实验环境:Visual studi ...

  2. PetaPoco4.0的事务为什么不会回滚

    using (var srop=DbHelper.CurrentDb.GetTransaction()) { ID = bp.AddModel(model).ToStr(); #region 参与楼盘 ...

  3. SSH输入错误Action

    在类型转化.输入验证校验 .文件上传等出错的时候,如Action中某个变量是int,而上传的值是"ABC",此时Action不会执行execute()函数,而是直接返回result ...

  4. Java中,包的概念、常量、静态成员、继承

    新建包:左上角-新建-包 命名规则(通常从大到小,方便整合不容易冲突)  例如:com.itnba.maya.test package必须在最顶行,之前不能再有其他代码 使用包: 快捷方式:使用包中的 ...

  5. IOS 2D游戏开发框架 SpriteKit-->续(完善角色功能)

    一.说明       今天给角色精灵增加了子弹发射功能,增加了子弹与敌对精灵的碰撞检测,当角色精灵子弹与敌对精灵碰撞后,它们都会从屏幕上消失. 二.场景层SKScene的修改 1. 在初始化场景层的方 ...

  6. entityframework学习笔记--004-无载荷与有载荷关系

    1.无载荷(with NO Payload)的多对多关系建模 在数据库中,存在通过一张链接表来关联两张表的情况.链接表仅包含连接两张表形成多对多关系的外键,你需要把这两张多对多关系的表导入到实体框架模 ...

  7. nhibernate 中 lazy="no-proxy" 时的问题

    在 nhibernate,如果将实体的一个关联属性配置为 lazy="no-proxy",那么,从其他属性计算出来的属性不能正确更新.例如,将以下代码中 Foo.Bar 配置为 l ...

  8. B树、B+树的实现

    B树的定义 假设B树的度为t(t>=2),则B树满足如下要求:(参考算法导论) (1)  每个非根节点至少包含t-1个关键字,t个指向子节点的指针:至多包含2t-1个关键字,2t个指向子女的指针 ...

  9. kafkaspot在ack机制下如何保证内存不溢

    新浪微博:intsmaze刘洋洋哥.   storm框架中的kafkaspout类实现的是BaseRichSpout,它里面已经重写了fail和ack方法,所以我们的bolt必须实现ack机制,就可以 ...

  10. gulp压缩css文件跟js文件

    越到最后啊 就越发现,真的很理解那句话 就是自己多学一点一点知识,就少一句问别人的东西 这是多么痛苦的领悟 今天需要压缩css跟js文件 然后不懂啊 就问别人啊 就问啊问啊 然后再上网了解啊了解啊 用 ...