沒有太多預裝軟體,最小化安裝
預裝很多東西,對科學計算可以輕易使用
$ conda -V
conda 23.1.0
$ python -V
Python 3.10.6
conda env list
conda create -n <env_name> python=<python_ver>
例如
conda create -n py39 python=3.9
conda activate <env> # 進入環境
conda deactivate <env> # 退出環境
例如
$ python -V
'python' 不是內部或外部命令、可執行的程式或批次檔。
$ conda activate py39
(py39) $ python -V
Python 3.9.13
(py39) $ conda deactivate
$ python -V
'python' 不是內部或外部命令、可執行的程式或批次檔。
在終端機直接打python
$ conda activate py39
(py39) $ python
Python 3.9.13 (tags/v3.9.13:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello, world")
hello, world
>>> exit()
$
(py39) $ pip install jupyter
查看版本:
(py39) $ jupyter --version
Selected Jupyter core packages...
IPython : 7.31.1
ipykernel : 6.19.2
ipywidgets : 7.6.5
jupyter_client : 7.3.4
jupyter_core : 5.1.1
jupyter_server : 1.23.4
jupyterlab : 3.5.2
nbclient : 0.5.13
nbconvert : 6.4.4
nbformat : 5.7.0
notebook : 6.5.2
qtconsole : 5.2.2
traitlets : 5.7.1
(py39) $ jupyter notebook
自己改筆記本名字
easyeee~
所有東西都是物件
變數、賦值
message = "Hello World" print(message)
條件判斷
x = 5 if x > 0 : print("x是正數")
函數定義
def sayHello(name): print("Hello " + name) sayHello("John")
for循環結構
for i in range(5): print(i) list1 = [1,2,3] for item in list1: print(item)
while 循環
i = 10 while i > 0: print(i)
i = 10 f = False while not f: if i == 0: f = True print(i)
python的變數可以隨意改變型別
number = 1 # int print(number) number = "asdf" # str print(number)
會印出
1 asdf
檢查型別
random_thing = 1.3 print(type(random_thing))
會印出
<class 'float'>
數據結構
myList = [1, 2, 3] # 列表 list myTuple = (1, 2, 3) # 元組 tuple myDict = {"name": "John", "age": 25} # 字典 dict mySet = {1, 2, 3} # 集合 set
取值
myList = [1, 2, 3] # 列表 list myTuple = (1, 2, 3) # 元組 tuple myDict = {"name": "John", "age": 25} # 字典 dict mySet = {1, 2, 3} # 集合 set print(myList[0]) print(myTuple[1]) print(myDict["name"]) print(mySet[2])
只有同種的可以互換
ex:
list <--> tuple
str <--> list
int <--> float
a = "abcdefg" print(list(a)) print(tuple( list(a) )) b = 1 c = 2 print(float(b) + float(c))
print(0.1 + 0.2)
pip install numpy
使用我的伺服器
pip install numpy --extra-index-url http://<my_ip> --trusted-host <my_ip>
首先需要匯入numpy模組,通常用np代稱
import numpy as np
ndarray為numpy的多維陣列物件
array = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
array.shape # (3,)
matrix.shape # (2, 2)
array.dtype # int64
可以使用np.newaxis或None來新增維度
array_2d = array[np.newaxis, :] # [[1,2,3]] array_3d = array[:, np.newaxis, np.newaxis] # [[[1]],[[2]],[[3]]]
向量化運算,適用於整個陣列
matrix + 2
matrix * matrix # 元素對應相乘
np.linspace
: 創建線段陣列np.mean
: 計算均值np.sum
: 總和np.min/max
: 最小值和最大值等等
pandas是基於numpy的Python數據分析庫,提供:
pip install pandas
使用我的伺服器
pip install pandas --extra-index-url http://<my_ip> --trusted-host <my_ip>
引入數據庫,通常會使用pd代稱
import pandas as pd
一維數據,可設置索引
s = pd.Series([1,2,3], index=['a','b','c'])
二維數據,類似表格/excel
data = {'Name': ['John', 'Mary'], 'Age': [25, 27]}
df = pd.DataFrame(data)
df = pd.read_csv('data.csv')
df.head()
/df.tail()
df.shape
df.dtypes
df.index
/df.columns
df.values
df.describe()
統計訊息df[col]
: columndf.loc[index]
: 行df.iloc[index]
: 位置del df[col]
df.fillna()
df.drop_duplicates()
使用pip裝:
pip install opencv-python
使用我的伺服器
pip install opencv-python --extra-index-url http://<my_ip> --trusted-host <my_ip>
OpenCV是開源的電腦視覺庫,提供許多影像處理和電腦視覺相關的功能。
最新版本是4.x,Python繫結是cv2。
import cv2
為何是cv2不是cv?
img = cv2.imread('image.jpg')
支援讀取JPG、PNG、TIFF等格式。
cv2.imshow('Image', img)
cv2.waitKey(0) # 按任意鍵繼續
cv2.imwrite('output.jpg', img)
img.shape
- 形狀img.size
- 大小img.dtype
- 數據類型cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.resize(img, (width, height))
img[y1:y2, x1:x2]
cv2.blur(img, (5, 5))
安裝:
pip install matplotlib
使用我的伺服器
pip install matplotlib --extra-index-url http://<my_ip> --trusted-host <my_ip>
Matplotlib是Python的2D繪圖庫,廣泛用於繪製論文的數據圖表。
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)
x = ['A', 'B', 'C', 'D']
y = [10, 30, 40, 20]
plt.bar(x, y)
img = cv2.imread('image.jpg')
plt.imshow(img)
plt.title('Title')
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.xlim(0, 5)
plt.ylim(0, 10)
plt.xticks([0, 1, 2, 3, 4, 5])
plt.yticks([0, 2, 4, 6, 8, 10])
plt.legend(['A', 'B'])
plt.show()
祝AI之路順遂