In [2]:
import numpy
from scipy import sparse

Prove manipolazioni array


In [240]:
unimatr = numpy.ones((10,10))
#unimatr
duimatr = unimatr*2
#duimatr

uniarray = numpy.ones((10,1))
#uniarray

triarray = uniarray*3

scalarray = numpy.arange(10)
scalarray = scalarray.reshape(10,1)

#NB fare il reshape da orizzontale a verticale è come se aggiungesse
#una dimensione all'array facendolo diventare un ndarray
#(prima era un array semplice, poi diventa un array (x,1), quindi puoi fare trasposto)
#NB NUMPY NON FA TRASPOSTO DI ARRAY SEMPLICE!
#scalarray
scalarray.T

ramatricia = numpy.random.randint(2, size=36).reshape((6,6))
ramatricia2 = numpy.random.randint(2, size=36).reshape((6,6))

In [241]:
#WARNING questa operazione moltiplica elemento per elemento
#se l'oggetto è di dimensione inferiore moltiplica ogni riga/colonna
# o matrice verticale/orizzontale a seconda della forma dell'oggetto

duimatr*scalarray
#duimatr*scalarray.T
#duimatr*duimatr
ramatricia*ramatricia2

#numpy dot invece fa prodotto matriciale righe per colonne

numpy.dot(duimatr,scalarray)
#numpy.dot(duimatr,duimatr)
numpy.dot(ramatricia2,ramatricia)


Out[241]:
array([[3, 2, 1, 0, 1, 2],
       [3, 2, 2, 0, 2, 3],
       [2, 0, 1, 0, 2, 1],
       [3, 2, 3, 0, 3, 2],
       [2, 2, 2, 0, 2, 2],
       [1, 1, 0, 0, 0, 1]])

In [6]:
unimatricia = numpy.ones((3,3))
rangematricia = numpy.arange(9).reshape((3,3))
numpy.dot(rangematricia, rangematricia)


Out[6]:
array([[ 15,  18,  21],
       [ 42,  54,  66],
       [ 69,  90, 111]])

In [6]:
duimatr + scalarray


Out[6]:
array([[  2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.],
       [  3.,   3.,   3.,   3.,   3.,   3.,   3.,   3.,   3.,   3.],
       [  4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.],
       [  5.,   5.,   5.,   5.,   5.,   5.,   5.,   5.,   5.,   5.],
       [  6.,   6.,   6.,   6.,   6.,   6.,   6.,   6.,   6.,   6.],
       [  7.,   7.,   7.,   7.,   7.,   7.,   7.,   7.,   7.,   7.],
       [  8.,   8.,   8.,   8.,   8.,   8.,   8.,   8.,   8.,   8.],
       [  9.,   9.,   9.,   9.,   9.,   9.,   9.,   9.,   9.,   9.],
       [ 10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.],
       [ 11.,  11.,  11.,  11.,  11.,  11.,  11.,  11.,  11.,  11.]])

Prove creazione matrice 3D con prodotti esterni


In [1]:
import numpy
scalarray = numpy.arange(4)
uniarray = numpy.ones(4)

matricia = numpy.outer(scalarray, uniarray)
matricia


Out[1]:
array([[ 0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.]])

In [3]:
scalarray2 = numpy.reshape(scalarray,(1,4))
matricia2 = uniarray*scalarray2
matricia2


Out[3]:
array([[ 0.,  1.,  2.,  3.]])

In [9]:
tensorio = numpy.outer(matricia,scalarray).reshape(4,4,4)
tensorio
# metodo di creazione array nd (numpy.ndarray)


Out[9]:
array([[[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]],

       [[ 0.,  1.,  2.,  3.],
        [ 0.,  1.,  2.,  3.],
        [ 0.,  1.,  2.,  3.],
        [ 0.,  1.,  2.,  3.]],

       [[ 0.,  2.,  4.,  6.],
        [ 0.,  2.,  4.,  6.],
        [ 0.,  2.,  4.,  6.],
        [ 0.,  2.,  4.,  6.]],

       [[ 0.,  3.,  6.,  9.],
        [ 0.,  3.,  6.,  9.],
        [ 0.,  3.,  6.,  9.],
        [ 0.,  3.,  6.,  9.]]])

Prove manipolazione matrici 3D numpy


In [244]:
tensorio = numpy.ones(1000).reshape(10,10,10)
tensorio
# metodo di creazione array nd (numpy.ndarray)
#altro metodo è con comando diretto
#tensorio = numpy.ndarray((3,3,3), dtype = int, buffer=numpy.arange(30))
#potrebbe essere utile con la matrice sparsa della peakmap, anche se difficilmente è maneggiabile come matrice densa
#oppure

# HO FINALMENTE SCOPERTO COME SI METTE IL DTYPE COME SI DEVE!! con "numpy.float32"!
#tensorio = numpy.zeros((3,3,3), dtype = numpy.float32)
#tensorio.dtype
#tensorio


scalarray = numpy.arange(10)
uniarray = numpy.ones(10)
scalamatricia = numpy.outer(scalarray,scalarray)
#scalamatricia

In [245]:
tensorio * 2
tensorio + 2
tensorio + scalamatricia
%time tensorio + scalarray
%time tensorio.__add__(scalarray)
#danno stesso risultato con tempi paragonabili


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 37.4 µs
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 22.2 µs
Out[245]:
array([[[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]],

       [[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
        [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]]])

Prove matrici sparse


In [2]:
from scipy import sparse

In [3]:
ramatricia = numpy.random.randint(2, size=25).reshape((5,5))
ramatricia


Out[3]:
array([[1, 1, 0, 1, 0],
       [1, 0, 0, 1, 1],
       [0, 0, 1, 0, 0],
       [1, 0, 0, 1, 0],
       [0, 0, 1, 0, 0]])

In [4]:
#efficiente per colonne
#sparsamatricia = sparse.csc_matrix(ramatricia)
#print(sparsamatricia)

#per righe
sparsamatricia = sparse.csr_matrix(ramatricia)
print(sparsamatricia)


  (0, 0)	1
  (0, 1)	1
  (0, 3)	1
  (1, 0)	1
  (1, 3)	1
  (1, 4)	1
  (2, 2)	1
  (3, 0)	1
  (3, 3)	1
  (4, 2)	1

In [7]:
sparsamatricia.toarray()


Out[7]:
array([[1, 1, 0, 1, 0],
       [1, 0, 0, 1, 1],
       [0, 0, 1, 0, 0],
       [1, 0, 0, 1, 0],
       [0, 0, 1, 0, 0]], dtype=int64)

In [28]:
righe = numpy.array([0,0,0,1,2,3,3,4])
colonne = numpy.array([0,0,4,2,1,4,3,0])
valori = numpy.ones(righe.size)
sparsamatricia = sparse.coo_matrix((valori, (righe,colonne)))

In [29]:
print(sparsamatricia)


  (0, 0)	1.0
  (0, 0)	1.0
  (0, 4)	1.0
  (1, 2)	1.0
  (2, 1)	1.0
  (3, 4)	1.0
  (3, 3)	1.0
  (4, 0)	1.0

In [30]:
sparsamatricia.toarray()


Out[30]:
array([[ 2.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  1.],
       [ 1.,  0.,  0.,  0.,  0.]])

Prodotto di matrici

Prodotti interni

Considera di avere 2 matrici, a e b, in forma numpy array:

  1. a*b fa il prodotto elemento per elemento (solo se a e b hanno stessa dimensione)
  2. numpy.dot(a,b) fa il prodotto matriciale righe per colonne

Ora considera di avere 2 matrici, a e b, in forma di scipy.sparse:

  1. a*b fa il prodotto matriciale righe per colonne
  2. numpy.dot(a,b) non funziona per nulla
  3. a.dot(b) fa il prodotto matriciale righe per colonne

In [249]:
#vari modi per fare prodotti di matrici (con somma con operatore + è lo stesso)
densamatricia = sparsamatricia.toarray()

#densa-densa
prodottoPerElementiDD = densamatricia*densamatricia
prodottoMatricialeDD = numpy.dot(densamatricia, densamatricia)

#sparsa-densa
prodottoMatricialeSD = sparsamatricia*densamatricia
prodottoMatricialeSD2 = sparsamatricia.dot(densamatricia)

#sparsa-sparsa
prodottoMatricialeSS = sparsamatricia*sparsamatricia
prodottoMatricialeSS2 = sparsamatricia.dot(sparsamatricia)

# "SPARSA".dot("SPARSA O DENSA") FA PRODOTTO MATRICIALE
# "SPARSA * SPARSA" FA PRODOTTO MATRICIALE

In [250]:
prodottoMatricialeDD - prodottoMatricialeSS
#nb somme e sottrazioni tra matrici sparse e dense sono ok
# prodotto matriciale tra densa e sparsa funziona come sparsa e sparsa


Out[250]:
matrix([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=int64)

Prodotti esterni


In [251]:
densarray = numpy.array(["a","b"],dtype = object)
densarray2 = numpy.array(["c","d"],dtype = object)

numpy.outer(densarray,[1,2])

densamatricia = numpy.array([[1,2],[3,4]])
densamatricia2 = numpy.array([["a","b"],["c","d"]], dtype = object)
numpy.outer(densamatricia2,densamatricia).reshape(4,2,2)


Out[251]:
array([[['a', 'aa'],
        ['aaa', 'aaaa']],

       [['b', 'bb'],
        ['bbb', 'bbbb']],

       [['c', 'cc'],
        ['ccc', 'cccc']],

       [['d', 'dd'],
        ['ddd', 'dddd']]], dtype=object)

In [252]:
densarray1 = numpy.array([0,2])
densarray2 = numpy.array([5,0])
densamatricia = numpy.array([[1,2],[3,4]])
densamatricia2 = numpy.array([[0,2],[5,0]])

nrighe = 2
ncolonne = 2
npiani = 4
prodottoEstDD = numpy.outer(densamatricia,densamatricia2).reshape(npiani,ncolonne,nrighe)
#prodottoEstDD
#prodottoEstDD = numpy.dstack((prodottoEstDD[0,:],prodottoEstDD[1,:]))

prodottoEstDD


Out[252]:
array([[[ 0,  2],
        [ 5,  0]],

       [[ 0,  4],
        [10,  0]],

       [[ 0,  6],
        [15,  0]],

       [[ 0,  8],
        [20,  0]]])

In [253]:
sparsarray1 = sparse.csr_matrix(densarray1)
sparsarray2 = sparse.csr_matrix(densarray2)
sparsamatricia = sparse.csr_matrix(densamatricia)
sparsamatricia2 = sparse.csr_matrix(densamatricia2)

prodottoEstSS = sparse.kron(sparsamatricia,sparsamatricia2).toarray()

prodottoEstSD = sparse.kron(sparsamatricia,densamatricia2).toarray()
prodottoEstSD


Out[253]:
array([[ 0,  2,  0,  4],
       [ 5,  0, 10,  0],
       [ 0,  6,  0,  8],
       [15,  0, 20,  0]], dtype=int64)

In [254]:
#prove prodotti esterni
# numpy.outer
# scipy.sparse.kron

#densa-densa
prodottoEsternoDD = numpy.outer(densamatricia,densamatricia)

#sparsa-densa
prodottoEsternoSD = sparse.kron(sparsamatricia,densamatricia)

#sparsa-sparsa
prodottoEsternoSS = sparse.kron(sparsamatricia,sparsamatricia)

In [255]:
prodottoEsternoDD-prodottoEsternoSS


Out[255]:
matrix([[ 0,  0,  1,  0],
        [-1,  0,  0,  0],
        [ 0,  0,  5,  4],
        [-5, -4,  0,  0]], dtype=int64)

In [256]:
# altre prove di prodotti esterni
rarray1 = numpy.random.randint(2, size=4)
rarray2 = numpy.random.randint(2, size=4)
print(rarray1,rarray2)
ramatricia = numpy.outer(rarray1,rarray2)
unimatricia = numpy.ones((4,4)).astype(int)
#ramatricia2 = rarray1 * rarray2.T
print(ramatricia,unimatricia)
#print(ramatricia)
#print("eppoi")
#print(ramatricia2)

#sparsarray = sparse.csr_matrix(rarray1)
#print(sparsarray)

#ramatricia2 =


[1 1 0 1] [1 0 0 0]
[[1 0 0 0]
 [1 0 0 0]
 [0 0 0 0]
 [1 0 0 0]] [[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]

In [257]:
#il mio caso problematico è che ho una matrice di cui so tutti gli elementi non zero,
#so quante righe ho (i tempi), ma non so quante colonne di freq ho
randomcolonne = numpy.random.randint(10)+1
ramatricia = numpy.random.randint(2, size=10*randomcolonne).reshape((10,randomcolonne))
print(ramatricia.shape)
#ramatricia
nonzeri = numpy.nonzero(ramatricia)
ndati = len(nonzeri[0])
ndati
ramatricia


(10, 9)
Out[257]:
array([[0, 1, 1, 1, 1, 0, 0, 0, 1],
       [1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 0, 0, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 1, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 1],
       [0, 1, 1, 1, 1, 1, 0, 0, 0],
       [1, 0, 1, 0, 1, 1, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0],
       [0, 1, 1, 1, 1, 0, 0, 0, 1],
       [0, 1, 0, 0, 0, 1, 1, 0, 1]])

In [260]:
#ora cerco di fare la matrice sparsa
print(ndati)
dati = numpy.ones(2*ndati).reshape(ndati,2)
dati
coordinateRighe = nonzeri[0]
coordinateColonne = nonzeri[1]
sparsamatricia = sparse.coo_matrix((dati,(coordinateRighe,coordinateColonne)))
densamatricia = sparsamatricia.toarray()
densamatricia


42
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-260-b593495b1cc3> in <module>()
      5 coordinateRighe = nonzeri[0]
      6 coordinateColonne = nonzeri[1]
----> 7 sparsamatricia = sparse.coo_matrix((dati,(coordinateRighe,coordinateColonne)))
      8 densamatricia = sparsamatricia.toarray()
      9 densamatricia

/home/protoss/.local/lib/python3.5/site-packages/scipy/sparse/coo.py in __init__(self, arg1, shape, dtype, copy)
    182             self.data = self.data.astype(dtype, copy=False)
    183 
--> 184         self._check()
    185 
    186     def getnnz(self, axis=None):

/home/protoss/.local/lib/python3.5/site-packages/scipy/sparse/coo.py in _check(self)
    226         self.data = to_native(self.data)
    227 
--> 228         if self.nnz > 0:
    229             if self.row.max() >= self.shape[0]:
    230                 raise ValueError('row index exceeds matrix dimensions')

/home/protoss/.local/lib/python3.5/site-packages/scipy/sparse/base.py in nnz(self)
    199         count_nonzero : Number of non-zero entries
    200         """
--> 201         return self.getnnz()
    202 
    203     def getformat(self):

/home/protoss/.local/lib/python3.5/site-packages/scipy/sparse/coo.py in getnnz(self, axis)
    193             if self.data.ndim != 1 or self.row.ndim != 1 or \
    194                     self.col.ndim != 1:
--> 195                 raise ValueError('row, column, and data arrays must be 1-D')
    196 
    197             return int(nnz)

ValueError: row, column, and data arrays must be 1-D

Provo a passare operazioni a array con array di coordinate


In [83]:
matrice = numpy.arange(30).reshape(10,3)
matrice


Out[83]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17],
       [18, 19, 20],
       [21, 22, 23],
       [24, 25, 26],
       [27, 28, 29]])

In [78]:
righe = numpy.array([1,0,1,1])
colonne = numpy.array([2,0,2,2])
pesi = numpy.array([100,200,300,10])
print(righe,colonne)


[1 0 1 1] [2 0 2 2]

In [22]:
matrice[righe,colonne]


Out[22]:
array([5, 0, 5, 5])

In [79]:
matrice[righe,colonne] = (matrice[righe,colonne] + numpy.array([100,200,300,10]))
matrice


Out[79]:
array([[200,   1,   2],
       [  3,   4,  15],
       [  6,   7,   8],
       [  9,  10,  11],
       [ 12,  13,  14],
       [ 15,  16,  17],
       [ 18,  19,  20],
       [ 21,  22,  23],
       [ 24,  25,  26],
       [ 27,  28,  29]])

In [76]:
%matplotlib inline
a = pyplot.imshow(matrice)



In [81]:
numpy.add.at(matrice, [righe,colonne],pesi)
matrice


Out[81]:
array([[200,   1,   2],
       [  3,   4, 415],
       [  6,   7,   8],
       [  9,  10,  11],
       [ 12,  13,  14],
       [ 15,  16,  17],
       [ 18,  19,  20],
       [ 21,  22,  23],
       [ 24,  25,  26],
       [ 27,  28,  29]])

In [82]:
%matplotlib inline
a = pyplot.imshow(matrice)



In [ ]:
matr

Prove plots


In [33]:
from matplotlib import pyplot
%matplotlib inline

In [156]:
##AL MOMENTO INUTILE, NON COMPILARE
x = numpy.random.randint(10,size = 10)
y = numpy.random.randint(10,size = 10)
pyplot.scatter(x,y, s = 5)
#nb imshow si può fare solo con un 2d array


Out[156]:
<matplotlib.collections.PathCollection at 0x7faca7884f28>

In [27]:
#visualizzazione di una matrice, solo matrici dense a quanto pare
a = pyplot.imshow(densamatricia)
#a = pyplot.imshow(sparsamatricia)
#c = pyplot.matshow(densamatricia)



In [ ]:
#spy invece funziona anche per le sparse!
pyplot.spy(sparsamatricia,precision=0.01, marker = ".", markersize=10)

In [171]:
#in alternativa, scatterplot delle coordinate dal dataframe
b = pyplot.scatter(coordinateColonne,coordinateRighe, s = 2)



In [122]:
import seaborn
%matplotlib inline


sbRegplot = seaborn.regplot(x=coordinateRighe, y=coordinateColonne, color="g", fit_reg=False)



In [113]:
import pandas

coordinateRighe = coordinateRighe.reshape(len(coordinateRighe),1)
coordinateColonne = coordinateColonne.reshape(len(coordinateColonne),1)
#print([coordinateRighe,coordinateColonne])
coordinate = numpy.concatenate((coordinateRighe,coordinateColonne),axis = 1)
coordinate

In [118]:
tabella = pandas.DataFrame(coordinate)
tabella.columns = ["righe", "colonne"]

In [121]:
sbPlmplot = seaborn.lmplot(x = "righe", y = "colonne", data = tabella, fit_reg=False)


Un esempio semplice del mio problema


In [26]:
import numpy
from scipy import sparse
import multiprocessing
from matplotlib import pyplot

#first i build a matrix of some x positions vs time datas in a sparse format
matrix = numpy.random.randint(2, size = 100).astype(float).reshape(10,10)
x = numpy.nonzero(matrix)[0]
times = numpy.nonzero(matrix)[1]
weights = numpy.random.rand(x.size)

In [27]:
import scipy.io

mint = numpy.amin(times)
maxt = numpy.amax(times)

scipy.io.savemat('debugExamples/numpy.mat',{
    'matrix':matrix, 
    'x':x, 
    'times':times, 
    'weights':weights,
    'mint':mint,
    'maxt':maxt,
    
})

In [28]:
times


Out[28]:
array([0, 1, 2, 3, 4, 6, 7, 8, 9, 0, 1, 2, 3, 4, 7, 8, 9, 0, 1, 2, 4, 5, 8,
       0, 2, 5, 6, 9, 0, 3, 5, 7, 8, 2, 3, 4, 6, 7, 8, 9, 4, 6, 0, 3, 5, 6,
       9, 1, 5, 6, 0, 1, 4, 5, 6])

In [29]:
#then i define an array of y positions
nStepsY = 5
y = numpy.arange(1,nStepsY+1)

In [30]:
# provo a iterare
# VERSIONE CON HACK CON SPARSE verificato viene uguale a tutti gli altri metodi più semplici che ho provato
# ma ha problemi con parallelizzazione

nRows = nStepsY
nColumns = 80
y = numpy.arange(1,nStepsY+1)
image = numpy.zeros((nRows, nColumns))
def itermatrix(ithStep):
    yTimed = y[ithStep]*times
    positions = (numpy.round(x-yTimed)+50).astype(int)

    fakeRow = numpy.zeros(positions.size)
    matrix = sparse.coo_matrix((weights, (fakeRow, positions))).todense()
    matrix = numpy.ravel(matrix)
    missColumns = (nColumns-matrix.size)
    zeros = numpy.zeros(missColumns)
    matrix = numpy.concatenate((matrix, zeros))
    return matrix

#for i in numpy.arange(nStepsY):
#    image[i] = itermatrix(i)

#or
imageSparsed = list(map(itermatrix, range(nStepsY)))
imageSparsed = numpy.array(imageSparsed)
scipy.io.savemat('debugExamples/numpyResult.mat', {'imageSparsed':imageSparsed})    
a = pyplot.imshow(imageSparsed, aspect = 10)
pyplot.show()



In [4]:
import numpy
from scipy import sparse
import multiprocessing
from matplotlib import pyplot

#first i build a matrix of some x positions vs time datas in a sparse format
matrix = numpy.random.randint(2, size = 100).astype(float).reshape(10,10)
times = numpy.nonzero(matrix)[0]
freqs = numpy.nonzero(matrix)[1]
weights = numpy.random.rand(times.size)

#then i define an array of y positions
nStepsSpindowns = 5
spindowns = numpy.arange(1,nStepsSpindowns+1)


#PROVA CON BINCOUNT

def mapIt(ithStep):
    ncolumns = 80
    image = numpy.zeros(ncolumns)

    sdTimed = spindowns[ithStep]*times
    positions = (numpy.round(freqs-sdTimed)+50).astype(int)

    values = numpy.bincount(positions,weights)
    values = values[numpy.nonzero(values)]
    positions = numpy.unique(positions)
    image[positions] = values
    return image


%time    imageMapped = list(map(mapIt, range(nStepsSpindowns)))
imageMapped = numpy.array(imageMapped)

%matplotlib inline
a = pyplot.imshow(imageMapped, aspect = 10)


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 330 µs

In [6]:
# qui provo fully vectorial
def fullmatrix(nRows, nColumns):
    spindowns = numpy.arange(1,nStepsSpindowns+1)
    image = numpy.zeros((nRows, nColumns))

    sdTimed = numpy.outer(spindowns,times)
    freqs3d = numpy.outer(numpy.ones(nStepsSpindowns),freqs)
    weights3d = numpy.outer(numpy.ones(nStepsSpindowns),weights)
    spindowns3d = numpy.outer(spindowns,numpy.ones(times.size))
    positions = (numpy.round(freqs3d-sdTimed)+50).astype(int)

    matrix = sparse.coo_matrix((numpy.ravel(weights3d), (numpy.ravel(spindowns3d), numpy.ravel(positions)))).todense()
    return matrix

%time image = fullmatrix(nStepsSpindowns, 80)
a = pyplot.imshow(image, aspect = 10)
pyplot.show()


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 366 µs

Confronti Debug!


In [34]:
#confronto con codice ORIGINALE in matlab
immagineOrig = scipy.io.loadmat('debugExamples/dbOrigResult.mat')['binh_df0']
a = pyplot.imshow(immagineOrig[:,0:80], aspect = 10)
pyplot.show()



In [16]:
#PROVA CON BINCOUNT

def mapIt(ithStep):
    ncolumns = 80
    image = numpy.zeros(ncolumns)

    yTimed = y[ithStep]*times
    positions = (numpy.round(x-yTimed)+50).astype(int)

    values = numpy.bincount(positions,weights)
    where = tf.not_equal(values, zero)
    values = values[numpy.nonzero(values)]
    positions = numpy.unique(positions)
    image[positions] = values
    return image


%time    imageMapped = list(map(mapIt, range(nStepsY)))
imageMapped = numpy.array(imageMapped)

%matplotlib inline
a = pyplot.imshow(imageMapped, aspect = 10)


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 528 µs

In [12]:
# qui provo con vettorializzazione di numpy (apply along axis)
nrows = nStepsY
ncolumns = 80
matrix = numpy.zeros(nrows*ncolumns).reshape(nrows,ncolumns)

def applyIt(image):
    ithStep = 1
    image = numpy.zeros(ncolumns)

    yTimed = y[ithStep]*times
    positions = (numpy.round(x-yTimed)+50).astype(int)
    #print(positions)
    values = numpy.bincount(positions,weights)
    values = values[numpy.nonzero(values)]
    positions = numpy.unique(positions)
    image[positions] = values
    
    return image


imageApplied = numpy.apply_along_axis(applyIt,1,matrix)
a = pyplot.imshow(imageApplied, aspect = 10)



In [13]:
# qui provo fully vectorial
def fullmatrix(nRows, nColumns):
    y = numpy.arange(1,nStepsY+1)
    image = numpy.zeros((nRows, nColumns))

    yTimed = numpy.outer(y,times)
    x3d = numpy.outer(numpy.ones(nStepsY),x)
    weights3d = numpy.outer(numpy.ones(nStepsY),weights)
    y3d = numpy.outer(y,numpy.ones(x.size))
    positions = (numpy.round(x3d-yTimed)+50).astype(int)

    matrix = sparse.coo_matrix((numpy.ravel(weights3d), (numpy.ravel(y3d), numpy.ravel(positions)))).todense()
    return matrix

%time image = fullmatrix(nStepsY, 80)
a = pyplot.imshow(image, aspect = 10)
pyplot.show()


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 309 µs

In [47]:
imageMapped = list(map(itermatrix, range(nStepsY)))
imageMapped = numpy.array(imageMapped)
a = pyplot.imshow(imageMapped, aspect = 10)
pyplot.show()



In [10]:
# prova con numpy.put

nStepsY = 5

def mapIt(ithStep):
    ncolumns = 80
    image = numpy.zeros(ncolumns)

    yTimed = y[ithStep]*times
    positions = (numpy.round(x-yTimed)+50).astype(int)

    values = numpy.bincount(positions,weights)
    values = values[numpy.nonzero(values)]
    positions = numpy.unique(positions)
    image[positions] = values
    return image


%time    imagePutted = list(map(mapIt, range(nStepsY)))
imagePutted = numpy.array(imagePutted)

%matplotlib inline
a = pyplot.imshow(image, aspect = 10)
pyplot.show()


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 541 µs

In [ ]:


In [20]:
ramatricia = numpy.random.randint(10, size=120).reshape((5,4,3,2))
print(ramatricia[0,0,:,:])
#print(ramatricia)
ramatricia


[[1 1]
 [9 0]
 [1 2]]

In [26]:
print(ramatricia.reshape(60,2).shape[0])


60

In [ ]: