Python Forum
Change internal representation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change internal representation
#1
I have written a class that takes in a matrix as argument and changes it's internal representation to CSR. Now I'm trying to write a class method that changes the internal representation from CSR to another format(in my case it's to CSC, but choose any simple format you wish). The problem is that I don't know how to change the attributes/methods when making such a method, I just get errors. Any TIPS? Here is my code:

I would like to write a method called to_csc that returns a new object represented in other formats.

import numpy as np

class SparseMatrix:

	

	def __init__(self,matrix):
		self.matrix=np.asarray(matrix)
		self.intern_represent = 'CSR'
		self.number_of_nonzero
		self.IA
		self.values
		self.JA
		
	def change (self,row,column,new):
		self.matrix [row][column]=new
	
	def __repr__(self):
		return (' A= '+ str(self.values) +
                '\nIA= ' + str(self.IA) +
                '\nJA= '+ str(self.JA))

	@property
	def IA (self):
		IA =[]
		IA.append (0)
		for i in range (1,np.shape(self.matrix)[0]+1):
			num=IA [i-1] + np.count_nonzero(self.matrix [i-1])
			IA.append(num)
		IA=np.asarray (IA)
		return IA
		
	@property	
	def values (self):
		return self.matrix [np.nonzero(self.matrix)]
		
	@property
	def JA (self):
		index=np.nonzero(self.matrix)
		return index [1]
		
	@property	
	def number_of_nonzero(self):
		return len (self.values)
	
Reply
#2
import numpy as np
 
class SparseMatrix:
 
     
 
    def __init__(self,matrix):
        self.matrix=np.asarray(matrix)
        self.intern_represent = 'CSR'
        self.number_of_nonzero # you can't declare a variable without initialisation
        self.IA # the same
        self.values # -- // --
        self.JA # --//--
         
    def change (self,row,column,new):
        self.matrix [row][column]=new
     
    def __repr__(self):
        return (' A= '+ str(self.values) +
                '\nIA= ' + str(self.IA) +
                '\nJA= '+ str(self.JA))
 
    @property
    def IA (self):
        IA =[] # overwriting the method? 
        IA.append (0)
        for i in range (1,np.shape(self.matrix)[0]+1):
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Changed the things you quoted.(it was still working before though)

import numpy as np

class SparseMatrix:

	

	def __init__(self,matrix):
		self.matrix=np.asarray(matrix)
		self.intern_represent = 'CSR'
		
	def change (self,row,column,new):
		self.matrix [row][column]=new
	
	def __repr__(self):
		return (' A= '+ str(self.values) +
                '\nIA= ' + str(self.IA) +
                '\nJA= '+ str(self.JA))

	@property
	def IA (self):
		IAlist =[]
		IAlist.append (0)
		for i in range (1,np.shape(self.matrix)[0]+1):
			num=IAlist [i-1] + np.count_nonzero(self.matrix [i-1])
			IAlist.append(num)
		IAlist=np.asarray (IAlist)
		return IAlist
		
	@property	
	def values (self):
		return self.matrix [np.nonzero(self.matrix)]
		
	@property
	def JA (self):
		index=np.nonzero(self.matrix)
		return index [1]
		
	@property	
	def number_of_nonzero(self):
		return len (self.values)
	
		
b=np.array ([[11,0,0,14,0,16],[0,22,0,0,25,26],[0,0,33,34,0,36],[41,0,43,44,0,46]])
a=SparseMatrix (b)

print(a.number_of_nonzero)
a.change(0,1,1)
print(a.number_of_nonzero)
print(a)
Reply
#4
Well, I am done cause I've never used Numpy and I don't know what is happening. What does np.shape(self.matrix)[0] return? range() expects integers.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Dec-17-2017, 03:17 PM)wavic Wrote: Well, I am done cause I've never used Numpy and I don't know what is happening. What does np.shape(self.matrix)[0] return? range() expects integers.

It just returns a tuple with the size of the matrix,so np.shape(self.matrix)[0] returns the number (int) of rows.
Reply
#6
Just an integer, ha? You are saying that it has worked before. Could you show the result? I found this: https://docs.scipy.org/doc/scipy-0.14.0/...csr-matrix
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove internal lines donut plot matplotlib diego_last 5 6,267 Dec-14-2017, 04:05 PM
Last Post: diego_last

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020