์ธ์ฝ๋ฉ ์ค์ตยถ
Inย [ย ]:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
Inย [ย ]:
# ํ์ดํ๋ ๋ฐ์ดํฐ
titanic = pd.read_csv('C:/Users/LOVE/Downloads/vscode/ML/titanic/train.csv')
titanic.head(3)
Out[ย ]:
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
Inย [ย ]:
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
le = LabelEncoder()
oe = OneHotEncoder()
1. ๋ ์ด๋ธ ์ธ์ฝ๋ฉ: ์ฑ๋ณ(Sex)ยถ
Inย [ย ]:
le.fit(titanic[['Sex']])
c:\Users\LOVE\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\preprocessing\_label.py:97: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True)
Out[ย ]:
LabelEncoder()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LabelEncoder()
Inย [ย ]:
le.classes_
Out[ย ]:
array(['female', 'male'], dtype=object)
Inย [ย ]:
titanic['Sex_le'] = le.transform(titanic[['Sex']])
titanic.head(3)
c:\Users\LOVE\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\preprocessing\_label.py:132: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, dtype=self.classes_.dtype, warn=True)
Out[ย ]:
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | Sex_le | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S | 1 |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | 0 |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S | 0 |
2. ์ํซ ์ธ์ฝ๋ฉ: ํญ๊ตฌ(Embarked)ยถ
Inย [ย ]:
oe.fit(titanic[['Embarked']])
Out[ย ]:
OneHotEncoder()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
OneHotEncoder()
Inย [ย ]:
oe.categories_
Out[ย ]:
[array(['C', 'Q', 'S', nan], dtype=object)]
Inย [ย ]:
embarked_csr = oe.transform(titanic[['Embarked']])
embarked_csr_df = pd.DataFrame(embarked_csr.toarray(), columns = oe.get_feature_names_out())
embarked_csr_df.head(3)
Out[ย ]:
Embarked_C | Embarked_Q | Embarked_S | Embarked_nan | |
---|---|---|---|---|
0 | 0.0 | 0.0 | 1.0 | 0.0 |
1 | 1.0 | 0.0 | 0.0 | 0.0 |
2 | 0.0 | 0.0 | 1.0 | 0.0 |
Inย [ย ]:
pd.concat([titanic, embarked_csr_df], axis = 1)
Out[ย ]:
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | Sex_le | Embarked_C | Embarked_Q | Embarked_S | Embarked_nan | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S | 1 | 0.0 | 0.0 | 1.0 | 0.0 |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | 0 | 1.0 | 0.0 | 0.0 | 0.0 |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S | 0 | 0.0 | 0.0 | 1.0 | 0.0 |
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S | 0 | 0.0 | 0.0 | 1.0 | 0.0 |
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S | 1 | 0.0 | 0.0 | 1.0 | 0.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
886 | 887 | 0 | 2 | Montvila, Rev. Juozas | male | 27.0 | 0 | 0 | 211536 | 13.0000 | NaN | S | 1 | 0.0 | 0.0 | 1.0 | 0.0 |
887 | 888 | 1 | 1 | Graham, Miss. Margaret Edith | female | 19.0 | 0 | 0 | 112053 | 30.0000 | B42 | S | 0 | 0.0 | 0.0 | 1.0 | 0.0 |
888 | 889 | 0 | 3 | Johnston, Miss. Catherine Helen "Carrie" | female | NaN | 1 | 2 | W./C. 6607 | 23.4500 | NaN | S | 0 | 0.0 | 0.0 | 1.0 | 0.0 |
889 | 890 | 1 | 1 | Behr, Mr. Karl Howell | male | 26.0 | 0 | 0 | 111369 | 30.0000 | C148 | C | 1 | 1.0 | 0.0 | 0.0 | 0.0 |
890 | 891 | 0 | 3 | Dooley, Mr. Patrick | male | 32.0 | 0 | 0 | 370376 | 7.7500 | NaN | Q | 1 | 0.0 | 1.0 | 0.0 | 0.0 |
891 rows ร 17 columns
'๐ Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ ํ ์คํธ ๋ฐ์ดํฐ ๋ถ๋ฆฌ ์ค์ต (0) | 2024.02.05 |
---|---|
ํ์ด์ฌ ์ค์ผ์ผ๋ง ์ค์ต (0) | 2024.02.05 |
ํ์ด์ฌ ๊ฒฐ์ธก์น ํ์ธ ์ค์ต (0) | 2024.02.05 |
ํ์ด์ฌ ์ด์์น ํ์ธ ์ค์ต (0) | 2024.02.05 |
240126 FRI ํ์ด์ฌ ๋ฐ์ดํฐ ๋ถ์ ๊ฐ์ธ ๊ณผ์ (0) | 2024.01.26 |