elena1234

Seaborn exercise in Python

May 1st, 2022 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import seaborn as sns
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. df = pd.read_csv('C:/Users/eli/Desktop/application_record.csv')
  8.  
  9. # Recreate the Scater Plot shown below
  10. new_df = df.copy()
  11. new_df = new_df[new_df['DAYS_EMPLOYED'] < 0]
  12. new_df['DAYS_EMPLOYED'] = -1 * new_df['DAYS_EMPLOYED']
  13. new_df['DAYS_BIRTH'] = -1 * new_df['DAYS_BIRTH']
  14.  
  15. plt.figure(figsize=(10, 8), dpi=200)
  16. plot1 = sns.scatterplot(x='DAYS_BIRTH', y='DAYS_EMPLOYED',
  17.                         data=new_df, linewidth=0, alpha=0.01)
  18. print(plot1)
  19.  
  20. # Recreate the Distribution Plot shown below
  21. new_df['Age in Years'] = new_df['DAYS_BIRTH'] / 365
  22. sns.set(style='darkgrid')
  23. plt.figure(figsize=(10, 8), dpi=200)
  24. distribution = sns.histplot(data=new_df, x='Age in Years',
  25.                             color='red', linewidth=2, edgecolor='black', kde=True)
  26. print(distribution)
  27.  
  28. # Recreate the Categorical Plot shown below
  29. plt.figure(figsize=(12, 5))
  30. bottom_half_income = df.nsmallest(
  31.     n=int(0.5*len(df)), columns='AMT_INCOME_TOTAL')
  32. box_plot = sns.boxplot(x='NAME_FAMILY_STATUS', y='AMT_INCOME_TOTAL',
  33.                        data=bottom_half_income, hue='FLAG_OWN_REALTY', linewidth=3)
  34. plt.legend(bbox_to_anchor=(1.05, 1), loc=2,
  35.            borderaxespad=0., title='FLAG_OWN_REALTY')
  36. plt.title('Income Totals per Family Status for Bottom Half of Earners')
  37.  
  38. # Rereate the Heatmap shown below
  39. df = df.drop('FLAG_MOBIL', axis=1)
  40. sns.heatmap(df.corr(), annot=False, fmt='.1g', cmap='coolwarm')
  41.  
Add Comment
Please, Sign In to add comment