
《DLS》第一章筆記:Python入門
1.1 Python安裝
Python版本:Python3
外部庫:NumPy庫和Matplotlib庫
推薦版本:Anaconda發行版
集成很多機器學習相關的庫,使用戶一次性完成安裝
1.2 Python算數運算
| Python算術運算 | 符號 |
|---|---|
| 加 | + |
| 減 | - |
| 乘 | * |
| 除 | / |
| 整除 | // |
| 乘方 | ** |
1.3 Python語法
1.3.1 資料型別
包括int、float、str、bool等,可以用type()函式查看,
1.3.2 變數
Python是動態型別語言,變數的情況根據情況自動決定,因此要注意變數型別的轉換,
1.3.3 串列
>>> a = [1, 2, 3, 4, 5] # 生成串列
[]中的數字稱為索引,串列可以通過切片訪問,從 0 到 n-1 或從-n 到 1,
# 使用切片:
>>> a[0] # 訪問第一個元素的值
>>> a[0:2] # 獲取索引為0到2(不包括2!)的元素
>>> a[1:] # 獲取從索引為1的元素到最后一個元素
>>> a[:3] # 獲取從第一個元素到索引為3(不包括3!)的元素
>>> a[:-1] # 獲取從第一個元素到最后一個元素的前一個元素之間的元素
>>> a[:-2] # 獲取從第一個元素到最后一個元素的前二個元素之間的元素
1.3.4 字典
字典則以鍵-值對(key-value)的形式存盤資料,
>>> me = {'height':180} # 生成字典
>>> me['height'] # 訪問元素
>>> me['weight'] = 70 # 添加新元素
1.3.5 if陳述句的語法
if condition:
do something
elif condition:
do something
………
else:
do something
1.3.6 for陳述句的語法
for item in iterable:
do something
1.3.7 函式
def function (arg1, arg2):
do something
return ‘Something’
1.3.8 類
clsaa name:
def __init__(self, argument, …):
do something
def method1((self, argument, …):
do something
def method2((self, argument, …):
do something
……
1.4 NumPy
1.4.1 匯入Numpy
import numpy as np
1.4.2 生成NumPy陣列
a = np.array([1.0, 2.0, 3.0]) # 一維陣列
A = np.array([[1, 2], [3, 4]]) # 二維陣列
1.4.3 NumPy陣列的運算
1.4.3.1 element-wise運算
NumPy陣列可以進行對應元素之間的運算,
1.4.3.2 和標量的運算(廣播)


1.5 Matplotlib
1.5.1 繪制影像
import matplotlib.pyplot as plt
plt.plot() # 繪制圖形
plt.show() # 顯示圖形
1.5.2 顯示影像
import matplotlib.pyplot as plt
imshow() # 顯示影像
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/259410.html
標籤:AI
上一篇:【知識索引】【李宏毅機器學習】
