IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

这篇具有很好参考价值的文章主要介绍了IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

运行代码发现了IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed这个报错,

 后来去百度发现是这段代码出了问题

tp, fp, precision_all, strResults, f1_all, acc_all, mcc_all = calculate_performance(y_val, y_predict_cv, 'val')

因为定义的calculate_performance里面要求的数据是有两个索引,但是这些数据是一维的,所以才会报这个错误。后来学习发现了.reshape这个用法改变数组的形状。reshape(-1,1)是将一维数据在行上变化,而reshape(1,-1)是将一维数据在列上变化。这里-1是指未设定行数,程序随机分配,所以这里-1表示任一正整数,所以reshape(-1,1)表示(任意行,1列)

下面举个具体的例子:

import numpy as np
a=np.array([[1,2,3],[4,5,6]]) #定义一个两行三列的数组
print(a)
b=a.reshape(-1,1)    #将a数组改编为一行任意列的数组
print(b)
c=a.reshape(1,-1)   #将a数组改编为任意行一列的数组
print(c)

输出的对应结果为

[[1 2 3]
 [4 5 6]]
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]
[[1 2 3 4 5 6]]

以上方法完美的解决了这个错误,所以当我们遇到需要改变数组的形状时,可以巧妙的去运用reshape去改变数组的形状来解决这个问题。文章来源地址https://www.toymoban.com/news/detail-513810.html

到了这里,关于IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 使用go mod tidy命令出现go.mod file indicates go 1.21, but maximum supported version is 1.19,如何解决

    使用go mod tidy命令出现go.mod file indicates go 1.21, but maximum supported version is 1.19,如何解决

    使用git拉取代码Golang代码到本地后,利用VS Code打开项目后,看到go.mod报红,现象如下图所示:` 这个问题是当前使用的go版本与git clone拉取下来的项目的go.mod所用的go版本不一致导致的: 在项目中打开一个terminal,查看当前安装的go版本是否与go.mod的第二行的声明一致:如下图

    2024年02月06日
    浏览(18)
  • datax同步数据到ClickHouse时同步时间特别长,原因:Too many partitions for single INSERT block (more than 100).

    今天将 Hive 分区中数据同步到 ClickHouse 时,发现有的任务运行时间很短,但是有的任务运行时间特别长,看了一下数据量,发现有的接近千万条数据,但是几分钟就同步完了,但是有的才几万条数据,要同步半个多小时,还有的任务几百万条数据,甚至要同步四五个小时。开

    2023年04月08日
    浏览(5)
  • Python报错:IndexError: index 0 is out of bounds for axis 0 with size 0

    Python报错: 原因: 索引超出了列表的长度。 eg1: eg2: 解决方法: 检查是自己的索引错了, 还是数组长度定义错了。

    2024年02月12日
    浏览(12)
  • 【Python】成功解决IndexError: index 1256 is out of bounds for axis 0 with size 629

    【Python】成功解决IndexError: index 1256 is out of bounds for axis 0 with size 629

    【Python】成功解决IndexError: index 1256 is out of bounds for axis 0 with size 629 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分92+),分享更多

    2024年04月12日
    浏览(11)
  • 解决python报错:IndexError: only integers, slices (`:`)、 、、and integer ...are valid indices

    解决python报错:IndexError: only integers, slices (`:`)、 、、and integer ...are valid indices

    今天在编写python程序时:出现了以下报错: IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices 翻译过来的意思是:只有整型,切片,省略号或布尔类型的索引是有效的。 换句话说:我当前的索引不是这些支持类中的某一个。

    2024年02月12日
    浏览(7)
  • Result window is too large, from + size must be less than or equal to: [10000] but was

    Result window is too large, from + size must be less than or equal to: [10000] but was

    做分页查询,当分页达到一定量的时候,报如下错误: 原因分析: es对from + size的大小进行限制,必须小于等于10000。 方案一(有风险) 将max_result_window参数阈值调大,在业务中限制分页大小,使from+size=10000; 具体操作 改法一: 动态更改索引设置,为max_result_window参数赋值足够

    2024年02月16日
    浏览(8)
  • 【MySQL系列】Too many connections

    【MySQL系列】Too many connections

    💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学习,不断总结,共同进步,活到老学到老 导航 檀越剑指大厂系列:全面总

    2024年01月21日
    浏览(10)
  • Mysql “Too many connections“ 异常

    当Mysql 数据库抛出如下异常 Caused by: com.mysql.cj.exceptions.CJException: Data source rejected establishment of connection,  message from server: \\\"Too many connections\\\"     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:

    2024年02月12日
    浏览(9)
  • ‘dependencies.dependency.systemPath‘ for *:*:jar must specify an absolute path but is

    ‘dependencies.dependency.systemPath‘ for *:*:jar must specify an absolute path but is

    下面是一张模块与子模块的目录图  kingdeekingdee-bizpom.xml依赖本地lib/k3cloud-webapi-sdk8.0.4.jar,配置如下: pom.xml报错提示: \\\'dependencies.dependency.systemPath\\\' for kingdee:kingdee:jar must specify an absolute path but is ${project.parent.basedir}/lib/k3cloud-webapi-sdk8.0.4.jar 意思是说:systemPath 只认识绝对路径

    2024年02月08日
    浏览(6)
  • Error running ‘FileApp‘: Command line is too long. Shorten command line for

    Error running ‘FileApp‘: Command line is too long. Shorten command line for

    报错如下 Error running \\\'FileApp\\\': Command line is too long. Shorten command line for 解决方案如下: 打开运行配置  点击上面,默认是收起来的,点击下,下面选择标注的红色的, 重新运行,可以正常启动了

    2024年02月11日
    浏览(8)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包