0. ์ฐธ๊ณ
(2) ์ํค๋ ์ค
1. Colab ์ฌ์ฉ ํ
(1) pandas์์ ์์ ํ์ผ ์ด๊ธฐ
titanic = pd.read_excel('ํ์ผ์ด๋ฆ.xlsx',engine='openpyxl')
(2) ํ์ผ ๊ฒฝ๋ก๋ก ์ด๊ธฐ
titanic = pd.read_table('ํ์ผ๊ฒฝ๋ก',sep=',')
2. ๋ฐ์ดํฐ ๋ถ์์ ํ์ํ ํ์ด์ฌ ๊ธฐ์ด
(1) ๋ณ์ : ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ์ปจํ ์ด๋
(2) ๋ฆฌ์คํธ : ์ธ๋ฑ์ค(์์, 0๋ถํฐ ์์)๊ฐ ์๋ ๋ฐ์ดํฐ๋ค์ ๋ชจ์์ง
(3) ๋์ ๋๋ฆฌ : ์ (key-value)์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฐ์ดํฐ๋ค์ ๋ชจ์์ง
#๋ณ์ ์ ์ธ
x=5, y=3, z="hello"
#๋ณ์ ํธ์ถ
print(x+y)
print(z)
#๋ฆฌ์คํธ ์ ์ธ w. ๋๊ดํธ []
a_list=[1, 2, 3]
b_list=[1, 2, 'hello', 4]
list_ex=[3, 4, [5, 6], 7]
#๋ฆฌ์คํธ ํธ์ถ
a_list[1] # 2 ์ถ๋ ฅ
b_list[2] # hello ์ถ๋ ฅ
list_ex[2] # [5, 6] ์ถ๋ ฅ
list_ex[2][0] # 5 ์ถ๋ ฅ
#๋์
๋๋ฆฌ ์ ์ธ w. ์ค๊ดํธ {}
student_age={'Jack':32, 'Mark':22, 'John':25}
dic_exercise = {'name':'bob','age':21,'height':180}
#๋์
๋๋ฆฌ ํธ์ถ
student_age{'John'} # 25 ์ถ๋ ฅ
dic_exercise['height'] # 180 ์ถ๋ ฅ
3. ๋ฐ์ดํฐ ๋ถ์์ ํ์ํ ์๊ฐํ ๊ธฐ์ด
(1) ๋ฐ์ดํฐ ๋ถ์ ๊ธฐ๋ณธ ์ธํ & ๋ถ์
- dropna() : ๊ฒฐ์ธก๊ฐ์ด ์๋ ํ ์ ๊ฑฐ
- head() : ํ ์ด๋ธ ์๋จ ์ผ๋ถ ์ถ๋ ฅ
- corr : ์๊ด๊ด๊ณ ๋ถ์
- ๋ฐ์ดํฐํ๋ ์.corr(method='A') : ํ ์ด๋ธ์์ A ๋ฐฉ๋ฒ์ผ๋ก ์๊ด๊ณ์ ๊ตฌํ๊ธฐ
import pandas as pd #pandas ์ฌ์ฉ ์ ์ธ
titanic = pd.read_table('/content/train.csv',sep=',') #titanic ํ
์ด๋ธ ๊ฐ์ ธ์ค๊ธฐ
titanic = titanic.dropna() #๊ฒฐ์ธก๊ฐ(null)์ด ์๋ ํ์ ์ ๊ฑฐ
titanic.head() #ํ
์ด๋ธ์ ์๋จ ์ผ๋ถ๋ฅผ ์ถ๋ ฅ
corr = titanic.corr(method='pearson') #ํผ์ด์จ ๋ฐฉ๋ฒ์ผ๋ก ์๊ด๊ณ์ ๊ตฌํ๊ธฐ
corr = corr[corr.Survived !=1] #์๊ด๊ณ์ Survived ์์๊ฐ 1(์ต๋)์ด ์๋ ์๋ง ๋ถ๋ฌ์ค๊ธฐ
corr #์กฐํ
(2) ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
- ๋ฐ์ดํฐํ๋ ์.drop([์ญ์ ํ ๋ ์ด๋ธ ๋ช ], axis='row/column') : ํ/์ด ์ญ์
- plot() : ๊ทธ๋ํ๋ก ๋ํ๋ด๊ธฐ
- plot.bar() : ๋ง๋๊ทธ๋ํ๋ก ๋ํ๋ด๊ธฐ
import matplotlib.pyplot as plt #matplotlib ์ฌ์ฉ ์ ์ธ
corr = corr.drop(['PassengerId'], axis ='rows') #Passenger Id ํ ์ ๊ฑฐ
corr['Survived'].plot.bar() #Survived ์ด ์ง์ ํ ๋ง๋๊ทธ๋ํ๋ก ์กฐํ
(3) ์ต์ข ์ฝ๋
import pandas as pd #pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ์ ์ธ
import matplotlib.pyplot as plt #matplotlib ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ์ ์ธ
titanic = pd.read_table('/train.csv',sep=',') #titanic ํ
์ด๋ธ ๊ฐ์ ธ์ค๊ธฐ
# 1.Null(๊ณต๋ฐฑ) ๋ฐ์ดํฐ ํ์
ํ๊ธฐ
print(titanic.isnull().sum())
# 2. ๊ณต๋ฐฑ ๋ฐ์ดํฐ ์ ๊ฑฐํ๊ธฐ
titanic = titanic.dropna()
#์๊ด๊ณ์ ๊ตฌํ๊ธฐ
corr=titanic.corr(method='pearson')
#survived 1์ธ ์์ ์ ์ธํ๊ธฐ
corr = corr[corr.Survived !=1]
#passengerId ์ด ์ญ์ ํ๊ธฐ
corr = corr.drop(['PassengerId'], axis ='rows')
#์์กด์จ ์๊ด๊ด๊ณ ๋ฐ ๊ทธ๋ํ ์์ฑํ๊ธฐ
corr['Survived'].plot.bar()
#x์ถ ๋ ์ด๋ธ 45๋ ํ์ ํ๊ธฐ
plt.xticks(rotation=45)
๋ถ์ ๊ฒฐ๊ณผ | |
์ฑ๋ณ | 1์ผ์๋ก(=์ฌ์ฑ) ์์กด ํ๋ฅ ์ด ๋๋ค |
์ข์ ๋ฑ๊ธ | ๋ฎ์์๋ก(1๋ฑ๊ธ์ ๊ฐ๊น์ธ์๋ก) ์์กด ํ๋ฅ ์ด ๋๋ค |
์๊ธ | ๋น์์๋ก ์์กด ํ๋ฅ ์ด ๋๋ค |
4. ๋ฐ์ดํฐ ๋ถ์์ ํ์ํ ์๊ฐํ ํ ๊ฑธ์ ๋
(1) ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
- numpy : ๋ฐ์ดํฐ ์ฐ์ฐ์ ๋์์ค๋ค.
- seaborn : matplotlib ์๊ฐํ๋ฅผ ๋์์ค๋ค.
(2) ์ต์ข ์ฝ๋
- hist(bins=๋ง๋๊ทธ๋ํ ํญ, figsize=(๊ฐ๋ก๊ธธ์ด, ์ธ๋ก๊ธธ์ด), grid=True/False) : ํ์คํ ๊ทธ๋จ ๋ง๋ค๊ธฐ
- cut(array, bins=[], label=[]) : ๋ฐ์ดํฐ ๊ตฌ๊ฐ ๋๋๊ธฐ
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
titanic = pd.read_table('/train.csv',sep=',') #titanic ํ
์ด๋ธ ๊ฐ์ ธ์ค๊ธฐ
titanic = titanic.dropna() #null๊ฐ ์ ๊ฑฐ
titanic.head()
titanic.describe() #๋ฐ์ดํฐ ํต๊ณ์น ์์ฝ
#์ฒซ ๋ฒ์งธ ๊ทธ๋ํ - ๋์ด๋ณ๋ก ํ์คํ ๊ทธ๋จ ๊ตฌํ๊ธฐ
titanic['Age'].hist(bins=40,figsize=(18,8),grid=True)
#๋์ด๋ณ ๊ตฌ๋ถ ๋ฐ ๊ฐ ๋์ด๋ณ ์์กด์จ ํ์ธ ํ๊ธฐ
titanic['Age_cat'] = pd.cut(titanic['Age'],bins=[0,3,7,15,30,60,100],include_lowest=True,labels=['baby','children','teenage','young','adult','old'])
#์ฐ๋ น๋๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ
titanic.groupby('Age_cat').mean()
#๊ทธ๋ํ ํฌ๊ธฐ ์ค์
plt.figure(figsize=(14,5))
# ๋ฐ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ (x์ถ = Age_cat, y์ถ = Survived)
sns.barplot(x='Age_cat',y='Survived',data=titanic)
# ๋ ๋ฒ์งธ ๊ทธ๋ํ ๋ํ๋ด๊ธฐ
plt.show()
'๐ Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ ๋ด์ฅํจ์ enumerate()์ for ๋ฐ๋ณต๋ฌธ (0) | 2024.01.10 |
---|---|
231228 THU ํ์ด์ฌ ํจ์ vs ๋ฉ์๋ (0) | 2023.12.28 |
ํ์ด์ฌ ๋ฐ๋ณต๋ฌธ - for, while (0) | 2023.12.28 |
ํ์ด์ฌ ๋ง๋ณด๊ธฐ (1) | 2023.12.27 |
231221 THU ํ์ด์ฌ ๋ณต์ต (2) CRM ํ์ด๋ฐ ๋ถ์ (0) | 2023.12.21 |