第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Python中連接兩個(gè)矩陣?

如何在Python中連接兩個(gè)矩陣?

白板的微信 2021-03-12 10:07:43
我有兩個(gè)csr_matrix,uniFeature和biFeature。我想要一個(gè)新的矩陣Feature = [uniFeature, biFeature]。但是,如果我以這種方式直接連接它們,則會(huì)出現(xiàn)一個(gè)錯(cuò)誤,指出矩陣Feature是一個(gè)列表。如何實(shí)現(xiàn)矩陣級(jí)聯(lián)并仍然獲得相同類型的矩陣,即a csr_matrix?如果我在連接后執(zhí)行此操作,將不起作用:Feature = csr_matrix(Feature) 它給出了錯(cuò)誤:Traceback (most recent call last):  File "yelpfilter.py", line 91, in <module>    Feature = csr_matrix(Feature)  File "c:\python27\lib\site-packages\scipy\sparse\compressed.py", line 66, in __init__    self._set_self( self.__class__(coo_matrix(arg1, dtype=dtype)) )  File "c:\python27\lib\site-packages\scipy\sparse\coo.py", line 185, in __init__    self.row, self.col = M.nonzero()TypeError: __nonzero__ should return bool or int, returned numpy.bool_
查看完整描述

1 回答

?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

該scipy.sparse模塊包括功能hstack和vstack。


例如:


In [44]: import scipy.sparse as sp


In [45]: c1 = sp.csr_matrix([[0,0,1,0],

    ...:                     [2,0,0,0],

    ...:                     [0,0,0,0]])


In [46]: c2 = sp.csr_matrix([[0,3,4,0],

    ...:                     [0,0,0,5],

    ...:                     [6,7,0,8]])


In [47]: h = sp.hstack((c1, c2), format='csr')


In [48]: h

Out[48]: 

<3x8 sparse matrix of type '<type 'numpy.int64'>'

    with 8 stored elements in Compressed Sparse Row format>


In [49]: h.A

Out[49]: 

array([[0, 0, 1, 0, 0, 3, 4, 0],

       [2, 0, 0, 0, 0, 0, 0, 5],

       [0, 0, 0, 0, 6, 7, 0, 8]])


In [50]: v = sp.vstack((c1, c2), format='csr')


In [51]: v

Out[51]: 

<6x4 sparse matrix of type '<type 'numpy.int64'>'

    with 8 stored elements in Compressed Sparse Row format>


In [52]: v.A

Out[52]: 

array([[0, 0, 1, 0],

       [2, 0, 0, 0],

       [0, 0, 0, 0],

       [0, 3, 4, 0],

       [0, 0, 0, 5],

       [6, 7, 0, 8]])


查看完整回答
反對 回復(fù) 2021-03-29
  • 1 回答
  • 0 關(guān)注
  • 330 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)