欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【疑问】使用numpy时shape[0]获取维度长度时提示unsubscriptable

程序员文章站 2022-07-03 14:50:29
在练习吴恩达第二周课后作业“猫的识别”时,下载的资源文件lr_utils.py中“train_set_y_orig.shape[0]”会提示“Value 'train_set_y_orig.shape' is unsubscriptablepylint(unsubscriptable-object)”,不知道是什么原因,大家是否遇到?lr_utils.py文件代码如下:importnumpyasnpimporth5pydefload_dataset():tr......

在练习吴恩达第二周课后作业“猫的识别”时,下载的资源文件lr_utils.py中“ train_set_y_orig.shape[0]”会提示“Value 'train_set_y_orig.shape' is unsubscriptablepylint(unsubscriptable-object)”,不知道是什么原因,大家是否遇到?

lr_utils.py文件代码如下:

import numpy as np

import h5py  



def load_dataset():

    train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")

    train_set_x_orig = np.array(train_dataset["train_set_x"[:]) # your train set features

    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels



    test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")

    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features

    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels



    classes = np.array(test_dataset["list_classes"][:]) # the list of classes

    

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))

    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

自己又随便写了一段代码验证了一下:

import numpy as np

a=np.zeros((3,5))

print(a.shape)

b=a.shape[0]

print(b)

print(type(a.shape))

仍然会报错,提示:“Value 'a.shape' is unsubscriptablepylint(unsubscriptable-object)”

但是代码可以运行,结果如下:

(3, 5)
3
<class 'tuple'>

不知道问题出在哪里?

本文地址:https://blog.csdn.net/vivaux/article/details/107895551

相关标签: 机器学习 python