最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

numpy.reshape()的函數(shù)的具體使用

 更新時間:2023年02月10日 09:58:21   作者:熊野君  
本文主要介紹了numpy.reshape()的函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

np.reshape()基本用法

常用于矩陣規(guī)格變換,將矩陣轉(zhuǎn)換為特定的行和列的矩陣
格式:a1.reshape(x,y,z,…)
注意:將矩陣a1轉(zhuǎn)變成(x, y,z,…)---->一維長度x,二維長度y,三維長度z,…的矩陣。
場景:matlibplot畫圖時x、y軸需要傳入的是一維,可以用reshape()實現(xiàn);再例如需要將多維的變成行向量或列向量時也經(jīng)常要用

numpy.reshape(a, newshape, order='C')[source],參數(shù)`newshape`是啥意思?

根據(jù)Numpy文檔(https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy-reshape)的解釋:

newshape : int or tuple of ints

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, **the value is inferred from the length of the array and remaining dimensions**.

大意是說,數(shù)組新的shape屬性應該要與原來的配套,如果等于-1的話,那么Numpy會根據(jù)剩下的維度計算出數(shù)組的另外一個shape屬性值。

舉幾個例子或許就清楚了,有一個數(shù)組z,它的shape屬性是(4, 4)

z = np.array([[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9, 10, 11, 12],
          [13, 14, 15, 16]])
z.shape
(4, 4)

z.reshape(-1)

z.reshape(-1)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])

z.reshape(-1, 1)

也就是說,先前我們不知道z的shape屬性是多少,但是想讓z變成只有一列,行數(shù)不知道多少,通過`z.reshape(-1,1)`,Numpy自動計算出有12行,新的數(shù)組shape屬性為(16, 1),與原來的(4, 4)配套。

z.reshape(-1,1)
 array([[ 1],
        [ 2],
        [ 3],
        [ 4],
        [ 5],
        [ 6],
        [ 7],
        [ 8],
        [ 9],
        [10],
        [11],
        [12],
        [13],
        [14],
        [15],
        [16]])

z.reshape(-1, 2)

newshape等于-1,列數(shù)等于2,行數(shù)未知,reshape后的shape等于(8, 2)

 z.reshape(-1, 2)
 array([[ 1,  2],
        [ 3,  4],
        [ 5,  6],
        [ 7,  8],
        [ 9, 10],
        [11, 12],
        [13, 14],
        [15, 16]])

同理,只給定行數(shù),newshape等于-1,Numpy也可以自動計算出新數(shù)組的列數(shù)。

到此這篇關于numpy.reshape()的函數(shù)的具體使用的文章就介紹到這了,更多相關numpy.reshape()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

凯里市| 同仁县| 礼泉县| 珲春市| 平武县| 太康县| 南汇区| 洞口县| 湖北省| 铁岭市| 屯昌县| 霞浦县| 阜平县| 贵阳市| 沧州市| 东兰县| 微博| 庆元县| 蓬安县| 葵青区| 北海市| 五台县| 乐平市| 凤台县| 漳平市| 滨海县| 安溪县| 绿春县| 砚山县| 静宁县| 科尔| 香格里拉县| 九龙坡区| 平凉市| 汝州市| 邵东县| 合山市| 唐河县| 沙田区| 巨野县| 友谊县|