欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文速覽

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

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

#導入本帖要用到的庫,聲明如下:
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ù)據集(方法一)
#該方法更有助于理解數(shù)據集
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ù)據類型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ù)據集(方法二)
#該方法有時候會卡巴斯基,所以棄而不用
#import seaborn as sns
#iris_sns = sns.load_dataset("iris")

數(shù)據集簡單查看

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)#設置主題,文本大小
g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',#設置marker及線的顏色
             marker='*',#設置marker形狀
             )

分別設置點和擬合線屬性

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',},#設置散點屬性,參考plt.scatter
              line_kws={'linestyle':'--','color':'r'}#設置線屬性,參考 plt.plot          

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

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

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ū)間設置,默認為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',},#設置散點屬性,參考plt.scatter
             line_kws={'linestyle':'--','color':'#c72e29'}#設置線屬性,參考 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結合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設置

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'],   #設置散點marker          
             )
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,
             hue='class',
             markers=['+','^','*'],
             scatter_kws={'s':180},
             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,
             hue='class',
             markers=['+','^','*'],
             scatter_kws={'s':180},
             line_kws={'linestyle':'--'},#擬合線屬性設置
             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'},#直線屬性設置
             )
g.fig.set_size_inches(10,8)

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

相關文章

  • git使用.gitignore設置不生效或不起作用問題的解決方法

    git使用.gitignore設置不生效或不起作用問題的解決方法

    下面小編就為大家?guī)硪黄猤it使用.gitignore設置不生效或不起作用問題的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • selenium在scrapy中的使用代碼

    selenium在scrapy中的使用代碼

    本文給大家分享selenium在scrapy中的使用代碼,使用selenium可以很好的幫助我們獲取一些重要數(shù)據信息,本文通過代碼給大家詳細介紹,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Python學習思維導圖(必看篇)

    Python學習思維導圖(必看篇)

    下面小編就為大家?guī)硪黄狿ython學習思維導圖(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Python二叉樹初識(新手也秒懂!)

    Python二叉樹初識(新手也秒懂!)

    二叉樹是一種簡單的樹形結構,其每個節(jié)點的分支節(jié)點數(shù)有0,1或2個,下面這篇文章主要給大家介紹了關于Python二叉樹的相關資料,本文介紹的非常通俗易懂,新手也秒懂,需要的朋友可以參考下
    2022-05-05
  • 使用Python發(fā)送Post請求以及解析響應結果

    使用Python發(fā)送Post請求以及解析響應結果

    發(fā)送post的請求參考例子很簡單,實際遇到的情況卻是很復雜的,下面這篇文章主要給大家介紹了關于如何使用Python發(fā)送Post請求以及解析響應結果的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • 分享6個隱藏的python功能

    分享6個隱藏的python功能

    給大家詳細分析了6個隱藏的python功能,并詳細講解了每個功能用法,需要的朋友學習下吧。
    2017-12-12
  • matplotlib基礎繪圖命令之errorbar的使用

    matplotlib基礎繪圖命令之errorbar的使用

    這篇文章主要介紹了matplotlib基礎繪圖命令之errorbar的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • python Hypothesis生成和執(zhí)行大量的測試用例

    python Hypothesis生成和執(zhí)行大量的測試用例

    Hypothesis是一個基于屬性的測試(property-based testing)庫,它能夠幫助我們生成和執(zhí)行大量的測試用例,與傳統(tǒng)的單元測試相比,屬性測試更加靈活和全面,能夠發(fā)現(xiàn)更多的邊界情況和潛在的錯誤
    2024-01-01
  • opencv python簡易文檔之圖片基本操作指南

    opencv python簡易文檔之圖片基本操作指南

    OpenCv除了可以獲取圖像之外,還可以對圖像進行一下處理操作,下面這篇文章主要給大家介紹了關于opencv python簡易文檔之圖片基本操作的相關資料,需要的朋友可以參考下
    2021-08-08
  • Python求算數(shù)平方根和約數(shù)的方法匯總

    Python求算數(shù)平方根和約數(shù)的方法匯總

    這篇文章主要介紹了 Python求算數(shù)平方根和約數(shù)的方法匯總的相關資料,需要的朋友可以參考下
    2016-03-03

最新評論