Kaggle competition: Titanic - Machine Learning from Disaster
look_back = 7
epochs = 10
batch_size = 32<define_variables>
train= pd.read_csv('.. /input/titanic/train.csv')
 test= pd.read_csv('.. /input/titanic/test.csv')
np.random.seed(7 )<load_from_csv>
train.isnull().sum()
prices_dataset = pd.read_csv('.. /input/train.csv', header=0)
 test_dataset = pd.read_csv('.. /input/test.csv', header=0 )<prepare_x_and_y>
train.isnull().sum()
def create_dataset(dataset, look_back):
inds = []
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
inds.append(i)
 a = dataset[i:(i+look_back), 0]
dataX.append(a)
 dataY.append(dataset[i + look_back, 0])
 return np.array(dataX), np.array(dataY )<normalization>
train['Embarked'].nunique()
res_all = np.array([] ).reshape(-1, 1)
 for asset in prices_dataset.asset.unique() :
apple = prices_dataset[prices_dataset['asset']==asset]
apple_stock_prices = apple.close.values.astype('float32')
 apple_stock_prices = apple_stock_prices.reshape(len(apple_stock_prices), 1)
 scaler = MinMaxScaler(feature_range=(0, 1))
apple_stock_prices = scaler.fit_transform(apple_stock_prices)
 train_size = int(len(apple_stock_prices)* 0.67)
 test_size = len(apple_stock_prices)- train_size
train, test = apple_stock_prices[0:train_size,:], apple_stock_prices[train_size:len(apple_stock_prices),:]
print('Split data into training set and test set...Number of training samples/ test samples:', len(train), len(test))
trainX, trainY = create_dataset(train, look_back)
 testX, testY = create_dataset(test, look_back)
 trainX = np.reshape(trainX,(trainX.shape[0], trainX.shape[1], 1))
testX = np.reshape(testX,(testX.shape[0], testX.shape[1], 1))
model = Sequential()
model.add(LSTM(4, input_shape=(look_back, 1)))
 model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
 model.fit(trainX, trainY, nb_epoch=epochs, batch_size=batch_size, verbose=0)
 trainPredict = model.predict(trainX)
 testPredict = model.predict(testX)
 trainPredict = scaler.inverse_transform(trainPredict)
 trainY = scaler.inverse_transform([trainY])
 testPredict = scaler.inverse_transform(testPredict)
 testY = scaler.inverse_transform([testY])
 trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' %(trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' %(testScore))
trainPredictPlot = np.empty_like(apple_stock_prices)
 trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
testPredictPlot = np.empty_like(apple_stock_prices)
 testPredictPlot[:, :] = np.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(apple_stock_prices)-1, :] = testPredict
plt.plot(scaler.inverse_transform(apple_stock_prices))
plt.plot(trainPredictPlot)
 plt.plot(testPredictPlot)
 plt.show()
apple = test_dataset[test_dataset['asset']==asset]
apple_stock_prices = apple.close.values.astype('float32')
 apple_stock_prices = apple_stock_prices.reshape(len(apple_stock_prices), 1)
 scaler = MinMaxScaler(feature_range=(0, 1))
apple_stock_prices = scaler.fit_transform(apple_stock_prices)
 testX, testY = create_dataset(apple_stock_prices, look_back)
 testX = np.reshape(testX,(testX.shape[0], testX.shape[1], 1))
testPredict = model.predict(testX)
 price = testPredict[1:]/testPredict[:-1] - 1
price = np.append([0], price ).reshape(-1, 1)
 z = 1440 - price.shape[0]
res = np.append(price, np.zeros(z)).reshape(-1, 1)
 res_all = np.vstack(( res_all,res))<normalization>
def preprocess_data(data):
data= data.drop(['Name', 'Ticket'], axis=1)
 data['Sex'].replace({'male':'0', 'female':'1'}, inplace= True)
 data['Embarked'] = data['Embarked'].fillna(0)
 data['Embarked'].replace({'S':'0', 'C':'1', 'Q':'2'}, inplace= True)
 data['Sex']= data['Sex'].astype(int)
 data['Embarked']= data['Embarked'].astype(int)
 data['Age']= data['Age'].fillna(method= 'ffill')
 data["Deck"] = data["Cabin"].str.slice(0,1)
 data["Room"] = data["Cabin"].str.slice(1,5 ).str.extract("([0-9]+)", expand=False ).astype("float")
 data['Deck']= data['Deck'].fillna(0)
 data['Room']= data['Room'].fillna(0)
 data['Fare'] = data['Fare'].fillna(0)
 data['Deck'].replace({'A':'0', 'B':'1', 'C':'2', 'D':'3', 'F':'4', 'G':'5', 'T':'6','E':'7'}, inplace= True)
 data['Deck']= data['Deck'].astype(int)
 return data
scaler = MinMaxScaler(feature_range=(-1, 1))
res_all = scaler.fit_transform(res_all )<filter>
final_train= preprocess_data(train)
 final_test= preprocess_data(test )
res_all[res_all<-1]=-1<filter>
final_train = final_train.drop(['Cabin'], axis=1)
 final_train
res_all[res_all>1]=1<create_dataframe>
final_test= final_test.drop(['Cabin'], axis=1)
 final_test.isnull().sum()
out_df = pd.DataFrame(res_all, columns=['expected'] )<save_to_csv>
y = final_train['Survived']
X = final_train.drop(['Survived'], axis=1 )
out_df.to_csv('Elastic_net.csv',index = True )<load_from_csv>
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state= 42, test_size=0.20)
 lin= LinearRegression()
df_res = pd.read_csv('Elastic_net.csv')
 df_res.columns = ['id', 'expected']<save_to_csv>
model= RandomForestClassifier(random_state=1, n_estimators=200, max_depth=10, criterion='entropy', verbose=1)
 model.fit(x_train, y_train)
 prediction= model.predict(x_test)
 final_predict = model.predict(final_test)
df_res.to_csv('submit.csv', index=False )<save_to_csv>
X, y = make_classification(n_samples=100, n_features=7,
n_informative=2, n_redundant=0,
random_state=1, shuffle=True)
 clf = BaggingClassifier(base_estimator=RandomForestClassifier() ,
n_estimators=10, random_state=1 ).fit(x_train, y_train)
 clf.predict(x_test)
 final_predict2= clf.predict(final_test )
print(os.listdir(".. /input"))
df = pd.DataFrame({'id': [1,2], 'y': [100,100]})
 df.head()
df.to_csv("submission.csv", header = True, index = False)
<load_from_csv>
output = pd.DataFrame({'PassengerId': final_test.PassengerId, 'Survived': final_predict})
 output.to_csv('my_submission_rf.csv', index=False)
 print(output )
name = os.path.join(".. /input/ai-academy-intermediate-class-competition-1", "BBC News Train.csv")
 data = pd.read_csv(name)
 data = data[["Text", "Category"]] 
<feature_engineering>
output = pd.DataFrame({'PassengerId': final_test.PassengerId, 'Survived': final_predict2})
 output.to_csv('my_submission_clf.csv', index=False)
 print(output )