import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
1. imports
2. 시각화
A. Histogrm
-
Histogrm (bin) : 구간의 넓이를 조정
= [68, 81, 64, 56, 78, 74, 61, 77, 66, 68, 59, 71,
weight 80, 59, 67, 81, 69, 73, 69, 74, 70, 65]
plt.hist(weight)
plt.show()
='bins=10')
plt.hist(weight, label=30, label='bins=30')
plt.hist(weight, bins
plt.legend() plt.show()
=True, label='cumulative=True')
plt.hist(weight, cumulative=False, label='cumulative=False')
plt.hist(weight, cumulative='upper left')
plt.legend(loc plt.show()
-
Histogrm (type) : 히스토그램의 유형
= [52, 67, 84, 66, 58, 78, 71, 57, 76, 62, 51, 79,
weight2 69, 64, 76, 57, 63, 53, 79, 64, 50, 61]
='bar')
plt.hist((weight), histtype'histtype - bar')
plt.title(
plt.figure()
='barstacked')
plt.hist((weight), histtype'histtype - barstacked')
plt.title(
plt.figure()
='step')
plt.hist((weight), histtype'histtype - step')
plt.title(
plt.figure()
='stepfilled')
plt.hist((weight, weight2), histtype'histtype - stepfilled')
plt.title( plt.show()
-
Histogrm (density) : 밀도함수
import seaborn as sns
import numpy as np
= np.random.normal(20, 1, 1000)
aa = np.random.normal(25, 3, 500)
bb = np.random.normal(10, 3, 500)
cc = np.concatenate([aa,bb,cc])
weight
=True,
sns.displot(weight, kde=10, color = 'darkblue') bins
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
B. Boxplot
-
Boxplot (Categorical) : 기본유형
= sns.load_dataset("tips")
tips =tips, x="day", y="total_bill") sns.catplot(data
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1075: FutureWarning: When grouping with a length-1 list-like, you will need to pass a length-1 tuple to get_group in a future version of pandas. Pass `(name,)` instead of `name` to silence this warning.
data_subset = grouped_data.get_group(pd_key)
-
Boxplot (Categorical) : 변동과 점 구분
=tips, x="day", y="total_bill", hue="sex", kind="swarm") sns.catplot(data
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/_oldcore.py:1075: FutureWarning: When grouping with a length-1 list-like, you will need to pass a length-1 tuple to get_group in a future version of pandas. Pass `(name,)` instead of `name` to silence this warning.
data_subset = grouped_data.get_group(pd_key)
-
boxplot (Categorical) : 구분자로 분포의 차이를 확인
=tips, x="day", y="total_bill", kind="box") sns.catplot(data
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
=tips, x="day", y="total_bill", hue="smoker", kind="box") sns.catplot(data
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
-
Boxplot (바이올린) : 분포의 형태를 같이 봄
sns.catplot(=tips, x="total_bill", y="day", hue="sex", kind="violin",
data
)
sns.catplot(=tips, x="total_bill", y="day", hue="sex",
data="violin", bw_adjust=.5, cut=0,
kind
)
sns.catplot(=tips, x="day", y="total_bill", hue="sex",
data="violin", split=True,
kind )
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
C. Barplot
-
Barplot : 단순유형
= sns.load_dataset("titanic")
titanic =titanic, x="deck", kind="count") sns.catplot(data
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
-
Barplot : 비교유형
=titanic, x="sex", y="survived", hue="class", kind="bar")
sns.catplot(data=titanic, x="age", y="deck", errorbar=("pi", 95), kind="bar") sns.catplot(data
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/seaborn/categorical.py:641: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
from statsmodels.graphics.mosaicplot import mosaic
import matplotlib.pyplot as plt
import seaborn as sns
'figure.figsize'] = [12, 8]
plt.rcParams[
import pandas as pd
= pd.read_csv("https://waf.cs.illinois.edu/discovery/berkeley.csv")
df
df= df[['Major', 'Gender', 'Admission']]
df
'Gender'), ['Gender', 'Admission'],
mosaic(df.sort_values(='Berkeley Admission')
title
plt.show()
'Gender'),['Major', 'Admission','Gender'],
mosaic(df.sort_values(='Berkeley Admission')
title plt.show()
D. Scatter Plot
0)
np.random.seed(= [0, 0]
mean = [[1, 0.8], [0.8, 1]]
cov = np.random.multivariate_normal(mean, cov, 100)
X = pd.DataFrame(X, columns=['X1', 'X2']) df
='X1', y='X2', data=df)
sns.scatterplot(x'산점도 (Seaborn)')
plt.title(True)
plt.grid( plt.show()
= df['X1'].mean()
mean_x = df['X2'].mean()
mean_y
=(8, 6))
plt.figure(figsize'X1'], df['X2'])
plt.scatter(df[min(df['X2']), max(df['X2'])], 'r--', label=f'평균 X1: {mean_x:.2f}')
plt.plot([mean_x, mean_x], [min(df['X1']), max(df['X1'])], [mean_y, mean_y], 'r--', label=f'평균 X2: {mean_y:.2f}')
plt.plot(['X1')
plt.xlabel('X2')
plt.ylabel('산점도 및 평균 수준')
plt.title(
plt.legend()True)
plt.grid( plt.show()
-
구글 AI 지수 시계열 분석: 주요 파장의 값을 뽑아내고 상관관계를 파악
= pd.read_csv('TS1.csv')
ts = ts.to_numpy()
ts = ts.shape[0]
ll = np.array(ts[range(2,ll),1], dtype='float')
ts
plt.plot(ts) plt.show()
from scipy import signal
= signal.stft(ts, fs=int(ll-2), nperseg=100)
f, t, Zxx print(Zxx.shape)
plt.plot(Zxx)
plt.show()
abs(Zxx), vmin=0, vmax=50)
plt.pcolormesh(t, f, np. plt.show()
(17, 3)
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/scipy/signal/_spectral_py.py:2017: UserWarning: nperseg = 100 is greater than input length = 32, using nperseg = 32
warnings.warn('nperseg = {0:d} is greater than input length '
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/matplotlib/cbook/__init__.py:1369: ComplexWarning: Casting complex values to real discards the imaginary part
return np.asarray(x, float)
= pd.DataFrame(Zxx).corr()
covariance_matrix =(10, 8))
plt.figure(figsize=True, cmap='coolwarm', fmt=".2f")
sns.heatmap(covariance_matrix, annot'Covariance Heatmap')
plt.title( plt.show()
/root/anaconda3/envs/pypy/lib/python3.10/site-packages/pandas/core/internals/managers.py:1688: ComplexWarning: Casting complex values to real discards the imaginary part
arr = np.array(blk.values, dtype=dtype, copy=copy)