Python實現(xiàn)線性擬合及繪圖的示例代碼
當時的數(shù)字地形實驗,使用 matplotlib庫繪制了一張圖表表示不同地形類別在不同分辨率下的RMSE值,并分別擬合了一條趨勢線?,F(xiàn)在來看不足就是地形較多時,需要使用循環(huán)更好一點,不然太冗余了。
環(huán)境:Python 3.9
代碼邏輯
導入所需庫以及初步設置
# coding=gbk # -*- coding = utf-8 -*- import matplotlib.pyplot as plt import numpy as np plt.subplots_adjust(left=0.05, right=0.7, top=0.9, bottom=0.1) plt.rcParams['font.sans-serif'] = ['SimHei']
準備數(shù)據(jù)(這里僅展示部分)
resolutions = [50, 100, 150, 200, 250] plain = [0, 0, 1, 1, 1] hill = [2.645751311, 7.071067812, 10.44030651, 11.48912529, 14.4222051]
這里可以改為在Excel中讀取,尤其是數(shù)據(jù)多的時候
分別繪制不同數(shù)據(jù)的趨勢線
# 繪制平原趨勢線 coefficients_plain = np.polyfit(resolutions, plain, 1) poly_plain = np.poly1d(coefficients_plain) plt.plot(resolutions, plain, '^', label="平原") plt.plot(resolutions, poly_plain(resolutions), label="平原趨勢線") # 繪制丘陵趨勢線 coefficients_hill = np.polyfit(resolutions, hill, 1) poly_hill = np.poly1d(coefficients_hill) plt.plot(resolutions, hill, '^', label="丘陵") plt.plot(resolutions, poly_hill(resolutions), label="丘陵趨勢線")
使用np.polyfit函數(shù)擬合一階多項式(直線),然后使用np.poly1d構造多項式對象。繪制原始數(shù)據(jù)點(用’^'標記)和對應的擬合趨勢線。
計算指標
# 計算平原趨勢線的r值和r方 residuals_plain = plain - poly_plain(resolutions) ss_residuals_plain = np.sum(residuals_plain**2) ss_total_plain = np.sum((plain - np.mean(plain))**2) r_squared_plain = 1 - (ss_residuals_plain / ss_total_plain) r_plain = np.sqrt(r_squared_plain) # 計算丘陵趨勢線的r值和r方 residuals_hill = hill - poly_hill(resolutions) ss_residuals_hill = np.sum(residuals_hill**2) ss_total_hill = np.sum((hill - np.mean(hill))**2) r_squared_hill = 1 - (ss_residuals_hill / ss_total_hill) r_hill = np.sqrt(r_squared_hill)
計算得到r方和r值
繪圖和打印指標
# 設置圖例和標題
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
plt.title("地形趨勢線")
# 設置坐標軸標題
new_ticks = np.arange(50, 251, 50)
plt.xticks(new_ticks)
plt.xlabel('分辨率(m)')
plt.ylabel('RMSE')
formula1 = "平原:{}".format(poly_plain)
plt.text(0.05, 0.95, formula1, transform=plt.gca().transAxes,
fontsize=10, verticalalignment='top')
formula1 = "丘陵:{}".format(poly_hill)
plt.text(0.35, 0.95, formula1, transform=plt.gca().transAxes,
fontsize=10, verticalalignment='top')
# 顯示圖形
plt.figure(figsize=(10, 10))
plt.show()
# 打印
print("平原趨勢線公式:", poly_plain)
print("丘陵趨勢線公式:", poly_hill)
print("平原趨勢線:")
print("r值:", r_plain)
print("r方:", r_squared_plain)
print()
print("丘陵趨勢線:")
print("r值:", r_hill)
print("r方:", r_squared_hill)
print()
完整代碼
# coding=gbk
# -*- coding = utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
plt.subplots_adjust(left=0.05, right=0.7, top=0.9, bottom=0.1)
plt.rcParams['font.sans-serif'] = ['SimHei']
resolutions = [50, 100, 150, 200, 250]
plain = [0, 0, 1, 1, 1]
hill = [2.645751311, 7.071067812, 10.44030651, 11.48912529, 14.4222051]
# 繪制平原趨勢線
coefficients_plain = np.polyfit(resolutions, plain, 1)
poly_plain = np.poly1d(coefficients_plain)
plt.plot(resolutions, plain, '^', label="平原")
plt.plot(resolutions, poly_plain(resolutions), label="平原趨勢線")
# 繪制丘陵趨勢線
coefficients_hill = np.polyfit(resolutions, hill, 1)
poly_hill = np.poly1d(coefficients_hill)
plt.plot(resolutions, hill, '^', label="丘陵")
plt.plot(resolutions, poly_hill(resolutions), label="丘陵趨勢線")
# 計算平原趨勢線的r值和r方
residuals_plain = plain - poly_plain(resolutions)
ss_residuals_plain = np.sum(residuals_plain**2)
ss_total_plain = np.sum((plain - np.mean(plain))**2)
r_squared_plain = 1 - (ss_residuals_plain / ss_total_plain)
r_plain = np.sqrt(r_squared_plain)
# 計算丘陵趨勢線的r值和r方
residuals_hill = hill - poly_hill(resolutions)
ss_residuals_hill = np.sum(residuals_hill**2)
ss_total_hill = np.sum((hill - np.mean(hill))**2)
r_squared_hill = 1 - (ss_residuals_hill / ss_total_hill)
r_hill = np.sqrt(r_squared_hill)
# 設置圖例和標題
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
plt.title("地形趨勢線")
# 設置坐標軸標題
new_ticks = np.arange(50, 251, 50)
plt.xticks(new_ticks)
plt.xlabel('分辨率(m)')
plt.ylabel('RMSE')
formula1 = "平原:{}".format(poly_plain)
plt.text(0.05, 0.95, formula1, transform=plt.gca().transAxes,
fontsize=10, verticalalignment='top')
formula1 = "丘陵:{}".format(poly_hill)
plt.text(0.35, 0.95, formula1, transform=plt.gca().transAxes,
fontsize=10, verticalalignment='top')
# 顯示圖形
plt.figure(figsize=(10, 10))
plt.show()
# 打印
print("平原趨勢線公式:", poly_plain)
print("丘陵趨勢線公式:", poly_hill)
print("平原趨勢線:")
print("r值:", r_plain)
print("r方:", r_squared_plain)
print()
print("丘陵趨勢線:")
print("r值:", r_hill)
print("r方:", r_squared_hill)
print()
結果

參考
到此這篇關于Python實現(xiàn)線性擬合及繪圖的示例代碼的文章就介紹到這了,更多相關Python 線性擬合及繪圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談python 中的 type(), dtype(), astype()的區(qū)別
這篇文章主要介紹了淺談python 中的 type(), dtype(), astype()的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python讀取配置文件(config.ini)以及寫入配置文件
這篇文章主要介紹了Python讀取配置文件(config.ini)以及寫入配置文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python的shutil模塊中文件的復制操作函數(shù)詳解
shutil被定義為Python中的一個高級的文件操作模塊,擁有比os模塊中更強大的函數(shù),這里我們就來看一下Python的shutil模塊中文件的復制操作函數(shù)詳解2016-07-07
python3利用venv配置虛擬環(huán)境及過程中的小問題小結
這篇文章主要介紹了python3利用venv配置虛擬環(huán)境及過程中的小問題小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

