Python 3.X error: valueerror: data type must provide an itemsize

1. Overview

The error occurs when multiplying the acquired data;
the reason for the error is that the data matrix is a string (read from the file);
the solution is to convert the data in batch, line by line, and convert the string data into floating point or integer.

2. Solutions

2.1 error code
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/2/21 14:16
# @Author  : Arrow and Bullet
# @FileName: error.py
# @Software: PyCharm


def loadDataSet(fileName):
    fr = open(fileName)
    dataMat = []
    for line in fr.readlines():
        currLineListStr = line.strip().split("\t") 
        dataMat.append(currLineListStr[0:numFeat])
    return dataMat

The data read out here are all strings, for example:

# [['1.000000', '0.067732'], ['1.000000', '0.427810']]

Then, when you multiply a matrix like this, the error data type must provide an itemsize will be reported.

2.2 correct code (solution)

1

def loadDataSet(fileName):
    fr = open(fileName)
    dataMat = []
    for line in fr.readlines():
        currLineListStr = line.strip().split("\t") 
        currLineListFloat = []
        for i in currLineListStr:  # Convert string data to floating point numbers line by line
            currLineListFloat.append(float(i))
        dataMat.append(currLineListFloat[0:numFeat])
    return dataMat

The data read out here are all floating-point numbers, for example:

# [[1.0, 0.067732], [1.0, 0.42781]]

Then when you multiply with such a matrix, there is no error.

I hope I can help you. If you have any questions, you can comment on them directly. If you like, you can praise them for more people to see. If you are not detailed enough, you can also say that I will reply in time.

Read More: