Pairwise distance and residual calculation
Let's start by setting
col2 = data[:,2]

to avoid some repetition. (If I knew what was in the data array then maybe I could pick a better name, but all I have to go on is what was in the post.)
Now here are two possible approaches to computing pwresidual:

pwresidual[i,j] is the result of an operation on col2[i] and col2[j]. You might recognize this as being similar to an outer product in mathematics — except that the operation is subtraction rather than multiplication.
But in NumPy every universal function has an &quot;outer&quot; counterpart, and np.subtract is a universal function, so all we need is:
 pwresidual = np.subtract.outer(col2, col2) ** 2


Alternatively, we can use NumPy's broadcasting mechanism, combined with np.newaxis to ensure every pair of items gets operated on:
 pwresidual = (col2[:,np.newaxis] - col2[np.newaxis,:]) ** 2