[청년취업사관학교 새싹]핀테커스 수업 6주차(10/5)

장민정·2023년 10월 5일
0

<수업 내용>

Iris 데이터 셋의 violin plot그리기

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

Iris = load_iris()

Iris_Data = pd.DataFrame(data=Iris['data'], columns= Iris['feature_names'])
Iris_target = pd.DataFrame(data=Iris['target'],columns = ['target'])
Iris_target.replace([0,1,2],Iris['target_names'],inplace=True)

total_data=pd.concat([Iris_Data, Iris_target], axis=1)

fig,ax=plt.subplots(figsize=(7,7))
df=total_data.groupby('target')['sepal width (cm)']
df1=df.get_group("setosa")
df2=df.get_group("versicolor")
df3=df.get_group("virginica")

xticks=np.arange(3)
violin=ax.violinplot([df1,df2,df3], showmeans=True, positions=xticks)
ax.set_xticks(xticks)
ax.set_xticklabels(["setosa","versicolor","virginica"])
ax.set_xlabel('Species', fontsize=15)
ax.set_ylabel('sepal length', fontsize=15)

fig,ax=plt.subplots(figsize=(7,7))
df=total_data.groupby('target')['sepal length (cm)']
df1=df.get_group("setosa")
df2=df.get_group("versicolor")
df3=df.get_group("virginica")

xticks=np.arange(3)
violin=ax.violinplot([df1,df2,df3], showmeans=True, positions=xticks)
ax.set_xticks(xticks)
ax.set_xticklabels(["setosa","versicolor","virginica"])
ax.set_xlabel('Species', fontsize=15)
ax.set_ylabel('sepal length', fontsize=15)

fig,ax=plt.subplots(figsize=(7,7))
df=total_data.groupby('target')['petal width (cm)']
df1=df.get_group("setosa")
df2=df.get_group("versicolor")
df3=df.get_group("virginica")

xticks=np.arange(3)
violin=ax.violinplot([df1,df2,df3], showmeans=True, positions=xticks)
ax.set_xticks(xticks)
ax.set_xticklabels(["setosa","versicolor","virginica"])
ax.set_xlabel('Species', fontsize=15)
ax.set_ylabel('sepal length', fontsize=15)

fig,ax=plt.subplots(figsize=(7,7))
df=total_data.groupby('target')['petal length (cm)']
df1=df.get_group("setosa")
df2=df.get_group("versicolor")
df3=df.get_group("virginica")

xticks=np.arange(3)
violin=ax.violinplot([df1,df2,df3], showmeans=True, positions=xticks)
ax.set_xticks(xticks)
ax.set_xticklabels(["setosa","versicolor","virginica"])
ax.set_xlabel('Species', fontsize=15)
ax.set_ylabel('sepal length', fontsize=15

한 figure 에서 2X2 Axes로 그리기

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

Iris = load_iris()

Iris_Data = pd.DataFrame(data=Iris['data'], columns=Iris['feature_names'])
Iris_target = pd.DataFrame(data=Iris['target'], columns=['target'])
Iris_target.replace([0, 1, 2], Iris['target_names'], inplace=True)

total_data = pd.concat([Iris_Data, Iris_target], axis=1)

sepal_length = total_data.groupby('target')['sepal length (cm)']
sepal_width = total_data.groupby('target')['sepal width (cm)']
petal_length = total_data.groupby('target')['petal length (cm)']
petal_width = total_data.groupby('target')['petal width (cm)']

fig, axes = plt.subplots(2, 2, figsize=(10, 10))
subplots = axes.flatten()

Iris_freature=[sepal_length,sepal_width,petal_length,petal_width]

for k in range(len(Iris_freature)):
      violin_data = [Iris_freature[k].get_group(name) for name in Iris['target_names']]
      subplots[k].violinplot(violin_data, showmeans=True)
      subplots[k].set_title(f'{Iris["feature_names"][k]} Violin Plot', fontsize=12)
      subplots[k].set_xticks([1,2,3])
      subplots[k].set_xticklabels(labels=Iris['target_names'])
      subplots[k].set_ylabel(f'{Iris["feature_names"][k]}', fontsize=10)
      subplots[k].tick_params(labelsize=8)

0개의 댓글