1. 程式人生 > >Seaborn檢視資料特徵

Seaborn檢視資料特徵

‘’’
另一篇部落格:https://blog.csdn.net/weixin_40924580/article/details/82809484 有關於使用matplotlib檢視titanic資料特徵的方法
‘’’

資料下載——儲存——匯入

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_style('darkgrid')

# names = ['mpg','cylinders','displacement','horsepower','weight','acceleration','model_year','origin','car_name']
# df =pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data',sep='\s+',names=names)
# df.to_csv("H:/seaborn2.csv")

df = pd.read_csv('H:/seaborn2.csv')
df['maker'] = df.car_name.map(lambda x:x.split()[0])        #以空格分割,取第一個
df.origin = df.origin.map({1:'Americ',2:'Europe',3:'Asia'})
df.applymap(lambda x:np.nan if x=='?' else x).dropna()
# sns.factorplot(data=df,x="model_year",y="mpg",col="origin")         #!1 按照origin維度取值不同畫圖

在這裡插入圖片描述

# sns.factorplot(data=df,x="model_year",y="mpg",col="origin",kind = 'bar')         #!2 柱狀圖

在這裡插入圖片描述

# sns.factorplot("model_year",data=df,col="origin",kind = 'count')    #!3 對某一列根據origin計數

在這裡插入圖片描述

# g = sns.FacetGrid(df,col='origin')                                #!4 並檢視分佈
# g.map(sns.distplot,'mpg')

在這裡插入圖片描述

# g = sns.FacetGrid(df, col="origin")                               #!5散點圖
# g.map(plt.scatter, "horsepower", "mpg")

在這裡插入圖片描述

# g = sns.FacetGrid(df, col="origin")                                 #!6 畫圖同時包含迴歸
# g.map(sns.regplot, "displacement", "mpg")
# plt.xlim(0, 250)
# plt.ylim(0, 60)

在這裡插入圖片描述

# df['tons'] = (df.weight/2000).astype(int)                       #! 7密度繪圖
# g = sns.FacetGrid(df, col="origin", row="tons")
# g.map(sns.kdeplot, "displacement", "mpg")
# plt.xlim(0, 250)
# plt.ylim(0, 60)

在這裡插入圖片描述

# sns.jointplot("mpg", "weight", data=df, kind='kde')                     #!8聯合繪圖

在這裡插入圖片描述

# sns.jointplot("mpg", "weight", data=df, kind='reg')                     #!9聯合繪圖加回歸

在這裡插入圖片描述

# g = sns.JointGrid(x="weight", y="mpg", data=df)                         #!10多項式聯合迴歸,"order"
# g.plot_joint(sns.regplot, order=2)
# g.plot_marginals(sns.distplot)

在這裡插入圖片描述