scipy append all rows of one sparse matrix to another

2024/10/12 12:31:32

I have a numpy matrix and want to append another matrix to that.

The two matrices have the shapes:

m1.shape = (2777, 5902)  m2.shape = (695, 5902)

I want to append m2 to m1 so that the new matrix is of shape:

m_new.shape = (3472, 5902)

When I use numpy.append or numpy.concatenate I just get a new array with the two matrix in it and the shape (2,1).

Any one of you have an Idea how to get one big matrix out of the two?

Additional info: both are sparse matrices.

EDIT: m1 looks like

(0, 1660)   0.444122811195
(0, 3562)   0.260868771714
(0, 4743)   0.288149437574
(0, 4985)   0.514889706991
(0, 5215)   0.272163636657
(0, 5721)   0.559006134727
(1, 555)    0.0992498400527
(1, 770)    0.133145289523
(1, 790)    0.0939044698233
(1, 1097)   0.259867567986
(1, 1285)   0.188836288168
(1, 1366)   0.24707459927
(1, 1499)   0.237997843516
(1, 1559)   0.120069347224
(1, 1701)   0.17660176488
(1, 1926)   0.185678520634
(1, 2177)   0.163066377369
(1, 2641)   0.079958199952
(1, 2937)   0.259867567986
(1, 3551)   0.198471489351
(1, 3562)   0.0926197593026
(1, 3593)   0.100537828805
(1, 4122)   0.198471489351
(1, 4538)   0.57162654484
(1, 4827)   0.105808609537

m2 looks like:

(0, 327)    0.0770581299315(0, 966)  0.309858753157(0, 1231) 0.286870892505(0, 1384) 0.281385698712(0, 1817) 0.204495931592(0, 2284) 0.182420951496(0, 2414) 0.114591086901(0, 2490) 0.261442040482(0, 3122) 0.321676138471(0, 3151) 0.286870892505(0, 4031) 0.172251612658(0, 5149) 0.25839783806(0, 5215) 0.125806303262(0, 5225) 0.336280781816(0, 5231) 0.135930403721(0, 5294) 0.145049459537(0, 5794) 0.20145172917(0, 5821) 0.224439589822(1, 327)  0.191031948626(1, 1171) 0.62081265022

Type of the matrices is:

<class 'scipy.sparse.csr.csr_matrix'> <class 'scipy.sparse.csr.csr_matrix'>

SOLVED:

m_new = scipy.sparse.vstack((m1, m2))

did the trick

Thanks for your help.

Answer

You can use numpy.vstack in your case (or numpy.hstack, when matrices shapes are (x,y) and (x,z))

Example:

a = np.zeros((3,7))
b = np.zeros((46,7))
c = np.vstack((a,b))
print c.shape
#(49,7)
https://en.xdnf.cn/q/69653.html

Related Q&A

Add argparse arguments from external modules

Im trying to write a Python program that could be extended by third parties. The program will be run from the command line with whatever arguments are supplied.In order to allow third parties to creat…

Cosine similarity for very large dataset

I am having trouble with calculating cosine similarity between large list of 100-dimensional vectors. When I use from sklearn.metrics.pairwise import cosine_similarity, I get MemoryError on my 16 GB ma…

What exactly are the csv modules Dialect settings for excel-tab?

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the formatpreferred by Excel,” or “read data from this file which wa…

Python: how to make a recursive generator function

I have been working on generating all possible submodels for a biological problem. I have a working recursion for generating a big list of all the submodels I want. However, the lists get unmanageably …

Change default options in pandas

Im wondering if theres any way to change the default display options for pandas. Id like to change the display formatting as well as the display width each time I run python, eg:pandas.options.display.…

python-messaging Failed to handle HTTP request

I am using the code below to try to send an MMS message with python-messaging https://github.com/pmarti/python-messaging/blob/master/doc/tutorial/mms.rst Although the connection seems to go smoothly I …

Plotting confidence and prediction intervals with repeated entries

I have a correlation plot for two variables, the predictor variable (temperature) on the x-axis, and the response variable (density) on the y-axis. My best fit least squares regression line is a 2nd or…

Saving and Loading of dataframe to csv results in Unnamed columns

prob in the title. exaple:x=[(a,a,c) for i in range(5)] df = DataFrame(x,columns=[col1,col2,col3]) df.to_csv(test.csv) df1 = read_csv(test.csv)Unnamed: 0 col1 col2 col3 0 0 a a c 1 …

Python: print specific character from string

How do I print a specific character from a string in Python? I am still learning and now trying to make a hangman like program. The idea is that the user enters one character, and if it is in the word…

Python AttributeError: module string has no attribute maketrans

I am receiving the below error when trying to run a command in Python 3.5.2 shell:Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32 Type "copyrig…