๋ฌธ1) iris.csv ํ์ผ์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ์์ค.
<์กฐ๊ฑด1> iris.csv ํ์ผ์ iris ๋ณ์๋ช ์ผ๋ก ๊ฐ์ ธ์จ ํ ํ์ผ ์ ๋ณด ๋ณด๊ธฐ
<์กฐ๊ฑด2> 1๋ฒ ์นผ๋ผ๊ณผ 3๋ฒ ์นผ๋ผ์ ๋์์ผ๋ก ์ฐ์ ๋ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
<์กฐ๊ฑด3> x์ถ : 1๋ฒ ์นผ๋ผ, y์ถ : 3๋ฒ ์นผ๋ผ, ์์ : 5๋ฒ ์นผ๋ผ์ ์ด์ฉํ์ฌ ์ฐ์ ๋ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
5๋ฒ ์นผ๋ผ : ๋ ์ด๋ธ ์ธ์ฝ๋ฉ(label encoding) ์ ์ฉ
import pandas as pd
import matplotlib.pyplot as plt
<์กฐ๊ฑด1> iris.csv ํ์ผ์ iris ๋ณ์๋ช
์ผ๋ก ๊ฐ์ ธ์จ ํ ํ์ผ ์ ๋ณด ๋ณด๊ธฐ
iris = pd.read_csv(r'C:\ITWILL\4_Python-2\data\iris.csv')
print(iris.info())
0 Sepal.Length 150 non-null float64 - x์ถ
1 Sepal.Width 150 non-null float64
2 Petal.Length 150 non-null float64 - y์ถ
3 Petal.Width 150 non-null float64
4 Species 150 non-null object - color ์๋ฃ : ๋ฌธ์์ด -> ์ซ์
<์กฐ๊ฑด2> 1๋ฒ ์นผ๋ผ(x)๊ณผ 3๋ฒ ์นผ๋ผ(y)์ ๋์์ผ๋ก ์ฐ์ ๋ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
plt.scatter(iris['Sepal.Length'], iris['Petal.Length'], c='r')
plt.xlabel('Sepal.Length')
plt.ylabel('Petal.Length')
plt.title('iris ๋ฐ์ดํฐ ์
์ฐ์ ๋')
plt.show()
color ์๋ฃ : ๋ฌธ์์ด -> ์ซ์
iris['Species'].value_counts()
virginica 50 - 2
versicolor 50 - 1
setosa 50 - 0
<์กฐ๊ฑด3> 1๋ฒ ์นผ๋ผ๊ณผ 3๋ฒ ์นผ๋ผ์ ๋์์ผ๋ก ์ฐ์ ๋ ๊ทธ๋ํ ๊ทธ๋ฆฐ ํ 5๋ฒ ์นผ๋ผ์ผ๋ก ์์(c) ์ ์ฉ
1) ๋ฌธ์ -> ์ซ์ ๋ณํ
from sklearn.preprocessing import LabelEncoder #class
import matplotlib.pyplot as plt
encoder = LabelEncoder() #object
encoder.fit(iris.Species) #dataset ์ ์ฉ
data๋ณํ : label -> 10์ง์ ๋ณํ
labels = encoder.transform(iris.Species)
2) ์ฐ์ ๋ ์๊ฐํ
plt.scatter(iris['Sepal.Length'], iris['Petal.Length'], c = labels)
plt.xlabel('Sepal.Length')
plt.ylabel('Petal.Length')
plt.title('iris ๋ฐ์ดํฐ ์
์ฐ์ ๋')
plt.show()
๋ฌธ2) dataset.csv ํ์ผ์ ์ด์ฉํ์ฌ ๊ต์ฐจํ ์ด๋ธ๊ณผ ๋์ ๋ง๋์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ์์ค.
<์กฐ๊ฑด1> ์ฑ๋ณ(gender)๊ณผ ๋ง์กฑ๋(survey) ์นผ๋ผ์ผ๋ก ๊ต์ฐจํ ์ด๋ธ ์์ฑ
<์กฐ๊ฑด2> ๊ต์ฐจํ ์ด๋ธ ๊ฒฐ๊ณผ๋ฅผ ๋์์ผ๋ก ๋ง์กฑ๋ 1,3,5๋ง ์ ํํ์ฌ ๋ฐ์ดํฐํ๋ ์ ์์ฑ
<์กฐ๊ฑด3> ์์ฑ๋ ๋ฐ์ดํฐํ๋ ์ ๋์ ์นผ๋ผ๋ช ์์ : ['seoul','incheon','busan']
<์กฐ๊ฑด4> ์์ฑ๋ ๋ฐ์ดํฐํ๋ ์ ๋์ index ์์ : ['male', 'female']
<์กฐ๊ฑด5> ์์ฑ๋ ๋ฐ์ดํฐํ๋ ์ ๋์ ๋์ ๊ฐ๋ก๋ง๋์ฐจํธ ๊ทธ๋ฆฌ๊ธฐ
import pandas as pd
ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
dataset = pd.read_csv('C:/ITWILL/4_Python-2/data/dataset.csv')
print(dataset.head())
์ฑ๋ณ(gender)๊ณผ ๋ง์กฑ๋(survey) ๊ต์ฐจํ
์ด๋ธ
dataset_table = pd.crosstab(dataset['gender'], dataset['survey']) # ํ,์ด
print(dataset_table)
๋ง์กฑ๋ 1,3,5 ์ ํ
dataset_result = dataset_table.loc[:, [1,3,5]]
column ๋ณ๊ฒฝ
dataset_result.columns = ['seoul','incheon','busan']
index ๋ณ๊ฒฝ
dataset_result.index = ['male', 'female']
print(dataset_result)
dataset_result.plot(kind = 'barh', stacked=True,
title = 'gender vs survey plotting')
๋ฌธ3) seaborn์ titanic ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ๋จ๊ณ๋ณ๋ก ์๊ฐํํ์์ค.
<๋จ๊ณ1> 'survived','pclass', 'age','fare' ์นผ๋ผ์ผ๋ก ์๋ธ์ ๋ง๋ค๊ธฐ
<๋จ๊ณ2> 'survived' ์นผ๋ผ์ ์ง๋จ๋ณ์๋ก ํ์ฌ 'pclass', 'age','fare' ์นผ๋ผ ๊ฐ์ ์ฐ์ ๋ํ๋ ฌ ์๊ฐํ
<๋จ๊ณ3> ์ฐ์ ๋ํ๋ ฌ์ ์๊ฐํ ๊ฒฐ๊ณผ ํด์คํ๊ธฐ
๋ฌธ4) seaborn์ tips ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ๋จ๊ณ๋ณ๋ก ์๊ฐํํ์์ค.
<๋จ๊ณ1> 'total_bill','tip','sex','size' ์นผ๋ผ์ผ๋ก ์๋ธ์ ๋ง๋ค๊ธฐ
<๋จ๊ณ2> ์ฑ๋ณ(sex) ์นผ๋ผ์ ์ง๋จ๋ณ์๋ก ํ์ฌ total_bill, tip, size ์นผ๋ผ ๊ฐ์ ์ฐ์ ๋ํ๋ ฌ ์๊ฐํ
<๋จ๊ณ3> ์ฐ์ ๋ํ๋ ฌ์ ์๊ฐํ ๊ฒฐ๊ณผ ํด์คํ๊ธฐ
import matplotlib.pyplot as plt
import seaborn as sn
๋ฌธ3) seaborn์ titanic ๋ฐ์ดํฐ์
์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ๋จ๊ณ๋ณ๋ก ์๊ฐํํ์์ค.
titanic = sn.load_dataset('titanic')
print(titanic.info())
<๋จ๊ณ1> 'survived','pclass', 'age','fare' ์นผ๋ผ์ผ๋ก ์๋ธ์
๋ง๋ค๊ธฐ
titanic_df = titanic[['survived','pclass', 'age','fare']]
print(titanic_df.info())
#sn.pairplot(data=DataFrame, hue='์ง๋จ๋ณ์', kind='scatter')
<๋จ๊ณ2> 'survived' ์นผ๋ผ์ ์ง๋จ๋ณ์๋ก ํ์ฌ 'pclass', 'age','fare' ์นผ๋ผ ๊ฐ์ ์ฐ์ ๋ํ๋ ฌ ์๊ฐํ
sn.pairplot(data=titanic_df, hue='survived')
plt.show()
<๋จ๊ณ3> ์ฐ์ ๋ํ๋ ฌ์ ์๊ฐํ ๊ฒฐ๊ณผ ํด์คํ๊ธฐ
pclass : 3๋ฑ์ ์ผ์๋ก ์ฌ๋ง๋น์จ ๋งค์ฐ ๋์
pclass vs fare : 1๋ฑ์ ์ผ์๋ก ๊ณ ์๊ธ
age : 25~50์ธ ์ฌ์ด์์ ๊ฐ์ฅ ๋์ ๋น๋, ์ฌ๋ง๊ณผ ์์กด ๋น์จ ๋น์ท
age vs fare : ๋์ฒด์ ์ผ๋ก ๋์ด๊ฐ ๋ง๊ณ , ์๊ธ์ด ๋ฎ์ ๊ฒฝ์ฐ ์ฌ๋ง ๋น์จ ๋์
fare : ๋น์ฉ์ด ์ ๋ ดํ ๊ฒฝ์ฐ๊ฐ ์๋์ ์ผ๋ก ๋ง์ ๋ถํฌ
fare vs age : ๋์ฒด์ ์ผ๋ก ์๊ธ์ด ๋ฎ๊ณ , ๋์ด๊ฐ 50๋ ์ด์์ธ ๊ฒฝ์ฐ ์ฌ๋ง ๋น์จ ๋์
survived vs age
์ฐ๋ น๋ ์์กด๋น์จ : 20~40
titanic[titanic['survived'] == 1].age.plot(kind = 'hist', color = 'blue')
์ฐ๋ น๋ ์ฌ๋ง๋น์จ : 20~40
titanic[titanic['survived'] == 0].age.plot(kind = 'hist', color = 'green')
๋ฌธ4) seaborn์ tips ๋ฐ์ดํฐ์
์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ๋จ๊ณ๋ณ๋ก ์๊ฐํํ์์ค.
tips = sn.load_dataset('tips')
print(tips.info())
<๋จ๊ณ1> 'total_bill','tip','sex','size' ์นผ๋ผ์ผ๋ก ์๋ธ์
๋ง๋ค๊ธฐ
tips_df = tips[['total_bill','tip','sex','size']]
<๋จ๊ณ2> ์ฑ๋ณ(sex) ์นผ๋ผ์ ์ง๋จ๋ณ์๋ก ํ์ฌ total_bill, tip, size ์นผ๋ผ ๊ฐ์ ์ฐ์ ๋ํ๋ ฌ ์๊ฐํ
sn.pairplot(data=tips_df, hue='sex')
plt.show()
<๋จ๊ณ3> ์ฐ์ ๋ํ๋ ฌ์ ์๊ฐํ ๊ฒฐ๊ณผ ํด์คํ๊ธฐ
total_bill : ์ด๊ธ์ก 15~20 ์ฌ์ด๊ฐ ๊ฐ์ฅ ๋์ ๋น๋, ๊ธ์ก์ด ํด ์๋ก ๋จ์ ์ง๋ถ
total_bill vs tip : ๋์ฒด์ ์ผ๋ก ๋น๋ก๊ด๊ณ, ์ด๊ธ์ก๊ณผ ํ์ด ๋ง์ ๊ฒฝ์ฐ ๋จ์ ์ง๋ถ
tip : ํ์ 1~5 ์ฌ์ด๊ฐ ๊ฐ์ฅ ๋์ ๋น๋, ํ ๊ธ์ก์ด ํด ์๋ก ๋จ์ ์ง๋ถ
total_bill vs size : ํ์ฌ๊ท๋ชจ๊ฐ ์์ ๊ฒฝ์ฐ ์ฌ์ฑ ์ง๋ถ
size : ํ์ฌ๊ท๋ชจ 2๊ฐ ๊ฐ์ฅ ๋์ ๋น๋, ํนํ 4์ผ๋ ๋จ์ ์ง๋ถ
size vs total_bill : ๋์ฒด์ ์ผ๋ก ๋น๋ก๊ด๊ณ, ๊ท๋ชจ๊ฐ ํฐ ๊ฒฝ์ฐ ์ด๊ธ์ก์ด ๋ง์
'๊ฐ์ธ๊ณต๋ถ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
59. NIPA AI์จ๋ผ์ธ ๊ต์ก AI ์ค๋ฌด ๊ธฐ๋ณธ ๊ณผ์ (2-1) (0) | 2021.11.23 |
---|---|
58. NIPA AI์จ๋ผ์ธ ๊ต์ก AI ์ค๋ฌด ๊ธฐ๋ณธ ๊ณผ์ (1) (0) | 2021.11.22 |
56. Python Pandas ์ฐ์ต๋ฌธ์ (0) | 2021.11.20 |
55. Python DB์ฐ๋, CRUD, table ์ฐ์ต๋ฌธ์ (0) | 2021.11.18 |
50. Python ํด๋์ค, ๋ฐ์ดํฐ ์ ์ถ๋ ฅ ์ฐ์ต๋ฌธ์ (0) | 2021.11.11 |