コード例 #1
0
ファイル: hw1-1.py プロジェクト: origamitoaster/sta141c
def gradient_csr(X, y, w):
    X_trans = csr_matrix.transpose(X)
    first = csr_matrix.transpose(np.dot(X_trans, X))
    second = csr_matrix.transpose(np.dot(X_trans, y))
    third = csr_matrix.transpose(np.dot(first, w))
    final = np.add(np.subtract(third, second), w)
    return final[:,1]
コード例 #2
0
ファイル: matrix.py プロジェクト: wonchang24/OBOE
def set_mtx_triad(mtx1, mtx2):
    mtx = [
        mtx1 * mtx2, mtx1 * csr_matrix.transpose(mtx2),
        (csr_matrix.transpose(mtx1) * mtx2).tocsr(),
        (csr_matrix.transpose(mtx1) * csr_matrix.transpose(mtx2)).tocsr()
    ]
    return mtx
コード例 #3
0
def __solve_nodal_circuit_sparse(Ac, Yb, E):
    '''
	Based on Chua, 1975
	Solve circuit based on nodal  analysis\n
	Ac: nodal incidence matrix (with reference node)\n
	Yb: branch admitance matrix\n
	E: branch source vector
	'''
    if isinstance(Ac, csr_matrix) and isinstance(
            Yb, csr_matrix) and isinstance(E, csr_matrix):
        A = matrix_aux.delete_sparse_mask(Ac, [0], 0)
        del (Ac)
        At = csr_matrix.transpose(A)
        Yn = A.dot(Yb).dot(At)
        Jn = -A.dot(Yb).dot(E)
        del (A)
        vn = spsolve(Yn, Jn)
        del (Yn)
        del (Jn)
        vn = vn.T
        v_b = At.dot(vn)
        #		v_b=csr_matrix.dot(At,vn)
        v = (v_b + E.T).T
        i = Yb.dot(v)
    return i
コード例 #4
0
    def transform(self, X, **transform_params):
        ''' Just get length over the whole tweet string '''
        if len(X) == len(self.fntrain):
            values = csr_matrix(self.fntrain)
        elif len(X) == len(self.fntest):
            values = csr_matrix(self.fntest)

        return csr_matrix.transpose(values)
コード例 #5
0
    def transform(self, X, y=None):
        """Compute Laplacians of complex.

        Compute Laplacians starting from a Graph Object up to certain
        order applying the formula from the boundary matrices

        Parameters
        ----------
         X : dictionary
            Dictionary containing information on the Clique Complex of which
            computing the boundary matrices.

        y : None
            There is no need for a target in a transformer, yet the pipeline
            API requires this parameter.

        Returns
        -------
        laplacians : list
            List containing the Laplacian matrices for all orders specified
            in tuple 'orders'.

        """
        check_is_fitted(self)
        laplacians = list()

        cb = CreateBoundaryMatrices().fit(X, self.bound_orders_)
        boundaries = cb.transform(X)

        for x in self.orders_:
            # if maximal order, don't use the x+1 boundary,
            # if minimal don't use 0 boundaries
            if x > 0:
                lap = csr_matrix.transpose(boundaries[self.order_id_[x]]) *\
                      boundaries[self.order_id_[x]]
                if x < len(X)-1:
                    lap += boundaries[self.order_id_[x+1]] *\
                        csr_matrix.transpose(boundaries[self.order_id_[x+1]])
            else:
                lap = boundaries[self.order_id_[x+1]] *\
                      csr_matrix.transpose(boundaries[self.order_id_[x+1]])

            laplacians.append(lap)

        return laplacians
コード例 #6
0
def distEuclid(mtrx):
    test = (mtrx).dot(csr_matrix.transpose(mtrx))
    data = test.diagonal().reshape(
        (1, test.shape[0])).repeat(2 * mtrx.shape[0] + 1, axis=0)
    offsets = np.arange(-mtrx.shape[0], mtrx.shape[0] + 1)
    test_dia = dia_matrix((data, offsets), shape=test.shape)

    data = None
    offsets = None
    test = (test_dia + test_dia.transpose() - 2 * test).sqrt()

    return test
コード例 #7
0
def eq1(Wcm, Wuu, Wl, H, T, ak, wf):
    # sku = 0.05
    # suu = 0.01
    # sl = 1
    # lamd = 100

    sku = 0.05
    suu = 0.01
    sl = 1
    lamd = 100

    X = csr.sum(Wcm, axis=1)  #numpy matrix
    Dcm = diags(X.A.ravel()).tocsr()

    X = csr.sum(Wuu, axis=1)  #numpy matrix
    Duu = diags(X.A.ravel()).tocsr()

    X = csr.sum(Wl, axis=1)  #numpy matrix
    Dl = diags(X.A.ravel()).tocsr()

    Lifm = csr.transpose(Dcm -
                         Wcm).dot(Dcm -
                                  Wcm) + suu * (Duu - Wuu) + sl * (Dl - Wl)
    # Lifm = suu*(Duu-Wuu) + sl*(Dl-Wl)

    A = Lifm + lamd * T + sku * H
    b = (lamd * T + sku * H).dot(wf)
    # print(csr.sum(b))
    M = diags(A.diagonal())
    # print(A.shape)
    # print(b.shape)
    alpha = cg(A,
               b,
               x0=wf,
               tol=1e-05,
               maxiter=100,
               M=None,
               callback=None,
               atol=None)
    # alpha = spsolve(A, b)
    # print(alpha)
    # print(type(alpha[0]))
    return alpha[0] * 255
コード例 #8
0
ファイル: Solver.py プロジェクト: nunesanderson/PhD_codes
def __solve_nodal_circuit_sparse(Ac,Yb,E):
	'''
	Based on Chua, 1975
	Solve circuit based on nodal  analysis\n
	Ac: nodal incidence matrix (with reference node)\n
	Yb: branch admitance matrix\n
	E: branch source vector
	'''
	if isinstance(Ac,csr_matrix) and isinstance(Yb,csr_matrix) and isinstance(E,csr_matrix):
		A=matrix_aux.delete_sparse_mask(Ac,[0],0)
		At=csr_matrix.transpose(A)
		Yn=A.dot(Yb).dot(At)
#		Yn=(A*Yb)*At
		Jn=-A.dot(Yb).dot(E)
#		Jn=-csr_matrix.dot(csr_matrix.dot(A,Yb),E)
		vn=spsolve(Yn, Jn)
		vn=vn.T
		v_b=At.dot(vn)
#		v_b=csr_matrix.dot(At,vn)
		v=(v_b+E.T).T
		i=Yb.dot(v)
	return i
コード例 #9
0
def global_edgerank(node1, node2, w_adj_matrix, d=0.5, w=1, depth=2):
    """
    The single highest-scoring feature found was termed
    EdgeRank and had an AUC of 0.926. This feature was
    a rooted PageRank (see [12]) over a weighted undirected
    adjacency matrix of the graph
    :param node1:
    :param node2:
    :param w_adj_matrix: weighted adjacency matrix
    :param d: random exploration probability
    :param w: weight to
    :param depth: how many times to iterate
    :return:
    """
    x = np.zeros(w_adj_matrix.shape[0])
    x[node1] = 1
    for i in range(depth):
        x = (1 - d) * x + d * (w_adj_matrix +
                               csr_matrix.transpose(w_adj_matrix) * w) @ x
    print("Edgerank is successfully calculated with matrix.shape =",
          w_adj_matrix.shape)
    return x[node2]
コード例 #10
0
    def damped_mass_spring(self, numOfMasses):
        'a large scale index-3 damped mass spring system, dimension = 2 * numOfMasses + 1'

        # This benchmark is from paper: Balanced Truncation Model Reduction for Large-Scale Systems
        # in Descriptor From.
        # It is also presented in the thesis: Index-aware Model Order Reduction Methods
        # Section 2.4.3

        isinstance(numOfMasses, int)
        assert numOfMasses > 0

        g = numOfMasses
        n = 2 * g + 1  # system's dimension

        # Assume we have a uniformed damped mass-spring
        # system's parameters :
        # m1 = m2 = ... = mg = 100
        # k1 = k2 = ... = k_(g-1)  = h2 = h3 = ... = h_(g-1) = k, h1 = hg = 2 * k
        # d1 = d2 = ... = d_(g-1) = l2 = l3 = ... l_(g-1) = l_g = d, l1 = lg =
        # 2 * d
        m = 100  # mass
        k = 2  # stiffness
        d = 5  # damping

        # damping and sfiffness matrices (tridiagonal sparse matrices)
        K = lil_matrix((g, g), dtype=float)
        D = lil_matrix((g, g), dtype=float)
        for i in xrange(0, g):
            K[i, i] = -3 * k
            D[i, i] = -3 * d
            if i > 0:
                K[i, i - 1] = 2 * k
                D[i, i - 1] = 2 * d
            if i < g - 2:
                K[i, i + 1] = k
                D[i, i + 1] = d

        K = K.tocsr()
        D = D.tocsr()

        # mass matrix diagonal sparse matrices
        M = lil_matrix((g, g), dtype=float)
        for i in xrange(0, g):
            M[i, i] = m
        M = M.tocsr()

        # Constraint matrix: this means that:
        # The first and the last masses move a same amount of distance at all
        # time
        G = lil_matrix((1, g), dtype=float)
        G[0, 0] = 1
        G[0, g - 1] = -1
        G = G.tocsr()

        # force matrix : B2 = e1, at t=0, the first mass is pushed with a force
        # 1 <= f <= 1.1
        B2 = lil_matrix((g, 1), dtype=float)
        B2[0, 0] = 1
        B2 = B2.tocsr()

        # identity matrix of g-dimension
        I = lil_matrix((g, g), dtype=float)
        for i in xrange(0, g):
            I[i, i] = 1
        I = I.tocsr()

        # zero matrix of g-dimension and zero vector
        Z = csr_matrix((g, g), dtype=float)
        zero_vec = csr_matrix((g, 1), dtype=float)

        # Constructing system's matrices: E, A, B, C

        E1 = hstack([I, Z, zero_vec])
        E2 = hstack([Z, M, zero_vec])
        E3 = csr_matrix((1, n), dtype=float)
        E = vstack([E1, E2, E3])
        self.E = E.tocsc()

        A1 = hstack([Z, I, zero_vec])
        A2 = hstack([K, D, csr_matrix.transpose(-G)])
        A3 = hstack([
            G,
            csr_matrix.transpose(zero_vec),
            csr_matrix((1, 1), dtype=float)
        ])
        A = vstack([A1, A2, A3])
        self.A = A.tocsc()

        B = vstack([zero_vec, B2, csr_matrix((1, 1), dtype=float)])
        self.B = B.tocsc()

        C = lil_matrix((2, n), dtype=float)
        # we are interested in the position and velocity of the middle mass/ first mass
        p = math.ceil(g / 2)
        C[0, p] = 1  # position of the middle mass
        C[1, g + p] = 1  # velocity of the middle mass
        # C[0, 0] = 1    # position of the first mass
        # C[1, g] = 1    # velocity of the first mass
        self.C = C.tocsc()

        return self.E, self.A, self.B, self.C
コード例 #11
0
ファイル: vp_RR_He (copy).py プロジェクト: vacary/inria-admm
def vp_RR_He(problem_data, rho_method):

	######################
	## IMPORT LIBRARIES ##
	######################

	#Math libraries
	import numpy as np
	from scipy.sparse import csc_matrix
	from scipy.sparse import csr_matrix
	from scipy.sparse import linalg

	#Timing
	import time

	#Import data
	from Data.read_fclib import *

	#Plot residuals
	from Solver.ADMM_iteration.Numerics.plot import *

	#Initial penalty parameter
	import Solver.Rho.Optimal

	#Max iterations and kind of tolerance
	from Solver.Tolerance.iter_totaltolerance import *

	#Acceleration
	from Solver.Acceleration.plusr_vp import *

	#Varying penalty parameter
	from Solver.Rho.Varying.He import *

	#b = Es matrix
	from Data.Es_matrix import *

	#Db = DEs matrix (derivative)
	from Data.DEs_matrix import *

	#Projection onto second order cone
	from Solver.ADMM_iteration.Numerics.projection import *

	##################################
	############# REQUIRE ############
	##################################

	start = time.clock()
	problem = hdf5_file(problem_data)

	M = problem.M.tocsc()
	f = problem.f
	A = csc_matrix.transpose(problem.H.tocsc())
	A_T = csr_matrix.transpose(A)
	w = problem.w
	mu = problem.mu

	#Dimensions (normal,tangential,tangential)
	dim1 = 3 
	dim2 = np.shape(w)[0]

	#Problem size
	n = np.shape(M)[0]
	p = np.shape(w)[0]

	b = [1/linalg.norm(A,'fro') * Es_matrix(w,mu,np.zeros([p,])) / np.linalg.norm(Es_matrix(w,mu,np.ones([p,])))]

	#################################
	############# SET-UP ############
	#################################

	#Set-up of vectors
	v = [np.zeros([n,])]
	u = [np.zeros([p,])] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
	u_hat = [np.zeros([p,])] #u_hat[0] #in the notation of the paper this used with a underline
	xi = [np.zeros([p,])] 
	xi_hat = [np.zeros([p,])]
	r = [np.zeros([p,])] #primal residual
	s = [np.zeros([p,])] #dual residual
	r_norm = [0]
	s_norm = [0]
	tau = [1] #over-relaxation
	e = [] #restart
	rho = []

	#Optimal penalty parameter
	rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
	rh = eval(rho_string)
	rho.append(rh) #rho[0]

	################
	## ITERATIONS ##
	################

	for k in range(MAXITER):
		print k
	
		#Super LU factorization of M + rho * dot(M_T,M)
		if k == 0:
			P = M + rho[k] * csc_matrix.dot(A_T,A)
			LU = linalg.splu(P)

		else:		
			DE = DEs_matrix(w, mu, Av + w, A.toarray())			
			P = M + rho[k] * csc_matrix.dot(A_T + DE,A)
			LU = linalg.splu(P)

		################
		## v - update ##
		################

		if k==0:
			RHS = -f + rho[k] * csc_matrix.dot(A_T, -w - b[k] - xi_hat[k] + u_hat[k])
			v.append(LU.solve(RHS)) #v[k+1]

		else:
			RHS = -f + rho[k] * csc_matrix.dot(A_T + DE, -w - b[k] - xi_hat[k] + u_hat[k])
			v.append(LU.solve(RHS)) #v[k+1]	

		################
		## u - update ##
		################
		Av = csr_matrix.dot(A,v[k+1])
		vector = Av + xi_hat[k] + w + b[k]
		u.append(projection(vector,mu,dim1,dim2)) #u[k+1]

		########################
		## residuals - update ##
		########################
		s.append(rho[k] * csc_matrix.dot(A_T,(u[k+1]-u_hat[k]))) #s[k+1]
		r.append(Av - u[k+1] + w + b[k]) #r[k+1]

		#################
		## xi - update ##
		#################
		ratio = rho[k-1]/rho[k] #update of dual scaled variable with new rho
		xi.append(ratio*(xi_hat[k] + r[k+1])) #xi[k+1]

		#b update
		b.append(Es_matrix(w,mu,Av + w))

		####################
		## stop criterion ##
		####################
		pri_evalf = np.amax(np.array([np.linalg.norm(csr_matrix.dot(A,v[k+1])),np.linalg.norm(u[k+1]),np.linalg.norm(w + b[k+1])]))
		eps_pri = np.sqrt(p)*ABSTOL + RELTOL*pri_evalf

		dual_evalf = np.linalg.norm(rho[k] * csc_matrix.dot(A_T,xi[k+1]))
		eps_dual = np.sqrt(n)*ABSTOL + RELTOL*dual_evalf

		r_norm.append(np.linalg.norm(r[k+1]))
		s_norm.append(np.linalg.norm(s[k+1]))
		if r_norm[k+1]<=eps_pri and s_norm[k+1]<=eps_dual:
			orthogonal = np.dot(u[-1],rho[-2]*xi[-1])
			print orthogonal
			break

		#b_per_contact_j1 = np.split(b[k+1],dim2/dim1)
		#b_per_contact_j0 = np.split(b[k],dim2/dim1)
		#count = 0
		#for j in range(dim2/dim1):
		#	if np.linalg.norm(b_per_contact_j1[j] - b_per_contact_j0[j]) / np.linalg.norm(b_per_contact_j0[j]) > 1e-03:
		#		count += 1
		#if count < 1:		
		#	break

		###################################
		## accelerated ADMM with restart ##
		###################################
		plusr(tau,u,u_hat,xi,xi_hat,k,e,rho,ratio)

		################################
		## penalty parameter - update ##
		################################
		rho.append(penalty(rho[k],r_norm[k+1],s_norm[k+1]))	
	
		#end rutine

	end = time.clock()

	####################
	## REPORTING DATA ##
	####################
	#print b[-1]
	#print np.linalg.norm(b[-1])
	#plotit(r,s,start,end,'With acceleration / Without restarting for '+problem_data+' for rho: '+rho_method)
	plotit(r,s,start,end,'Internal update with vp_RR_He (Di Cairano)')

	time = end - start
	print 'Total time: ', time
	return time
コード例 #12
0
            artist = song['artist_name']
            artist_name.append(artist)
            unique_artists.add(artist)
        playlist_vs_artists.append(collections.Counter(artist_name))

    print("Slice : " + str(i) + " - Parsed")
    start = end + 1

challenge_artists = set()
slice = json.load(open('challenge_set.json'))
for playlist in slice['playlists']:
    for song in playlist['tracks']:
        challenge_artists.add(song['artist_name'])

v = DictVectorizer(sparse=True)
X = csr_matrix.transpose(v.fit_transform(playlist_vs_artists))
print 'Vectorized!'
neighbor = NearestNeighbors(n_neighbors=21, metric='cosine')
neighbor.fit(X)
print X

unique_artists = list(unique_artists)
unique_artists.sort()
print(len(unique_artists))
print 'Training Done!'

nearest_artists = {}
for artist_index in range(len(unique_artists)):
    if unique_artists[artist_index] in challenge_artists:
        neighbors = neighbor.kneighbors([X[artist_index]],
                                        return_distance=False)
コード例 #13
0
print("Overall accuracy: %.2f%% (+/- %.2f%%)" %
      (np.mean(cvscores), np.std(cvscores)))

print('Sanity checks on cvpreds:')
# print('type(cvpreds):', type(cvpreds))
# print('len(cvpreds):', len(cvpreds))
print('Same length as Y ?', len(cvpreds) == len(Y))
print('Any dummy 0.0 still in cvpreds?', 0.0 in cvpreds)

Yguess = cvpreds
# Removing predictions for the Espresso samples using idx_espresso
#print('len(Yguess) including Espresso:', len(Yguess))
#Yguess_final = np.delete(Yguess, idx_espresso)
#print('len(Yguess_final) without Espresso:', len(Yguess_final))

# Turning to scipy
print('Turning to scipy:')
#Ycnn = csr_matrix.transpose(csr_matrix(Yguess_final))
Ycnn = csr_matrix.transpose(csr_matrix(Yguess))
print(type(Ycnn))
print(Ycnn.shape)

# Pickling the predictions
#save_to = open('NEW-train-cnn-predict-FB-ensamble.p', 'wb') # FB cross predictions
#save_to = open('NEW-train-cnn-predict-TW-ensamble.p', 'wb')
#pickle.dump(Ycnn, save_to)
#save_to.close()

print('Done')
コード例 #14
0
def project_selection_matrix(
    selections, 
    how, 
    transaction_id='transaction_id', 
    fact_id='fact_id', 
    norm=True, 
    remove_loops=True, 
    symmetrize=True
):
    '''
    Description: This function uses the terminology of the compsoc unified data model 
        according to which "transactions select facts"; these selections are stored in a 
        selection matrics; the function projects a selection matrix to a transaction 
        similarity matrix or a fact co-selection matrix; computes fact attributes; 
        computes cumulative co-selection fractions for matrix filtering.
    
    Inputs:
        selections: Dataframe containing the selection matrix indices and data; must 
            contain a 'weight' column that contains the cell weights.
        how: String that specifies which projection is to be made; must be either 
            'transactions' or 'facts'; if 'transactions', then the matrix of transactions 
            coupled by facts will be created; if 'facts', then the matrix of facts 
            coupled by transactions will be created.
        transaction_id: Name of the column of the dataframe ``selections`` that holds the 
            identifiers of the transactions selecting facts.
        fact_id: Name of the column of the dataframe ``selections`` that holds the 
            identifiers of the facts getting selected in transactions.
        norm: Boolean parameter specifying if matrix normalization should be performed.
        remove_loops: Boolean parameter specifying if the matrix diagonal should be 
            removed; if False, loops will be included in computing cumulative 
            co-selection fractions.
        symmetrize: Boolean parameter specifying if the lower portion of the matrix 
            should be removed.
    
    Output: A dataframe containing the projected matrices (enriched by cumulative 
        fractions in the case of a normalized projection to the fact mode); a dataframe 
        containing matrix-based attributes of transactions or facts (depending on the 
        type of projection)
    '''
    
    # function
    def get_unique(s):
        l = s.unique().tolist()
        return {identifier: index for index, identifier in enumerate(l)}
    
    # map identifiers of transactions and facts to unique integers
    import pandas as pd
    d_transactions_indices = get_unique(selections[transaction_id])
    d_facts_indices = get_unique(selections[fact_id])
    
    # construct selection matrix
    rows = [d_transactions_indices[transaction_id] for transaction_id in selections[transaction_id].values]
    columns = [d_facts_indices[fact_id] for fact_id in selections[fact_id].values]
    cells = selections['weight'].tolist()
    from scipy.sparse import csr_matrix, coo_matrix, triu
    G = coo_matrix((cells, (rows, columns))).tocsr()
    GT = csr_matrix.transpose(G)
    from sklearn.preprocessing import normalize
    GN = normalize(G, norm='l1', axis=1)
    
    # project selection matrix ...
    import numpy as np
    
    # ... to transaction similarity matrix
    if how == 'transactions':
        if norm == True:
            GNT = csr_matrix.transpose(GN)
            H = GN*GNT
        else:
            H = G*GT
        
        # derive transaction attributes dataframe
        H_nodiag = H.tolil()
        H_nodiag.setdiag(values=0)
        
        k = pd.Series([len(i) for i in H_nodiag.data.tolist()])
        w = pd.Series(np.array(H.diagonal()))
        if norm == True:
            w = (1/w).round(4)
        else:
            w = w.round(4)
        
        d_indices_transactions = {index: identifier for identifier, index in d_transactions_indices.items()}
        
        transaction_attributes = pd.concat([pd.Series(d_indices_transactions), k, w], axis=1)
        transaction_attributes.columns = [transaction_id, 'degree', 'weight']
        
        # construct similarities dataframe
        if remove_loops == True:
            H = H.tolil()
            H.setdiag(0)

        if symmetrize == True:
            H = triu(H.tocoo()).tocsr()
        else:
            H = H.tocsr()
        
        transaction_id_from = [d_indices_transactions[index] for index in H.nonzero()[0].tolist()]
        transaction_id_to = [d_indices_transactions[index] for index in H.nonzero()[1].tolist()]
        weight = H.data.tolist()
        
        similarities = pd.concat([pd.Series(transaction_id_from), pd.Series(transaction_id_to), pd.Series(weight)], axis=1)
        similarities.columns = [transaction_id+'_from', transaction_id+'_to', 'similarity']
        
        return similarities, transaction_attributes
    
    # ... to fact co-selection matrix
    if how == 'facts':
        if norm == True:
            I = GT*GN
        else:
            I = GT*G
        
        # derive fact attributes dataframe
        I_nodiag = I.tolil()
        I_nodiag.setdiag(values=0)
        
        k = pd.Series([len(i) for i in I_nodiag.data.tolist()])
        
        d_indices_facts = {index: identifier for identifier, index in d_facts_indices.items()}
        
        if norm == True:
            w = pd.Series(np.squeeze(np.array(I.sum(axis=1)))).round(4)
            a = pd.Series(np.array(I.diagonal())).round(4)
            e = (1-a/w).round(4)
            s = (k/w).round(4)
            
            fact_attributes = pd.concat([pd.Series(d_indices_facts), k, w, a, e, s], axis=1)
            fact_attributes.columns = [fact_id, 'degree', 'weight', 'autocatalysis', 'embeddedness', 'sociability']
            
        else:
            fact_attributes = pd.concat([pd.Series(d_indices_facts), k], axis=1)
            fact_attributes.columns = [fact_id, 'degree']
        
        # construct co-selections dataframe with cumulative co-selection fractions
        if remove_loops == True:
            I = I.tolil()
            I.setdiag(0)
        
        if symmetrize == True:
            I = triu(I.tocoo()).tocsr()
        else:
            I = I.tocsr()
                
        fact_id_from = [d_indices_facts[index] for index in I.nonzero()[0].tolist()]
        fact_id_to = [d_indices_facts[index] for index in I.nonzero()[1].tolist()]
        weight = I.data.tolist()
        
        co_selections = pd.concat([pd.Series(fact_id_from), pd.Series(fact_id_to), pd.Series(weight)], axis=1)
        co_selections.columns = [fact_id+'_from', fact_id+'_to', 'weight']
        
        co_selections_cumfrac = co_selections.copy()
        co_selections_cumfrac.index = co_selections_cumfrac.weight
        co_selections_cumfrac = co_selections_cumfrac['weight'].groupby(co_selections_cumfrac.index).sum()
        co_selections_cumfrac = co_selections_cumfrac.sort_index(ascending=False)
        co_selections_cumfrac = co_selections_cumfrac.cumsum()/sum(co_selections_cumfrac)
        co_selections_cumfrac = co_selections_cumfrac.round(4)
        co_selections_cumfrac.rename('cumfrac', inplace=True)
        
        co_selections = pd.merge(left=co_selections, right=co_selections_cumfrac, left_on='weight', right_on=co_selections_cumfrac.index)
        
        return co_selections, fact_attributes
コード例 #15
0
def meshSaliency(blade_verts, blade_faces, a=0.15):

    blade_faces = blade_faces.astype(int)

    numVerts = len(blade_verts)
    numFaces = len(blade_faces)

    #to store the angles sum on vertices
    sum_angles = np.zeros([numVerts, 1])

    #to store the facet areas
    area = np.zeros([numFaces, 1])

    #for numerical computation stability
    epsilon = 1e-10

    blade_verts = blade_verts.T
    blade_faces = blade_faces.T

    for i in [1, 2, 3]:
        i1 = np.mod(i - 1, 3)
        i2 = np.mod(i, 3)
        i3 = np.mod(i + 1, 3)

        #    pp = vertices(:,faces(i2,:)) - vertices(:,faces(i1,:));
        pp = blade_verts[:,
                         blade_faces[i2, :]] - blade_verts[:,
                                                           blade_faces[i1, :]]

        #   qq = vertices(:,faces(i3,:)) - vertices(:,faces(i1,:));
        qq = blade_verts[:,
                         blade_faces[i3, :]] - blade_verts[:,
                                                           blade_faces[i1, :]]

        #    normalize the vectors

        pp_length = np.sqrt(np.sum(pp**2, axis=0))
        qq_length = np.sqrt(np.sum(qq**2, axis=0))

        Ipp_zero = (pp_length < epsilon).nonzero()
        pp_length[Ipp_zero] = 1

        Iqq_zero = (qq_length < epsilon).nonzero()
        qq_length[Iqq_zero] = 1

        pp_nor = pp / np.kron(np.ones((3, 1)), pp_length)
        qq_nor = qq / np.kron(np.ones((3, 1)), qq_length)

        #    compute angles and clamped cotans

        cos_ang = np.sum(pp_nor * qq_nor, axis=0)
        cos_ang = np.clip(cos_ang, -1, 1)
        ang = np.arccos(cos_ang)
        print(i)
        if i == 1:
            ctan_1 = (1 / np.tan(ang)) / 2
            ctan_1 = np.clip(ctan_1, 0.001, 1000)

            ii_lap_1 = blade_faces[i2, :]
            jj_lap_1 = blade_faces[i3, :]
        elif i == 2:
            ctan_2 = (1 / np.tan(ang)) / 2
            ctan_2 = np.clip(ctan_2, 0.001, 1000)

            ii_lap_2 = blade_faces[i2, :]
            jj_lap_2 = blade_faces[i3, :]
        elif i == 3:
            ctan_3 = (1 / np.tan(ang)) / 2
            ctan_3 = np.clip(ctan_3, 0.001, 1000)

            ii_lap_3 = blade_faces[i2, :]
            jj_lap_3 = blade_faces[i3, :]

    #accumulate the angles on vertices

        for j in range(0, numFaces):
            indextemp = blade_faces[i1, j]
            sum_angles[indextemp, 0] = sum_angles[indextemp, 0] + ang[j]

    #compute the facet areas
        if i == 1:

            rr = crossinline(pp.T, -qq.T)
            rr = rr.T
            area = np.sqrt(np.sum(rr**2, axis=0)) / 2

    # Laplacian matrix (stiffness matrix)
    ii_lap = np.concatenate(
        [ii_lap_1, jj_lap_1, ii_lap_2, jj_lap_2, ii_lap_3, jj_lap_3])
    jj_lap = np.concatenate(
        [jj_lap_1, ii_lap_1, jj_lap_2, ii_lap_2, jj_lap_3, ii_lap_3])
    ss_lap = np.concatenate([ctan_1, ctan_1, ctan_2, ctan_2, ctan_3, ctan_3])

    laplacian = csr_matrix((ss_lap, (ii_lap, jj_lap)),
                           shape=(numVerts, numVerts),
                           dtype=np.float)
    laplacian.eliminate_zeros()

    diag_laplacian = np.sum(laplacian, axis=0)

    Diag_laplacian = spdiags(diag_laplacian[:], 0, numVerts, numVerts)

    laplacian = Diag_laplacian - laplacian

    #lumped mass matrix
    ii_mass = np.concatenate(
        [blade_faces[0, :], blade_faces[1, :], blade_faces[2, :]])
    jj_mass = np.concatenate(
        [blade_faces[0, :], blade_faces[1, :], blade_faces[2, :]])
    area = area / 3.0
    ss_mass = np.concatenate([area, area, area])

    mass = csr_matrix((ss_mass, (ii_mass, jj_mass)),
                      shape=(numVerts, numVerts),
                      dtype=np.float)
    mass.eliminate_zeros()

    #facet-edge adjacency matrix
    ii_adja = np.concatenate(
        [blade_faces[0, :], blade_faces[1, :], blade_faces[2, :]])
    jj_adja = np.concatenate(
        [blade_faces[1, :], blade_faces[2, :], blade_faces[0, :]])
    ss_adja = np.concatenate([
        np.arange(0, numFaces),
        np.arange(0, numFaces),
        np.arange(0, numFaces)
    ])
    adja = csr_matrix((ss_adja, (ii_adja, jj_adja)),
                      shape=(numVerts, numVerts),
                      dtype=np.float)

    #add missing points
    #I_adja = find(csr_matrix.transpose(adja)!=0)
    arow, acol, adata = find(csr_matrix.transpose(adja) != 0)
    onlyLeft = adja[arow, acol] == 0
    onlyLeft = np.asarray(onlyLeft)
    onlyLeft = onlyLeft.T
    arow = arow[onlyLeft[:, -1]]
    acol = acol[onlyLeft[:, -1]]
    adja[arow, acol] = -1

    #find the boundary
    I, J, V = find(adja)
    I = I[V == -1]
    J = J[V == -1]

    flag_boundary = np.zeros([numVerts, 1])
    flag_boundary[I, 0] = 1
    flag_boundary[J, 0] = 1

    #set different constant values (for Gaussian curvature computation) for
    #boundary vertices and non-boundary vertices
    constants = np.ones([numVerts, 1]) * 2 * math.pi
    I_boundary = np.where(flag_boundary == 1)[0]
    constants[I_boundary] = np.ones([len(I_boundary), 1]) * math.pi

    cgauss = constants - sum_angles

    surface = np.sum(mass.diagonal())

    L = laplacian

    L_diag = L.diagonal(0)

    cgaussabs = np.abs(cgauss)

    cgauss_rough = cgaussabs.T * L

    cgauss_rough = cgauss_rough.T

    for p in range(0, len(L_diag)):
        cgauss_rough[p] = cgauss_rough[p] / L_diag[p]

    cgauss_rough = np.abs(cgauss_rough)

    cgauss_rough_mean = np.dot(cgauss_rough.T, mass.diagonal(0))
    cgauss_rough_mean = cgauss_rough_mean / surface

    #power model for modulating the roughness

    minrough = 0.0005
    maxrough1 = 0.20
    maxrough2 = 5.0 * cgauss_rough_mean[0]
    maxrough = max(maxrough1, maxrough2)

    cgauss_rough = np.clip(cgauss_rough, minrough, maxrough)

    #    a = 0.15
    epsilon = minrough
    cgauss_rough_final = (cgauss_rough)**a - (epsilon)**a

    return cgauss_rough_final
コード例 #16
0
 def grad(self, w):
     n_samples, n_shapes = self.X.shape
     return csr_matrix.transpose(self.X).dot(self.X.dot(w) - self.y) / \
            n_samples
コード例 #17
0
def split_matrix(matrix_a):
    """
    A function to perform svd decomposition on the matrix given to it.The
    algorithm to peform the SVD decomposition for a matrix A is as follows:
        - Calculate x = A * A.transpose()
        - find all eigen values of x, and find corresponding eigen vectors
        - Construct matrix U by arragning eigen vectors, in decreasing order
          of their eigen value magnitude, as columns.
        - Similarly construct V from matrix y = A.transpose() * A
        - Construct the matrix sigma by placing square roots of eigen values
          of x in decreasing order of their magnitudes along principal diagonal
          of a zero square matrix of appropriate dimensions
        - return U, Sigma, V
    :param matrix_a: (scipy.sparse.csr_matrix) A sparse matrix that is to be
    decomposed via svd
    :return: the three matrices, U, Sigma, and V that are the result of SVD
    decomposition
    """
    transpose_a = csr_matrix.transpose(matrix_a)
    matrix_u = np.dot(matrix_a, transpose_a)
    matrix_v = np.dot(transpose_a, matrix_a)
    # scipy.linalg.eigh() is a function that returns two values - a list of of
    # all its eigen values, and corresponding, normalized eigen vectors
    # arranged as columns in increasing order of the magnitudes of corresponding
    # eigen values. Eigen values are returned in increasing order of their
    # magnitude.
    vals_u, vecs_u = linalg.eigh(matrix_u.todense())
    vals_v, vecs_v = linalg.eigh(matrix_v.todense())
    eig_vals = list(vals_u)
    # we need eigen values in decreasing order of their magnitudes.
    eig_vals.reverse()
    # construct matrix sigma.
    matrix_sigma = np.zeros(shape=(total_users, total_movies))
    for i in xrange(total_users):
        try:
            matrix_sigma[i, i] = sqrt(eig_vals[i])
        except ValueError:
            matrix_sigma[i, i] = sqrt(-1 * eig_vals[i])
        except IndexError as e:
            print e
    matrix_sigma = csr_matrix(matrix_sigma)
    # we need to reverse the order of columns because we need them arranged in
    #  decreasing order of corresponding eigen values, not increasing order.
    # to accomplish this, we transpose our numpy.ndarray, convert to a list
    # of lists, perform list reverse operation, construct a numpy.nd array
    # from the obtained list and then transpose it back.
    # >>> [1,2]
    #     [3,4]
    # >>> (after step 1)[1, 3]
    #                   [2, 4]
    # >>> (after list reverse) [2 ,4]
    #                          [1, 3]
    # >>> after final transpose, [2, 1]
    # >>>                        [4, 3]
    vecs_u = vecs_u.transpose()
    temp = list(vecs_u)
    temp.reverse()
    matrix_u = np.array(temp).transpose()
    # do the same for matrix_v
    vecs_v = vecs_v.transpose()
    temp1 = list(vecs_v)
    temp1.reverse()
    matrix_v = np.array(temp1)
    return matrix_u, matrix_sigma, matrix_v
コード例 #18
0
def vp_RR_He(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Acceleration
    from Solver.Acceleration.plusr_vp import *

    #Varying penalty parameter
    from Solver.Rho.Varying.He import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [Es_matrix(w, mu, np.zeros([
        p,
    ]))]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    u_hat = [np.zeros([
        p,
    ])]  #u_hat[0] #in the notation of the paper this used with a underline
    xi = [np.zeros([
        p,
    ])]
    xi_hat = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    tau = [1]  #over-relaxation
    e = []  #restart
    rho = []

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rh = eval(rho_string)
    rho.append(rh)  #rho[0]

    #Plot
    rho_plot = []
    r_plot = []
    s_plot = []
    b_plot = []
    u_bin_plot = []
    xi_bin_plot = []
    siconos_plot = []

    ################
    ## ITERATIONS ##
    ################

    for j in range(200):
        print j

        len_u = len(u) - 1
        for k in range(len_u, MAXITER):

            #Super LU factorization of M + rho * dot(M_T,M)
            if rho[k] != rho[k - 1] or k == len_u:  #rho[k] != rho[k-1] or
                P = M + rho[k] * csc_matrix.dot(A_T, A)
                LU = linalg.splu(P)
                LU_old = LU

            else:
                LU = LU_old

            ################
            ## v - update ##
            ################
            RHS = -f + rho[k] * csc_matrix.dot(
                A_T, -w - b[j] - xi_hat[k] + u_hat[k])
            v.append(LU.solve(RHS))  #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi_hat[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho[k] * csc_matrix.dot(A_T,
                                             (u[k + 1] - u_hat[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            ratio = rho[k - 1] / rho[
                k]  #update of dual scaled variable with new rho
            xi.append(ratio * (xi_hat[k] + r[k + 1]))  #xi[k+1]

            ###################################
            ## accelerated ADMM with restart ##
            ###################################
            plusr(tau, u, u_hat, xi, xi_hat, k, e, rho, ratio)

            ################################
            ## penalty parameter - update ##
            ################################
            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            rho.append(penalty(rho[k], r_norm[k + 1], s_norm[k + 1]))

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho[k] *
                                        csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            R = -rho[k] * xi[k + 1]
            N1 = csc_matrix.dot(M, v[k + 1]) - csc_matrix.dot(A_T, R) + f
            N2 = u[k + 1] - projection(u[k + 1] - R, mu, dim1, dim2)
            N1_norm = np.linalg.norm(N1)
            N2_norm = np.linalg.norm(N2)
            siconos_plot.append(np.sqrt(N1_norm**2 + N2_norm**2))

            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
                #if k == len_u:

                for element in range(len(u)):
                    #Relative velocity
                    u_proj = projection(u[element], mu, dim1, dim2)

                    u_proj_contact = np.split(u_proj, dim2 / dim1)
                    u_contact = np.split(u[element], dim2 / dim1)

                    u_count = 0.0
                    for contact in range(dim2 / dim1):
                        #if np.linalg.norm(u_contact[contact] - u_proj_contact[contact]) / np.linalg.norm(u_contact[contact]) < 1e-01:
                        if np.allclose(u_contact[contact],
                                       u_proj_contact[contact],
                                       rtol=0.1,
                                       atol=0.0):
                            u_count += 1.0

                    u_bin = 100 * u_count / (dim2 / dim1)
                    u_bin_plot.append(u_bin)

                    #Reaction
                    xi_proj = projection(-1 * xi[element], 1 / mu, dim1, dim2)

                    xi_proj_contact = np.split(xi_proj, dim2 / dim1)
                    xi_contact = np.split(-1 * xi[element], dim2 / dim1)

                    xi_count = 0.0
                    for contact in range(dim2 / dim1):
                        #if np.linalg.norm(xi_contact[contact] - xi_proj_contact[contact]) / np.linalg.norm(xi_contact[contact]) < 1e-01:
                        if np.allclose(xi_contact[contact],
                                       xi_proj_contact[contact],
                                       rtol=0.1,
                                       atol=0.0):
                            xi_count += 1.0

                    xi_bin = 100 * xi_count / (dim2 / dim1)
                    xi_bin_plot.append(xi_bin)

                for element in range(len(r_norm)):
                    rho_plot.append(rho[element])
                    r_plot.append(r_norm[element])
                    s_plot.append(s_norm[element])
                    b_plot.append(np.linalg.norm(b[j]))

                #print 'First contact'
                #print -rho[k]*xi[k+1][:3]
                #uy = projection(-rho[k]*xi[k+1],1/mu,dim1,dim2)
                #print uy[:3]
                #print 'Last contact'
                #print -rho[k]*xi[k+1][-3:]
                #print uy[-3:]

                #R = -rho[k]*xi[k+1]
                #N1 = csc_matrix.dot(M, v[k+1]) - csc_matrix.dot(A_T, R) + f
                #N2 = u[k+1] - projection(u[k+1] - R, mu, dim1, dim2)
                #N1_norm = np.linalg.norm(N1)
                #N2_norm = np.linalg.norm(N2)

                #print np.sqrt( N1_norm**2 + N2_norm**2 )
                print b_plot[-1]
                print b[-1][:3]
                print b[-1][-3:]
                break

            #end rutine

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, Av + w))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-03:
                    count += 1
            if count < 1:
                break

        v = [np.zeros([
            n,
        ])]
        u = [
            np.zeros([
                p,
            ])
        ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
        u_hat = [np.zeros([
            p,
        ])]  #u_hat[0] #in the notation of the paper this used with a underline
        xi = [np.zeros([
            p,
        ])]
        xi_hat = [np.zeros([
            p,
        ])]
        r = [np.zeros([
            p,
        ])]  #primal residual
        s = [np.zeros([
            p,
        ])]  #dual residual
        r_norm = [0]
        s_norm = [0]
        tau = [1]  #over-relaxation
        e = []  #restart
        rho = [rho[-1]]

    end = time.clock()
    time = end - start
    ####################
    ## REPORTING DATA ##
    ####################

    f, axarr = plt.subplots(5, sharex=True)
    f.suptitle('External update with vp_RR_He (Di Cairano)')

    axarr[0].semilogy(b_plot)
    axarr[0].set(ylabel='||Phi(s)||')

    axarr[1].plot(rho_plot)
    axarr[1].set(ylabel='Rho')

    axarr[2].semilogy(r_plot, label='||r||')
    axarr[2].semilogy(s_plot, label='||s||')
    axarr[2].legend()
    axarr[2].set(ylabel='Residuals')

    axarr[3].semilogy(siconos_plot)
    axarr[3].set(ylabel='SICONOS error')

    axarr[4].plot(u_bin_plot, label='u in K*')
    axarr[4].plot(xi_bin_plot, label='-xi in K')
    axarr[4].legend()
    axarr[4].set(xlabel='Iteration', ylabel='Projection (%)')

    plt.show()

    print 'Total time: ', time
    return time
コード例 #19
0
 def transform(self, X, **transform_params):
     ''' Transforming X: X is list of samples, type: list(str) '''
     spmat_values = csr_matrix([self.get_sent_len(sample) for sample in X])
     # need to be transposed
     return csr_matrix.transpose(spmat_values)
コード例 #20
0
def cp_RR(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Acceleration
    from Solver.Acceleration.plusr import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    #####################################################
    ############# TERMS / NOT A FUNCTION YET ############
    #####################################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [
        1 / linalg.norm(A, 'fro') * Es_matrix(w, mu, np.zeros([
            p,
        ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
            p,
        ])))
    ]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    u_hat = [np.zeros([
        p,
    ])]  #u_hat[0] #in the notation of the paper this used with a underline
    xi = [np.zeros([
        p,
    ])]
    xi_hat = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    tau = [1]  #over-relaxation
    e = []  #restart

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rho = eval(rho_string)

    #Plot
    r_plot = []
    s_plot = []
    b_plot = []
    u_bin_plot = []
    xi_bin_plot = []

    ########################################
    ## TERMS COMMON TO ALL THE ITERATIONS ##
    ########################################

    #Super LU factorization of M + rho * dot(M_T,M)
    P = M + rho * csc_matrix.dot(A_T, A)
    LU = linalg.splu(P)

    ################
    ## ITERATIONS ##
    ################
    for j in range(20):
        print j

        len_u = len(u) - 1
        for k in range(len_u, MAXITER):

            ################
            ## v - update ##
            ################
            RHS = -f + rho * csc_matrix.dot(A_T,
                                            -w - b[j] - xi_hat[k] + u_hat[k])
            v.append(LU.solve(RHS))  #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi_hat[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho * csc_matrix.dot(A_T, (u[k + 1] - u_hat[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            xi.append(xi_hat[k] + r[k + 1])  #xi[k+1]

            ###################################
            ## accelerated ADMM with restart ##
            ###################################
            plusr(tau, u, u_hat, xi, xi_hat, k, e, rho)

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:

                for element in range(len(u)):
                    #Relative velocity
                    u_proj = projection(u[element], mu, dim1, dim2)

                    u_proj_contact = np.split(u_proj, dim2 / dim1)
                    u_contact = np.split(u[element], dim2 / dim1)

                    u_count = 0.0
                    for contact in range(dim2 / dim1):
                        if np.array_equiv(u_contact[contact],
                                          u_proj_contact[contact]):
                            u_count += 1.0

                    u_bin = 100 * u_count / (dim2 / dim1)
                    u_bin_plot.append(u_bin)

                    #Reaction
                    xi_proj = projection(xi[element], 1 / mu, dim1, dim2)

                    xi_proj_contact = np.split(xi_proj, dim2 / dim1)
                    xi_contact = np.split(xi[element], dim2 / dim1)

                    xi_count = 0.0
                    for contact in range(dim2 / dim1):
                        if np.array_equiv(xi_contact[contact],
                                          xi_proj_contact[contact]):
                            xi_count += 1.0

                    xi_bin = 100 * xi_count / (dim2 / dim1)
                    xi_bin_plot.append(xi_bin)

                for element in range(len(r_norm)):
                    r_plot.append(r_norm[element])
                    s_plot.append(s_norm[element])
                    b_plot.append(np.linalg.norm(b[j]))

                #print 'First contact'
                #print rho*xi[k+1][:3]
                #uy = projection(rho*xi[k+1],1/mu,dim1,dim2)
                #print uy[:3]
                #print 'Last contact'
                #print rho*xi[k+1][-3:]
                #print uy[-3:]

                #print u[k+1]

                #R = rho*xi[k+1]
                #N1 = csc_matrix.dot(M, v[k+1]) - csc_matrix.dot(A_T, R) + f
                #N2 = R - projection(R - u[k+1], 1/mu, dim1, dim2)
                #N1_norm = np.linalg.norm(N1)
                #N2_norm = np.linalg.norm(N2)

                #print np.sqrt( N1_norm**2 + N2_norm**2 )
                break

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, Av + w))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-03:
                    count += 1
            if count < 1:
                break

        v = [np.zeros([
            n,
        ])]
        u = [
            np.zeros([
                p,
            ])
        ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
        u_hat = [np.zeros([
            p,
        ])]  #u_hat[0] #in the notation of the paper this used with a underline
        xi = [np.zeros([
            p,
        ])]
        xi_hat = [np.zeros([
            p,
        ])]
        r = [np.zeros([
            p,
        ])]  #primal residual
        s = [np.zeros([
            p,
        ])]  #dual residual
        r_norm = [0]
        s_norm = [0]
        tau = [1]  #over-relaxation
        e = []  #restart

    end = time.clock()
    ####################
    ## REPORTING DATA ##
    ####################

    f, axarr = plt.subplots(4, sharex=True)
    f.suptitle('External update with cp_RR (Acary)')

    axarr[0].semilogy(b_plot)
    axarr[0].set(ylabel='||Phi(s)||')

    axarr[1].axhline(y=rho)
    axarr[1].set(ylabel='Rho')

    axarr[2].semilogy(r_plot, label='||r||')
    axarr[2].semilogy(s_plot, label='||s||')
    axarr[2].set(ylabel='Residuals')

    axarr[3].plot(u_bin_plot, label='u in K*')
    axarr[3].plot(xi_bin_plot, label='xi in K')
    axarr[3].legend()
    axarr[3].set(xlabel='Iteration', ylabel='Projection (%)')
    plt.show()

    #plotit(r,b,start,end,'With acceleration / With restarting for '+problem_data+' for rho: '+rho_method)

    time = end - start
    print 'Total time: ', time
    return time
コード例 #21
0
    def stoke_equation_2d(self, length, numOfCells):
        'A index-2 large scale DAE, stoke quation in a square region'

        # This benchmark is from the paper:
        # Balanced Truncation Model Reduction for Systems in Descriptor Form
        # system's dimension = 3n^2 + 2n, n = numOfCells

        isinstance(numOfCells, int)
        isinstance(length, float)
        assert numOfCells > 1, 'number of mesh points shoubld be >= 2'
        assert length > 0, 'length of square should be large than 0'

        n = numOfCells
        h = length / (n + 1)  # discretization step

        k = 1 / h**2
        l = 1 / h

        # handle dynamic part: dv/dt = div^2 v - div p + f
        # using MAC scheme
        ##################################################################
        #    v = v_x + v_y (velocity vector)
        #    dv_x/dt = div^2 (v_x) - div_x (p) + f_x
        #    dv_y/dt = div^2 (v_y) - div_y (p) + f_y
        #    div_x(v_x) + div_y(v_y) = 0
        #
        #    state space format:
        #    dv_x/dt = matrix_V_x * v_x + matrix_P_x * p + f_x
        #    dv_y/dt = matrix_V_y * v_y + matrix_P_y * p + f_y
        #    transpose(matrix_P_x)* v_x + transpose(matrix_P_y) * v_y = 0
        #
        #    Let x = [v_x v_y p]^T we have:
        #    E * dx/dt = A * x + B * u(t)
        #    E = [I 0; 0 0]; A = [V_x 0 P_x; 0 V_y P_y; P_x^T P_y^T 0]
        ##################################################################

        # Boundary conditions : at the boundary we have v( 0, y, t) = 0, v(1, y, t) = 0 (Dirichlet boundary conditions)
        # condition for the force f: x = 0, f_x(0, y, t) = 1, f_y(0, y, t) = 1; x > 0, f = 0
        # More boundary conditions can be found in the paper: Various boundary conditions for
        # Navier-stokes equation in bounded Lipschitz domains

        # number of variables (points) along one x/y - axis
        num_var = n * (n + 1)

        # matrix corresponds to velocity v_x
        V_x = lil_matrix((num_var, num_var), dtype=float)
        # matrix corresponds to velocity v_y
        V_y = lil_matrix((num_var, num_var), dtype=float)
        # matrix corresponds to the force fx
        B_x = lil_matrix((num_var, 1), dtype=float)
        # matrix corresponds to the force fy
        B_y = lil_matrix((num_var, 1), dtype=float)

        # filling V_x, B_x
        for i in xrange(0, num_var):
            # y-position of i-th state variable
            y_pos = int(math.ceil(i / n))
            x_pos = i - y_pos * n  # x_position of i-th state variable
            print "\nV_x: the {}th variable is the velocity of the flow at the point ({}, {})".format(
                i, x_pos, y_pos)
            V_x[i, i] = -4

            if y_pos == 0:
                B_x[i, 0] = 1  # input force fx at boundary x = 0

            if x_pos - 1 >= 0:
                V_x[i, i - 1] = 1  # boundary condition at x = 0, v_x = 0

            if x_pos + 1 <= n - 1:
                V_x[i, i + 1] = 1  # boundary condition at x = 1, v_x = 0

            if y_pos - 1 >= 0:
                V_x[i, (y_pos - 1) * n + x_pos] = 1
            else:
                V_x[i, i] = V_x[i, i] - 1  # extrapolation at y = 0

            if y_pos + 1 <= n:
                V_x[i, (y_pos + 1) * n + x_pos] = 1
            else:
                V_x[i, i] = V_x[i, i] - 1  # extrapolation at y = 1

        # filling V_y

        for i in xrange(0, num_var):
            # y-position of i-th state variable
            y_pos = int(math.ceil(i / (n + 1)))
            x_pos = i - y_pos * (n + 1)  # x-position of i-th state variable
            print "\nV_y: the {}th variable is the velocity of the flow at the point ({}, {})".format(
                i, x_pos, y_pos)
            V_y[i, i] = -4

            if y_pos == 0:
                B_y[i, 0] = 1  # input force fy at boudary x = 0

            if x_pos - 1 >= 0:
                V_y[i, i - 1] = 1  # boundary condition at x = 0, v_y = 0

            if x_pos + 1 <= n:
                V_y[i, i + 1] = 1  # boundary condition at x = 1, v_y = 0

            if y_pos - 1 >= 0:
                V_y[i, (y_pos - 1) * (n + 1) + x_pos] = 1
            else:
                V_y[i, i] = V_y[i, i] - 1  # extrapolation at y = 0

            if y_pos + 1 <= n - 1:
                V_y[i, (y_pos + 1) * (n + 1) + x_pos] = 1
            else:
                V_y[i, i] = V_y[i, i] - 1  # extrapolation at y = 1

        # matrix corresponds to pressure px
        P_x = lil_matrix((n * (n + 1), n * n), dtype=float)
        # matrix corresponds to pressure py
        P_y = lil_matrix((n * (n + 1), n * n), dtype=float)

        # filling P_x
        for i in xrange(0, num_var):
            # y-position of i-th state variable
            y_pos = int(math.ceil(i / n))
            x_pos = i - y_pos * n  # x_position of i-th state variable

            if i <= n * n - 1:
                P_x[i, i] = 1

            if y_pos - 1 >= 0:
                P_x[i, (y_pos - 1) * n + x_pos] = -1

        # filling P_y
        for i in xrange(0, num_var):
            # y-position of i-th state variable
            y_pos = int(math.ceil(i / (n + 1)))
            x_pos = i - y_pos * (n + 1)  # x-position of i-th state variable

            if x_pos <= n - 1:
                j = y_pos * n + x_pos  # the j-th correpsonding pressure variable
                P_y[i, j] = 1
                if x_pos - 1 >= 0:
                    P_y[i, j - 1] = -1
            else:
                P_y[i, y_pos * n + x_pos - 1] = -1

        V_x.tocsr()
        V_y.tocsr()
        P_x.tocsr()
        P_y.tocsr()
        matrix_V_x = V_x.multiply(k)  # scale matrix with k = 1/h**2
        matrix_V_y = V_y.multiply(k)
        matrix_P_x = P_x.multiply(l)  # scale matrix with l = 1/h
        matrix_P_y = P_y.multiply(l)

        # constructing matrix E, A, B, C
        identity_mat = eye(2 * num_var, dtype=float, format='csr')
        zero_mat = csr_matrix((2 * num_var, n * n), dtype=float)
        E1 = hstack([identity_mat, zero_mat])
        E2 = hstack([
            csr_matrix.transpose(zero_mat),
            csr_matrix((n * n, n * n), dtype=float)
        ])
        E = vstack([E1, E2])
        self.E = E.tocsc()

        V1 = hstack([matrix_V_x, csr_matrix((num_var, num_var), dtype=float)])
        V2 = hstack([csr_matrix((num_var, num_var), dtype=float), matrix_V_y])
        matrix_V = vstack([V1, V2])
        matrix_P = vstack([matrix_P_x, matrix_P_y])
        A1 = hstack([matrix_V, matrix_P])
        A2 = hstack([
            csr_matrix.transpose(matrix_P.tocsr()),
            csr_matrix((n * n, n * n), dtype=float)
        ])
        A = vstack([A1, A2])
        self.A = A.tocsc()

        zero_vec = csr_matrix((n * n, 1), dtype=float)
        B = vstack([B_x, B_y, zero_vec])
        self.B = B.tocsc()

        # we are interested in the velocity v_x and v_y of the middle point of
        # the middle cell, we use the average value between two points

        centre = int(math.ceil((n - 1) / 2))
        vx1_index = centre * n + centre
        vx2_index = (centre + 1) * n + centre
        Cx = lil_matrix((1, num_var), dtype=float)
        Cx[0, vx1_index] = 0.5
        Cx[0, vx2_index] = 0.5

        vy1_index = (centre + 1) * n + centre
        vy2_index = (centre + 1) * n + centre + 1
        Cy = lil_matrix((1, num_var), dtype=float)
        Cy[0, vy1_index] = 0.5
        Cy[0, vy2_index] = 0.5
        zero_mat2 = csr_matrix((1, num_var), dtype=float)
        zero_mat3 = csr_matrix((1, n * n), dtype=float)
        C1 = hstack([Cx, zero_mat2, zero_mat3])
        C2 = hstack([zero_mat2, Cy, zero_mat3])
        C = vstack([C1, C2])
        self.C = C.tocsc()

        return self.E, self.A, self.B, self.C
コード例 #22
0
 def transform(self, tweets):
     values = csr_matrix([self._get_features(tweet) for tweet in tweets])
     return csr_matrix.transpose(values)
コード例 #23
0
def vp_N_He(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #Varying penalty parameter
    from Solver.Rho.Varying.He import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = 0.1 * Es_matrix(w, mu, np.ones([
        p,
    ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
        p,
    ])))

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    xi = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    e = []  #restart
    rho = []

    #Optimal penalty parameter
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rh = eval(rho_string)
    rho.append(rh)  #rho[0]

    ################
    ## ITERATIONS ##
    ################

    for k in range(MAXITER):

        #Super LU factorization of M + rho * dot(M_T,M)
        if rho[k] != rho[k - 1] or k == 0:
            P = M + rho[k] * csc_matrix.dot(A_T, A)
            LU = linalg.splu(P)
            LU_old = LU
        else:
            LU = LU_old

        ################
        ## v - update ##
        ################
        RHS = -f + rho[k] * csc_matrix.dot(A_T, -w - b - xi[k] + u[k])
        v.append(LU.solve(RHS))  #v[k+1]

        ################
        ## u - update ##
        ################
        Av = csr_matrix.dot(A, v[k + 1])
        vector = Av + xi[k] + w + b
        u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

        ########################
        ## residuals - update ##
        ########################
        s.append(rho[k] * csc_matrix.dot(A_T, (u[k + 1] - u[k])))  #s[k+1]
        r.append(Av - u[k + 1] + w + b)  #r[k+1]

        #################
        ## xi - update ##
        #################
        ratio = rho[k -
                    1] / rho[k]  #update of dual scaled variable with new rho
        xi.append(ratio * (xi[k] + r[k + 1]))  #xi[k+1]

        ####################
        ## stop criterion ##
        ####################
        pri_evalf = np.amax(
            np.array([
                np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                np.linalg.norm(u[k + 1]),
                np.linalg.norm(w + b)
            ]))
        eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

        dual_evalf = np.linalg.norm(rho[k] * csc_matrix.dot(A_T, xi[k + 1]))
        eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

        r_norm.append(np.linalg.norm(r[k + 1]))
        s_norm.append(np.linalg.norm(s[k + 1]))
        if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
            break

        ################################
        ## penalty parameter - update ##
        ################################
        rho.append(penalty(rho[k], r_norm[k + 1], s_norm[k + 1]))

        #end rutine

    end = time.clock()
    ####################
    ## REPORTING DATA ##
    ####################
    #plotit(r,s,start,end,'Without acceleration / Without restarting for '+problem_data+' for rho: '+rho_method)

    time = end - start
    return time
コード例 #24
0
def cp_RR(problem_data, rho_method):

	######################
	## IMPORT LIBRARIES ##
	######################

	#Math libraries
	import numpy as np
	from scipy.sparse import csc_matrix
	from scipy.sparse import csr_matrix
	from scipy.sparse import linalg

	#Timing
	import time

	#Import data
	from Data.read_fclib import *

	#Plot residuals
	from Solver.ADMM_iteration.Numerics.plot import *

	#Initial penalty parameter
	import Solver.Rho.Optimal

	#Max iterations and kind of tolerance
	from Solver.Tolerance.iter_totaltolerance import *

	#Acceleration
	from Solver.Acceleration.plusr import *

	#b = Es matrix
	from Data.Es_matrix import *

	#Projection onto second order cone
	from Solver.ADMM_iteration.Numerics.projection import *

	#####################################################
	############# TERMS / NOT A FUNCTION YET ############
	#####################################################

	start = time.clock()
	problem = hdf5_file(problem_data)

	M = problem.M.tocsc()
	f = problem.f
	A = csc_matrix.transpose(problem.H.tocsc())
	A_T = csr_matrix.transpose(A)
	w = problem.w
	mu = problem.mu

	#Dimensions (normal,tangential,tangential)
	dim1 = 3 
	dim2 = np.shape(w)[0]

	#Problem size
	n = np.shape(M)[0]
	p = np.shape(w)[0]

	b = 0.1 * Es_matrix(w,mu,np.ones([p,])) / np.linalg.norm(Es_matrix(w,mu,np.ones([p,])))

	#################################
	############# SET-UP ############
	#################################

	#Set-up of vectors
	v = [np.zeros([n,])]
	u = [np.zeros([p,])] #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
	u_hat = [np.zeros([p,])] #u_hat[0] #in the notation of the paper this used with a underline
	xi = [np.zeros([p,])] 
	xi_hat = [np.zeros([p,])]
	r = [np.zeros([p,])] #primal residual
	s = [np.zeros([p,])] #dual residual
	r_norm = [0]
	s_norm = [0]
	tau = [1] #over-relaxation
	e = [] #restart

	#Optimal penalty parameter
	rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
	rho = eval(rho_string)

	########################################
	## TERMS COMMON TO ALL THE ITERATIONS ##
	########################################

	#Super LU factorization of M + rho * dot(M_T,M)
	P = M + rho * csc_matrix.dot(A_T,A)
	LU = linalg.splu(P)

	################
	## ITERATIONS ##
	################
	for k in range(MAXITER):
	
		################
		## v - update ##
		################
		RHS = -f + rho * csc_matrix.dot(A_T, -w - b - xi_hat[k] + u_hat[k])
		v.append(LU.solve(RHS)) #v[k+1]

		################
		## u - update ##
		################
		Av = csr_matrix.dot(A,v[k+1])
		vector = Av + xi_hat[k] + w + b
		u.append(projection(vector,mu,dim1,dim2)) #u[k+1]

		########################
		## residuals - update ##
		########################
		s.append(rho * csc_matrix.dot(A_T,(u[k+1]-u_hat[k]))) #s[k+1]
		r.append(Av - u[k+1] + w + b) #r[k+1]

		#################
		## xi - update ##
		#################
		xi.append(xi_hat[k] + r[k+1]) #xi[k+1]

		####################
		## stop criterion ##
		####################
		pri_evalf = np.amax(np.array([np.linalg.norm(csr_matrix.dot(A,v[k+1])),np.linalg.norm(u[k+1]),np.linalg.norm(w + b)]))
		eps_pri = np.sqrt(p)*ABSTOL + RELTOL*pri_evalf

		dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T,xi[k+1]))
		eps_dual = np.sqrt(n)*ABSTOL + RELTOL*dual_evalf

		r_norm.append(np.linalg.norm(r[k+1]))
		s_norm.append(np.linalg.norm(s[k+1]))
		if r_norm[k+1]<=eps_pri and s_norm[k+1]<=eps_dual:
			break	

		###################################
		## accelerated ADMM with restart ##
		###################################
		plusr(tau,u,u_hat,xi,xi_hat,k,e,rho)
	
		#end rutine

	end = time.clock()
	####################
	## REPORTING DATA ##
	####################
	#plotit(r,s,start,end,'With acceleration / With restarting for '+problem_data+' for rho: '+rho_method)

	time = end - start
	return time
コード例 #25
0
def optimization():
    counts = np.loadtxt("counts_copy.txt")
    label = get_entire_corpus_label()

    new_dic_corpus = get_new_dic_corpus_from_Raw()
    train_corpus = get_train_corpus_from_Raw()
    devel_corpus = get_devel_corpus_from_Raw()
    test_corpus = get_test_corpus_from_Raw()

    size_1 = len(new_dic_corpus)
    size_2 = len(new_dic_corpus) + len(train_corpus)
    size_3 = len(new_dic_corpus) + len(train_corpus) + len(devel_corpus)
    size_4 = len(new_dic_corpus) + len(train_corpus) + len(devel_corpus) + len(
        test_corpus)

    dic = counts[:size_1]
    train = counts[size_1:size_3]
    test = counts[size_3:]

    new_dic_label = label[:size_1]
    train_label = label[size_1:size_3]
    test_label = label[size_3:size_4]

    reversed_dic = {}
    for i in range(len(dic)):
        if new_dic_label[i] not in reversed_dic:
            reversed_dic[new_dic_label[i]] = i

    W = Variable(100, 100)
    expr_list = []
    W = np.identity(100)
    for i in range(len(train)):
        label = train_label[i]
        idx = reversed_dic.get(label, 1)
        vec = dic[idx]
        np_vec = np.asarray(vec)
        np_vec = np_vec.reshape(100, 1)
        train_i = np.asarray(train[i])
        train_i = train_i.reshape(100, 1)
        sT = sparse.csr_matrix(train_i)
        STT = csr_matrix.transpose(sT)
        score_1_1 = STT.dot(W)
        score_1 = np.dot(score_1_1, np_vec)

        for con in range(0, 25):
            idx_d = random.randint(0, 264850)
            if idx_d != idx:
                np_data = np.asarray(dic[idx_d])
                np_data = np_data.reshape(100, 1)
                score_1_1s = sparse.csr_matrix(score_1_1)
                score_2 = score_1_1s.dot(np_data)
                expr_list.append(max(0, 1 - score_1 + score_2))

                if (1 - score_1 + score_2 > 0):
                    W = [[
                        W[x][y] + 0.0001 * sT.dot(np_vec.T)[x][y] -
                        0.0001 * sT.dot(np_data.T)[x][y]
                        for y in range(0, 100)
                    ] for x in range(0, 100)]

        print(i)

    np.savetxt("W_matrix_before copy 2.txt", W)
    prob = Problem(Minimize(sum(expr_list)))
    result = prob.solve()

    np.savetxt("W_matrix copy 2.txt", W)
コード例 #26
0
ファイル: ItemBased.py プロジェクト: vshokorov/CIAN_MLS
def distEuclid(a, b):
    return float((csr_matrix.transpose(a - b).dot((a - b))).toarray())
コード例 #27
0
def score():
    counts = np.loadtxt("counts_copy.txt")
    label = get_entire_corpus_label()

    new_dic_corpus = get_new_dic_corpus_from_Raw()
    train_corpus = get_train_corpus_from_Raw()
    devel_corpus = get_devel_corpus_from_Raw()
    test_corpus = get_test_corpus_from_Raw()

    size_1 = len(new_dic_corpus)
    size_2 = len(new_dic_corpus) + len(train_corpus)
    size_3 = len(new_dic_corpus) + len(train_corpus) + len(devel_corpus)
    size_4 = len(new_dic_corpus) + len(train_corpus) + len(devel_corpus) + len(
        test_corpus)

    dic = counts[:size_1]
    train = counts[size_1:size_3]
    devel = counts[size_2:size_3]
    test = counts[size_3:]

    new_dic_label = label[:size_1]
    train_label = label[size_1:size_3]
    devel_label = label[size_2:size_3]
    test_label = label[size_3:size_4]

    W = np.loadtxt("W_matrix.txt")

    y_true = []
    y_pre = []

    for i in range(len(test)):

        label = test_label[i]
        mini_score = 0

        test_i = np.asarray(test[i])
        test_i = test_i.reshape(100, 1)
        sT = sparse.csr_matrix(test_i)
        STT = csr_matrix.transpose(sT)
        score_1_1 = STT.dot(W)

        for idx_s in range(len(dic)):
            vec = dic[idx_s]
            np_vec = np.asarray(vec)
            np_vec = np_vec.reshape(100, 1)
            score_1 = np.dot(score_1_1, np_vec)
            if (score_1 < mini_score):
                mini_score = score_1
                pre_label = new_dic_label[idx_s]

        y_pre.append(pre_label)
        y_true.append(label)

        print(i)

    precision = precision_score(y_true, y_pre, average='micro')
    recall = recall_score(y_true, y_pre, average='micro')
    f1 = f1_score(y_true, y_pre, average='micro')

    print("precision")
    print(precision)
    print("recall")
    print(recall)
    print("f1")
    print(f1)
コード例 #28
0
 def batch_grad(self, w, batch_size):
     X_sub, y_sub = shuffle(self.X, self.y, n_samples=batch_size)
     n_samples, n_shapes = self.sub_X.shape
     return csr_matrix.transpose(X_sub).dot(X_sub.dot(w) -
                                            y_sub) / n_samples
コード例 #29
0
ファイル: ItemBased.py プロジェクト: vshokorov/CIAN_MLS
def distCos(a, b):
    na = float((csr_matrix.transpose(a).dot(a)).toarray())
    nb = float((csr_matrix.transpose(b).dot(b)).toarray())
    if (na * nb == 0):
        return 1001
    return float((csr_matrix.transpose(a).dot(b)).toarray()) / (na * nb)
コード例 #30
0
    def transform(self, X, **transform_params):
        ''' Just get length over the whole tweet string '''

        # Desired output: matrix where axis 0 = n_sample
        values = csr_matrix([len(tweet) for tweet in X])
        return csr_matrix.transpose(values)
コード例 #31
0
def cp_N(problem_data, rho_method):

    ######################
    ## IMPORT LIBRARIES ##
    ######################

    #Math libraries
    import numpy as np
    from scipy.sparse import csc_matrix
    from scipy.sparse import csr_matrix
    from scipy.sparse import linalg

    #Timing
    import time

    #Import data
    from Data.read_fclib import *

    #Plot residuals
    from Solver.ADMM_iteration.Numerics.plot import *

    #Initial penalty parameter
    import Solver.Rho.Optimal

    #Max iterations and kind of tolerance
    from Solver.Tolerance.iter_totaltolerance import *

    #b = Es matrix
    from Data.Es_matrix import *

    #Projection onto second order cone
    from Solver.ADMM_iteration.Numerics.projection import *

    ##################################
    ############# REQUIRE ############
    ##################################

    start = time.clock()
    problem = hdf5_file(problem_data)

    M = problem.M.tocsc()
    f = problem.f
    A = csc_matrix.transpose(problem.H.tocsc())
    A_T = csr_matrix.transpose(A)
    w = problem.w
    mu = problem.mu

    #Dimensions (normal,tangential,tangential)
    dim1 = 3
    dim2 = np.shape(w)[0]

    #Problem size
    n = np.shape(M)[0]
    p = np.shape(w)[0]

    b = [
        1 / linalg.norm(A, 'fro') * Es_matrix(w, mu, np.ones([
            p,
        ])) / np.linalg.norm(Es_matrix(w, mu, np.ones([
            p,
        ])))
    ]

    #################################
    ############# SET-UP ############
    #################################

    #Set-up of vectors
    v = [np.zeros([
        n,
    ])]
    u = [
        np.zeros([
            p,
        ])
    ]  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
    xi = [np.zeros([
        p,
    ])]
    r = [np.zeros([
        p,
    ])]  #primal residual
    s = [np.zeros([
        p,
    ])]  #dual residual
    r_norm = [0]
    s_norm = [0]
    e = []  #restart

    #Optimal penalty parameter
    start = time.clock()
    rho_string = 'Solver.Rho.Optimal.' + rho_method + '(A,M,A_T)'
    rho = eval(rho_string)

    ########################################
    ## TERMS COMMON TO ALL THE ITERATIONS ##
    ########################################

    #Super LU factorization of M + rho * dot(M_T,M)
    P = M + rho * csc_matrix.dot(A_T, A)
    LU = linalg.splu(P)

    ################
    ## ITERATIONS ##
    ################
    for j in range(MAXITER):

        for k in range(len(u) - 1, MAXITER):

            ################
            ## v - update ##
            ################
            RHS = -f + rho * csc_matrix.dot(A_T, -w - b[j] - xi[k] + u[k])
            v.append(LU.solve(RHS))  #v[k+1]

            ################
            ## u - update ##
            ################
            Av = csr_matrix.dot(A, v[k + 1])
            vector = Av + xi[k] + w + b[j]
            u.append(projection(vector, mu, dim1, dim2))  #u[k+1]

            ########################
            ## residuals - update ##
            ########################
            s.append(rho * csc_matrix.dot(A_T, (u[k + 1] - u[k])))  #s[k+1]
            r.append(Av - u[k + 1] + w + b[j])  #r[k+1]

            #################
            ## xi - update ##
            #################
            xi.append(xi[k] + r[k + 1])  #xi[k+1]

            ####################
            ## stop criterion ##
            ####################
            pri_evalf = np.amax(
                np.array([
                    np.linalg.norm(csr_matrix.dot(A, v[k + 1])),
                    np.linalg.norm(u[k + 1]),
                    np.linalg.norm(w + b[j])
                ]))
            eps_pri = np.sqrt(p) * ABSTOL + RELTOL * pri_evalf

            dual_evalf = np.linalg.norm(rho * csc_matrix.dot(A_T, xi[k + 1]))
            eps_dual = np.sqrt(n) * ABSTOL + RELTOL * dual_evalf

            r_norm.append(np.linalg.norm(r[k + 1]))
            s_norm.append(np.linalg.norm(s[k + 1]))
            if r_norm[k + 1] <= eps_pri and s_norm[k + 1] <= eps_dual:
                break

            #end rutine

        #b(s) stop criterion
        b.append(Es_matrix(w, mu, csr_matrix.dot(A, v[k + 1])))

        if j == 0:
            pass
        else:
            b_per_contact_j1 = np.split(b[j + 1], dim2 / dim1)
            b_per_contact_j0 = np.split(b[j], dim2 / dim1)
            count = 0
            for i in range(dim2 / dim1):
                if np.linalg.norm(b_per_contact_j1[i] -
                                  b_per_contact_j0[i]) / np.linalg.norm(
                                      b_per_contact_j0[i]) > 1e-02:
                    count += 1
            if count < 1:
                break

        v.append(np.zeros([
            n,
        ]))
        u.append(
            np.zeros([
                p,
            ])
        )  #this is u tilde, but in the notation of the paper is used as hat [np.zeros([10,0])]
        xi.append(np.zeros([
            p,
        ]))
        r.append(np.zeros([
            p,
        ]))  #primal residual
        s.append(np.zeros([
            p,
        ]))  #dual residual
        r_norm.append(0)
        s_norm.append(0)

    #print(1 / linalg.norm(A,'fro'))
    #print(1 / linalg.norm(A,1))
    print(b[-1])

    end = time.clock()
    ####################
    ## REPORTING DATA ##
    ####################
    plotit(
        b, s, start, end, 'Without acceleration / Without restarting for ' +
        problem_data + ' for rho: ' + rho_method)

    time = end - start
    return time