李宏毅機器學習實戰代碼篇——作業筆記

1.python的一些用法

np.arange(start,stop,step,dtype):return array 

Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop)

 

np.zeros(shape, dtype=None, order='C')

Return a new array of given shape and type, filled with zeros.
shape : int or tuple of ints
Shape of the new array, e.g., (2, 3) or 2.
dtype : data-type, optional
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order : {'C', 'F'}, optional, default: 'C'
Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.

 

np.meshgrid(*xi, **kwargs)

Return coordinate matrices from coordinate vectors.(從座標向量返回座標矩陣)

 

import matplotlib.pyplot as plt

plt.contourf:畫三維等高線

plt.plot:畫點

plt.xlim: limit of x(x軸取值範圍),plt.ylim同理

plt.xlabel(r'$b$') Set the x-axis label of the current axes.(設置x軸標籤)

plt.show():display the figure

2.自己犯的錯誤:在設置y軸範圍是將(-5,5)寫成了(-5,-5),導致圖像怎麼畫都不正確,一度懷疑是數據出錯了,結果只是手動設置範圍時出錯

 

3.

import numpy as np

np.random.shuffle(X)

對數組的第一維度進行隨機打亂,即在訓練中打亂每個樣本的順序,經常在epoch之後使用,充分打亂數據可以增加訓練的多樣性

4. python一次可以返回多個參數

def func_test():
 return (1,2,3)
test1, test2, test3 = func_test()

5.np.concatenate:拼接數組,axis=0表示拼接行,axis=1表示拼接列

a = np.array([[1,2],[3,4]])
b = np.array([[5,6]])
np.concatenate((a,b),axis = 0)
array([[1, 2],
       [3, 4],
       [5, 6]])
np.concatenate((a,b.T),axis = 1)
array([[1, 2, 5],
       [3, 4, 6]])
# 作爲參數的數組形狀必須要一致

6.np.std:計算標準差,無axis表示全局標準差,axis = 0表示每列的標準差,axis=1表示每行的標準差

>>> a = np.array([[1,2],[3,4]])
>>> np.std(a)
1.118033988749895
>>> np.std(a,axis=0)
array([1., 1.])
>>> np.std(a,axis=1)
array([0.5, 0.5])
#方差=標準差的平方

7.np.tile:複製數組,np.tile(a,2)表示將column複製2倍,np.tile(b,(2,1)):將row複製2倍,將column複製1倍

>>> b = np.array([[1,2],[3,4]])
>>> np.tile(b,2)
array([[1, 2, 1, 2],
       [3, 4, 3, 4]])
>>> np.tile(b,(2,1))
array([[1, 2],
       [3, 4],
       [1, 2],
       [3, 4]])

8.reshape:改變數組的維度

   np.squeeze():把shape中爲1的維度去掉

>>> e = np.arange(10)
>>> e
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> e.reshape(1,1,10)
array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]])
>>> e.reshape(1,10,1)
array([[[0],
        [1],
        [2],
        [3],
        [4],
        [5],
        [6],
        [7],
        [8],
        [9]]])
>>> np.squeeze(e)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

9.np.clip(num,bound1,bound2)

給定區間[bound1,bound2],如果num<bound1,num=bound1;如果num>bound2,num=bound2

>>> np.clip(0.5555,0,1)
0.5555

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章