• 圖例Legend 表明圖形裏的內容
• 網格Grid,圖形中的虛線,True顯示網格
• 點 Markers:表示點的形狀。html
基本的視覺元素有三種:點、線、柱狀。分析下面需求繪製什麼圖形?
學生爲某科課程花費的時間和考試成績二者之間的關係,查看二者之間的相關性。python
make標記樣式算法
標記顏色color數組
#1). 導入繪圖庫 import matplotlib.pyplot as plt import numpy as np #2). 建立畫板figure figure = plt.figure(figsize=(10, 10)) #3). 建立子圖subplot/Axes ax = plt.subplot(1, 1, 1) #4). 準備數據 #從0-50分割成100份 x = np.linspace(0, 10, 100) y = np.sin(x) #6). 繪製 ax.plot(x, y, color='orange', marker='*', linestyle='-.') ax.set_title('y = sinx') ax.set_xlabel('x') ax.set_ylabel('y') #7). 顯示圖形 plt.show()
import matplotlib.pyplot as plt import numpy as np #1). 準備數據信息 x = np.linspace(0, 20, 100) y = np.sin(x) #2). 直接繪圖 plt.plot(x, y) #plt.scatter(x, y) plt.title('y = sinx') plt.xlabel('x') plt.ylabel('y') #3). 繪製圖形並顯示 plt.show()
多圖繪製
多圖案例dom
繪製步驟機器學習
案例一: 散點圖和折線圖
繪圖需求: 基於某函數,並在其必定範圍震動的離散圖。必定範圍內震動呢?y加個隨機數ide
1.1 散點圖繪製
.
子圖繪製函數
#1). 導入繪圖庫 import matplotlib.pyplot as plt import numpy as np #2). 建立畫板figure figure = plt.figure(figsize=(10, 10)) #3). 建立子圖subplot/Axes, 生成2行一列的子圖, #第一行第一列繪製sinx的圖形, 第二行第一列繪製cosx的圖形 ax1 = plt.subplot(2, 1, 1) ax2 = plt.subplot(2, 1, 2) #4). 準備數據 #從0-50分割成100份 x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) #6). 繪製 ax1.plot(x, y1, color='orange', linestyle='-.') ax1.set_title('y = sinx') ax1.set_xlabel('x') ax1.set_ylabel('y') ax2.plot(x, y2, color='m', linestyle='-.') ax2.set_title('y = cosx') ax2.set_xlabel('x') ax2.set_ylabel('y') #7). 顯示圖形 plt.show()
1.2 曲線圖
2.學習
案例二: 柱狀圖
條形圖(bar chart),也稱爲柱狀圖,是一種以長方形的長度爲變量的統計圖表,長方形的長
度與它所對應的變量數值呈必定比例。假設某項針對男女大學生購買飲用水愛好的調查結果以下
表:
豎向條形繪製測試
import matplotlib.pyplot as plt # 1. 建立figure fig = plt.figure() # 2. 建立子圖(1行2列) ax1 = plt.subplot(1, 2, 1) ax1.set_title('男生購買飲用水狀況的調查結果') ax2 = plt.subplot(1, 2, 2) ax2.set_title('女生購買飲用水狀況的調查結果') # ***********中文亂碼如何解決 plt.rcParams['font.sans-serif'] = ['SimHei'] # 3. 加載數據信息 waters = ['碳酸飲料', '綠茶', '礦泉水', '其它', '果汁'] boy_buy_num = [6, 7, 6, 2, 1] girl_buy_num = [9, 4, 4, 6, 5] # 4. 繪製條形圖 bar_width = 0.4 ax1.bar(waters, boy_buy_num, bar_width, color='orange') ax2.bar(waters, girl_buy_num, bar_width, color='g') # 5. 保存圖片到本地 # plt.show() plt.savefig('bar.png')
橫向條形圖繪製
import matplotlib.pyplot as plt # 1. 建立figure fig = plt.figure() # 2. 建立子圖(1行2列) ax1 = plt.subplot(2, 1, 1) ax1.set_title('男生購買飲用水狀況的調查結果') ax2 = plt.subplot(2, 1, 2) ax2.set_title('女生購買飲用水狀況的調查結果') # ***********中文亂碼如何解決 plt.rcParams['font.sans-serif'] = ['SimHei'] # 3. 加載數據信息 waters = ['碳酸飲料', '綠茶', '礦泉水', '其它', '果汁'] boy_buy_num = [6, 7, 6, 2, 1] girl_buy_num = [9, 4, 4, 6, 5] # 4. 繪製條形圖 bar_width = 0.4 ax1.barh(waters, boy_buy_num, height=bar_width, color='orange') ax2.barh(waters, girl_buy_num, height=bar_width, color='g') # 5. 保存圖片到本地 # plt.show() plt.savefig('bar.png')
案例二: 柱狀圖
並列條形圖: 若要將男生與女生的調查狀況畫出兩個條形圖一塊顯示,則可使用 bar兩次,
並調整 條形圖位置座標以及相應刻度,使得兩組條形圖可以並排顯示
並列條形圖繪製
import matplotlib.pyplot as plt import numpy as np # ***********中文亂碼如何解決 plt.rcParams['font.sans-serif'] = ['SimHei'] # 加載數據信息 waters = ['碳酸飲料', '綠茶', '礦泉水', '其它', '果汁'] boy_buy_num = [6, 7, 6, 2, 1] girl_buy_num = [9, 4, 4, 6, 5] # 調整條形圖的橫座標 bar_width = 0.4 boy_index = np.arange(len(waters)) # [0, 1, 2, 3, 4] girl_index = boy_index + bar_width # numpy傳播機制, [0.4, 1.4, 2.4, 3.4, 4.4] # 繪製條形圖 plt.bar(boy_index, boy_buy_num, bar_width, color='orange', label='男生') plt.bar(girl_index, girl_buy_num, bar_width, color='g', label='女生') # 修改無心義的橫座標爲有意義的橫座標 plt.xticks(boy_index + bar_width / 2, waters) plt.ylabel("購買量") plt.title("購買飲水狀況的調查表") plt.legend() # 保存圖片到本地 # plt.show() plt.savefig('bar.png')
K近鄰算法原理
K近鄰算法: 近朱者赤,近墨者黑。新的數據點離誰(一個或多個近鄰點)最近, 就和誰屬於同一類
K近鄰算法數據集
在skilit-learn中內置了若干個玩具數據集(Toy Datasets), 還有一些API能夠本身動手生成數
據集, 以下面代碼所示:
K近鄰算法分類
咱們已經生成一系列數據集看成機器學習的訓練數據集,接下來就是根據KNN算法找一個
模型, 而後根據模型對未知數據進行分類。
K近鄰算法迴歸可視化分析
K近鄰算法迴歸原理
K近鄰算法也能夠用於迴歸, 原理和分類相同。 計算每一個數據點的預測值時,模型會選擇離該
數據點最近的若干個點,並將它們的y值取平均值,並做爲新數據點的預測值
from sklearn.datasets import make_blobs from sklearn.neighbors import KNeighborsClassifier import matplotlib.pyplot as plt # 生成數據集, eg: x=(花瓣長度, 花莖的長度), y=(第一種花: 鳶尾花1). 訓練集 X, y = make_blobs(n_samples=200, n_features=2, centers=2, random_state=8) # 如何去尋找一個模型,並最終根據模型預判新的測試數據所屬的分類? # 機器學習: 尋找一個函數/模型的過程. f(x)='xxxxxxx', f(image)='cat', f(alpha-go)=5x5, f('對話')=‘對話’ clf = KNeighborsClassifier() clf.fit(X, y) # 擬合(找模型的過程) test_data = [6, 3] # 測試集: 測試模型好壞/正確率的數據集 class_name = clf.predict([test_data]) print("新數據點的分類是: ", class_name) # # 給定一個新的特徵信息, 分析屬於哪一類? # test_data = [6, 3] # # # 經過繪製散點圖, 清楚的看到分爲2類 # plt.scatter(X[:, 0], X[:, 1], edgecolors='orange', color='white') # plt.scatter(test_data[0], test_data[1], marker='*', color='r') # plt.show()
import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_regression from sklearn.neighbors import KNeighborsRegressor #產生迴歸的數據集(訓練集) X, y = make_regression(n_samples=100, n_features=1, n_informative=1, noise=50, random_state=8) #經過K近鄰的迴歸器, 擬合/尋找模型 reg = KNeighborsRegressor() reg.fit(X, y) #給定一些新的數據(測試集),預測y值 """ 一維數組: np.linspace [1, 2, 3, 4, 5] n*1數組: rwshape(-1, 1) reg.predict([[1], [2]]) # 預測市須要傳遞的信息 """ test_x = np.linspace(-3, 3, 100).reshape(-1, 1) test_y = reg.predict(test_x) #print("模型的準確度: ", reg.score(X, y)) #***********中文亂碼如何解決 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False #繪製圖形 plt.scatter(X, y, marker='*', color='orange', edgecolors='orange', label='訓練集') plt.plot(test_x, test_y, color='black', label='測試集') plt.title('KNN Regressor') plt.legend() plt.show()
官方網址:
https://seaborn.pydata.org/introduction.html
seaborn總體風格
Seaborn共提供5種主題風格,分別爲darkgrid、whitegrid、dark、 white以及ticks。
利用set()和set_style()兩個函數對總體風格進行控制
設置子圖風格
經過關鍵字with,對不一樣子圖設置風格
seaborn內容風格
對圖中內容進行設置,包括線條顏色,粗細和刻度等