I am doing the Kaggle competition “Titanic - Machine Learning from Disaster”, my code:…
- User
I am doing the Kaggle competition “Titanic - Machine Learning from Disaster”, my code: “# This Python 3 environment comes with many helpful analytics libraries installed
It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
For example, here’s several helpful packages to load
!pip install tensorflow import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
Input data files are available in the read-only “…/input/” directory
For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os for dirname, _, filenames in os.walk(‘/kaggle/input’): for filename in filenames: print(os.path.join(dirname, filename))
You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using “Save & Run All”
You can also write temporary files to /kaggle/temp/, but they won’t be saved outside of the current session
train_data = pd.read_csv(”/kaggle/input/titanic/train.csv") test_data = pd.read_csv(“/kaggle/input/titanic/test.csv”) train_data.head(100) from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler
features = [‘PassengerId’, ‘Survived’, ‘Pclass’,‘Name’,‘Sex’,‘Age’,‘SibSp’,‘Parch’,‘Ticket’,‘Fare’,‘Cabin’,‘Embarked’] X = pd.get_dummies(train_data[features]) y = train_data[‘Survived’]
Splitting the training data for validation
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
It’s generally a good idea to scale your data for neural networks
scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_val_scaled = scaler.transform(X_val) import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout
model = Sequential([ Dense(32, activation=‘relu’, input_shape=(X_train_scaled.shape[1],)), Dropout(0.5), Dense(32, activation=‘relu’), Dropout(0.5), Dense(32, activation=‘relu’), Dropout(0.5), Dense(1, activation=‘sigmoid’) ])
model.compile(optimizer=‘adam’, loss=‘binary_crossentropy’, metrics=[‘accuracy’])
history = model.fit(X_train_scaled, y_train, epochs=100, validation_data=(X_val_scaled, y_val), verbose=1, batch_size=2)
Scale the test data
X_test = pd.get_dummies(test_data[features]) X_test_scaled = scaler.transform(X_test)
Prediction
test_predictions = model.predict(X_test_scaled) test_predictions = (test_predictions > 0.5).astype(int).reshape(-1)
Preparing for submission
- Assistant
- User
I think is the data processing problem, this :"features = ['PassengerId', 'Pclass','Name','Sex','Age','SibSp','Parch','Ticket','Fare','Cabin','Embarked'] " haven't preprocessed.
- Assistant
- User
No, I need ‘PassengerId’, ‘Name’, ‘Ticket’, ‘Cabin’
- Assistant