List tuple 类型转成数组
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 类型转成数组的更多相关文章
- PHP将对象转换成数组的方法(兼容多维数组类型)
/** * @author gayayang * @date 2012-8-21 * @todo 将对象转换成数组 * @param unknown_type $obj * @return unkno ...
- python学习第五天 List和tuple类型介绍及其List切片
List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...
- 从jquery源码中看类型判断和数组的一些操作
在深入看jquery源码中,大家会发现源码写的相当巧妙.那我今天也通过几个源码中用到的技巧来抛砖引玉,希望大家能共同研究源码之精华,不要囫囵吞枣. 1.将类数组转化成数组 我想大家首先想到的方法是fo ...
- oracle根据分隔符将字符串分割成数组函数
--创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...
- 黄聪:PHP字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、切割成数组等)
一.字符串替换 str_replace("iwind", "kiki", "i love iwind, iwind said"); 将输出 ...
- 将COleDateTime类型数据转换成char *数据
用OpenCV做多摄像头校准时间,在图像上显示时间信息,需求要将COleDateTime类型数据转换成char *数据 具体代码如下: 1: COleDateTime m_checkDate; 2: ...
- Yii Active Record 查询结果转化成数组
使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
- js 判断是否为数组的方式 及 类数组转换成数组格式
1. 判断是否为数组的通用方式 Object.prototype.toString.call(o)=='[object Array]' 其他方式: typeof , instanceof, ary ...
- .net 4.0 中的特性总结(四):Tuple类型
Tuple是具有指定数量和顺序的值的一种数据结构.针对这种数据结构,.Net4.0中提供了一组Tuple类型,具体如下: Tuple Tuple<T> Tuple<T1, T ...
随机推荐
- PHP数组及简单函数
PHP函数: 1.简单函数 四要素:返回类型,函数名,参数列表,函数体 function Show() { echo "hello"; } Show(); 运行结果:hellow ...
- HandlerThread 创建一个异步的后台线程
使用HandlerThread几大优点: 1.制作一个后台异步线程,需要的时候就可以丢一个任务给它,使用比较灵活; 2.Android系统提供的,使用简单方便,内部自己封装了Looper+Handle ...
- CClayer ignoreAnchorPointForPosition 参数的作用
ignoreAnchorPointForPosition:忽略锚点对于位置的设定.即非位置的设定(比如说缩放),则不受此参数的控制. 并且默认情况下CCLayer的默认锚点是 中点,而不是左下角的点. ...
- asp.net WebService+Ajax调用
default.aspx 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile=&qu ...
- SQL Server 存储过程遇到“表 '#TT' 没有标识属性。无法执行 SET 操作”错误
创建临时表,往临时表插入数据的时候报的错误. 一开始提示没有打开主键,后来打开主键就提示上述错误异常. 从网上查找资料没有找到,然后又到群里问各位大牛,一位大牛告诉我是没有设置主键. 我又仔细看看提示 ...
- 利用SHELL脚本实现文件完整性检测程序(1.2版更新)
一..开发背景 因时势所逼,需要对服务器的文件系统实行监控.虽然linux下有不少入侵检测和防窜改系统,但都比较麻烦,用起来也不是很称手.自己琢磨着也不需要什么多复杂的功能,写个脚本应该就可以满足基本 ...
- MVC学习系列——Filter扩展
在MVC中,Filter也是可以扩展的.在此,本人对Filter的理解就是AOP,不知道各位大侠,有什么高的见解,呵呵... 首先MVC四大过滤神器IAuthorizationFilter,IActi ...
- MessageBox的常用方法
一 函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; hWnd:对话框父窗口句柄, ...
- nodejs连接mongodb的方法
一. var express = require('express'); var mongodb = require('mongodb'); var app = express(); app.use( ...
- poj2186--tarjan+缩点
题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...