<jupyter_start><jupyter_text>Importing data from csv files.<jupyter_code>#Dataset is taken from kaggle thus is divided into 3 files.
train=pd.read_csv(r'train.csv')
test=pd.read_csv(r'test.csv')
gender_submission=pd.read_csv(r'gender_submission.csv')<jupyter_output><empty_output><jupyter_text>Pre-Processing testing dataset.<jupyter_code># gender_submission file consist the output of testing dataset thus mergeing that data with out output data file.
test.insert(1, "Survived", gender_submission['Survived'], True) 
test.head()
train.head()
#Taking the columns which are needed for classification and ignoring the columns like PassengerID,Name,Ticket,Fare and Cabin 
train=train[['Survived','Pclass','Sex','SibSp','Parch','Embarked']]
test=test[['Survived','Pclass','Sex','SibSp','Parch','Embarked']]
#Droping the Null value rows and performing one hot encoding
train.dropna()
train=pd.get_dummies(train)
test=pd.get_dummies(test)<jupyter_output><empty_output><jupyter_text>Pre-Processed Dataset.<jupyter_code>train.head()
test.head()<jupyter_output><empty_output><jupyter_text>Making y_train and y_test from train and test DataFrame<jupyter_code>y_train=train['Survived']
train.drop(['Survived'], axis=1)

y_test=test['Survived']
test.drop(['Survived'], axis=1)

print(len(y_train))
print(len(y_test))<jupyter_output>891
418<jupyter_text>Accuracy Function<jupyter_code>def accuracy(y_pred,y_test):
    from sklearn.metrics import accuracy_score,confusion_matrix, f1_score
    import seaborn as sns
    import matplotlib.pyplot as plt
    print("accuracy score:",accuracy_score(y_test, y_pred))
    print("confusion matrix:\n",confusion_matrix(y_test, y_pred))
    print("f1 score:",f1_score(y_test, y_pred, average='macro'))
    # using heatmat to plot accuracy
    a=np.array(y_pred).reshape(-1,1)
    b=np.array(y_test).reshape(-1,1)
    df=pd.DataFrame(np.append(a,b,axis=1))
    df.columns=["predicted_vals","true_vals"]
    cor = df.corr()
    sns.heatmap(cor)
    #to use scatter plot uncomment the below given code
    #plt.scatter(y_test,y_pred)
    plt.show()<jupyter_output><empty_output><jupyter_text>1) Using RandomForestClassifier from sklearn.ensemble to generate, fit the model and predict the output.<jupyter_code>from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100, bootstrap = True, max_features = 'sqrt')
model.fit(train,y_train)
y_pred_randF= model.predict(test)
y_pred_randF=y_pred_randF.tolist()<jupyter_output><empty_output><jupyter_text>2) Using Naive Bayes from sklearn.ensemble to generate, fit the model and predict the output.<jupyter_code>from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
y_pred_naiveBayes = gnb.fit(train, y_train).predict(test)<jupyter_output><empty_output><jupyter_text>3) Using Support Vector Machine from sklearn.ensemble to generate, fit the model and predict the output.<jupyter_code>from sklearn import svm
clf = svm.SVC()
clf.fit(train, y_train)
y_pred_SVM=clf.predict(test)<jupyter_output><empty_output><jupyter_text>4) Using Stochastic Gradient Descent from sklearn.ensemble to generate, fit the model and predict the output.<jupyter_code>from sklearn.linear_model import SGDClassifier
clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=5)
clf.fit(train, y_train)
SGDClassifier(max_iter=5)
y_pred_SGD=clf.predict(test)<jupyter_output>C:\Users\Dell\Anaconda3\lib\site-packages\sklearn\linear_model\_stochastic_gradient.py:557: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  ConvergenceWarning)<jupyter_text>5) Using Stochastic Gradient Descent from sklearn.ensemble to generate, fit the model and predict the output.<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors=2)
neigh.fit(train,y_train)
y_pred_KNN=neigh.predict(test)
print("Random Forest Accuracy")
accuracy(y_pred_randF,y_test)
print("\nNaive Bayes Accuracy")
accuracy(y_pred_naiveBayes,y_test)
print("\nSupport Vector Machine Accuracy")
accuracy(y_pred_SVM,y_test)
print("\nStochastic Gradient Decent Accuracy")
accuracy(y_pred_SGD,y_test)
print("\n KNN Accuracy")
accuracy(y_pred_KNN,y_test)<jupyter_output>Random Forest Accuracy
accuracy score: 1.0
confusion matrix:
 [[266   0]
 [  0 152]]
f1 score: 1.0
{'fraction_non_alphanumeric': 0.11347353824820892, 'fraction_numerical': 0.01086202911948232, 'mean_word_length': 8.016666666666667, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '344147', 'license': 'MIT', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1546, 'n_words': 311, 'path': 'Classification/2) Titanic/Titanic Dataset Solution.ipynb', 'repo_name': 'Kashyap85/Machine-Learning-101', 'size': '4327'}