KDE shows higher count as compared acutal pandas dataframe

Viewed 38

I am getting started with Titanic Spaceship dataset. (https://www.kaggle.com/competitions/spaceship-titanic)

I am trying to understand the effect of the foodcourt feature on the transported result. On plotting a violin plot, I see that the number of passengers who did not use the foodcourt (0 expense) and did not get transported is higher as compared to the number of passengers who did not use the foodcourt (0 expense) and get transported.

However, when I use the following code, I see that the passengers who did not use the foodcourt and did not get transported are lesser as compared to the number of passengers who did not use the foodcourt and got transported.

temp = df_train.loc[df_train['FoodCourt'] == 0]
temp_0 = temp.loc[temp['Transported'] == False][enter image description here][1]
temp_1 = temp.loc[temp['Transported'] == True]

print(len(temp_0))
print(len(temp_1))
1 Answers

I have tried the same code that you posted, the results I got from the violin plot (and swarm plot) are identical to the data. Slicing the dataframe to the passengers that got transported and not transported:

temp_0 = temp[temp["Transported"] == True]
temp_1 = temp[temp["Transported"] == False]

The lengths of these dataframes are:

temp_0.shape[0] #3224
temp_1.shape[0] #2232

The plot is: plot

If you want the width of the violin to scale up using the number of observations in each group, add scale="count" argument as the following:

plt.figure(figsize=(16, 8))
plot = sns.violinplot(y="FoodCourt", x="Transported", data=data, scale="count")
plt.show()

The result: plot2

Just note that, it is better to create a countplot, to see how many observation we have in each subgroup, like this:

plt.figure(figsize=(16, 8))
plot = sns.countplot(x=temp["Transported"])
plt.bar_label(plot.containers[0])
plt.show()

The result: countplot

Related