最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)線(xiàn)性擬合及繪圖的示例代碼

 更新時(shí)間:2024年04月28日 14:42:20   作者:浩瀚地學(xué)  
在數(shù)據(jù)處理和繪圖中,我們通常會(huì)遇到直線(xiàn)或曲線(xiàn)的擬合問(wèn)題,本文主要介紹了Python實(shí)現(xiàn)線(xiàn)性擬合及繪圖的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下

當(dāng)時(shí)的數(shù)字地形實(shí)驗(yàn),使用 matplotlib庫(kù)繪制了一張圖表表示不同地形類(lèi)別在不同分辨率下的RMSE值,并分別擬合了一條趨勢(shì)線(xiàn)?,F(xiàn)在來(lái)看不足就是地形較多時(shí),需要使用循環(huán)更好一點(diǎn),不然太冗余了。

環(huán)境:Python 3.9

代碼邏輯

導(dǎo)入所需庫(kù)以及初步設(shè)置

# 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']

準(zhǔn)備數(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í)候

分別繪制不同數(shù)據(jù)的趨勢(shì)線(xiàn)

# 繪制平原趨勢(shì)線(xiàn)
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="平原趨勢(shì)線(xiàn)")

# 繪制丘陵趨勢(shì)線(xiàn)
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="丘陵趨勢(shì)線(xiàn)")

使用np.polyfit函數(shù)擬合一階多項(xiàng)式(直線(xiàn)),然后使用np.poly1d構(gòu)造多項(xiàng)式對(duì)象。繪制原始數(shù)據(jù)點(diǎn)(用’^'標(biāo)記)和對(duì)應(yīng)的擬合趨勢(shì)線(xiàn)。

計(jì)算指標(biāo)

# 計(jì)算平原趨勢(shì)線(xiàn)的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)

# 計(jì)算丘陵趨勢(shì)線(xiàn)的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)

計(jì)算得到r方和r值

繪圖和打印指標(biāo)

# 設(shè)置圖例和標(biāo)題
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
plt.title("地形趨勢(shì)線(xiàn)")

# 設(shè)置坐標(biāo)軸標(biāo)題
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("平原趨勢(shì)線(xiàn)公式:", poly_plain)
print("丘陵趨勢(shì)線(xiàn)公式:", poly_hill)
print("平原趨勢(shì)線(xiàn):")
print("r值:", r_plain)
print("r方:", r_squared_plain)
print()
print("丘陵趨勢(shì)線(xiàn):")
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]

# 繪制平原趨勢(shì)線(xiàn)
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="平原趨勢(shì)線(xiàn)")

# 繪制丘陵趨勢(shì)線(xiàn)
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="丘陵趨勢(shì)線(xiàn)")

# 計(jì)算平原趨勢(shì)線(xiàn)的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)

# 計(jì)算丘陵趨勢(shì)線(xiàn)的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)

# 設(shè)置圖例和標(biāo)題
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
plt.title("地形趨勢(shì)線(xiàn)")

# 設(shè)置坐標(biāo)軸標(biāo)題
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("平原趨勢(shì)線(xiàn)公式:", poly_plain)
print("丘陵趨勢(shì)線(xiàn)公式:", poly_hill)
print("平原趨勢(shì)線(xiàn):")
print("r值:", r_plain)
print("r方:", r_squared_plain)
print()
print("丘陵趨勢(shì)線(xiàn):")
print("r值:", r_hill)
print("r方:", r_squared_hill)
print()

結(jié)果

在這里插入圖片描述

參考

Matplotlib pyplot文檔

到此這篇關(guān)于Python實(shí)現(xiàn)線(xiàn)性擬合及繪圖的示例代碼的文章就介紹到這了,更多相關(guān)Python 線(xiàn)性擬合及繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python與Node.js之間實(shí)現(xiàn)通信的JSON數(shù)據(jù)接收發(fā)送

    Python與Node.js之間實(shí)現(xiàn)通信的JSON數(shù)據(jù)接收發(fā)送

    Python和Node.js是兩個(gè)流行且功能強(qiáng)大的編程語(yǔ)言,它們之間使用JSON格式進(jìn)行數(shù)據(jù)交換是一種高效和靈活的方式,本文將詳細(xì)介紹如何在Python和Node.js之間通過(guò)JSON進(jìn)行數(shù)據(jù)通信,包括發(fā)送和接收J(rèn)SON數(shù)據(jù)以及一些常見(jiàn)的交互示例代碼
    2024-01-01
  • 深入剖析Python的列表和元組

    深入剖析Python的列表和元組

    這篇文章主要介紹了深入剖析Python的列表和元組,Python有4個(gè)內(nèi)建的數(shù)據(jù)結(jié)構(gòu),它們可以統(tǒng)稱(chēng)為容器,因?yàn)樗鼈儗?shí)際上是一些“東西”組合而成的結(jié)構(gòu),而這些“東西”,可以是數(shù)字、字符甚至列表,或是它們的組合,需要的朋友可以參考下
    2023-07-07
  • Python的string模塊中的Template類(lèi)字符串模板用法

    Python的string模塊中的Template類(lèi)字符串模板用法

    通過(guò)string.Template我們可以為Python定制字符串的替換標(biāo)準(zhǔn),這里我們就來(lái)通過(guò)示例解析Python的string模塊中的Template類(lèi)字符串模板用法:
    2016-06-06
  • Python使用PIL打開(kāi)圖片后對(duì)圖片重命名報(bào)錯(cuò)的解決方案

    Python使用PIL打開(kāi)圖片后對(duì)圖片重命名報(bào)錯(cuò)的解決方案

    在Windows系統(tǒng)中,當(dāng)文件被某個(gè)進(jìn)程占用時(shí),其他進(jìn)程無(wú)法修改/重命名該文件,使用PIL打開(kāi)圖片后,確實(shí)需要顯式關(guān)閉圖片對(duì)象以釋放文件句柄,本文給大家介紹了詳細(xì)的解決方案,需要的朋友可以參考下
    2026-01-01
  • 詳細(xì)介紹在pandas中創(chuàng)建category類(lèi)型數(shù)據(jù)的幾種方法

    詳細(xì)介紹在pandas中創(chuàng)建category類(lèi)型數(shù)據(jù)的幾種方法

    這篇文章主要介紹了詳細(xì)介紹在pandas中創(chuàng)建category類(lèi)型數(shù)據(jù)的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • python通過(guò)zlib實(shí)現(xiàn)壓縮與解壓字符串的方法

    python通過(guò)zlib實(shí)現(xiàn)壓縮與解壓字符串的方法

    這篇文章主要介紹了python通過(guò)zlib實(shí)現(xiàn)壓縮與解壓字符串的方法,較為詳細(xì)的介紹了zlib的用法及使用zlib.compressobj和zlib.decompressobj對(duì)文件進(jìn)行壓縮解壓的方法,需要的朋友可以參考下
    2014-11-11
  • pyinstaller打包路徑的總結(jié)

    pyinstaller打包路徑的總結(jié)

    本文主要介紹了pyinstaller打包路徑的總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • python 利用文件鎖單例執(zhí)行腳本的方法

    python 利用文件鎖單例執(zhí)行腳本的方法

    今天小編就為大家分享一篇python 利用文件鎖單例執(zhí)行腳本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 使用PyQt實(shí)現(xiàn)簡(jiǎn)易文本編輯器

    使用PyQt實(shí)現(xiàn)簡(jiǎn)易文本編輯器

    這篇文章主要為大家詳細(xì)介紹了如何使用PyQt5框架構(gòu)建一個(gè)簡(jiǎn)單的文本編輯器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-02-02
  • Python使用lxml庫(kù)和Xpath提取網(wǎng)頁(yè)數(shù)據(jù)的完整指南與最佳實(shí)戰(zhàn)

    Python使用lxml庫(kù)和Xpath提取網(wǎng)頁(yè)數(shù)據(jù)的完整指南與最佳實(shí)戰(zhàn)

    本文介紹使用Python的lxml庫(kù)和Xpath提取網(wǎng)頁(yè)數(shù)據(jù),涵蓋lxml庫(kù)基礎(chǔ)用法、代碼實(shí)戰(zhàn),還提及處理動(dòng)態(tài)加載和命名空間等進(jìn)階用法,同時(shí)闡述了爬蟲(chóng)的錯(cuò)誤處理、數(shù)據(jù)存儲(chǔ)、部署、監(jiān)控等內(nèi)容,以及后續(xù)的數(shù)據(jù)處理、分析、可視化和報(bào)告撰寫(xiě)等步驟,需要的朋友可以參考下
    2025-11-11

最新評(píng)論

宁城县| 永新县| 邳州市| 利辛县| 高清| 临桂县| 西峡县| 冕宁县| 沙田区| 兴城市| 内江市| 酒泉市| 寿宁县| 平阴县| 桓台县| 平舆县| 灵丘县| 繁昌县| 蒙山县| 卢氏县| 策勒县| 红桥区| 库尔勒市| 苍山县| 察隅县| 麻城市| 江达县| 孟津县| 株洲县| 盘锦市| 如东县| 西盟| 黄冈市| 收藏| 佛冈县| 老河口市| 七台河市| 达拉特旗| 洪雅县| 漳浦县| 海城市|