(X ** 2).sum()

is 

    np.dot(X.ravel(), X.ravel())

and similarly for `s`. Using `dot` is typically several times faster and doesn't create big temporary arrays.