Kaggle competition: Titanic - Machine Learning from Disaster
df_train.groupby(u ).size()<groupby>
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from mlxtend.feature_selection import SequentialFeatureSelector as SFS
df_train.groupby(u ).size()<feature_engineering>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
<groupby>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
nf = 'count_Employment'
df_train[nf] = df_train[u].map(df_train.groupby(u ).ConvertedSalary.count())
 df_test[nf] = df_test[u].map(df_train.groupby(u ).ConvertedSalary.count() )<groupby>
train_data['Age'] = train_data['Age'].fillna(train_data.Age.mean())
 test_data['Age'] = test_data['Age'].fillna(test_data.Age.mean())
 test_data['Fare'] = test_data['Fare'].fillna(test_data.Fare.mean() )
df_train.groupby(u ).size()<groupby>
train_data["Embarked"].value_counts()
u = 'CompanySize'
df_train.groupby(u ).size()<groupby>
train_data = train_data.fillna({"Embarked": "S"} )
df_test.groupby(u ).size()<groupby>
train_data.drop(['Name','Ticket','Cabin'], axis = 1, inplace = True)
 test_data.drop(['Name','Ticket','Cabin'], axis = 1, inplace = True )
df_train.groupby(u ).size()<groupby>
train_data.isnull().sum()
test_data.isnull().sum()
u = 'YearsCoding'
df_train.groupby(u ).size()<groupby>
train_data = pd.get_dummies(train_data, columns=["Sex"])
 train_data = pd.get_dummies(train_data, columns=["Embarked"])
 test_data = pd.get_dummies(test_data, columns=["Sex"])
 test_data = pd.get_dummies(test_data, columns=["Embarked"] )
df_test.groupby(u ).size()<categorify>
X = train_data
X = train_data.drop("Survived",axis=1)
 y = train_data["Survived"]
df_train[u].replace({'0-2 years':0,
'12-14 years':12,
'15-17 years':15,
'18-20 years':18,
'21-23 years':21,
'24-26 years':24,
'27-29 years':27,
'3-5 years':3,
'30 or more years':30,
'6-8 years':6,
'9-11 years':9},inplace=True )<groupby>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1)
 X_train.shape, X_test.shape
df_train.groupby(u ).size()<categorify>
sfs = SFS(RandomForestClassifier(n_estimators=250, max_depth=5, random_state=1, n_jobs = -1),
k_features = 6,
forward = False,
floating = False,
verbose = 2,
scoring = 'accuracy',
cv = 4,
n_jobs = -1)
 sfs = sfs.fit(X_train, y_train )
df_test[u].replace({'0-2 years':0,
'12-14 years':12,
'15-17 years':15,
'18-20 years':18,
'21-23 years':21,
'24-26 years':24,
'27-29 years':27,
'3-5 years':3,
'30 or more years':30,
'6-8 years':6,
'9-11 years':9},inplace=True )<groupby>
print(sfs.k_feature_names_)
 print('Sequential Forward Selection:')
 print(sfs.k_feature_idx_)
 print('CV Score:')
 print(sfs.k_score_ )
u = 'YearsCodingProf'
df_train.groupby(u ).size()<groupby>
features = ['Pclass', 'Age', 'SibSp', 'Fare', 'Sex_male', 'Embarked_S']
X = train_data[features]
X_test = test_data[features]
model = RandomForestClassifier(n_estimators=250, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )