Python 機器學習基礎

2021-08-11 18:06:46 字數 4045 閱讀 7830

pandas

matplotlib

reference

numpy 的資料結構

numpy的基本操作

當進行+-*/==!=等邏輯判斷時,numpy會對array中所有元素進行判斷。

import numpy

vector = numpy.array([5,10,6,7])

vector == 10

#output: array([false, true, false, false], dtype = bool)

numpy.array()的判定結果可以被當成是index,從而取得結果為true的值。

import numpy

vector = numpy.array([2,6,10,9])

#將結果為true的結果的index賦值給ten變數

ten = (vector == 10)

print(vector[ten])

#output:[10]

&|邏輯運算

型別轉換

最大值和最小值

求和 矩陣變換

矩陣維度

矩陣大小

矩陣操作

numpy.zeros((a,b)):建立乙個 a 行 b 列的 0 矩陣

#建立乙個起始值為10,終止值為20,步長為2的矩陣

numpy.arange(10,20,2)

#output:array([10, 12, 14, 16, 18])

#生成2行3列的隨機矩陣,元素值在+1和-1之間

#random.random():在random模組下使用random函式

numpy.random.random((2,3))

#output:array([[ 0.25712849, 0.52280068, 0.37931522],[ 0.37061583, 0.66376507, 0.33100993]])

.linespace(a, b, c):在起始值 a 與終點值 b 之間平均得到 c 個元素。

**n:取n次方

a*b: 矩陣a和b求內積, 對應項相乘

a.dot(b): 矩陣的點乘

numpy. dot(a, b): 同a.dot(b)

numpy的常用函式

.argmax(axis=0):返回當前array每一行最大的元素的index

.argmax(axis=1):返回當前array每一列最大的元素的index

.tile(): 複製貼上

a.sort(axis=0):對a的每一行進行從小到大的排序

numpy.argsort(a): 按照索引值進行排序

pandas主要用於資料的處理

pandas資料選擇

#通過列名索引,得到列的切片

ndb_col = food_info("ndb_no")

#在food_info檔案中查詢名稱為"ndb_no"的列並返回

pandas資料型別

pandas資料預處理

def

hundredth_row

(column):

#extract the hundredth item

hundredth_item = column.loc[99]

return hundredth_item

print hundredth_row

matplotlib畫折線圖
imoprt pandas

#import the pyplot library in matplotlib

import matplotlib.pyplot as plot

#read the .csv file

unrate = pandas.read_csv('unrate.csv')

#transfer the date column in .csv file to datetime version by using the function to_datetime

unrate['date'] = pandas.to_datetime(unrate['date'])

#plot nothing

plot.plot()

#show the canvas

plot.show()

figure = unrate[0: 12]

plot.plot(figure['date'], figure['value'])

plot.show()

plot.xticks(rotation = 45)

.plot(x , y): 畫折線圖, 以 x 引數為 x 軸, 以 y 引數為 y 軸

.xticks(rotation = xx): 變換座標的角度, 轉換xx角度

xlable('***x'): add lable to x-axis which indicates the name of x - axis

ylable('***x'): add lable to y-axis which indicates the name of y - axis

.title('***x'): add the title to the whole chart

matplotlib 畫子圖

.add_subplot(r, c, x): 新增 r 行, c 列個子圖, 並選擇第 x 個子圖畫圖

figure(figsize = (3,3)):指定子圖的長和寬

.plot(x, y, c='', lable = label): c 引數指定畫出折現的color, 當需要在同乙個子圖中華多個折線時, 只需要 plot 多次即可, 使用lable引數為每個單獨的折線新增label, 但是此時label並不會立即顯示

.legend(loc = '***'): 將plot中的label顯示在折線圖的***位置

matplotlib散點圖和柱形圖

柱形圖

.bar(position, height, width):生成柱形圖, 以positon作為柱形圖中每個柱離遠點的距離, 以height作為每個柱的高度, width作為每個柱的寬度

.barh(position, height, width):畫橫向柱形圖

散點圖

.scatter(x,y): x作為x軸引數, y作為y軸引數畫散點圖

matplotlib柱形圖與盒圖

柱形圖

.hist(***, bins= ): 畫出區間數量為bins的柱形圖, bins的預設值為10

.set_ylim(a,b):設定y軸的區間為[a: b]

盒圖boxplot():畫盒圖

matplotlib細節設定

tick_param(bottom="", top="", left="", right=""):去掉軸標線

(a/255, b/255, c/255):獲取值為abc的顏色引數

以上內容, 摘自

python基礎(機器學習)

高階函式 import time def deco func starttime time.time func endtime time.time msecs endtime starttime 100 print time is d ms msecs def func print hello ti...

python機器學習基礎

可定義為 機器學習是從資料中自動分析獲得的模型,並利用模型對未知資料進行 可分為 監督學習 主要特點是要在訓練模型時提供給學習系統訓練樣本以及樣本隊員的類別標籤,因此又稱為有導師學習。例 學生從老師那裡獲取知識,資訊,老師提供對錯知識 告知最終答案的學習過程。典型的監督學習方法 決策樹,支援向量機 ...

機器學習基礎環境 Python基礎

用途 python應用例項 編碼方式 在python2中使用python3的函式 輸入方式 輸出方式 anaconda資料科學開發環境 版本 anaconda3 5.2.0 py36 windows x86 64 jupyter notebook 修改jupyter的檔案路勁 ipython spy...