1.安裝numpypython
pip install numpy數組
numpy是python語言的一個擴充程序庫。支持高級大量的維度數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫。numpy內部解除了python的PIL(全局解釋器鎖),運算效率極好,是大量機器學習框架的基礎庫!框架
numpy導包機器學習
import numpy as npide
#打印版本號函數
print(np.version.version)學習
#聲明一個numpy數組spa
nlist = np.array([1,2,3])orm
print(nlist)索引
#ndim方法用來查看數組維度
print(nlist.ndim)
#聲明一個二維數組
nlist_2 = np.array([[1,2,3],[4,5,6]])
print(nlist_2)
print(nlist_2.ndim)
#使用shape屬性來打印多維數組的形狀
print(nlist.shape)
print(nlist_2.shape)
#使用shape屬性來打印多維數組的形狀
print(nlist.shape)
print(nlist_2.shape)
#打印numpy多維數組的數據類型
#打印普通list
print(type([1,2,3]))
print(type(nlist))
#使用dtype屬性來打印多維數組內部元素的數據類型
print(type(123))
print(nlist.dtype)
#itemsize屬性,來打印多維數組中的數據類型大小,字節
print(nlist.itemsize)
#data屬性,用來打印數據緩衝區 buffer
print(nlist.data)
#使用reshape方法來反向生成多維數組
nlist_3 = np.array(range(24)).reshape((3,2,4))
print(nlist_3)
#使用浮點做爲元素類型
nlist_float = np.array([1.0,2.0,3.0])
print(nlist_float.dtype)
#使用字符串
nlist_string = np.array(['1','2','3'])
print(nlist_string.dtype)
# data = np.arange(5)#生成區間數組 左閉右開
# data
# print(np.sum(data))#求和
# print(np.max(data))#最大值
# print(np.min(data))#最小值
# print(np.mean(data))#平均值
# print(np.std(data))#標準差
# print(np.var(data))#方差
Sum 求和
Max 最大值
Min 最小值
Mean 平均值
Std 標準差
Var 方差
#轉換
data = data.tolist() #tolist將ndarray轉成list
print(data)
data = np.array([1,2,3,4]) #將list轉成ndarrary
print(data)
總結:
array 聲明numpy數組
ndim 查看數組的維度
shape 查看多維數組的形狀
size 查看多維數組元素的個數
type 查看多維數組的數據類型
dtype 查看多維數組內部元素的數據類型
即多維數組中元素的數據類型,能夠是自定義的數據類型,能夠是python原生數據類型,也能夠是numpy中獨有的數據類型。好比numpy.int32, numpy.int16, and numpy.float64
reshape反向生成多維數組()括號裏面有幾個參數就是幾維,參數相乘等於range裏面的參數
arange 生成區間數組
itemsize 查看多維數組中的數據類型大小,字節
即多維數組元素的字節數,一個元素類型爲float64的數組itemsiz屬性值爲8(=64/8),又如,一個元素類型爲complex32的數組item屬性爲4(=32/8)
data 查看數據緩衝區
即實際數組元素的緩衝區,也就是內在地址。一般用不到,由於通常咱們是經過索引來訪問元素的。在python原生的數據類型中,可經過id(變量名)來得到變量的內存地址