欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的Numpy矩陣操作

 更新時(shí)間:2018年08月12日 17:16:43   作者:峻之嶺峰  
這篇文章主要介紹了Python中的Numpy矩陣操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Numpy

通過(guò)觀察Python的自有數(shù)據(jù)類(lèi)型,我們可以發(fā)現(xiàn)Python原生并不提供多維數(shù)組的操作,那么為了處理矩陣,就需要使用第三方提供的相關(guān)的包。

NumPy 是一個(gè)非常優(yōu)秀的提供矩陣操作的包。NumPy的主要目標(biāo),就是提供多維數(shù)組,從而實(shí)現(xiàn)矩陣操作。

NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes.

基本操作

 #######################################
# 創(chuàng)建矩陣
#######################################
from numpy import array as matrix, arange

# 創(chuàng)建矩陣
a = arange(15).reshape(3,5)
a

# Out[10]:
# array([[0., 0., 0., 0., 0.],
#    [0., 0., 0., 0., 0.],
#    [0., 0., 0., 0., 0.]])

b = matrix([2,2])
b

# Out[33]: array([2, 2])

c = matrix([[1,2,3,4,5,6],[7,8,9,10,11,12]], dtype=int)
c

 
# Out[40]:
# array([[ 1, 2, 3, 4, 5, 6],
#    [ 7, 8, 9, 10, 11, 12]])
#######################################
# 創(chuàng)建特殊矩陣
#######################################
from numpy import zeros, ones,empty

z = zeros((3,4))
z

# Out[43]:
# array([[0., 0., 0., 0.],
#    [0., 0., 0., 0.],
#    [0., 0., 0., 0.]])

o = ones((3,4))
o

# Out[46]:
# array([[1., 1., 1., 1.],
#    [1., 1., 1., 1.],
#    [1., 1., 1., 1.]])

e = empty((3,4))
e

# Out[47]:
# array([[0., 0., 0., 0.],
#    [0., 0., 0., 0.],
#    [0., 0., 0., 0.]])

 #######################################
# 矩陣數(shù)學(xué)運(yùn)算
#######################################
from numpy import array as matrix, arange

a = arange(9).reshape(3,3)
a

# Out[10]:
# array([[0, 1, 2],
#    [3, 4, 5],
#    [6, 7, 8]])

b = arange(3)
b

# Out[14]: array([0, 1, 2])

a + b

# Out[12]:
# array([[ 0, 2, 4],
#    [ 3, 5, 7],
#    [ 6, 8, 10]])

a - b

# array([[0, 0, 0],
#    [3, 3, 3],
#    [6, 6, 6]])

a * b

# Out[11]:
# array([[ 0, 1, 4],
#    [ 0, 4, 10],
#    [ 0, 7, 16]])

a < 5

# Out[12]:
# array([[ True, True, True],
#    [ True, True, False],
#    [False, False, False]])

a ** 2

# Out[13]:
# array([[ 0, 1, 4],
#    [ 9, 16, 25],
#    [36, 49, 64]], dtype=int32)

a += 3
a

# Out[17]:
# array([[ 3, 4, 5],
#    [ 6, 7, 8],
#    [ 9, 10, 11]]) 
#######################################
# 矩陣內(nèi)置操作
#######################################
from numpy import array as matrix, arange

a = arange(9).reshape(3,3)
a

# Out[10]:
# array([[0, 1, 2],
#    [3, 4, 5],
#    [6, 7, 8]])

a.max()

# Out[23]: 8

a.min()

# Out[24]: 0

a.sum()

# Out[25]: 36 
#######################################
# 矩陣索引、拆分、遍歷
#######################################
from numpy import array as matrix, arange

a = arange(25).reshape(5,5)
a

# Out[9]:
# array([[ 0, 1, 2, 3, 4],
#    [ 5, 6, 7, 8, 9],
#    [10, 11, 12, 13, 14],
#    [15, 16, 17, 18, 19],
#    [20, 21, 22, 23, 24]])

a[2,3]   # 取第3行第4列的元素

# Out[3]: 13

a[0:3,3]  # 取第1到3行第4列的元素

# Out[4]: array([ 3, 8, 13])

a[:,2]   # 取所有第二列元素

# Out[7]: array([ 2, 7, 12, 17, 22])

a[0:3,:]  # 取第1到3行的所有列

# Out[8]:
# array([[ 0, 1, 2, 3, 4],
#    [ 5, 6, 7, 8, 9],
#    [10, 11, 12, 13, 14]])

a[-1]  # 取最后一行

# Out[10]: array([20, 21, 22, 23, 24])

for row in a:  # 逐行迭代
  print(row)

# [0 1 2 3 4]
# [5 6 7 8 9]
# [10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]

for element in a.flat: # 逐元素迭代,從左到右,從上到下
  print(element)

# 0
# 1
# 2
# 3
# ... #######################################
# 改變矩陣
#######################################
from numpy import array as matrix, arange

b = arange(20).reshape(5,4)

b

# Out[18]:
# array([[ 0, 1, 2, 3],
#    [ 4, 5, 6, 7],
#    [ 8, 9, 10, 11],
#    [12, 13, 14, 15],
#    [16, 17, 18, 19]])

b.ravel()

# Out[16]:
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
#    17, 18, 19])

b.reshape(4,5)

# Out[17]:
# array([[ 0, 1, 2, 3, 4],
#    [ 5, 6, 7, 8, 9],
#    [10, 11, 12, 13, 14],
#    [15, 16, 17, 18, 19]])

b.T   # reshape 方法不改變?cè)仃嚨闹?,所以需要使?.T 來(lái)獲取改變后的值

# Out[19]:
# array([[ 0, 4, 8, 12, 16],
#    [ 1, 5, 9, 13, 17],
#    [ 2, 6, 10, 14, 18],
#    [ 3, 7, 11, 15, 19]]) 
#######################################
# 合并矩陣
#######################################
from numpy import array as matrix,newaxis
import numpy as np

d1 = np.floor(10*np.random.random((2,2)))
d2 = np.floor(10*np.random.random((2,2)))

d1

# Out[7]:
# array([[1., 0.],
#    [9., 7.]])

d2

# Out[9]:
# array([[0., 0.],
#    [8., 9.]])

np.vstack((d1,d2)) # 按列合并

# Out[10]:
# array([[1., 0.],
#    [9., 7.],
#    [0., 0.],
#    [8., 9.]])

np.hstack((d1,d2)) # 按行合并

# Out[11]:
# array([[1., 0., 0., 0.],
#    [9., 7., 8., 9.]])

np.column_stack((d1,d2)) # 按列合并

# Out[13]:
# array([[1., 0., 0., 0.],
#    [9., 7., 8., 9.]])

c1 = np.array([11,12])
c2 = np.array([21,22])

np.column_stack((c1,c2))

# Out[14]:
# array([[11, 21],
#    [12, 22]])

c1[:,newaxis]  # 添加一個(gè)“空”列

# Out[18]:
# array([[11],
#    [12]])

np.hstack((c1,c2))

# Out[27]: array([11, 12, 21, 22])

np.hstack((c1[:,newaxis],c2[:,newaxis]))

# Out[28]:
# array([[11, 21],
#    [12, 22]])

參考

1.NumPy官方文檔

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中print函數(shù)的用法示例與詳細(xì)講解

    python中print函數(shù)的用法示例與詳細(xì)講解

    這篇文章主要給大家介紹了關(guān)于python中print函數(shù)的用法示例與詳細(xì)講解,print()函數(shù)可以將輸出的信息打印出來(lái),即發(fā)送給標(biāo)準(zhǔn)輸出流,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Python實(shí)現(xiàn)ATM簡(jiǎn)單功能的示例詳解

    Python實(shí)現(xiàn)ATM簡(jiǎn)單功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)ATM的簡(jiǎn)單功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-11-11
  • Python學(xué)習(xí)之.iloc與.loc的區(qū)別、聯(lián)系和用法

    Python學(xué)習(xí)之.iloc與.loc的區(qū)別、聯(lián)系和用法

    loc和iloc都是pandas工具中定位某一行的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python學(xué)習(xí)之.iloc與.loc的區(qū)別、聯(lián)系和用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • 用Python中的__slots__緩存資源以節(jié)省內(nèi)存開(kāi)銷(xiāo)的方法

    用Python中的__slots__緩存資源以節(jié)省內(nèi)存開(kāi)銷(xiāo)的方法

    這篇文章主要介紹了用Python中的__slots__通過(guò)緩存資源的方式以節(jié)省內(nèi)存開(kāi)銷(xiāo)的方法,且示例代碼非常簡(jiǎn)單,需要的朋友可以參考下
    2015-04-04
  • 淺談keras使用中val_acc和acc值不同步的思考

    淺談keras使用中val_acc和acc值不同步的思考

    這篇文章主要介紹了淺談keras使用中val_acc和acc值不同步的思考,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索

    pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索

    這篇文章主要介紹了pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索?下面小編就為大家介紹一下解決方法,還等什么?一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • 樹(shù)莓派極簡(jiǎn)安裝OpenCv的方法步驟

    樹(shù)莓派極簡(jiǎn)安裝OpenCv的方法步驟

    這篇文章主要介紹了樹(shù)莓派極簡(jiǎn)安裝OpenCv的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python線程啟動(dòng)的四種方式總結(jié)

    python線程啟動(dòng)的四種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于python線程啟動(dòng)的四種方式,線程可以完成一定任務(wù),可以和其它線程共享父進(jìn)程的共享變量和部分環(huán)境,相互協(xié)作來(lái)完成任務(wù),需要的朋友可以參考下
    2024-01-01
  • python將unicode轉(zhuǎn)為str的方法

    python將unicode轉(zhuǎn)為str的方法

    下面小編就為大家?guī)?lái)一篇python將unicode轉(zhuǎn)為str的方法。小編覺(jué)得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Python Django Cookie 簡(jiǎn)單用法解析

    Python Django Cookie 簡(jiǎn)單用法解析

    這篇文章主要介紹了Python Django Cookie 簡(jiǎn)單用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評(píng)論