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

Python基礎(chǔ)之numpy庫的使用

 更新時間:2021年04月29日 08:43:42   作者:省級干飯王  
這篇文章主要介紹了Python基礎(chǔ)之numpy庫的使用,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下

numpy庫概述

numpy庫處理的最基礎(chǔ)數(shù)據(jù)類型是由同種元素構(gòu)成的多維數(shù)組,簡稱為“數(shù)組”

數(shù)組的特點

  • 數(shù)組中所有元素的類型必須相同
  • 數(shù)組中元素可以用整數(shù)索引
  • 序號從0開始

ndarray類型的維度叫做軸,軸的個數(shù)叫做秩

numpy庫的解析

由于numpy庫中函數(shù)較多而且容易與常用命名混淆,建議采用如下方法引用numpy庫

import numpy as np

numpy庫中常用的創(chuàng)建數(shù)組函數(shù)

函數(shù) 描述
np.array([x,y,z],dtype=int) 從Python列表和元組中創(chuàng)建數(shù)組
np.arange(x,y,i) 創(chuàng)建一個由x到y(tǒng),以i為步長的數(shù)組
np.linspace(x,y,n) 創(chuàng)建一個由x到y(tǒng),等分成n個元素的數(shù)組
np.indices((m,n)) 創(chuàng)建一個m行n列的矩陣
np.random.rand(m,n) 創(chuàng)建一個m行n列的隨機數(shù)組
np.ones((m,n),dtype) 創(chuàng)建一個m行n列全1的數(shù)組,dtype是數(shù)據(jù)類型
np.empty((m,n),dtype) 創(chuàng)建一個m行n列全0的數(shù)組,dtype是數(shù)據(jù)類型
import numpy as np
a1 = np.array([1,2,3,4,5,6])
a2 = np.arange(1,10,3)
a3 = np.linspace(1,10,3)
a4 = np.indices((3,4))
a5 = np.random.rand(3,4)
a6 = np.ones((3,4),int)
a7 = np.empty((3,4),int)
print(a1)
print("===========================================================")
print(a2)
print("===========================================================")
print(a3)
print("===========================================================")
print(a4)
print("===========================================================")
print(a5)
print("===========================================================")
print(a6)
print("===========================================================")
print(a7)
=================================================================================
[1 2 3 4 5 6]
===========================================================
[1 4 7]
===========================================================
[ 1.   5.5 10. ]
===========================================================
[[[0 0 0 0]
  [1 1 1 1]
  [2 2 2 2]]

 [[0 1 2 3]
  [0 1 2 3]
  [0 1 2 3]]]
===========================================================
[[0.00948155 0.7145306  0.50490391 0.69827703]
 [0.18164292 0.78440752 0.75091258 0.31184394]
 [0.17199081 0.3789     0.69886588 0.0476422 ]]
===========================================================
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
===========================================================
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

在建立一個簡單的數(shù)組后,可以查看數(shù)組的屬性

屬性 描述
ndarray.ndim 數(shù)組軸的個數(shù),也被稱為秩
ndarray.shape 數(shù)組在每個維度上大小的整數(shù)元組
ndarray.size 數(shù)組元素的總個數(shù)
ndarray.dtype 數(shù)組元素的數(shù)據(jù)類型,dtype類型可以用于創(chuàng)建數(shù)組
ndarray.itemsize 數(shù)組中每個元素的字節(jié)大小
ndarray.data 包含實際數(shù)組元素的緩沖區(qū)地址
ndarray.flat 數(shù)組元素的迭代器
import numpy as np
a6 = np.ones((3,4),int)
print(a6)
print("=========================================")
print(a6.ndim)
print("=========================================")
print(a6.shape)
print("=========================================")
print(a6.size)
print("=========================================")
print(a6.dtype)
print("=========================================")
print(a6.itemsize)
print("=========================================")
print(a6.data)
print("=========================================")
print(a6.flat)
=================================================================================
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
=========================================
2
=========================================
(3, 4)
=========================================
12
=========================================
int32
=========================================
4
=========================================
<memory at 0x0000020D79545908>
=========================================
<numpy.flatiter object at 0x0000020D103B1180>

數(shù)組在numpy中被當(dāng)做對象,可以采用< a >.< b >()方式調(diào)用一些方法。

ndarray類的形態(tài)操作方法

方法 描述
ndarray.reshape(n,m) 不改變數(shù)組ndarray,返回一個維度為(n,m)的數(shù)組
ndarray.resize(new_shape) 與reshape()作用相同,直接修改數(shù)組ndarray
ndarray.swapaxes(ax1,ax2) 將數(shù)組n個維度中任意兩個維度進行調(diào)換
ndarray.flatten() 對數(shù)組進行降維,返回一個折疊后的一維數(shù)組
ndarray.ravel() 作用同np.flatten(),但返回的是一個視圖

ndarray類的索引和切片方法

方法 描述
x[i] 索引第i個元素
x[-i] 從后向前索引第i個元素
x[n:m] 默認步長為1,從前向后索引,不包含m
x[-m:-n] 默認步長為1,從前向后索引,結(jié)束位置為n
x[n: m :i] 指定i步長的由n到m的索引

除了ndarray類型方法外,numpy庫提供了一匹運算函數(shù)

函數(shù) 描述
np.add(x1,x2[,y]) y = x1 + x2
np.subtract(x1,x2[,y]) y = x1 -x2
np.multiply(x1,x2[,y]) y = x1 * x2
np.divide(x1,x2[,y]) y = x1 /x2
np floor_divide(x1,x2[,y]) y = x1 // x2
np.negative(x[,y]) y = -x
np.power(x1,x2[,y]) y = x1 ** x2
np.remainder(x1,x2[,y]) y = x1 % x2

numpy庫的比較運算函數(shù)

函數(shù) 符號描述
np.equal(x1,x2[,y]) y = x1 == x2
np.not_equal(x1,x2[,y]) y = x1 != x2
np.less(x1,x2,[,y]) y = x1 < x2
np.less_equal(x1,x2,[,y]) y = x1 < = x2
np.greater(x1,x2,[,y]) y = x1 > x2
np.greater_equal(x1,x2,[,y]) y >= x1 >= x2
np.where(condition[x,y]) 根據(jù)條件判斷是輸出x還是y

numpy庫的其他運算函數(shù)

函數(shù) 描述
np.abs(x) 計算濟源元素的整形、浮點、或復(fù)數(shù)的絕對值
np.sqrt(x) 計算每個元素的平方根
np.squre(x) 計算每個元素的平方
np.sign(x) 計算每個元素的符號1(+),0,-1(-)
np.ceil(x) 計算大于或等于每個元素的最小值
np.floor(x) 計算小于或等于每個元素的最大值
np.rint(x[,out]) 圓整,取每個元素為最近的整數(shù),保留數(shù)據(jù)類型
np.exp(x[,out]) 計算每個元素的指數(shù)值
np.log(x),np.log10(x),np.log2(x) 計算自然對數(shù)(e),基于10,,2的對數(shù),log(1+x)

到此這篇關(guān)于Python基礎(chǔ)之numpy庫的使用的文章就介紹到這了,更多相關(guān)Python numpy庫的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論