Chapter 09

# テストのCode

(株)ディープブラック
2021.03.21に更新

テストのCodeを以下にまとめます。

``````import pickle
import pandas as pd
import numpy as np
import category_encoders as ce
from sklearn.ensemble import RandomForestClassifier

######### 関数群 #########
def extract_honorific(name):

name_lst = name.split(",")
honorific_lst = name_lst[1].split(".")
honorific = honorific_lst[0][1:]

return honorific

######### pickleを読み込み #########
with open('models/clf_ohe.pkl', 'rb') as f:

######### テストデータを読み込み #########
id_lst = list(data["PassengerId"].values)
data = data[["Pclass", "Name", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]]

######### データの前処理(名前の整理) #########
name_lst = list(data["Name"].values)

honorific_lst = list()
for i in range(0, len(name_lst)):
honorific_lst.append(extract_honorific(name_lst[i]))

data["Honorific"] = honorific_lst

data["Pclass"] = data["Pclass"].replace({1:3,2:2,3:1})

data = data.drop("Name", axis=1)

######### データの前処理(category_encoders) #########
data = ce_ohe.transform(data)
data = data.fillna(data.mean())

pred_lst = list(clf.predict(data.values))

sub_df = pd.DataFrame(np.array(id_lst+pred_lst).reshape(2, -1))
sub_df = sub_df.T
sub_df = sub_df.rename(columns={0: 'PassengerId', 1:"Survived"})