Kaggle competition: Personality Profile Prediction
<SOS> metric: meancolumnwiselogloss Kaggle data source: personality-profile-prediction<categorify>
%matplotlib inline
warnings.filterwarnings('ignore' )
dataset.Sex = dataset.Sex.replace("female", 0)
 dataset.Sex = dataset.Sex.replace("male", 1 )<prepare_x_and_y>
train_df = pd.read_csv('.. /input/train.csv' )
y = dataset.Survived
features = ["Sex", "Age", "Parch", "SibSp"]
X = dataset[features]<split>
test_df=pd.read_csv('.. /input/test.csv' )
X_tr, X_val, y_tr, y_val = train_test_split(X, y, random_state = 0 )<choose_model_class>
combined_df=pd.concat([train_df[['posts']],test_df[['posts']]] )
my_model = XGBRegressor(n_estimators = 1000, learning_rate = 0.01)
 my_pipeline = Pipeline(steps=[ 
("model", my_model)
 ] )<train_model>
vectorizer = CountVectorizer(stop_words='english',analyzer = "word",tokenizer = None,preprocessor = None,max_features = 10000)
 combined_df=vectorizer.fit_transform(combined_df['posts'] )
my_pipeline.fit(X_tr, y_tr, model__early_stopping_rounds = 10, model__eval_set = [(X_val, y_val)], model__verbose = False)
 preds = my_pipeline.predict(X_val )<compute_test_metric>
train = combined_df[:train_df.shape[0]]
test = combined_df[train_df.shape[0]:]
y=train_df['type']
mean_absolute_error(y_val, preds )<load_from_csv>
y_mind = y.apply(lambda x: 0 if x[0] == 'I' else 1)
 y_energy = y.apply(lambda x: 0 if x[1] == 'S' else 1)
 y_nature = y.apply(lambda x: 0 if x[2] == 'F' else 1)
 y_tactics = y.apply(lambda x: 0 if x[3] == 'P' else 1 )
dataset2 = pd.read_csv(".. /input/test.csv")
 dataset2.Sex = dataset2.Sex.replace("female", 0)
 dataset2.Sex = dataset2.Sex.replace("male", 1)
 X = dataset2[features]
preds2 = my_pipeline.predict(X )<save_to_csv>
from sklearn.metrics import f1_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_validate
from sklearn.ensemble import GradientBoostingClassifier
submission.to_csv('submission.csv', index=False )<save_to_csv>
clf = GradientBoostingClassifier(n_estimators=200,learning_rate=0.1, random_state=0)
 kfolds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
 scoring = {'f1': 'f1_micro'}
submission.to_csv('submission.csv', index=False )<load_from_csv>
result = cross_validate(clf,train, y_mind, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
trainSet = pd.read_csv('/kaggle/input/titanic/train.csv')
 testSet = pd.read_csv('/kaggle/input/titanic/test.csv')
 y = trainSet['Survived']
trainSet1 = trainSet.copy()
testSet1 = testSet.copy()
combineData = list([trainSet1, testSet1])
 print(1 )<categorify>
print('Y_mind model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
for data in combineData:
data.drop(columns = ['PassengerId', 'Name', 'Ticket', 'Fare', 'Cabin'], inplace = True)
 data['Sex'] = data['Sex'].map({'male':0, 'female':1})
 def age(x):
if 0<x<=12.0 :
return 1
elif 12.0<x<=18.0 :
return 2
elif 18.0<x<=40.0 :
return 3
elif 40.0<x<=60.0 :
return 4
elif 60.0<x :
return 5
else:
return 0
trainSet1['Embarked'] = trainSet1['Embarked'].fillna('S')
 for i in combineData:
i['Age'] = i['Age'].map(age)
 i['Embarked'] = i['Embarked'].map({'C':0, 'Q':1, 'S':2})
 def SibSpAndParch(x):
if x == 0 :
return 0
if x == 1 :
return 1
if x > 1 :
return 2
for i in combineData:
i['farmily'] = i['SibSp'] + i['Parch']
i.drop(columns = ['SibSp', 'Parch'],inplace = True)
 i['farmily'] = i['farmily'].map(SibSpAndParch)
 print(1 )<prepare_x_and_y>
result = cross_validate(clf,train, y_energy, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
x = trainSet1.drop(columns = ['Survived'])
 y = trainSet1['Survived']
x.head()<split>
print('Y_energy model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
train_x,test_x,train_y,test_y = train_test_split(x, y, test_size = 0.2, random_state = 1 )<find_best_params>
result = cross_validate(clf,train, y_nature, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
cv_scores = []
maxDepths = [i for i in range(2,10)]
for maxDepth in maxDepths:
model = DecisionTreeClassifier(max_depth=maxDepth)
 scores = cross_val_score(model, train_x, train_y, cv = 5)
 cv_score = scores.mean()
print('maxDepth={}，score={:.3f}'.format(maxDepth, cv_score))
cv_scores.append(cv_score )<train_model>
print('Y_nature model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
depth = maxDepths[np.argmax(cv_scores)]
model = DecisionTreeClassifier(max_depth=depth)
 model.fit(train_x,train_y)
 score = model.score(test_x,test_y)
 print(score )<save_to_csv>
result = cross_validate(clf,train, y_tactics, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
id = testSet['PassengerId']
id = id.as_matrix()
result = list(zip(id,model.predict(testSet1)))
 df = pd.DataFrame(result, columns = ['PassengerId', 'Survived'])
 df.to_csv('decisionTreeResult.csv', index = False)
 print(df.shape)
 df.head()<import_modules>
print('Y_tactics model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
import seaborn as sns<load_from_csv>
clf.fit(train,y_mind)
 X_t = test
predictions_Mind = clf.predict_proba(X_t)
 Mind = pd.DataFrame(predictions_Mind[:,1], columns = ['Mind'] )
train=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.head()<count_missing_values>
clf.fit(train,y_energy)
 predictions_Energy = clf.predict_proba(X_t)
 Energy = pd.DataFrame(predictions_Energy[:,1], columns = ['Energy'] )
train.isnull().any()<count_missing_values>
clf.fit(train,y_tactics)
 predictions_Tactic = clf.predict_proba(X_t)
 Tactic = pd.DataFrame(predictions_Energy[:,1], columns = ['Tactic'] )
miss_col=[col for col in train.columns if train[col].isnull().any() ]
print(miss_col )<data_type_conversions>
clf.fit(train,y_nature)
 predictions_Nature = clf.predict_proba(X_t)
 Nature = pd.DataFrame(predictions_Nature[:,1], columns = ['Nature'] )
for col in miss_col:
train[col]=train[col].fillna(train[col].mode() [0])
<count_missing_values>
submission = pd.concat([Mind,Energy,
Nature,Tactic], axis=1)
train.isnull().sum()<count_missing_values>
submission['index'] = submission['index'] +1
submission.columns = ['id', 'mind', 'energy', 'nature', 'tactics']
submission.head()
<categorify><EOS> 
submission.to_csv('submission_Gradient.csv', index=False )