<jupyter_start><jupyter_text>Machine Learning 실습  타이타닉 탑승객 생존 예측 Classification with Python  1. 데이터 불러오기<jupyter_code>import pandas as pd
import numpy as np
import sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
%config Completer.use_jedi = False
! pip install seaborn
df= pd.read_csv('03Titanic_dataset.csv')
df.head()<jupyter_output><empty_output><jupyter_text>2.데이터 확인하기<jupyter_code>print(df.info())
print('==='* 30)
print(df.describe())<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1309 entries, 0 to 1308
Data columns (total 14 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   pclass     1309 non-null   int64  
 1   survived   1309 non-null   int64  
 2   name       1309 non-null   object 
 3   gender     1309 non-null   object 
 4   age        1046 non-null   float64
 5   sibsp      1309 non-null   int64  
 6   parch      1309 non-null   int64  
 7   ticket     1309 non-null   object 
 8   fare       1308 non-null   float64
 9   cabin      295 non-null    object 
 10  embarked   1307 non-null   object 
 11  boat       486 non-null    object 
 12  body       121 non-null    float64
 13  home.dest  745 non-null    object 
dtypes: float64(3), int64(4), object(7)
memory usage: 143.3+ KB
None
==========================================================================================
            pclass     survived          age        sibsp        parch  \
count  1309.000000 [...]<jupyter_text>3. 빠진 값 확인<jupyter_code># 3. 빠진 값 확인
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>4. 사용하지 않을 feature 제거<jupyter_code>df = df.drop(['name','ticket','boat','cabin','body','home.dest'],axis=1)<jupyter_output><empty_output><jupyter_text>5. Impute - Fare<jupyter_code>df[df['fare'].isnull()]<jupyter_output><empty_output><jupyter_text>위에서는 전체 feature의 null 값을 sum 하여 summary로 보았다면, 결측치를 변형하기 전에 그 특정 feature을 따로 선택하여 보는 것이 필요하다. 예를들어 fare 은 결측치가 하나 인걸 확인 할 수있고그에 따른 row에서 상황 판단을 할 수 있다.<jupyter_code>df.loc[(df.fare.isnull()&(df.pclass==3),'fare')] = df.fare[df.pclass == 3].mean()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>6. Impute - Age<jupyter_code>df[df['age'].isnull()]
df.loc[(df.age.isnull()&(df.gender== 'male'),'age')] = df.age[df.gender == 'male'].median()
df.loc[(df.age.isnull()&(df.gender== 'female'),'age')] = df.age[df.gender == 'female'].median()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>7. Impute - Embarked<jupyter_code>df[df['embarked'].isnull()]
imp = SimpleImputer(strategy = 'most_frequent')

imp.fit(df.loc[:,['embarked']]) 
df.loc[:,['embarked']] = imp.transform(df.loc[:,['embarked']])<jupyter_output><empty_output><jupyter_text>8. Feature Heatmap 시각화<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

# feature heatmap 그려보기
featurecorr = df.corr()
plt.figure(figsize=(15,10))
sns.heatmap(featurecorr, cmap='viridis', annot=True)
#sns.heatmap(df_17corr, annot=True, fmt='f')
plt.title('Heatmap of featurecorr by seaborn', fontsize=15)

plt.show()
#시각화해서 색이 진한건 상관관계가 높다는 걸 알 수 있다.<jupyter_output><empty_output><jupyter_text>8-1. 시각화 - 성별에 따른 생존자 수  8-2 시각화 - 선실 등급에 따른 생존여부  9. X/y  분리<jupyter_code>feature= df.drop(['survived'],axis=1)
label = df['survived']
feature<jupyter_output><empty_output><jupyter_text>10. 데이터 변환(one hot encoding)<jupyter_code>from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

ct = ColumnTransformer([('one_hot_encoder', OneHotEncoder(), [1,6])], remainder = 'passthrough')

feature= ct.fit_transform(feature)<jupyter_output><empty_output><jupyter_text>11. 훈련셋/평가셋 분리<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(feature,label, test_size = 0.2, random_state=100)

print(X_train)
print('=====' *15)
print(y_train)<jupyter_output>[[  0.       1.       1.     ...   0.       1.     247.5208]
 [  0.       1.       1.     ...   0.       0.      12.7375]
 [  0.       1.       0.     ...   0.       0.      13.    ]
 ...
 [  1.       0.       0.     ...   0.       2.      39.    ]
 [  1.       0.       0.     ...   2.       0.      25.7   ]
 [  0.       1.       1.     ...   1.       1.       7.2292]]
===========================================================================
16     0
538    1
532    0
285    0
365    0
      ..
802    0
53     0
350    1
79     1
792    0
Name: survived, Length: 1047, dtype: int64<jupyter_text>12. 모델 학습<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV, cross_val_score, StratifiedKFold
RFC = RandomForestClassifier()
RFC.fit(X_train, y_train)
model =RandomForestClassifier()
model.fit(X_train, y_train)

y_pred = RFC.predict(X_test)
print(y_pred)
print(np.array(y_test))<jupyter_output>[0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 1 0
 0 1 0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 1
 0 1 0 1 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0
 0 1 0 0 0 1 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0
 1 0 0 1 0 0 1 1 1 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 0 0 0 0 1 0 0 0
 0 0 1 1 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0
 0 1 0 0 1 1 1 0 1 0 1 1 0 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0
 1 0 1]
[0 0 0 0 1 0 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0
 0 1 0 1 0 1 1 0 1 0 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0
 1 1 0 1 0 1 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1
 0 1 0 0 0 1 0 1 1 0 1 1 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
 1 0 0 1 0 0 1 0 1 1 1 0 0 0 1 0 1 1 0 1 1 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 0
 0 0 1 1 1 0 1 0 1 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 1 0 0 0 1 1 0 1 1 0
 0 1 1 0 1 1 0 0 [...]<jupyter_text>13. 모델 성능 확인(evaluate)<jupyter_code>from sklearn.metrics import accuracy_score, precision_score, recall_score

acc = accuracy_score(y_test, y_pred)
pres =  precision_score(y_test, y_pred)
recall= recall_score(y_test, y_pred)

print(acc)
print(pres)
print(recall)<jupyter_output>0.7977099236641222
0.7142857142857143
0.7368421052631579<jupyter_text>13-1. confusion matrix 확인<jupyter_code>from sklearn.model_selection import KFold, cross_val_score

data_kf = KFold(n_splits=5, shuffle=True, random_state=9)
score = cross_val_score(model, feature, label, cv=data_kf, scoring= 'accuracy')
print("5_fold_acc:", score)
print("Mean_Acc:", score.mean())<jupyter_output>5_fold_acc: [0.79007634 0.76335878 0.76335878 0.77862595 0.78544061]
Mean_Acc: 0.7761720920710129
{'fraction_non_alphanumeric': 0.1380080393032604, 'fraction_numerical': 0.1299687360428763, 'mean_word_length': 2.9798578199052135, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 79, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '631115', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3575, 'n_tokens_neox': 2986, 'n_words': 943, 'path': 'Machine_Learning/03.Classification_with_scikitlearn(Titanic).ipynb', 'repo_name': 'jeewoolee-jasmine/TIL', 'size': '6717'}