A vectorised implementation (though perhaps not the simplest):
`C += np.repeat([labels], labels.size, axis=0).T == labels`
(this result `.triu()` should be the same as what you calculate.)