Seaborn數據探索可視化


seaborn

修改圖片尺寸
from matplotlib import pyplot as plt
plt.figure(figsize=(100, 5))
通過以上兩行起作用
sns.barplot(x=age_survived.index, y=age_survived['Survived'] + age_survived['Dead'], color='red')
sns.barplot(x=age_survived.index, y=age_survived['Survived'], color='green')

數據探索性分析

區分boxplot, barplot

barplot與boxplot畫圖效果不一致,barplot不能顯示較高精度。boxplot可以顯示均值范圍以及異常值個數。

數據離散程度以及異常值

sns.pairplot(iris, hue='Name')
單個變量離散程度
sns.scatterplot(x=train.index, y=train.SibSp)

數據范圍

由pandas提供boxplot方法
iris.boxplot(by="Name", figsize=(12, 6))

roc/auc

y_test若不為二分類label,則需要指定pos_label, pos_labelz作為正例,其余class都作為反例
fpr, tpr, threshold = roc_curve(y_test, svc.decision_function(X_test)[:, 1], pos_label=1)
sns.lineplot(x=fpr, y=tpr)

混淆矩陣

sns.heatmap(confusion_matrix(y_test, svc.predict(X_test)), annot=True)

grid search查找參數

y軸為param_grid中第一個參數,x軸為param_grid中第二個參數,但不能設置xlable, ylabel
sns.heatmap(scores, yticklabels=param_grid['C'], xticklabels=param_grid['gamma'],
annot=True, )

特征重要性

設置orient,且交換x, y
sns.barplot(x=sorted_feature_importances, y=sorted_feature_names, orient='h')
sns.barplot(x=sorted_feature_names, y=sorted_feature_importances)

keras model train_history

構建一個DataFrame,x='epoches', y='acc'分別為DataFrame key
sns.lineplot(x='epoches', y='acc', data=train_history_df, markers=True)

由dict構建一個DataFrame
history_dic = train_history.history
epochs = np.shape(train_history.history['acc'])[0]
history_dic['epoches'] = range(epochs)

直方圖

ratings['rating']為pandas中Series結構
sns.distplot(ratings['rating'])

matploitlib直方圖
plt.hist(ratings['rating'])

對離散變量,且取值個數較少情況

用barplot或者catplot,可以引用hue屬性,查看3個變量情況
sns.barplot(x='SibSp', y='Survived', hue='Sex', data=train_ready)
sns.catplot(x='SibSp', y='Survived', hue='Sex', data=train_ready, kind='bar')
sns.catplot(x='SibSp', y='Survived', hue='Sex', data=train_ready, kind='box')

對連續變量,且取值個數較多情況(如Age, Fare)

不能用barplot,否則會由於bar過多而密集

用distplot,不允許數據中有空值,需要先填充空值
sns.distplot(a=train_ready['Age'])

用FacetGrid,允許數據中有空值
g = sns.FacetGrid(train_ready, col='Survived')
g.map(sns.distplot, 'Age')

柱狀圖疊加效果,計算總量及比率 (查找更簡潔方法)

sns.barplot(x=sex_survived.index, y=sex_survived['Survived']+sex_survived['Dead'], color='red')
sns.barplot(x=sex_survived.index, y=sex_survived['Survived'], color='green')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM