错误不能将序列乘以'float'类型的非int

时间:2018-05-28 14:22:57

标签: python numpy

我正在关注转学习的在线教程,我遇到了这个问题: 这是代码:

train_data = np.load(open('bottleneck_features_train.npy', 'rb'))
train_labels = np.array(
    [0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2))

validation_data = np.load(open('bottleneck_features_validation.npy'))
validation_labels = np.array(
    [0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2))

我收到此错误:

[0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2))
can't multiply sequence by non-int of type 'float'

1 个答案:

答案 0 :(得分:1)

问题是[0][1]是列表。列表的乘法运算符是重复它,只有乘以整数才有可能。所以[1]*2==[1,1]。如果这是期望的结果,那么您只需简单地对它们进行int[0] * int(nb_train_samples / 2) + [1] * int(nb_train_samples / 2))