[SciPy-user] Creating a sparse matrix

Nils Wagner nwagner@iam.uni-stuttgart...
Thu Dec 13 06:57:25 CST 2007


Hi all,

How can I build a sparse matrix from the following array ?
>>> data
array([['1', '1', '1.7244067583090530E+05'],
        ['1', '2', '4.7526228631699840E+04'],
        ['2', '2', '1.7244064192412970E+05'],
        ['3', '3', '2.7456944493758570E+02'],
        ['1', '4', '2.3763114315849920E+05'],
        ['2', '4', '8.6220320962064820E+05'],
        ['3', '4', '5.9378661309939430E+02'],
        ['4', '4', '4.3948451437807490E+06'],
        ['1', '5', '-8.6220337915452610E+05'],
        ['2', '5', '-2.3763114315849920E+05'],
        ['3', '5', '-5.9378754413417760E+02'],
        ['4', '5', '-1.1952119204729520E+06'],
        ['5', '5', '4.3948459988011970E+06'],
        ['6', '6', '4.2234547275787270E+03'],
        ['1', '7', '-1.6598232969237920E+04'],
        ['2', '7', '-6.4128987931960360E+03'],
        ['4', '7', '-3.2064493965980180E+04'],
        ['5', '7', '8.2991164846189540E+04'],
        ['7', '7', '1.7244065067439350E+05'],
        ['1', '8', '6.4129110657815880E+03'],
        ['2', '8', '-6.0501784478906850E+04'],
        ['4', '8', '-3.0250892239453440E+05'],
        ['5', '8', '-3.2064555328907960E+04'],
        ['7', '8', '-4.7526234795758550E+04'],
        ['8', '8', '1.7244065036281190E+05'],
        ['3', '9', '-6.6701524177624400E+01'],
        ['4', '9', '1.0014969892580470E+03'],
        ['5', '9', '1.8006472158568300E+02'],
        ['9', '9', '2.7456950540497500E+02'],
        ['1', '10', '3.2064555328907960E+04'],
        ['2', '10', '-3.0250892239453440E+05'],
        ['3', '10', '-1.0014968431605310E+03'],
        ['4', '10', '-1.4981909287503440E+06'],
        ['5', '10', '-1.5690996463630880E+05'],
        ['7', '10', '-2.3763117397879280E+05'],
        ['8', '10', '8.6220325181405960E+05'],
        ['9', '10', '-5.9378786697859830E+02'],
        ['10', '10', '4.3948453583907450E+06'],
        ['1', '11', '8.2991164846189560E+04'],
        ['2', '11', '3.2064493965980140E+04'],
        ['3', '11', '1.8006434238808650E+02'],
        ['4', '11', '1.5690966299256120E+05'],
        ['5', '11', '-4.2216699312324380E+05'],
        ['7', '11', '-8.6220325337196720E+05'],
        ['8', '11', '2.3763117397879260E+05'],
        ['9', '11', '-5.9378773737342270E+02'],
        ['10', '11', '1.1952120829330800E+06'],
        ['11', '11', '4.3948453666102750E+06'],
        ['6', '12', '-1.0245631116652000E+03'],
        ['12', '12', '4.2234546752310640E+03'],
        ['1', '13', '-2.1562536183412980E+04'],
        ['2', '13', '-1.3326854263605380E+04'],
        ['4', '13', '-6.6634271318026890E+04'],
        ['5', '13', '1.0781268091706490E+05'],
        ['7', '13', '-6.0501787019015210E+04'],
        ['8', '13', '-6.4129083147729780E+03'],
        ['10', '13', '-3.2064541573864920E+04'],
        ['11', '13', '3.0250893509507610E+05'],
        ['13', '13', '1.7244065108409200E+05'],
        ['1', '14', '-1.3326856939548480E+04'],
        ['2', '14', '-2.1562538882437520E+04'],
        ['4', '14', '-1.0781269441218760E+05'],
        ['5', '14', '6.6634284697742330E+04'],
        ['7', '14', '6.4129066592753080E+03'],
        ['8', '14', '-1.6598230710213180E+04'],
        ['10', '14', '-8.2991153551065850E+04'],
        ['11', '14', '-3.2064533296376550E+04'],
        ['13', '14', '4.7526232819775960E+04'],
        ['14', '14', '1.7244065558155810E+05'],
        ['3', '15', '3.9071327498923200E+00'],
        ['4', '15', '1.1208665237160810E+02'],
        ['5', '15', '-1.1208648718752250E+02'],
        ['9', '15', '-6.6701524986800910E+01'],
        ['10', '15', '1.8006454779629130E+02'],
        ['11', '15', '-1.0014969364802770E+03'],
        ['15', '15', '2.7456952836064130E+02'],
        ['1', '16', '-6.6634284697742330E+04'],
        ['2', '16', '-1.0781269441218760E+05'],
        ['3', '16', '-1.1208653552827830E+02'],
        ['4', '16', '-5.3738372531588020E+05'],
        ['5', '16', '3.3136527542132390E+05'],
        ['7', '16', '3.2064533296376570E+04'],
        ['8', '16', '-8.2991153551065860E+04'],
        ['9', '16', '1.8006450558919260E+02'],
        ['10', '16', '-4.2216693696279110E+05'],
        ['11', '16', '-1.5690985700615200E+05'],
        ['13', '16', '2.3763116409887990E+05'],
        ['14', '16', '8.6220327790779050E+05'],
        ['15', '16', '-5.9378754489808300E+02'],
        ['16', '16', '4.3948454932894830E+06'],
        ['1', '17', '1.0781268091706490E+05'],
        ['2', '17', '6.6634271318026870E+04'],
        ['3', '17', '1.1208660539636940E+02'],
        ['4', '17', '3.3136520639089480E+05'],
        ['5', '17', '-5.3738365971501340E+05'],
        ['7', '17', '3.0250893509507610E+05'],
        ['8', '17', '3.2064541573864890E+04'],
        ['9', '17', '1.0014969080580460E+03'],
        ['10', '17', '1.5690989812553410E+05'],
        ['11', '17', '-1.4981909926673520E+06'],
        ['13', '17', '-8.6220325542046030E+05'],
        ['14', '17', '-2.3763116409887980E+05'],
        ['15', '17', '5.9378787780320290E+02'],
        ['16', '17', '-1.1952120315502540E+06'],
        ['17', '17', '4.3948453781456250E+06'],
        ['6', '18', '-4.0834357736871400E+02'],
        ['12', '18', '-1.0245631207891270E+03'],
        ['18', '18', '4.2234546579613690E+03'],
        ['1', '19', '-6.0501796429481100E+04'],
        ['2', '19', '6.4128982934224380E+03'],
        ['4', '19', '3.2064491467112190E+04'],
        ['5', '19', '3.0250898214740550E+05'],
        ['7', '19', '-2.1562538882438510E+04'],
        ['8', '19', '1.3326853882609100E+04'],
        ['10', '19', '6.6634269413045520E+04'],
        ['11', '19', '1.0781269441219260E+05'],
        ['13', '19', '-1.6598239767392180E+04'],
        ['14', '19', '-6.4129061595026810E+03'],
        ['16', '19', '-3.2064530797513400E+04'],
        ['17', '19', '8.2991198836960840E+04'],
        ['19', '19', '1.7244064683129310E+05'],
        ['1', '20', '-6.4129127399249150E+03'],
        ['2', '20', '-1.6598242026418230E+04'],
        ['4', '20', '-8.2991210132091170E+04'],
        ['5', '20', '3.2064563699624550E+04'],
        ['7', '20', '1.3326857320545740E+04'],
        ['8', '20', '-2.1562536183413320E+04'],
        ['10', '20', '-1.0781268091706660E+05'],
        ['11', '20', '-6.6634286602728680E+04'],
        ['13', '20', '6.4129099889152090E+03'],
        ['14', '20', '-6.0501798969589890E+04'],
        ['16', '20', '-3.0250899484794950E+05'],
        ['17', '20', '-3.2064549944576080E+04'],
        ['19', '20', '-4.7526226655715920E+04'],
        ['20', '20', '1.7244067655218550E+05'],
        ['3', '21', '-6.6701552188476970E+01'],
        ['4', '21', '-1.8006429542064950E+02'],
        ['5', '21', '-1.0014970368071590E+03'],
        ['9', '21', '3.9071327498907450E+00'],
        ['10', '21', '-1.1208655989768520E+02'],
        ['11', '21', '-1.1208657177767270E+02'],
        ['15', '21', '-6.6701552997659000E+01'],
        ['16', '21', '-1.0014972526987650E+03'],
        ['17', '21', '-1.8006453997854880E+02'],
        ['21', '21', '2.7456946789324640E+02'],
        ['1', '22', '-3.2064563699624570E+04'],
        ['2', '22', '-8.2991210132091170E+04'],
        ['3', '22', '-1.8006471376798420E+02'],
        ['4', '22', '-4.2216722298368720E+05'],
        ['5', '22', '1.5691000662161880E+05'],
        ['7', '22', '6.6634286602728650E+04'],
        ['8', '22', '-1.0781268091706660E+05'],
        ['9', '22', '1.1208653268623420E+02'],
        ['10', '22', '-5.3738365971502150E+05'],
        ['11', '22', '-3.3136528414261740E+05'],
        ['13', '22', '3.2064549944576050E+04'],
        ['14', '22', '-3.0250899484794950E+05'],
        ['15', '22', '1.0014971017047370E+03'],
        ['16', '22', '-1.4981912910366350E+06'],
        ['17', '22', '-1.5690994011081730E+05'],
        ['19', '22', '-2.3763113327857960E+05'],
        ['20', '22', '8.6220338276092740E+05'],
        ['21', '22', '5.9378755495883890E+02'],
        ['22', '22', '4.3948460185560810E+06'],
        ['1', '23', '3.0250898214740550E+05'],
        ['2', '23', '-3.2064491467112220E+04'],
        ['3', '23', '1.0014973054764940E+03'],
        ['4', '23', '-1.5690965052770850E+05'],
        ['5', '23', '-1.4981912271196160E+06'],
        ['7', '23', '1.0781269441219260E+05'],
        ['8', '23', '-6.6634269413045550E+04'],
        ['9', '23', '1.1208661612225140E+02'],
        ['10', '23', '-3.3136519766962600E+05'],
        ['11', '23', '-5.3738372531590490E+05'],
        ['13', '23', '8.2991198836960880E+04'],
        ['14', '23', '3.2064530797513400E+04'],
        ['15', '23', '-1.8006445862190810E+02'],
        ['16', '23', '1.5690984454132130E+05'],
        ['17', '23', '-4.2216716682320250E+05'],
        ['19', '23', '-8.6220323415646550E+05'],
        ['20', '23', '2.3763113327857960E+05'],
        ['21', '23', '5.9378642062413570E+02'],
        ['22', '23', '1.1952118690900900E+06'],
        ['23', '23', '4.3948452704599260E+06'],
        ['6', '24', '-1.0245630840369950E+03'],
        ['12', '24', '-4.0834357736871400E+02'],
        ['18', '24', '-1.0245630931609220E+03'],
        ['24', '24', '4.2234547103090340E+03']],
       dtype='|S23')

data contains information about row, column and the 
corresponding entry.

Any pointer would be appreciated.

Thanks in advance
                       Nils


More information about the SciPy-user mailing list