天朝网络锁国,百度找了半个小时找不出来原因,只能谷歌

谷歌第一条就是,顿时感觉幸福感来的太突然

原因是输入的矩阵均是字符串(从文件里读的)

那么就需要批量转数组,一行一行的转。

下面是我的代码:

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的方式来操作这个数组了

Python Numpy ValueError: data type must provide an itemsize的更多相关文章

  1. Python报错:TypeError: data type not understood

    K-Means聚类算法 def randCent(dataSet, k): m, n = dataSet.shape # numpy中的shape函数的返回一个矩阵的规模,即是几行几列 centrod ...

  2. Python NumPy学习总结

    一.NumPy简介 其官网是:http://www.numpy.org/ NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.Num ...

  3. Seven Python Tools All Data Scientists Should Know How to Use

    Seven Python Tools All Data Scientists Should Know How to Use If you’re an aspiring data scientist, ...

  4. CS231n课程笔记翻译1:Python Numpy教程

    译者注:本文智能单元首发,翻译自斯坦福CS231n课程笔记Python Numpy Tutorial,由课程教师Andrej Karpathy授权进行翻译.本篇教程由杜客翻译完成,Flood Sung ...

  5. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  6. 诡异错误二:TypeError: data type not understood

    如何使用Python产生一个数组,数组的长度为1024,数组的元素全为0? 很简单啊, 使用zeros(1024) 即可实现! 如何产生一个2×1024的全0矩阵呢?是否是zeros(2,1024) ...

  7. Python numpy中矩阵的用法总结

    关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...

  8. [转]Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

    Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate() 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...

  9. R vs Python:构建data.frame、读取csv与统计描述

    一.Python 数据框就是典型的关系型数据库的数据存储形式,每一行是一条记录,每一列是一个属性,最终构成表格的形式,这是数据科学家必须熟悉的最典型的数据结构. 1.构建数据框 import pand ...

随机推荐

  1. Oracle API Gateway连接WebService服务,攻击保护

    1.启动和连接OAG OAG连接的时候除了不选择analysis,其他都选上,然后启动Gateway实例以及Nodemanager. 命令如下: /$OAG_HOME/apigateway/posix ...

  2. Java8 新的日期和时间API(笔记)

    LocalDate LocalTime Instant duration以及Period 使用LocalDate和LocalTime //2017-03-20 LocalDate date = Loc ...

  3. django dispatch

    from django.views import View # 这里Home需要继承View class Home(View): # 这样这里就相当于一个装饰器的功能,可以自己定制化内容 def di ...

  4. bzoj1710【Usaco2007 Open】Cheappal 便宜回文

    1710: [Usaco2007 Open]Cheappal 便宜回文 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 466  Solved: 262 ...

  5. vue 表单验证实例

    1.注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  6. C语言学习笔记(二) 基础知识

    数据类型 C语言数据可以分为两大类: 基本类型数据和复合类型数据: 基本类型数据 整数 整型   (int)  ——占4字节 短整型(short int)  ——占2字节    长整型(long in ...

  7. linux 查看cpu相关信息命令

  8. Javascript - demo 与 捷径

    1.页面的后退.刷新.前进 function back(){ history.go(-1); // 后退 } function forward(){ history.go(+1); // 前进 1 页 ...

  9. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  10. formail 发送HTML 邮件通过 SENDMAIL

    cat a.html | formail -I "Content-type:text/html;charset=utf-8" -I "Subject:layer4 con ...