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:
- Python errors: valueerror: if using all scalar values, you must pass an index (four solutions)
- Extracting Data from XML (Using Python to Access Web Data)
- Python ValueError: only 2 non-keyword arguments accepted
- [Solved] python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.
- Python Error: ValueError: invalid literal for int() with base 16: ‘ ‘
- Python Valueerror: cannot index with vector containing Na / Nan values
- How to Solve Python Xlwt ValueError: More than 4094 XFs (styles)
- Pandas Error: ValueError: setting an array element with a sequence.
- Python: Panda scramble data
- [Solved] Backtrader_plotting Error: RuntimeError: Unexpected data type
- [leetcode] 295. Find Median from Data Stream Python
- [Solved] ValueError: only one element tensors can be converted to Python scalars
- Python Pandas Typeerror: invalid type comparison
- Pandas ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.an
- [Solved] YOLOv5 Model training error: TypeError: new(): invalid data type ‘str’
- Python: How to parses HTML, extracts data, and generates word documents
- How to Fix “HTTP error 403: forbidden” in Python 3. X
- [Solved] ERROR: No matching distribution found for torch-cluster==x.x.x
- Python TypeError: Unrecognized value type: <class ‘str‘>dateutil.parser._parser.ParserError: Unknow
- ValueError: Found array with dim 4. Estimator expected and ValueError: Expected 2D array, got 1D array i