
라이브러리 불러오기
import pandas as pd
import numpy as np
import seaborn as sns
import matplotib.pyplot as plt
pd.read_csv("data/diabetes.csv")
df.describe() -> 수치 데이터 요약값
[평균값 > 중위값 ==> 최대값이 높기 때문]
혈압,피부두께, BMI는 0이 될 수 없는데 최솟값 모두 0 임 ==> 결측치일 가능성 높음
df_null = df[cols].replace(0,np.nan)
df_null = df_null.isnull()
#나온 결측치 각 갯수
df_null.sum()
# 결측치 수치 시각화
df_null.sum().plot.barh()
#히트맵 그리기
plt.figure(figsize = (15,4))
sns.heatmap(df_null, cmap="Greys_r")

모든 행의 결과 값 나타냄
df["Outcome"]
(1 = 발병 , 0 = 발병x)
정답값 빈도수
df["Outcome"].value_counts()
정답값 비율
df["OUtcome"].value_counts(normalize=Treue)
임신 횟수에 따른 발병 비율 그룹화
df.groupby(["pregnancies"])["Outcome"].mean()
-> 임신 횟수가 14번 이상이면 발병률이 100% (데이터 빈도 수가 적어서?)
df_po = df.groupby(["pregnancies"])["Outcome"].agg(["mean","count"]).reset_index()
df_po.plot()
df_po["mean"].plot().bar(rot = 0)
sns.countplot(data = df, x = "Pregnancies")
sns.countplot(datda=df, x="Pregnancies", hue="Outcome")
--> 조건 숫자의 범위가 많아서 오버피팅 우려
! 범주로 묶어주기 (임신 횟수 적은 그룹 / 많은 그룹)
df["Pregnancies_high"] = df["Pregnancies"] >6
df[["Pregnancies","Pregnancies_high"]].head()
y축의 값은 평균값
sns.barplot(data=df, x= "Outcome", y="BMI")
sns.barplot(data=df, x="Pregnancies", y="Outcome")
sns.barplot(data=df, x="Pregnancies", y="Insulin",hue="Outcome")
--> 당뇨병 발병한 사람이 인슐린 수치 더 높음, 신뢰구간 차이 많이남
boxplot 똑같이 이용 가능
(주저 앉은 것은 0의 값이 많기 때문 0보다 큰 값으로 설정하기)
sns.boxplot(data=df[df["Insulin"] > 0], x="Pregnancies", y="Insulin", hue="Outcome")
violinplot 똑같이 이용 가능
(상자도표에서 상자 안 분포를 확인하기 힘든 점을 보완함)
plt.figure(figsize=(15, 4))
sns.violinplot(data=df[df["Insulin"] > 0], x="Pregnancies", y="Insulin", hue="Outcome", split=True)
swarmplot 똑같이 이용 가능
(산포도 그리는 데 적합)
plt.figure(figsize=(15, 4))
sns.violinplot(data=df[df["Insulin"] > 0], x="Pregnancies", y="Insulin", hue="Outcome")
sns.distplot(df_0["Pregnancies"])
sns.distplot(df_1["Pregnancies"])
-->막대는 발생 빈도, 선그래프는 빈도의 밀도 정도를 나타냄
hist = Fales 사용하면 막대그래프 제거
rug = True 하면 밑에 러그 생김
lable = 0,1 옆에 선의 뜻(0/1) 표시
plt.subplots(nrows=3, ncols=3, figsize(15,15))
sns.distplot(df["Outcome"], ax=axes[0][0])
for i, col_name in enumerate(cols):
row = i // 3
col = i % 3
sns.distplot(df[col_name], ax=axes[row][col])
--> 빈도수를 y축 값으로 표현
#4행 2열로 만듦
fig, axes = plt.subplots(nrows=4, ncols=2, figsize=(15, 15))
# df_0 = 값이 0인 것 df_1 = 값이 1인 것
for i, col_name in enumerate(cols[:-1]):
row = i // 2
col = i % 2
sns.distplot(df_0[col_name], ax=axes[row][col])
sns.distplot(df_1[col_name], ax=axes[row][col])
fig, axes = plt.subplots(nrows=4, ncols=2, figsize=(15, 15))
for i, col_name in enumerate(cols[:-1]):
row = i // 2
col = i % 2
sns.violinplot(data=df, , x="Outcome", y=col_name, ax=axes[row][col])
--> 0의 값에 몰려있음 = 결측치
가설 : 포도당 수치와 인슐린 수치는 당뇨병과 관련 있을 것이다!
regplot으로 그리면 insulindl 0 인 값이 있음
sns.regplot(data=df, x="Glucose", y="Insulin")
regplot은 hue 옵션이 없어서 색상 지정을 못함
색상 다르게 하기 위해 lmplot 이용 + 인슐린이 0 이상일 때
sns.lmplot(data=df[df["Insulin"] > 0], x="Glucose", y="Insulin", hue="Outcome")