为什么 "assert 211 == 211" 失败了?

Why is "assert 211 == 211" failing?

这失败了:

train_data = pd.concat([train_cancelled, train_not_cancelled]).as_matrix()

mat_col_size = int((num_days * 3) + 1)

assert isinstance(mat_col_size, int), "mat_col_size is not an int"
assert isinstance(train_data.shape[1], int), "train_data.shape[1] is not an int"

assert train_data.shape[1] == mat_col_size, \
    "Number of columns in train data must be 'num_fetaures + 1 = {0:d}' (label) but is '{0:1}'." \
        .format(mat_col_size, train_data.shape[1])

它将打印:

AssertionError: Number of columns in train data must be 'num_fetaures + 1 = 211' (label) but is '211'.

我的问题是:有多少不同的事情会出错并导致失败,因为我根本找不到问题或我的代码没有成为第三个的原因 assert!

您的格式字符串通过两次使用位置选择器 0 选择第一个参数两次。您实际上并没有看到 train_data.shape[1] 的值,它在两种情况下都打印了 mat_col_size 的值。我想你的意思是:

"Number of columns in train data must be 'num_fetaures + 1 = {0:d}' (label) but is '{1}'."

或者,假设它是 Py 2.7+,您可以简化为:

"Number of columns in train data must be 'num_fetaures + 1 = {}' (label) but is '{}'."

允许它自动将占位符与位置参数匹配,而无需明确指定数字(也没有理由指定 d 格式单元;它不是 printf,它将在它自己的)。