Python

Seaborn 실습(2)

learning-log22 2025. 5. 6. 14:42

Colab의 Seaborn library 예제

# Colab의 Seaborn library 예제
 
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set_theme()
%matplotlib inline
plt.rc('font', family='Malgun Gothic')
 
# Generate some random data
num_points = 20
x = np.arange(num_points)

x
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

a=np.random.randn(num_points)
a
array([ 0.76737723,  0.74808631, -1.40908439,  0.50140385,  0.57358429,
       -1.29864835, -0.38070425,  1.4689143 , -0.12219668,  0.11714305,
       -0.16577194, -0.11912036, -0.44208883, -0.48615368, -0.93481228,
       -0.18158849, -0.34943833,  0.99535592,  0.12970962,  0.5009274 ])
 
x = 5 + np.arange(num_points) + a
 
x
array([ 5.76737723,  6.74808631,  5.59091561,  8.50140385,  9.57358429,
        8.70135165, 10.61929575, 13.4689143 , 12.87780332, 14.11714305,
       14.83422806, 15.88087964, 16.55791117, 17.51384632, 18.06518772,
       19.81841151, 20.65056167, 22.99535592, 23.12970962, 24.5009274 ])
 
y = 10 + np.arange(num_points) + 5 * np.random.randn(num_points)
y
array([15.59634693,  3.2145261 , 18.44429626, 17.0767762 ,  9.17262966,
        9.89444909, 19.02088585, 10.02313961, 20.42521897, 24.5479937 ,
       23.07477989, 22.83860457, 28.70266049, 21.64165591, 25.02886601,
       20.02892914, 32.29185043, 25.53626081, 24.866488  , 28.00425957])
 
plt.subplots(figsize=(20,10))
sns.regplot(x,y)
# Seaborn library 예제

# Import seaborn
import seaborn as sns

# Apply the default theme
sns.set_theme()

# Load an example dataset
tips = sns.load_dataset("tips")

# Create a visualization
sns.relplot(
    data=tips,
    x="total_bill", y="tip", col="time",
    hue="smoker", style="smoker", size="size",
)
 
tips

tips.info()
# Pandas를 이용한 Seaborn library 학습
 
import pandas as pd

from google.colab import drivedrive.mount('/content/drive')

Mounted at /content/drive
df=pd.read_csv('/content/drive/Shareddrives/VisualPython/DATA/cosmetics_.csv', encoding='utf-8')
df


df.describe()

df.info()
 <class 'pandas.core.frame.DataFrame'>
RangeIndex: 247 entries, 0 to 246
Data columns (total 17 columns):
 #   Column      Non-Null Count  Dtype
---  ------      --------------  -----
 0   gender      247 non-null    int64
 1   marriage    247 non-null    int64
 2   edu         247 non-null    int64
 3   job         247 non-null    int64
 4   mincome     247 non-null    int64
 5   aware       247 non-null    int64
 6   count       247 non-null    int64
 7   amount      247 non-null    int64
 8   decision    247 non-null    int64
 9   propensity  247 non-null    int64
 10  skin        247 non-null    int64
 11  promo       247 non-null    int64
 12  location    247 non-null    int64
 13  satisf_b    247 non-null    int64
 14  satisf_i    247 non-null    int64
 15  satisf_al   247 non-null    int64
 16  repurchase  247 non-null    int64
dtypes: int64(17)
memory usage: 32.9 KB
 

import seaborn as sns
sns.distplot(df["job"], hist=True, kde=True, rug=True, color='green', vertical=False, axlabel="JOB", label="SMUW")
 
 
df["amount"].describe()
count    2.470000e+02
mean     1.539393e+05
std      3.980750e+05
min      3.000000e+03
25%      3.000000e+04
50%      5.200000e+04
75%      1.000000e+05
max      5.000000e+06
Name: amount, dtype: float64
각각의 column에 대해서 추가로 시각화하여 특성을 파악해보세요
 
sns.jointplot(x="amount", y="count", data=df, kind="hist")
 

sns.jointplot(x="amount", y="count", data=df,  kind="hist", space=1, color="b").plot_joint(sns.kdeplot, zorder=1, n_levels=6)
#두 가지 이상의 column에 대해 추가로 jointplot을 적용하여 특성관계를 파악해보세요.
 
plt.subplots(figsize=(10,5))

sns.set()

sns.set_style("whitegrid")
 
sns.set_style("ticks", {"xtick.major.size":8,"ytick.major.size":8})
 
sns.axes_style("whitegrid")
 {'axes.axisbelow': True,
 'axes.edgecolor': '.8',
 'axes.facecolor': 'white',
 'axes.grid': True,
 'axes.labelcolor': '.15',
 'axes.spines.bottom': True,
 'axes.spines.left': True,
 'axes.spines.right': True,
 'axes.spines.top': True,
 'figure.facecolor': 'white',
 'font.family': ['sans-serif'],
 'font.sans-serif': ['Arial',
  'DejaVu Sans',
  'Liberation Sans',
  'Bitstream Vera Sans',
  'sans-serif'],
 'grid.color': '.8',
 'grid.linestyle': '-',
 'image.cmap': 'rocket',
 'lines.solid_capstyle': 'round',
 'patch.edgecolor': 'w',
 'patch.force_edgecolor': True,
 'text.color': '.15',
 'xtick.bottom': False,
 'xtick.color': '.15',
 'xtick.direction': 'out',
 'xtick.top': False,
 'ytick.color': '.15',
 'ytick.direction': 'out',
 'ytick.left': False,
 'ytick.right': False}
 
sns.set_context("talk")

sns.set_context("notebook", font_scale=1, rc={"lines.linewidth":1})
sns.set_palette("husl",3)
sns.color_palette("husl")

flatui = ["#9b59b6","#3498db","#95a5a6","#e74c3c","#34495e","#2ecc71"]
sns.set_palette(flatui) 

sns.jointplot(x="amount", y="count", data=df,  kind="kde", space=0, color="g").plot_joint(sns.kdeplot, zorder=0, n_levels=6)
728x90
LIST

'Python' 카테고리의 다른 글

EDA - Titanic Dataset  (0) 2025.05.06
Sentiment Analysis  (0) 2025.05.06
Seaborn 라이브러리  (0) 2025.05.04
Python 기초 개념 정리  (0) 2025.05.01
Pandas 실습  (3) 2025.05.01