当前位置: 移动技术网 > IT编程>脚本编程>Python > 【疑问】使用numpy时shape[0]获取维度长度时提示unsubscriptable

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

2020年08月10日  | 移动技术网IT编程  | 我要评论
在练习吴恩达第二周课后作业“猫的识别”时,下载的资源文件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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网