<jupyter_start><jupyter_text>1.Import data<jupyter_code>data = pd.read_csv('../data/data_cleaned.csv')

X, y = data.drop('Survived', axis=1), data.pop('Survived')

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2)<jupyter_output><empty_output><jupyter_text>2. Creade models<jupyter_code>DT = DecisionTreeClassifier()
KNN = KNeighborsClassifier()
LR = LogisticRegression(solver='lbfgs', max_iter=1000)<jupyter_output><empty_output><jupyter_text>3. Evaluate each model<jupyter_code>DT.fit(X_train, y_train)
KNN.fit(X_train, y_train)
LR.fit(X_train, y_train)


DT.score(X_test, y_test), KNN.score(X_test, y_test) , LR.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>3.1 Second approach: using out-of-folds predictions<jupyter_code>import numpy as np
from sklearn.model_selection import KFold

def model_predictions_kfold(models, kfolds, X_train, y_train, X_test):

	kf = KFold(n_splits=kfolds, shuffle=True)
	kf.get_n_splits(data)

	pred_train = np.zeros((X_train.shape[0], len(models)))
	pred_test = np.zeros((X_test.shape[0], len(models)))

	# Make the prediction for each model
	for i, model in enumerate(models):

		# In step use the k-1 folds to train, the k fold to prediction
		for train_index, test_index in kf.split(X_train):
				
				# Train on k-1 folders
				model.fit(X_train.iloc[train_index], y_train.iloc[train_index])
				# Predict using k folder
				y_pred = model.predict(X_train.iloc[test_index])

				pred_train[test_index, i] = y_pred

		pred_test[:,i] = model.predict(X_test)
		
	return pred_train, pred_test<jupyter_output><empty_output><jupyter_text>a) Create the base models predictions dataset<jupyter_code>DT = DecisionTreeClassifier()
KNN = KNeighborsClassifier()
LR = LogisticRegression(solver='lbfgs', max_iter=1000)

X_train_meta, X_test_meta = model_predictions_kfold([DT, KNN, LR], 10, X_train, y_train, X_test)<jupyter_output><empty_output><jupyter_text>c) Make predictions<jupyter_code># Stacker Model
meta_model = LogisticRegression()
meta_model.fit(X_train_meta, y_train)
meta_model.score(X_test_meta,y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12310606060606061, 'fraction_numerical': 0.010416666666666666, 'mean_word_length': 7.25390625, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '374562', 'license': 'MIT', 'n_tokens_mistral': 889, 'n_tokens_neox': 831, 'n_words': 143, 'path': 'stacking/stacking2.ipynb', 'repo_name': 'maikereis/ensembling_techniques', 'size': '2112'}