<jupyter_start><jupyter_text>combining dataset train and test into one<jupyter_code>df2['Survived']=2
df2.info()
frames = [df1,df2]
df = pd.concat(frames,axis=0,sort=True)
df.head()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1309 entries, 0 to 417
Data columns (total 12 columns):
Age            1046 non-null float64
Cabin          295 non-null object
Embarked       1307 non-null object
Fare           1308 non-null float64
Name           1309 non-null object
Parch          1309 non-null int64
PassengerId    1309 non-null int64
Pclass         1309 non-null int64
Sex            1309 non-null object
SibSp          1309 non-null int64
Survived       1309 non-null int64
Ticket         1309 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 132.9+ KB<jupyter_text>cabin has more than 50 % null values so droping<jupyter_code>df_final=df.drop(['Cabin'], axis=1)
df_final.isnull().sum()<jupyter_output><empty_output><jupyter_text>imputation on Embarked feature<jupyter_code>df_final.Embarked.fillna(df_final.Embarked.mode()[0],inplace=True)
df_final.isnull().sum()<jupyter_output><empty_output><jupyter_text>imputation on fare feature<jupyter_code>df_final.Fare.fillna(df_final.Fare.mean(),inplace=True)
df_final.isnull().sum()<jupyter_output><empty_output><jupyter_text>title wise categoring based on name<jupyter_code>df_final['Title'] = df_final.Name.apply(lambda Name: Name.split(',')[1].split('.')[0].strip())
df_final.head()

df_final.Title.value_counts()<jupyter_output><empty_output><jupyter_text>imputation on age feature<jupyter_code>Title_Age = df_final.groupby(df_final['Title']).Age.transform('median')
df_final.Age.fillna(Title_Age,inplace=True)
df_final.isnull().sum()
df_final.head()
df_final.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1309 entries, 0 to 417
Data columns (total 12 columns):
Age            1309 non-null float64
Embarked       1309 non-null object
Fare           1309 non-null float64
Name           1309 non-null object
Parch          1309 non-null int64
PassengerId    1309 non-null int64
Pclass         1309 non-null int64
Sex            1309 non-null object
SibSp          1309 non-null int64
Survived       1309 non-null int64
Ticket         1309 non-null object
Title          1309 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 132.9+ KB<jupyter_text>feature engineearing  Feature : Age<jupyter_code># AgeState based on Age
df_final['AgeState'] = np.where(df_final['Age'] >= 18, 'Adult','Child')
# AgeState Counts
df_final['AgeState'].value_counts()
# crosstab
pd.crosstab(df_final[df_final.Survived != 2].Survived, df_final[df_final.Survived != 2].AgeState)<jupyter_output><empty_output><jupyter_text>familiy size<jupyter_code># Family : Adding Parents with Siblings
df_final['FamilySize'] = df_final.Parch + df_final.SibSp + 1 # 1 for self
pd.crosstab(df_final[df_final.Survived != -2].Survived, df_final[df_final.Survived != -2].FamilySize)<jupyter_output><empty_output><jupyter_text>Feature : IsMother<jupyter_code># a lady aged more thana 18 who has Parch >0 and is married (not Miss)
df_final['IsMother'] = np.where(((df_final.Sex == 'female') & (df_final.Parch > 0) &
                                 (df_final.Age > 18) & (df_final.Title != 'Miss')), 1, 0)
# Crosstab with IsMother
pd.crosstab(df_final[df_final.Survived != 2].Survived, df_final[df_final.Survived != 2].IsMother)
df_final.columns
df_final.head()<jupyter_output><empty_output><jupyter_text>Name, ticket all are unique effects the model may leads to overfitting  passenger id will kept for further reference but not using in modelling<jupyter_code>df_final.drop('Name',axis=1,inplace=True)
df_final.drop('Ticket',axis=1,inplace=True)
df_final.head()
df_final.nunique()
df_final.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1309 entries, 0 to 417
Data columns (total 13 columns):
Age            1309 non-null float64
Embarked       1309 non-null object
Fare           1309 non-null float64
Parch          1309 non-null int64
PassengerId    1309 non-null int64
Pclass         1309 non-null int64
Sex            1309 non-null object
SibSp          1309 non-null int64
Survived       1309 non-null int64
Title          1309 non-null object
AgeState       1309 non-null object
FamilySize     1309 non-null int64
IsMother       1309 non-null int32
dtypes: float64(2), int32(1), int64(6), object(4)
memory usage: 138.1+ KB<jupyter_text>converying datatypepclass: A proxy for socio-economic status (SES)1st = Upper2nd = Middle3rd = Lower<jupyter_code>df_final.Pclass=df_final.Pclass.astype('object')
df_final.Pclass.dtype
df_final.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1309 entries, 0 to 417
Data columns (total 13 columns):
Age            1309 non-null float64
Embarked       1309 non-null object
Fare           1309 non-null float64
Parch          1309 non-null int64
PassengerId    1309 non-null int64
Pclass         1309 non-null object
Sex            1309 non-null object
SibSp          1309 non-null int64
Survived       1309 non-null int64
Title          1309 non-null object
AgeState       1309 non-null object
FamilySize     1309 non-null int64
IsMother       1309 non-null int32
dtypes: float64(2), int32(1), int64(5), object(5)
memory usage: 138.1+ KB<jupyter_text>dummies variable<jupyter_code>df_final = pd.get_dummies(df_final,drop_first=True)
df_final.columns<jupyter_output><empty_output><jupyter_text>spliting train and test<jupyter_code>df1.shape
df2.shape
df_final.shape
train=df_final[df_final.Survived!=2].head(891)
train.shape# because of feature engineering columns increased
test=df_final.drop(['Survived'],axis=1).tail(418)
test.shape# because of feature engineering columns increased
train.nunique()
test.nunique()
train.Survived.value_counts()<jupyter_output><empty_output><jupyter_text>modelling<jupyter_code>from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn import model_selection
from sklearn.metrics import roc_curve, auc
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
Dt_model = DecisionTreeClassifier(criterion = 'entropy',max_depth=4,random_state=0 )
Rf_model = RandomForestClassifier(n_estimators=50,criterion='entropy',max_depth=4,random_state=0 )
Nb_model=GaussianNB()
Lr_model=LogisticRegression()
Kn_model=KNeighborsClassifier()
models = []
models.append(('DecisionTree', Dt_model))
models.append(('RandomForest', Rf_model))
models.append(('GaussianNB', Nb_model))
models.append(('K Neighbors', Kn_model))
models.append(('LogisticRegression', Lr_model))

train.columns
# only droping target variable survived
x=train.loc[:,['Age', 'Fare', 'Parch', 'PassengerId', 'SibSp',
       'FamilySize', 'IsMother', 'Embarked_Q', 'Embarked_S', 'Pclass_2',
       'Pclass_3', 'Sex_male', 'Title_Col', 'Title_Don', 'Title_Dona',
       'Title_Dr', 'Title_Jonkheer', 'Title_Lady', 'Title_Major',
       'Title_Master', 'Title_Miss', 'Title_Mlle', 'Title_Mme', 'Title_Mr',
       'Title_Mrs', 'Title_Ms', 'Title_Rev', 'Title_Sir', 'Title_the Countess',
       'AgeState_Child']]
y=train['Survived']<jupyter_output><empty_output><jupyter_text>Standardising independent variable<jupyter_code>sx = StandardScaler().fit_transform(x)<jupyter_output>C:\Users\pokas\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\data.py:625: DataConversionWarning: Data with input dtype uint8, int32, int64, float64 were all converted to float64 by StandardScaler.
  return self.partial_fit(X, y)
C:\Users\pokas\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\base.py:462: DataConversionWarning: Data with input dtype uint8, int32, int64, float64 were all converted to float64 by StandardScaler.
  return self.fit(X, **fit_params).transform(X)<jupyter_text>kfold validation<jupyter_code># evaluate each model in turn
results = []
names = []
scoring = 'accuracy'
for name, model in models:
	kfold = model_selection.KFold(n_splits=10,random_state=2)
	cv_results = model_selection.cross_val_score(model, x, y, cv=kfold, scoring=scoring)
	results.append(cv_results)
	names.append(name)
	msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
	print(msg)
# boxplot algorithm comparison
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()


# see the below warning error the logistic values
#Random forest is the best from above model<jupyter_output><empty_output><jupyter_text>Modelling after hyperparameter tunning  KNeighborsClassifier hyperparameter tunning<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier 
import numpy as np
params={'n_neighbors':np.arange(1,30)}
knn=KNeighborsClassifier()
GS=GridSearchCV(knn,params,cv=5)
GS.fit(x,y)
k=GS.best_params_
kn1=k['n_neighbors']
print(' best n_neighbors value: ',k)<jupyter_output>best n_neighbors value:  {'n_neighbors': 28}<jupyter_text>DecisionTreeClassifier hyperparameter tunning<jupyter_code>import numpy as np
params={'max_depth':np.arange(1,30)}
DT=DecisionTreeClassifier()
GS=GridSearchCV(DT,params,cv=5)
GS.fit(x,y)
k=GS.best_params_
dt1=k['max_depth']
print(' best max_depth value: ',dt1)<jupyter_output>best max_depth value:  3<jupyter_text>RandomForest Classifier hyperparameter tunning<jupyter_code>params={'max_depth':np.arange(1,30),'n_estimators':np.arange(1,30)}
R=RandomForestClassifier()
GS=GridSearchCV(R,params,cv=5)
GS.fit(x,y)
k=GS.best_params_
k

Dt_model = DecisionTreeClassifier(max_depth=3)
Rf_model = RandomForestClassifier( max_depth=5,n_estimators=15)
Nb_model=GaussianNB()
Lr_model=LogisticRegression()
Kn_model=KNeighborsClassifier(28)

models = []
models.append(('DecisionTree', Dt_model))
models.append(('RandomForest', Rf_model))
models.append(('GaussianNB', Nb_model))
models.append(('K Neighbors', Kn_model))
models.append(('LogisticRegression', Lr_model))

# evaluate each model in turn
results = []
names = []
scoring = 'accuracy'
for name, model in models:
	kfold = model_selection.KFold(n_splits=10,random_state=2)
	cv_results = model_selection.cross_val_score(model, x, y, cv=kfold, scoring=scoring)
	results.append(cv_results)
	names.append(name)
	msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
	print(msg)
# boxplot algorithm comparison
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()


# see the below warning error the logistic values
# after tunning before tunning random forest have more accurate
# but after tunning random forest have low variance error So I will pick this<jupyter_output><empty_output><jupyter_text>Best model is random forest so predicting the values using this model for test data<jupyter_code>Rf_model.fit(x,y)
y_prediction=Rf_model.predict(test)

y_prediction.shape

y_prediction<jupyter_output><empty_output><jupyter_text>Submission file<jupyter_code>mysubmission= pd.DataFrame({'PassengerId':test.PassengerId,'Survived':y_prediction})
mysubmission.head()
mysubmission.to_csv('mysubmission.csv',index=0)<jupyter_output><empty_output><jupyter_text>ITERATION 2<jupyter_code># only droping target variable survived
x=train.loc[:,['Age', 'Fare',
       'FamilySize', 'IsMother', 'Embarked_Q', 'Embarked_S', 'Pclass_2',
       'Pclass_3',
       'AgeState_Child']]
y=train['Survived']
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier 
import numpy as np
params={'n_neighbors':np.arange(1,30)}
knn=KNeighborsClassifier()
GS=GridSearchCV(knn,params,cv=5)
GS.fit(x,y)
k=GS.best_params_
kn1=k['n_neighbors']
print(' best n_neighbors value: ',k)
import numpy as np
params={'max_depth':np.arange(1,30)}
DT=DecisionTreeClassifier()
GS=GridSearchCV(DT,params,cv=5)
GS.fit(x,y)
k=GS.best_params_
dt1=k['max_depth']
print(' best max_depth value: ',dt1)

params={'max_depth':np.arange(1,30),'n_estimators':np.arange(1,30)}
R=RandomForestClassifier()
GS=GridSearchCV(R,params,cv=5)
GS.fit(x,y)
k=GS.best_params_
k

Dt_model = DecisionTreeClassifier(max_depth=4)
Rf_model = RandomForestClassifier( max_depth=6,n_estimators=27)
Nb_model=GaussianNB()
Lr_model=LogisticRegression()
Kn_model=KNeighborsClassifier(22)

models = []
models.append(('DecisionTree', Dt_model))
models.append(('RandomForest', Rf_model))
models.append(('GaussianNB', Nb_model))
models.append(('K Neighbors', Kn_model))
models.append(('LogisticRegression', Lr_model))

# evaluate each model in turn
results = []
names = []
scoring = 'accuracy'
for name, model in models:
	kfold = model_selection.KFold(n_splits=10,random_state=2)
	cv_results = model_selection.cross_val_score(model, x, y, cv=kfold, scoring=scoring)
	results.append(cv_results)
	names.append(name)
	msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
	print(msg)
# boxplot algorithm comparison
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()


# see the below warning error the logistic values

R# there is no increade model so best is above iteration<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1199343234569744, 'fraction_numerical': 0.038137174416001195, 'mean_word_length': 6.048921620199895, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '433035', 'license': 'MIT', 'n_tokens_mistral': 5189, 'n_tokens_neox': 4814, 'n_words': 985, 'path': 'Supervised_Classification/Classification_TitanicProblem/Poka_exam.ipynb', 'repo_name': 'Aujasvi-Moudgil/ML', 'size': '13399'}