Python Numpy ValueError: data type must provide an itemsize
2014-08-01 15:28
513 查看
天朝网络锁国,百度找了半个小时找不出来原因,只能谷歌
谷歌第一条就是,顿时感觉幸福感来的太突然
原因是输入的矩阵均是字符串(从文件里读的)
那么就需要批量转数组,一行一行的转。
下面是我的代码:
其中将每一行的float切分 返回string的数组后再转化为float
这么做有个缺点,是返回的是list,针对二维数组初始化是用的
这种方法而写的。
那么我们要的是矩阵运算,numpy里面的narray提供了良好的封装
我们应该这么写
这样就可以按照numpy array的方式来操作这个数组了
谷歌第一条就是,顿时感觉幸福感来的太突然
原因是输入的矩阵均是字符串(从文件里读的)
那么就需要批量转数组,一行一行的转。
下面是我的代码:
rownum = 0 f = open(train_Y_path) for line in f.readlines(): train_Y_matrix[rownum] = map(float,line.strip('\n ').split(' ')) rownum += 1 print train_Y_matrix
其中将每一行的float切分 返回string的数组后再转化为float
这么做有个缺点,是返回的是list,针对二维数组初始化是用的
train_Y_matrix = [[0 for col in range(n_outs)] for row in range(train_rows)]
这种方法而写的。
那么我们要的是矩阵运算,numpy里面的narray提供了良好的封装
我们应该这么写
train_X_matrix = numpy.empty((train_rows,n_ins),numpy.float64) for line in f.readlines(): train_X_matrix[rownum] = numpy.asarray(line.strip('\n ').split(' '), dtype=float) rownum += 1
这样就可以按照numpy array的方式来操作这个数组了
相关文章推荐
- python3 爬虫TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be
- python-rrdtool创建rrd文件的TypeError: argument 0 must be string 错误
- An exception of type System.Data.SqlClient.SqlException was thrown. Additional exception information: A network-related or instance-specific error occ
- Python持久化时抛出异常:TypeError: must be str, not bytes
- keil(MDK)中出现error: #513: a value of type "int" cannot be assigned to an entity of type "char *",即函数返
- python中super出现的TypeError: must be type, not classobj 原因及解决
- python "".join() 出错TypeError: sequence item 0: expected string
- 手把手教你 python读取文件报错:ValueError encoding must be one of 'utf_8', 'big5', or 'gbk'.
- ajax上传图片TypeError: 'append' called on an object that does not implement interface FormData.
- 处理错误TypeError: initial_value must be unicode or None, not str
- [entity framework ] The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
- python 的cx_Oracle模块的cx_Oracle.NotSupportedError: Variable_TypeByValue(): unhandled data type unicode解决办法
- python中super出现的TypeError: must be type, not classobj 原因及解决
- VS EF Error: Configuration Error extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- python:TypeError: must be type, not classobj
- python2.7 使用super关键词 报错 TypeError: must be type, not&n
- Error:You must write an attribute 'type'='object' after writing the attribute with local name '__type'
- C++ Error : initial value of reference to non-const must be an lvalue