MyCloud

[NumPy] 서로 다른 Matrix를 합치는 방법 본문

Programming/Python

[NumPy] 서로 다른 Matrix를 합치는 방법

Swalloow 2016. 8. 10. 03:21



Numpy - Sparse Matrix


TfidfVectorizer에 bigram을 사용하여 변환된 3068x23466 sparse matrix에 unigram을 이어붙이고 싶을 때,

여러 개의 서로 다른 sparse matrix를 이어붙이는 방법에 대해 적어두려고 합니다.



1. np.c_

import numpy as np


# matrix a, b

np.c_[a, b]

* sparse matrix의 경우 'CClass object is not callable' 오류



2. np.concatenate

import numpy as np


# matrix a, b

np.concatenate((a, b))

* sparse matrix의 경우 'Error While Concatenation - zero-dimensional arrays cannot be concatenated' 오류



3. scipy.hstack

from scipy import sparse


# sparse matrix a, b

sparse.hstack((a, b))

* hstack은 row에 이어 붙이고 싶을 때, vstack은 column에 이어 붙이고 싶을 때 사용






Comments