Kaggle competition: DS特論2019年度 演習課題2
df_all['BsmtCond'] = df_all['BsmtCond'].fillna('TA')
 df_dummy = pd.get_dummies(df_all['BsmtCond'],prefix='BsmtCond',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['BsmtCond']<categorify>
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
df_all['BsmtExposure'] = df_all['BsmtExposure'].fillna('TA')
 df_dummy = pd.get_dummies(df_all['BsmtExposure'],prefix='BsmtExposure',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['BsmtExposure']<categorify>
train_users_game1_df = pd.read_csv(".. /input/ds2019uec-task2/train_users_game1.csv")
 train_users_game2_df = pd.read_csv(".. /input/ds2019uec-task2/train_users_game2.csv")
 test_users_game1_df = pd.read_csv(".. /input/ds2019uec-task2/test_users_game1.csv" )
def Functional(f):
if f == 'Typ':
return 1
elif f == 'Min1':
return 2
elif f == 'Min2':
return 3
elif f == 'Mod':
return 4
elif f == 'Maj1':
return 5
elif f == 'Maj2':
return 6
elif f == 'Sev':
return 7
elif f == 'Sal':
return 8
else:
return 0<data_type_conversions>
train_user_encoder = LabelEncoder()
train_user_encoder.fit(np.concatenate([train_users_game1_df["user_id"], train_users_game2_df["user_id"]]))
print(train_user_encoder.classes_.shape)
 game1_encoder = LabelEncoder()
game1_encoder.fit(np.concatenate([train_users_game1_df["game_title"], test_users_game1_df["game_title"]]))
print(game1_encoder.classes_.shape )
df_all['Functional'] = df_all['Functional'].apply(Functional ).astype(int )<categorify>
X = np.zeros(( 9393, 4155))
user_vec = train_user_encoder.transform(train_users_game1_df["user_id"])
 game_vec = game1_encoder.transform(train_users_game1_df["game_title"])
 for i, j in zip(user_vec, game_vec):
X[i, j] = 1
df_dummy = pd.get_dummies(df_all['GarageType'],prefix='GarageType',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['GarageType']<categorify>
Y_mat = np.zeros(( 9393, 1000))
user_vec = train_user_encoder.transform(train_users_game2_df["user_id"])
 game_vec = train_users_game2_df["predict_game_id"]
for i, j in zip(user_vec, game_vec):
Y_mat[i, j] = 1
df_dummy = pd.get_dummies(df_all['SaleType'],prefix='SaleType',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['SaleType']<categorify>
test_users = pd.read_csv(".. /input/ds2019uec-task2/test_user_ids.csv")["user_id"]
df_dummy = pd.get_dummies(df_all['SaleCondition'],prefix='SaleCondition',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['SaleCondition']<categorify>
test_user_encoder = LabelEncoder()
test_user_encoder.fit(test_users)
 print(test_user_encoder.classes_.shape )
df_dummy = pd.get_dummies(df_all['Heating'],prefix='Heating',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['Heating']<categorify>
X_test = np.zeros(( 3000, 4155))
user_vec_test = test_user_encoder.transform(test_users_game1_df["user_id"])
 game_vec = game1_encoder.transform(test_users_game1_df["game_title"])
 for i, j in zip(user_vec_test, game_vec):
X_test[i, j] = 1
df_dummy = pd.get_dummies(df_all['KitchenQual'],prefix='KitchenQual',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['KitchenQual']<categorify>
pred_test_mat = np.zeros(( 3000, 1000))
df_all['GarageQual'] = df_all['GarageQual'].fillna('TA')
 df_dummy = pd.get_dummies(df_all['GarageQual'],prefix='GarageQual',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['GarageQual']<categorify>
X_test[:, X.sum(axis=0)< 100] = 0
X[:, X.sum(axis=0)< 100] = 0
df_dummy = pd.get_dummies(df_all['Foundation'],prefix='Foundation',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['Foundation']<categorify>
for r in tqdm(range(1000)) :
if Y_mat[:, r].sum() > 100:
lr = LogisticRegression(solver='lbfgs')
 lr.fit(X, Y_mat[:, r])
 pred_test_mat[:, r] = lr.predict_proba(X_test)[:, 1]
df_dummy = pd.get_dummies(df_all['GarageCond'],prefix='GarageCond',dummy_na=True)
 df_all = pd.concat([df_all,df_dummy],axis=1)
 del df_all['GarageCond']<categorify>
res_list = [""] * 3000
for i in range(3000):
res_list[i] = " ".join([str(i)for i in np.argsort(-pred_test_mat[i, :])[:10]])
 sample_submission_df = pd.read_csv(".. /input/ds2019uec-task2/sample_submission.csv")
 sub_df = sample_submission_df.copy()
sub_df["purchased_games"] = res_list
<data_type_conversions><EOS> 
sub_df.to_csv("logistic_regression_1000.csv", index=False )