内存不够时,替代sns.countplot()的实现方法

内存不够时,替代sns.countplot()的实现方法

最近做EDA的的时候会存在这样一种情况,数据量过大时,直接使用sns.countplot()内存很容易直接被榨干掉。

暂时没想清为什么会直接占用那么多内存,应该去StackOverflow提个Q的。

不过想了下又查了下StackOverflow,应该有用df.value_counts() 和 matplotlib来实现sns.countplot()的实现方法,如此,What is Matplotlib's alternative for countplot from seaborn?

引用自StackOverflow,ImportanceOfBeingErnest

Say you have this data:

1
2
3
4
5
6
import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Sex" : np.random.choice(["male", "female"], size=1310, p=[.65, .35]),
"other" : np.random.randint(0,80, size=1310)})

You can plot a countplot in seaborn as

1
2
3
import seaborn as sns
sns.countplot(x="Sex", data=df)
plt.show()

Or you can create a bar plot in pandas

1
2
df["Sex"].value_counts().plot.bar()
plt.show()

Or you can create a bar plot in matplotlib

1
2
3
counts = df["Sex"].value_counts()
plt.bar(counts.index, counts.values)
plt.show()

Reference