機器學習(4 Numpy)

2021-09-30 01:25:39 字數 4832 閱讀 3340

ndarray介紹

ndarray的優勢【掌握】

ndarray支援並行化運算(向量化運算)

ndarray底層是用c語言寫的,效率更高,釋放了gil

n維陣列-ndarray

ndarray的形狀

ndarray的型別

基本操作

生成陣列的方法

ones = np.ones([4,8])

ones

np.zeros_like(ones)

2、從現有陣列生成

a = np.array([[

1,2,

3],[

4,5,

6]])

# 從現有的陣列當中建立

a1 = np.array(a)

# 相當於索引的形式,並沒有真正的建立乙個新的

a2 = np.asarray(a)

3、生成固定範圍的陣列

# 生成等間隔的陣列

np.linspace(0, 100, 11)

array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])

建立等差陣列 — 指定步長

np.arange(start,stop, step, dtype)

np.arange(10,

50,2)

建立等比數列

np.logspace(start,stop, num)

# 生成10^x

np.logspace(0, 2, 3)

array([ 1., 10., 100.])

4、生成隨機數組

陣列的索引、切片

形狀修改

# 在轉換形狀的時候,一定要注意陣列的元素匹配

stock_change.reshape([5, 4])

stock_change.reshape([-1,10]) # 陣列的形狀被修改為: (2, 10), -1: 表示通過待計算

stock_change.resize([5, 4])

# 檢視修改後結果

stock_change.shape

(5, 4)

stock_change.t.shape

(4, 5)

型別修改

stock_change.astype(np.int32)

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]])

arr.tostring()

陣列的去重

temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])

>>> np.unique(temp)

array([1, 2, 3, 4, 5, 6])

ndarray運算

# 生成10名同學,5門功課的資料

>>> score = np.random.randint(40, 100, (10, 5))

# 取出最後4名同學的成績,用於邏輯判斷

>>> test_score = score[6:, 0:5]

# 邏輯判斷, 如果成績大於60就標記為true 否則為false

>>> test_score > 60

array([[ true, true, true, false, true],

[ true, true, true, false, true],

[ true, true, false, false, true],

[false, true, true, true, true]])

# bool賦值, 將滿足條件的設定為指定的值-布林索引

>>> test_score[test_score > 60] = 1

>>> test_score

array([[ 1, 1, 1, 52, 1],

[ 1, 1, 1, 59, 1],

[ 1, 1, 44, 44, 1],

[59, 1, 1, 1, 1]])

np.

all(

)# 判斷前兩名同學的成績[0:2, :]是否全及格

>>

> np.

all(score[0:

2,:]

>60)

false

np.any()

# 判斷前兩名同學的成績[0:2, :]是否有大於90分的

>>

> np.

any(score[0:

2,:]

>80)

true

# 判斷前四名學生,前四門課程中,成績中大於60的置為1,否則為0

temp = score[:4, :4]

np.where(temp > 60, 1, 0)

* 復合邏輯需要結合np.logical_and和np.logical_or使用
# 判斷前四名學生,前四門課程中,成績中大於60且小於90的換為1,否則為0

np.where(np.logical_and(temp > 60, temp < 90), 1, 0)

# 判斷前四名學生,前四門課程中,成績中大於90或小於60的換為1,否則為0

np.where(np.logical_or(temp > 90, temp < 60), 1, 0)

# 接下來對於前四名學生,進行一些統計運算

# 指定列 去統計

temp = score[:4, 0:5]

print("前四名學生,各科成績的最大分:{}".format(np.max(temp, axis=0)))

print("前四名學生,各科成績的最小分:{}".format(np.min(temp, axis=0)))

print("前四名學生,各科成績波動情況:{}".format(np.std(temp, axis=0)))

print("前四名學生,各科成績的平均分:{}".format(np.mean(temp, axis=0)))

print("前四名學生,各科成績最高分對應的學生下標:{}".format(np.argmax(temp, axis=0)))

arr = np.array([[

1,2,

3,2,

1,4]

,[5,

6,1,

2,3,

1]])

arr +

1arr /

2# 可以對比python列表的運算,看出區別

a =[1,

2,3,

4,5]

a *3

arr1 = np.array([[

0],[

1],[

2],[

3]])

arr1.shape

# (4, 1)

arr2 = np.array([1

,2,3

])arr2.shape

# (3,)

arr1+arr2

# 結果是:

array([[

1,2,

3],[

2,3,

4],[

3,4,

5],[

4,5,

6]])

矩陣加法和標量乘法

矩陣和矩陣(向量)相乘

矩陣性質

單位矩陣

矩陣運算

>>> a = np.array([[80, 86],

[82, 80],

[85, 78],

[90, 90],

[86, 82],

[82, 90],

[78, 80],

[92, 94]])

>>> b = np.array([[0.7], [0.3]])

>>> np.matmul(a, b)

array([[81.8],

[81.4],

[82.9],

[90. ],

[84.8],

[84.4],

[78.6],

[92.6]])

>>> np.dot(a,b)

array([[81.8],

[81.4],

[82.9],

[90. ],

[84.8],

[84.4],

[78.6],

[92.6]])

注意:np.matmul和np.dot的區別

二者都是矩陣乘法。 np.matmul中禁止矩陣與標量的乘法。 在向量乘向量的內積運算中,np.matmul與np.dot沒有區別。

python 資料分析 (4)numpy陣列的運算

陣列的運算 對應元素的運算,結構完全相同 import numpy as np arr1 np.arange 1,17 reshape 4 4 print arr1 arr2 np.diag 2 3,1 5 建立乙個對角陣列 print arr2 arr3 arr1 arr2 乘法 print ar...

機器學習預備 numpy

引入 import numpy as np numpy 資料結構 ndarray numpy 使用的陣列類是 ndarray 一些重要屬性如下 ndarray.ndim 維數 ndarray.shape 返回 n,m n行 m列 ndarray.dtype 型別 numpy 資料結構 mat mat...

python機器學習 Numpy

numpy是python語言的乙個擴充程式庫。支援高階大量的維度陣列與矩陣運算,此外也針對陣列運算提供大量的數學函式庫。numpy內部解除了python的pil 全域性直譯器鎖 運算效率極好,是大量機器學習框架的基礎庫 arr np.array 1 2,3 4,5 arr.shape 顯示的是維數,...