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

Python可視化學習之seaborn繪制線型回歸曲線

 更新時間:2022年02月24日 15:59:49   作者:qq_21478261  
這篇文章主要為大家介紹了如何利用seaborn繪制變量之間線型回歸(linear regression)曲線,2文中涉及如下兩個重要函數(shù):seaborn.regplot和seaborn.lmplot,感興趣的小伙伴可以跟隨小編一起學習一下

本文速覽

1、繪圖數(shù)據(jù)準備

依舊使用鳶尾花iris數(shù)據(jù)集,詳細介紹見之前文章。

#導入本帖要用到的庫,聲明如下:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd 
import palettable
from pandas import Series,DataFrame
from sklearn import datasets
import seaborn as sns
import palettable
#導入鳶尾花iris數(shù)據(jù)集(方法一)
#該方法更有助于理解數(shù)據(jù)集
iris=datasets.load_iris()
x, y =iris.data,iris.target
y_1 = np.array(['setosa' if i==0 else 'versicolor' if i==1 else 'virginica' for i in y])
pd_iris = pd.DataFrame(np.hstack((x, y_1.reshape(150,1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'])
 
#astype修改pd_iris中數(shù)據(jù)類型object為float64
pd_iris['sepal length(cm)']=pd_iris['sepal length(cm)'].astype('float64')
pd_iris['sepal width(cm)']=pd_iris['sepal width(cm)'].astype('float64')
pd_iris['petal length(cm)']=pd_iris['petal length(cm)'].astype('float64')
pd_iris['petal width(cm)']=pd_iris['petal width(cm)'].astype('float64')
 
 
#導入鳶尾花iris數(shù)據(jù)集(方法二)
#該方法有時候會卡巴斯基,所以棄而不用
#import seaborn as sns
#iris_sns = sns.load_dataset("iris")

數(shù)據(jù)集簡單查看

2、seaborn.regplot

seaborn.regplot(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)

regplot默認參數(shù)線型回歸圖

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)#設(shè)置主題,文本大小
g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',#設(shè)置marker及線的顏色
             marker='*',#設(shè)置marker形狀
             )

分別設(shè)置點和擬合線屬性

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
              color='#000000',
              marker='*',
              scatter_kws={'s': 60,'color':'g',},#設(shè)置散點屬性,參考plt.scatter
              line_kws={'linestyle':'--','color':'r'}#設(shè)置線屬性,參考 plt.plot          

置信區(qū)間(confidence interval)設(shè)置

注意擬合線周圍陰影面積變化 

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',
             marker='*',
             ci=60,#置信區(qū)間設(shè)置,默認為95%置信區(qū)間,越大線周圍陰影部分面積越大
             )

擬合線延伸與坐標軸相交 

# extend the regression line to the axis limits
plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',
             marker='*',
             truncate=False,#讓擬合線與軸相交
             )

擬合離散變量曲線

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
x_discrete=[0 if i=='setosa' else 1 if i=='versicolor' else 2 for i in pd_iris['class']]#
g=sns.regplot(x=x_discrete, y='sepal width(cm)', data=pd_iris,#x此時為離散變量
             color='#000000',
             marker='*',
             )

多項式回歸( polynomial regression)擬合曲線

plt.figure(dpi=110)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             marker='*',
             order=4,#默認為1,越大越彎曲
             scatter_kws={'s': 60,'color':'#016392',},#設(shè)置散點屬性,參考plt.scatter
             line_kws={'linestyle':'--','color':'#c72e29'}#設(shè)置線屬性,參考 plt.plot             
             
             )

3、seaborn.lmplot

seaborn.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, height=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)

seaborn.lmplot結(jié)合seaborn.regplot()和FacetGrid,比seaborn.regplot()更靈活,可繪制更個性化的圖形。

按變量分類擬合回歸線

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             )
g.fig.set_size_inches(10,8)

散點marker設(shè)置

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             markers=['+','^','o'],   #設(shè)置散點marker          
             )
g.fig.set_size_inches(10,8)

散點調(diào)色盤

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             markers=['+','^','*'],
             scatter_kws={'s':180},
             palette=["#01a2d9", "#31A354", "#c72e29"],#調(diào)色盤
             )
g.fig.set_size_inches(10,8)

擬合線屬性設(shè)置

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             markers=['+','^','*'],
             scatter_kws={'s':180},
             line_kws={'linestyle':'--'},#擬合線屬性設(shè)置
             palette=["#01a2d9", "#31A354", "#c72e29"],
             )
g.fig.set_size_inches(10,8)

繪制分面圖 

plt.figure(dpi=100)
sns.set(style="whitegrid",font_scale=1.2)
g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             col='class',#按class繪制分面圖
             markers='*',
             scatter_kws={'s':150,'color':'#01a2d9'},
             line_kws={'linestyle':'--','color':'#c72e29'},#直線屬性設(shè)置
             )
g.fig.set_size_inches(10,8)

以上就是Python可視化學習之seaborn繪制線型回歸曲線的詳細內(nèi)容,更多關(guān)于Python seaborn線型回歸曲線的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python中調(diào)用其他程序的方式詳解

    Python中調(diào)用其他程序的方式詳解

    這篇文章主要介紹了Python中調(diào)用其他程序的方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • 基于Python實現(xiàn)全自動二維碼識別

    基于Python實現(xiàn)全自動二維碼識別

    這篇文章主要為大家詳細介紹了如何基于Python實現(xiàn)全自動二維碼識別功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • 使用Python繪制詞云圖的詳細教程

    使用Python繪制詞云圖的詳細教程

    詞云(Word Cloud)是一種數(shù)據(jù)可視化技術(shù),用于顯示文本數(shù)據(jù)中的頻繁單詞,在本教程中,我們將使用 Python 的 wordcloud 庫,結(jié)合 matplotlib 和 jieba 等工具,展示如何從文本數(shù)據(jù)生成詞云圖,需要的朋友可以參考下
    2025-01-01
  • Python實現(xiàn)根據(jù)指定端口探測服務(wù)器/模塊部署的方法

    Python實現(xiàn)根據(jù)指定端口探測服務(wù)器/模塊部署的方法

    這篇文章主要介紹了Python根據(jù)指定端口探測服務(wù)器/模塊部署的方法,非常具有實用價值,需要的朋友可以參考下
    2014-08-08
  • kali中python版本的切換方法

    kali中python版本的切換方法

    今天小編就為大家分享一篇kali中python版本的切換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • TensorFlow自定義模型保存加載和分布式訓練

    TensorFlow自定義模型保存加載和分布式訓練

    本篇文章將涵蓋 TensorFlow 的高級應(yīng)用,包括如何自定義模型的保存和加載過程,以及如何進行分布式訓練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • GraphQL在Django中的使用教程

    GraphQL在Django中的使用教程

    這篇文章主要介紹了GraphQL在Django中的使用教程,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • Python 實現(xiàn)將某一列設(shè)置為str類型

    Python 實現(xiàn)將某一列設(shè)置為str類型

    這篇文章主要介紹了Python 實現(xiàn)將某一列設(shè)置為str類型,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python中Playwright模塊進行自動化測試的實現(xiàn)

    Python中Playwright模塊進行自動化測試的實現(xiàn)

    playwright是由微軟開發(fā)的Web UI自動化測試工具,本文主要介紹了Python中Playwright模塊進行自動化測試的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • Python開發(fā)的十個小貼士和技巧及長常犯錯誤

    Python開發(fā)的十個小貼士和技巧及長常犯錯誤

    這篇文章主要介紹了Python開發(fā)的十個小貼士和技巧,其中一些是初學這門語言常常會犯的錯誤,小編給大家一一列舉出來了,需要的朋友可以參考下
    2018-09-09

最新評論

韶关市| 柳河县| 台中县| 芦山县| 安宁市| 琼中| 南投市| 泸定县| 灵川县| 新丰县| 广宁县| 辽宁省| 崇文区| 城固县| 永川市| 瑞金市| 航空| 澜沧| 旺苍县| 樟树市| 台东市| 亳州市| 遵化市| 新乡市| 镇安县| 三明市| 托里县| 阳信县| 北京市| 洮南市| 宝应县| 宁夏| 吴川市| 普陀区| 长宁县| 深州市| 湖北省| 德令哈市| 宜丰县| 中江县| 崇信县|