搜尋
首頁後端開發Python教學Python中的np.vstack()和np.hstack()怎麼使用

Python中的np.vstack()和np.hstack()怎麼使用

Apr 18, 2023 pm 01:04 PM
pythonnp.hstack()

在這裡我們介紹兩個拼接數組的方法:

np.vstack():在垂直方向上堆疊

np.hstack():在水平方向上平鋪

import numpy as np
arr1=np.array([1,2,3])
arr2=np.array([4,5,6])
print np.vstack((arr1,arr2))
 
print np.hstack((arr1,arr2))
 
a1=np.array([[1,2],[3,4],[5,6]])
a2=np.array([[7,8],[9,10],[11,12]])
print a1
print a2
print np.hstack((a1,a2))

結果如下:

[[1 2 3]
 [4 5 6]]
[1 2 3 4 5 6]
[[ 1 2]
 [3 4]
 [5 6]]
[[ 7  8]
 [ 9 10]
 [11 12]]
#[[ 1  2  7  8 ]
 [ 3  4  9 10]
 [ 5  6 11 12]]

這裡還需要強調一點,在hstack應用的時候,我在做cs231n上的assignment1的時候,我總是在hstack這裡出錯!才發現我以前學的很膚淺啊!

(1)np.hstack()

函數原型:numpy.hstack(tup)

#其中tup是arrays序列,tup : sequence of ndarrays

The arrays must have the same shape along all but the second axis,except 1-D arrays which can be any length.

等價於:np .concatenate(tup, axis=1)

例子一:

import numpy as np
brr1=np.array([1,2,3,4,55,6,7,77,8,9,99])
brr1_folds=np.array_split(brr1,3)
print brr1_folds
print brr1_folds[0:2]+brr1_folds[1:3]
print np.hstack((brr1_folds[:2]+brr1_folds[1:3]))
print brr1_folds[0:2]
print brr1_folds[1:3]
#print np.hstack((brr1_folds[0:2],brr1_folds[1:3]))

最後一行如果不註解掉就會出錯;

[array([1, 2, 3, 4]), array([55,  6,  7, 77]), array([ 8,  9, 99])]
[array([1, 2, 3, 4]), array ([55,  6,  7, 77]), array([55,  6,  7, 77]), array([ 8,  9, 99])]
[ 1   7 77  8  9 99]
[array([1, 2, 3, 4]), array([55,  6,  7, 77])]
[array([55,  6,  7, 77 ]), array([ 8,  9, 99])]

錯誤的原因就是以為我的array的維度不一致。改成 就好啦,加號是list的拼接!

範例二:

print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))

結果是:顯示了一維的陣列hstack是隨意的。

[1 2 3 3 4 3 4 5 8 6 6 7]

範例三:

顯示我們的hstack必須要第二維度是一樣的:

print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))
print np.hstack(([[1,2,3],[2,3,4]],[[1,2],[2,3]]))

結果:

[1 2 3 3 4 3 4 5 8 6 6 7]
[[1 2 3 1 2]

 [2 3 4 2 3]]

如果你把上面改成下面就會報錯了! ! !

print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))
print np.hstack(([[1,2,3],[2,3,4]],[[1,2]]))

(2)np.vstack()

函數原型:numpy.hstack(tup)

tup : sequence of ndarrays

The arrays must have the same shape along all but the first axis.1-D arrays must have the same length.

#表示我們除了第一個維度可以不一樣外,其他的維度上必須相同的shape。一維的陣列必須大小一樣。

範例一:

print np.vstack(([1,2,3],[3,4,3]))
print np.vstack(([1,2,3],[2,3]))

但你要注意的是第二行是出錯的!

例子二:

print np.vstack(([[1,2,3],[3,4,3]],[[1,3,4],[2,4,5]]))
print np.vstack(([[1,2,3],[3,4,3]],[[3,4],[4,5]]))

同樣的顯示了,如果我們的陣列的第二維不一樣所以出錯了。

print np.vstack(([[1,2,3],[3,4,3]],[[2,4,5]]))
print np.vstack(([[1,2,3],[3,4,3]],[[4,5]]))

範例三:

我們傳入的是list:

import numpy as np
arr1=np.array([[1,2],[2,4],[11,33],[2,44],[55,77],[11,22],[55,67],[67,89]])
arr11=np.array([[11,2,3],[22,3,4],[4,5,6]])
arr1_folds=np.array_split(arr1,3)
print arr1_folds
print np.vstack(arr1_folds)

結果:

[array([[ 1,  2] ,
       [ 2,  4],
       [11, 33]]), array([[ 2, 44],
  #    1155, 77],#  array([[55, 67],
       [67, 89]])]
[[ 1  2]
 [ 2  4]
 [11 33]
 [ 2 44]
 ## [55 77]
 [11 22]
 [55 67]
 [67 89]]

以上是Python中的np.vstack()和np.hstack()怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。