Kaggle competition: Titanic - Machine Learning from Disaster
train = pd.read_csv('/kaggle/input/liverpool-ion-switching/train.csv')
 test = pd.read_csv('/kaggle/input/liverpool-ion-switching/test.csv')
 sample = pd.read_csv('/kaggle/input/liverpool-ion-switching/sample_submission.csv' )<define_variables>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data.head()
def make_batch(df, batchsize=500000):
batches = df.shape[0] // batchsize
df['batch'] = 0
for i in range(batches):
idx = np.arange(i*batchsize,(i+1)*batchsize)
 df.loc[idx, 'batch'] = i + 1
return df<define_variables>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
train = make_batch(train)
 test = make_batch(test, batchsize=500000)
 train.groupby('batch')['signal'].describe()<define_variables>
data = [train_data, test_data]
for dataset in data:
mean = train_data['Age'].mean()
dataset['Age'].fillna(mean, inplace = True)
 dataset["Age"] = dataset["Age"].astype(int)
<split>
for dataset in data:
dataset['relatives'] = dataset['SibSp'] + dataset['Parch']
dataset.loc[dataset['relatives'] > 0, 'relatives'] = 1
dataset.loc[dataset['relatives'] == 0, 'relatives'] = 0
train_segm_separators = np.concatenate([[0,500000,600000], np.arange(1000000,5000000+1,500000)])
 train_segm_signal_groups = [0,0,0,1,2,4,3,1,2,3,4]
train_segm_is_shifted = [False, True, False, False, False, False, False, True, True, True, True]
train_signal = np.split(train['signal'].values, train_segm_separators[1:-1])
 train_opench = np.split(train['open_channels'].values, train_segm_separators[1:-1] )<define_search_space>
for dataset in data:
dataset['Embarked'].fillna("S", inplace = True )
test_segm_separators = np.concatenate([np.arange(0,1000000+1,100000), [1500000,2000000]])
 test_segm_signal_groups = [0,2,3,0,1,4,3,4,0,2,0,0]
test_segm_is_shifted = [True, True, False, False, True, False, True, True, True, False, True, False]
test_signal = np.split(test['signal'].values, test_segm_separators[1:-1] )<train_model>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("女性生还率:", rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("男性生还率:", rate_men )
<concatenate><EOS> 
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Embarked", "relatives"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, 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!" )