02.numpy 的使用

2、numpy 的使用

2.1 numpy 的創(chuàng)建:

(1)使用 np.array() 創(chuàng)建

使用 array() 創(chuàng)建一維數(shù)組

import numpy as np
arr = np.array([1,2,3])
arr
image.png

(2)使用array()創(chuàng)建二維數(shù)組

arr = np.array([[1,2,3],[4,5,6]])
arr
image.png

(3)數(shù)組和列表的區(qū)別是什么?

  • 數(shù)組中存儲的數(shù)據(jù)元素類型必須是統(tǒng)一類型
  • 優(yōu)先級:
    • 字符串 > 浮點(diǎn)數(shù) > 整數(shù)
image.png

2.2 numpy 的使用

將外部的一張圖片讀取加載到numpy數(shù)組中,然后嘗試改變數(shù)組元素的數(shù)值查看對原始圖片的影響

(1)使 matplotlib.pyplot 創(chuàng)建

# 導(dǎo)入需要的模塊
import matplotlib.pyplot as plt
img1 = plt.imread('1.jpg') # 返回的數(shù)組,數(shù)組中裝載的就是圖片內(nèi)容
plt.imshow(img1) #將numpy數(shù)組進(jìn)行可視化展示
image.png

(2)將圖片數(shù)組的每一個元素減去100

img1 = img1 - 100  #將每一個數(shù)組元素都減去100
plt.imshow(img1)
image.png

會發(fā)現(xiàn)的各個像素點(diǎn)都發(fā)生的變化。

2.3 numpy的常用方法

(1)ones() 創(chuàng)建一個用1填充的數(shù)組

np.ones(shape=(3,4)) #創(chuàng)建一個3行4列的數(shù)組,用1填充
image.png

(2)linspace()一維的等差數(shù)列數(shù)組,num設(shè)置元素個數(shù)

np.linspace(0,100,num=21)  #一維的等差數(shù)列數(shù)組
image.png

(3)arange()創(chuàng)建等差數(shù)列,step設(shè)置步長

np.arange(10,20,step=2)  #創(chuàng)建一位的等差數(shù)列,一個10到20,步長為2的數(shù)組
image.png

(4)random.randint()創(chuàng)建隨機(jī)數(shù)列

np.random.randint(0,100,size=(5,4)) #創(chuàng)建一個5行4列元素在0到100之間的隨機(jī)數(shù)組
image.png

2.4 numpy 的常用屬性

先創(chuàng)建一個numpy數(shù)組:

arr = np.random.randint(0,100,size=(5,4)) #創(chuàng)建一個5行4列元素在0到100之間的隨機(jī)數(shù)組
arr 
image.png

(1)shape返回?cái)?shù)組的形狀

arr.shape #返回?cái)?shù)組的形狀
image.png

(2)ndim返回?cái)?shù)組中的維度

arr.ndim #返回?cái)?shù)組中的維度
image.png

(3)size返回?cái)?shù)組的大小

arr.size #返回?cái)?shù)組的大小
image.png

(4)dtype 返回?cái)?shù)組的元素類型

arr.dtype #返回?cái)?shù)組的元素類型
image.png

(5)type 返回?cái)?shù)組的數(shù)據(jù)類型

type(arr) #數(shù)組的數(shù)據(jù)類型
image.png

(6)創(chuàng)建一個數(shù)組,指定數(shù)組元素類型為int32

arr = np.array([1,2,3],dtype='int32')
arr.dtype
image.png

(7)修改數(shù)組元素類型

arr.dtype = 'uint8'  #修改數(shù)組的元素類型
arr.dtype
image.png

2.5 numpy 切片操作

目標(biāo):

  • 切片操作
    • 切出前兩列的數(shù)據(jù)
    • 切出前兩行的數(shù)據(jù)
    • 切出前兩行的前兩列數(shù)據(jù)
    • 數(shù)組數(shù)據(jù)反轉(zhuǎn)
    • 練習(xí):將一張圖片上下左右進(jìn)行反轉(zhuǎn)操作
    • 練習(xí):將圖片進(jìn)行指定區(qū)域的裁剪

(1)切出arr數(shù)組的前兩行數(shù)據(jù)

arr[0:2]
image.png

(2)切出arr數(shù)組的前兩列數(shù)據(jù)

arr[:,0:2]
image.png

(3)切出arr數(shù)組的前兩行前兩列數(shù)據(jù)

arr[0:2,0:2]
image.png

(4)先查看原數(shù)組,然后將原數(shù)組進(jìn)行行倒置

arr
image.png
#將數(shù)組的行倒置
arr[::-1]
image.png

(5)將數(shù)組的列導(dǎo)致

arr[:,::-1]
image.png

(6)將所有元素倒置

arr[::-1,::-1]
image.png

(7)將一張圖片進(jìn)行左右反轉(zhuǎn)

  • 加載圖片
img2 = plt.imread('1.jpg')
plt.imshow(img2)
image.png
  • 查看圖片數(shù)組形狀,參數(shù)1:行數(shù),參數(shù)2:列數(shù),參數(shù)3:顏色。
img2.shape
image.png
  • 查看圖片數(shù)組的元素
img2
image.png
  • 將圖片左右進(jìn)行反轉(zhuǎn)
plt.imshow(img2[:,::-1,:])
image.png

(8)對圖片進(jìn)行裁剪

#圖片裁剪功能
plt.imshow(img2[200:550,100:450,:])
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容