numpy的loadtxt遇到问题:ValueError: Wrong number of columns at line 2 原因调查

目录

一、问题描述

二、loadtxt函数

三、错误分析 

四、解决办法 


本文讲解在调用numpy函数loadtxt遇到的问题:ValueError: Wrong number of columns at line 2 ,讲解出现问题的原因以及如何解决这个问题。

一、问题描述

在使用python做验证码识别的过程中,在调用numpy的loadtxt函数时发生如下错误

Traceback (most recent call last):
  File "D:\My Documents\test_jpg\ok.py", line 362, in <module>
    validation()
  File "D:\My Documents\test_jpg\ok.py", line 350, in validation
    dataset = load_data()
  File "D:\My Documents\test_jpg\ok.py", line 343, in load_data
    dataset = np.loadtxt(u'./train_data.txt', delimiter=',')
  File "C:\Python34\lib\site-packages\numpy\lib\npyio.py", line 1021, in loadtxt
    % line_num)
ValueError: Wrong number of columns at line 2

二、loadtxt函数

首先讲解下这个loadtxt 函数,loadtxt 是 Python 中 numpy 库提供的一个非常实用的函数,主要用于从文本文件中加载数据并将其转换为 numpy 数组。大概就是把文本文件(*.txt)读入并以矩阵或向量的形式输出。loadtxt的基本语法如下所示。

numpy.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None)

loadtxt函数的参数说明如下所示。

  • fname:表示文件、文件名或生成器,可以是文件的路径,也可以是一个文件对象。
  • dtype:指定数据类型,默认是 float 类型。
  • comments:用于标识注释行的字符,默认是 #。
  • delimiter:数据分隔符,默认是任意的空白字符(如空格、制表符等)。
  • converters:用于对特定列数据进行转换的函数字典。
  • skiprows:需要跳过的行数,通常用于跳过文件的标题行,默认值是 0。
  • usecols:指定要读取的列,例如 (0, 2) 表示只读取第 0 列和第 2 列。
  • unpack:若为 True,则会对数组进行解包,将不同列的数据分别存储到不同的变量中,默认是 False。
  • ndmin:指定返回数组的最小维数,默认是 0。
  • encoding:文件编码方式,默认是 'bytes'。
  • max_rows:指定要读取的最大行数,默认是 None,即读取全量数据。

三、错误分析 

我在SVM训练识别图片验证码时使用loadtxt函数读入train data来读取训练数据,调用参数配置如下所示。

  • delimiter表示用什么来分隔列,在默认情况下是通过空格来分割列的,我这里使用的是','
  • skiprows,作用是跳过头行,
  • usecoles,作用是跳过几列来读取
  • unpack,这个参数则是  正常情况下该返回的是二维矩阵,若设置了unpack=True将返回各列

我的使用方法是在将验证码分隔后,将特征写入到traindata.txt中,然后训练时使用loadtxt读入数据,但是报错信息如下所示。

ValueError: Wrong number of columns at line 2

调查后证实原因如下:

因为我的验证码分割后的图片大小不一致,特征值写入后的文件每行的长度不一样

这样loadtxt读取到的二维矩阵就出现了异常

四、解决办法 

将验证码分割后的图片修改为大小一致,即可解决报错。

(mast3r-slam) root@autodl-container-89914fa1bb-a01dbef1:~/MASt3R-SLAM# bash ./scripts/eval_euroc.sh ~/autodl-fs/datasets/euroc/MH_05_difficult/ {'use_calib': True, 'single_thread': True, 'dataset': {'subsample': 2, 'img_downsample': 1, 'center_principle_point': True}, 'matching': {'max_iter': 10, 'lambda_init': 1e-08, 'convergence_thresh': 1e-06, 'dist_thresh': 0.1, 'radius': 3, 'dilation_max': 5}, 'tracking': {'min_match_frac': 0.05, 'max_iters': 50, 'C_conf': 0.0, 'Q_conf': 1.5, 'rel_error': 0.001, 'delta_norm': 0.001, 'huber': 1.345, 'match_frac_thresh': 0.333, 'sigma_ray': 0.003, 'sigma_dist': 10.0, 'sigma_pixel': 1.0, 'sigma_depth': 10.0, 'sigma_point': 0.05, 'pixel_border': -10, 'depth_eps': 1e-06, 'filtering_mode': 'weighted_pointmap', 'filtering_score': 'median'}, 'local_opt': {'pin': 1, 'window_size': 1000000.0, 'C_conf': 0.0, 'Q_conf': 1.5, 'min_match_frac': 0.1, 'pixel_border': -10, 'depth_eps': 1e-06, 'max_iters': 10, 'sigma_ray': 0.003, 'sigma_dist': 10.0, 'sigma_pixel': 1.0, 'sigma_depth': 10.0, 'sigma_point': 0.05, 'delta_norm': 1e-08, 'use_cuda': True}, 'retrieval': {'k': 3, 'min_thresh': 0.005}, 'reloc': {'min_match_frac': 0.3, 'strict': True}, 'inherit': 'config/base.yaml'} Traceback (most recent call last): File "/root/MASt3R-SLAM/main.py", line 170, in <module> dataset = load_dataset(args.dataset) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/MASt3R-SLAM/mast3r_slam/dataloader.py", line 325, in load_dataset return EurocDataset(dataset_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/MASt3R-SLAM/mast3r_slam/dataloader.py", line 100, in __init__ tstamp_rgb = np.loadtxt(rgb_list, delimiter=",", dtype=np.unicode_, skiprows=0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/miniconda3/envs/mast3r-slam/lib/python3.11/site-packages/numpy/lib/npyio.py", line 1373, in loadtxt arr = _read(fname, dtype=dtype, comment=comment, delimiter=delimiter, ^^^^^^^^^^^^^^^^
最新发布
04-01
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mooyuan天天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值