def discretize(vectors, copy=True, max_svd_restarts=30, n_iter_max=20, random_state=None):
 from scipy.sparse import csc_matrix
 from scipy.linalg import LinAlgError
 random_state = check_random_state(random_state)
 vectors = as_float_array(vectors, copy=copy)
 eps = np.finfo(float).eps
 (n_samples, n_components) = vectors.shape
 norm_ones = np.sqrt(n_samples)
 for i in range(vectors.shape[1]):
  vectors[:, i] = ((vectors[:, i] / norm(vectors[:, i])) * norm_ones)
  if (vectors[(0, i)] != 0):
   vectors[:, i] = (((-1) * vectors[:, i]) * np.sign(vectors[(0, i)]))
 vectors = (vectors / np.sqrt((vectors ** 2).sum(axis=1))[:, np.newaxis])
 svd_restarts = 0
 has_converged = False
 while ((svd_restarts < max_svd_restarts) and (not has_converged)):
  rotation = np.zeros((n_components, n_components))
  rotation[:, 0] = vectors[random_state.randint(n_samples), :].T
  c = np.zeros(n_samples)
  for j in range(1, n_components):
   c += np.abs(np.dot(vectors, rotation[:, (j - 1)]))
   rotation[:, j] = vectors[c.argmin(), :].T
  last_objective_value = 0.0
  n_iter = 0
  while (not has_converged):
   n_iter += 1
   t_discrete = np.dot(vectors, rotation)
   labels = t_discrete.argmax(axis=1)
   vectors_discrete = csc_matrix((np.ones(len(labels)), (np.arange(0, n_samples), labels)), shape=(n_samples, n_components))
   t_svd = (vectors_discrete.T * vectors)
   try:
    (U, S, Vh) = np.linalg.svd(t_svd)
    svd_restarts += 1
   except LinAlgError:
    print 'SVD did not converge, randomizing and trying again'
    break
   ncut_value = (2.0 * (n_samples - S.sum()))
   if ((abs((ncut_value - last_objective_value)) < eps) or (n_iter > n_iter_max)):
    has_converged = True
   else:
    last_objective_value = ncut_value
    rotation = np.dot(Vh.T, U.T)
 if (not has_converged):
  raise LinAlgError('SVD did not converge')
 return labels

def discretize ( vectors , copy = True , max_svd_restarts = 30 , n_iter_max = 20 , random_state = None ) : from scipy . sparse import csc_matrix from scipy . linalg import LinAlgError random_state = check_random_state ( random_state ) vectors = as_float_array ( vectors , copy = copy ) eps = np . finfo ( float ) . eps ( n_samples , n_components ) = vectors . shape norm_ones = np . sqrt ( n_samples ) for i in range ( vectors . shape [ 1 ] ) : vectors [ : , i ] = ( ( vectors [ : , i ] / norm ( vectors [ : , i ] ) ) * norm_ones ) if ( vectors [ ( 0 , i ) ] != 0 ) : vectors [ : , i ] = ( ( ( - 1 ) * vectors [ : , i ] ) * np . sign ( vectors [ ( 0 , i ) ] ) ) vectors = ( vectors / np . sqrt ( ( vectors ** 2 ) . sum ( axis = 1 ) ) [ : , np . newaxis ] ) svd_restarts = 0 has_converged = False while ( ( svd_restarts < max_svd_restarts ) and ( not has_converged ) ) : rotation = np . zeros ( ( n_components , n_components ) ) rotation [ : , 0 ] = vectors [ random_state . randint ( n_samples ) , : ] . T c = np . zeros ( n_samples ) for j in range ( 1 , n_components ) : c += np . abs ( np . dot ( vectors , rotation [ : , ( j - 1 ) ] ) ) rotation [ : , j ] = vectors [ c . argmin ( ) , : ] . T last_objective_value = 0.0 n_iter = 0 while ( not has_converged ) : n_iter += 1 t_discrete = np . dot ( vectors , rotation ) labels = t_discrete . argmax ( axis = 1 ) vectors_discrete = csc_matrix ( ( np . ones ( len ( labels ) ) , ( np . arange ( 0 , n_samples ) , labels ) ) , shape = ( n_samples , n_components ) ) t_svd = ( vectors_discrete . T * vectors ) try : ( U , S , Vh ) = np . linalg . svd ( t_svd ) svd_restarts += 1 except LinAlgError : print 'SVD did not converge, randomizing and trying again' break ncut_value = ( 2.0 * ( n_samples - S . sum ( ) ) ) if ( ( abs ( ( ncut_value - last_objective_value ) ) < eps ) or ( n_iter > n_iter_max ) ) : has_converged = True else : last_objective_value = ncut_value rotation = np . dot ( Vh . T , U . T ) if ( not has_converged ) : raise LinAlgError ( 'SVD did not converge' ) return labels
discretize x parameters bins : int .