Kaggle competition: Titanic - Machine Learning from Disaster
testAdult["MissingCountry"] = testAdult["Country"].transform(lambda x: 1 if x==x else 0)
 testAdult["Country"].fillna(testAdult["Country"].mean() , inplace=True)
 testAdult["MissingWorkclass"] = testAdult["Workclass"].transform(lambda x: 1 if x==x else 0)
 testAdult["Workclass"].fillna(testAdult["Workclass"].mean() , inplace=True)
 testAdult["MissingOccupation"] = testAdult["Occupation"].transform(lambda x:1 if x==x else 0)
 testAdult["Occupation"].fillna(testAdult["Occupation"].mean() ,inplace=True)
<count_values>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
testAdult["Country"].value_counts() 
<define_variables>
women = train_data.loc[train_data.Sex == 'female']['Survived']
print('Women survived',sum(women)/len(women))
men = train_data.loc[train_data.Sex == 'male']['Survived']
print('Men survived',sum(men)/len(men))
nTestAdult = testAdult<prepare_x_and_y>
train_data['female'] = pd.get_dummies(train_data['Sex'])['female']
test_data['female'] = pd.get_dummies(test_data['Sex'])['female']
Xadult = nadult[["Age", "Workclass","Education-Num","Occupation","Race", "Sex","Capital Gain", "Capital Loss", "Hours per week", "Country"]] 
<prepare_x_and_y>
sum(train_data['Age'].isnull())
 train_data['Age'] = train_data['Age'].fillna(train_data['Age'].mean())
 test_data['Age'] = test_data['Age'].fillna(test_data['Age'].mean() )
Yadult = nadult.Target<drop_column>
high_fare = train_data.loc[train_data.Fare > 100]['Survived']
print('High fare survivors',sum(high_fare)/len(high_fare))
low_fare = train_data.loc[train_data.Fare < 32]['Survived']
print('High fare survivors',sum(low_fare)/len(low_fare))
XtestAdult = nTestAdult[["Age", "Workclass","Education-Num","Occupation","Race", "Sex","Capital Gain", "Capital Loss", "Hours per week", "Country"]] 
<count_values>
pclass1 = train_data.loc[train_data.Pclass == 1]['Survived']
print('Class1',sum(pclass1)/len(pclass1))
pclass2 = train_data.loc[train_data.Pclass == 2]['Survived']
print('Class2',sum(pclass2)/len(pclass2))
pclass3 = train_data.loc[train_data.Pclass == 3]['Survived']
print('Class3',sum(pclass3)/len(pclass3))
XtestAdult["Country"].value_counts()<import_modules>
sum(test_data.Pclass.isna() )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
train_data['class1'] = pd.get_dummies(train_data.Pclass)[1]
test_data['class1'] = pd.get_dummies(test_data.Pclass)[1]
train_data['class2'] = pd.get_dummies(train_data.Pclass)[2]
test_data['class2'] = pd.get_dummies(test_data.Pclass)[2]
from sklearn.neighbors import KNeighborsClassifier<import_modules>
sum(test_data.SibSp.isna() )
from sklearn.neighbors import KNeighborsClassifier<choose_model_class>
sibs = train_data.loc[train_data.SibSp <= 1]['Survived']
print(sum(sibs)/len(sibs))
train_data['many_sibs'] =(train_data.SibSp > 1)*1
test_data['many_sibs'] =(test_data.SibSp > 1)*1
knn = KNeighborsClassifier(n_neighbors=25 )<import_modules>
young = train_data.loc[train_data.Age <= 15]['Survived']
print(sum(young)/len(young))
old = train_data.loc[train_data.Age >=40]['Survived']
print(sum(old)/len(old))
from sklearn.model_selection import cross_val_score<compute_train_metric>
bins = [0.42, 15, 30, 50,80]
train_data['bin_age'] = pd.cut(x=train_data.Age, bins=bins)
 test_data['bin_age'] = pd.cut(x=test_data.Age, bins=bins )
scores = cross_val_score(knn, Xadult, Yadult, cv=10 )<train_model>
train_data['young'] = pd.get_dummies(train_data.bin_age ).iloc[:,0]
test_data['young'] = pd.get_dummies(test_data.bin_age ).iloc[:,0]
train_data['senior'] = pd.get_dummies(train_data.bin_age ).iloc[:,3]
test_data['senior'] = pd.get_dummies(test_data.bin_age ).iloc[:,3]
knn.fit(Xadult,Yadult )<predict_on_test>
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
YtestPred = knn.predict(XtestAdult )<define_variables>
X = train_data[features]
y = train_data.Survived
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0 )
savepath = "results.csv"<save_to_csv>
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
 y_pred = log_reg.predict(X_test)
 y_pred
ypanda = pd.DataFrame(YtestPred, columns = ["income"])
 ypanda.to_csv(savepath, index_label="Id")
 ypanda<load_from_csv>
accuracy_score(y_pred, y_test )
adult = pd.read_csv(".. /input/dataset-adult/train_data.csv",sep=",", na_values="?" )<count_values>
confusion_matrix(y_pred, y_test )
adult['native.country'].value_counts()<drop_column>
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_test )
na_adult=adult.set_index("Id" ).dropna()<load_from_csv>
accuracy_score(y_pred, y_test )
test_adult= pd.read_csv(".. /input/dataset-adult/test_data.csv",sep=",",na_values="?" )<drop_column>
confusion_matrix(y_pred, y_test )
test_adult=test_adult.set_index("Id" )<prepare_x_and_y>
test_data.Fare = test_data.Fare.fillna(test_data.Fare.mean() )
X_adult = na_adult.iloc[:,:-1]
Y_adult = na_adult.income<prepare_x_and_y>
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [4,5,6,7,8],
'criterion' :['gini', 'entropy']
}
X_test = test_adult.iloc[:,:]<prepare_x_and_y>
rfc1=RandomForestClassifier(random_state=42, max_features='log2', n_estimators= 200, max_depth=6, criterion='entropy')
 rfc1.fit(X_train, y_train )
<import_modules><EOS> 
predictions = rfc1.predict(test_data[features])
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission3.csv', index=False)
 print("Your submission was successfully saved!" )