<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
from google.colab import files
file = files.upload()
df = pd.read_csv('train.csv')
print(df.shape)
df.head(5)
df.dropna().sum()
df1 = df.drop(['PassengerId','Name','Ticket','Cabin','Embarked'], axis = 1)
df1.head()
(df1['Sex'])
sex_set = set(df1['Sex'])
sex_set
sibsp_set = set(df1['SibSp'])
sibsp_set
parch_set = set(df1['Parch'])
parch_set
df.info()
df1['Sex'] = df1['Sex'].map({'female': 0, 'male': 1}).astype(int)
print(df1.head)
df
data = df.dropna()
data
df1
data1 = df1.dropna()
data1
# returns all rows from index 1(which is pclass)
X = data1.iloc[:,1:]
X
X = data1.iloc[:,1:].values
X
Y = data1.iloc[:,0]
Y
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0) #only test_size taken as the train_size will adjust accordingly
print(X_train.shape)
print(X_test.shape)
print(X_test)
print(X_train)
print(y_train)
print(y_test)<jupyter_output>423    0
177    0
305    1
292    0
889    1
      ..
352    0
99     0
689    1
858    1
137    0
Name: Survived, Length: 179, dtype: int64<jupyter_text>Logistic Regression<jupyter_code>from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test =  sc.transform(X_test)
print(X_train)
print(X_test)
X_trainf = sc.fit(X_train)
print(X_trainf)
fit_t = X_trainf.transform(X_train)
print(fit_t)
X_testf = sc.fit(X_test)
print(X_testf)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(random_state = 0 , max_iter =100 , verbose =0) #max iterations
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
plt.figure(figsize=(18,4));
plt.scatter(data['Age'] , data['Survived'] , color = 'green' , marker ='+')
plt.xlabel("Age")
from sklearn.metrics import accuracy_score
print("Accuracy of Model is {0}%".format(accuracy_score(y_test,y_pred)*100))<jupyter_output>Accuracy of Model is 81.56424581005587%<jupyter_text>Support Vector Machine<jupyter_code>from sklearn import svm
model = svm.SVC(kernel='linear' ,probability= True) #prob set true because it gives each class a score.
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
from sklearn.metrics import  accuracy_score
print("Accuracy of Model is {0}%".format(accuracy_score(y_test,y_pred)*100))
from sklearn import svm
model1 = svm.SVC(kernel='linear')
model2 = svm.SVC(kernel='rbf')
model3 = svm.SVC(gamma = 0.001)
model4 = svm.SVC(gamma = 0.001,C = 1)

model1.fit(X_train,y_train)
model2.fit(X_train,y_train)
model3.fit(X_train,y_train)
model4.fit(X_train,y_train)

y_predModel1 = model1.predict(X_test)
y_predModel2 = model2.predict(X_test)
y_predModel3 = model3.predict(X_test)
y_predModel4 = model4.predict(X_test)

print("Accuracy of the Model 1: {0}%".format(accuracy_score(y_test, y_predModel1)*100))
print("Accuracy of the Model 2: {0}%".format(accuracy_score(y_test, y_predModel2)*100))
print("Accuracy of the Model 3: {0}%".format(accuracy_score(y_test, y_predModel3)*100))
print("Accuracy of the Model 4: {0}%".format(accuracy_score(y_test, y_predModel4)*100))<jupyter_output>Accuracy of the Model 1: 77.6536312849162%
Accuracy of the Model 2: 78.77094972067039%
Accuracy of the Model 3: 73.18435754189943%
Accuracy of the Model 4: 73.18435754189943%<jupyter_text>Random Forest<jupyter_code>n_estimators = [int(x) for x in np.linspace(start = 10, stop = 100, num = 10)]
n_estimators
n_estimators = [int(x) for x in np.linspace(start = 10, stop = 100, num = 10)]

max_features = ['auto', 'sqrt']

max_depth = [int(x) for x in np.linspace(start = 1, stop = 10, num =2)]

min_samples_split = [int(x) for x in np.linspace(start = 1, stop = 10, num = 2)]

min_samples_leaf = [int(x) for x in np.linspace(start = 1, stop = 5, num = 2)]

bootstrap = [True, False]
param_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf,
               'bootstrap': bootstrap}
print(param_grid)
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
rf_Model = RandomForestClassifier(random_state=0)
rf_Grid = GridSearchCV(estimator = rf_Model, param_grid = param_grid, cv = 3, verbose=2, n_jobs = 4)
rf_Grid.fit(X_train, y_train)
rf_Grid.best_params_
gc = RandomForestClassifier(random_state = 0, bootstrap = True , max_depth=10 , max_features= 'auto' , min_samples_leaf= 1, min_samples_split= 10, n_estimators= 90)
gc.fit(X_train , y_train)
pred = gc.predict(X_test)
acc = accuracy_score(y_test , pred)
acc
y_pred = rf_Grid.predict(X_test)

from sklearn.metrics import accuracy_score
print("Accuracy of the Model: {0}%".format(accuracy_score(y_test, y_pred)*100))<jupyter_output>Accuracy of the Model: 82.68156424581005%<jupyter_text>Ensemble methods (BAGGING)<jupyter_code>from sklearn.ensemble import BaggingClassifier
from sklearn.neighbors import KNeighborsClassifier
bagging = BaggingClassifier(KNeighborsClassifier(), random_state=0, max_samples=0.5, max_features=0.5)
bagging.fit(X_train, y_train)
pred2 = bagging.predict(X_test)
from sklearn.metrics import accuracy_score
acc2 = accuracy_score(y_test , pred2)
acc2<jupyter_output><empty_output><jupyter_text>Boosting<jupyter_code>#from sklearn.datasets import make_hastie_10_2
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
clf.fit(X_train, y_train)
#clf.score(X_test, y_test)
pred3 = clf.predict(X_test)
from sklearn.metrics import accuracy_score
acc3 = accuracy_score(y_test , pred3)
acc3<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12549560420617134, 'fraction_numerical': 0.05171522151353215, 'mean_word_length': 6.288944723618091, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '15766', 'license': 'MIT', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2250, 'n_words': 479, 'path': 'titanic_train.ipynb', 'repo_name': 'ausafahmed08/Anonymous', 'size': '5801'}