df1 = df.ix[:,0:8]
df1.tail()  # 박사님께서 설명을 위해 뒷부분에 컬럼을 채워놓은 것같아서 who 부터 끝까지 잘랐습니다

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df1['sex']= le.fit_transform(df1['sex'])
df1['embarked'] = le.fit_transform(df1['embarked'])

from sklearn.preprocessing import Imputer
imp = Imputer(missing_values='NaN', strategy = 'median', axis = 0)
df2 = imp.fit_transform(df1)
df2

df3 = pd.DataFrame(df2, columns = ['survived', 'pclass','sex','age','sibsp','parch','fare','embarked'])
df3.tail()

df3.info()

y = df3.ix[:,0]
x = df3.ix[:,1:]

"""
Explanation: column explanation

survived : 생존여부
pclass : 1,2,3등석
sex : 성별
age : 나이
sibsb : 같이 탑승한 형제, 배우자수
parch : 같이 탑승한 부모, 자녀의수
fare : 요금
embarked : 탑승한곳
End of explanation
"""


from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.33, random_state= 42 )

"""
Explanation: train_test split
End of explanation
"""


from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
qda = QuadraticDiscriminantAnalysis(store_covariances = True).fit(x,y)

qda.priors_

549.0/(549 + 342)

qda.means_

qda.covariances_

qda_score_train = qda.score(x_train,y_train)
print "Train Score :", qda_score_train
print "=" * 40
qda_score_test = qda.score(x_test, y_test)
print "Test Score :", qda_score_test

"""
Explanation: QDA
End of explanation
"""


from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
LDA = LinearDiscriminantAnalysis(n_components = 2, solver = 'svd', store_covariance = True).fit(x,y)

"""
Explanation: LDA
End of explanation
"""


LDA.coef_

LDA.intercept_

LDA.covariance_

LDA.means_

LDA.n_components

LDA.explained_variance_ratio_ 

LDA_score_train = LDA.score(x_train,y_train)
print "Train Score :", LDA_score_train
print "=" * 40
LDA_score_test = LDA.score(x_test, y_test)
print "Test Score :", LDA_score_test

"""
Explanation: solver 
svd : Singular value decomposition (default). Does not compute the covariance matrix, therefore this solver is recommended for data with a large number of features.
lsqr : Least squares solution, can be combined with shrinkage.
eigen : Eigenvalue decomposition, can be combined with shrinkage
End of explanation
"""


from sklearn.naive_bayes import GaussianNB
NB = GaussianNB().fit(x,y)

NB.classes_

NB.class_count_

NB.class_prior_

NB.theta_

NB_score_train = NB.score(x_train,y_train)
print "Train Score :", NB_score_train
print "=" * 40
NB_score_test = NB.score(x_test, y_test)
print "Test Score :", NB_score_test

"""
Explanation: NB
End of explanation
"""


from sklearn.naive_bayes import MultinomialNB
MNB = MultinomialNB().fit(x,y)

MNB.classes_

MNB.class_count_

MNB_score_train = MNB.score(x_train,y_train)
print "Train Score :", MNB_score_train
print "=" * 40
MNB_score_test = MNB.score(x_test, y_test)
print "Test Score :", MNB_score_test

from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report
clf_1 = Pipeline([
    ('clf', MultinomialNB())
])

clf_1.fit(x_train, y_train)
print(classification_report(y_test, clf_1.predict(x_test), digits = 4))

"""
Explanation: BernoulliNB 쓸수 없음 => 타겟변수뿐만아니라 독립변수도 0또는 1값을 가져야 하기때문
Multinomial NB
End of explanation
"""


from sklearn.tree import DecisionTreeClassifier
tree1 = DecisionTreeClassifier(criterion = 'entropy', max_depth = 1, random_state = 0).fit(x,y)

from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(criterion='entropy', max_depth=3, min_samples_leaf=5).fit(x_train, y_train)

from sklearn import tree
decision = tree.DecisionTreeClassifier()
decision.fit(x_train, y_train)
decision_score_train = decision.score(x_train, y_train)
print "Train score : ",decision_score_train
decision_score_test = decision.score(x_test, y_test)
print "-" * 40
print "Test score : ",decision_score_test

"""
Explanation: Decision Tree
End of explanation
"""


from sklearn.linear_model import Perceptron
perceptron = Perceptron(n_iter = 1, eta0 = 0.3, random_state= 1).fit(x,y)
perceptron_score_train = perceptron.score(x_train, y_train)
print "Train score : ", perceptron_score_train
print "=" *40
perceptron_score_test = perceptron.score(x_test, y_test)
print "Test score : ", perceptron_score_test

perceptron = Perceptron(n_iter = 5, eta0 = 0.3, random_state= 1).fit(x,y)
perceptron_score_train = perceptron.score(x_train, y_train)
print "Train score : ", perceptron_score_train
print "=" *40
perceptron_score_test = perceptron.score(x_test, y_test)
print "Test score : ", perceptron_score_test

perceptron = Perceptron(n_iter = 10, eta0 = 0.3, random_state= 1).fit(x,y)
perceptron_score_train = perceptron.score(x_train, y_train)
print "Train score : ", perceptron_score_train
print "=" *40
perceptron_score_test = perceptron.score(x_test, y_test)
print "Test score : ", perceptron_score_test

perceptron = Perceptron(n_iter = 20, eta0 = 0.3, random_state= 1).fit(x,y)
perceptron_score_train = perceptron.score(x_train, y_train)
print "Train score : ", perceptron_score_train
print "=" *40
perceptron_score_test = perceptron.score(x_test, y_test)
print "Test score : ", perceptron_score_test

perceptron = Perceptron(n_iter = 100, eta0 = 0.3, random_state= 1).fit(x,y)
perceptron_score_train = perceptron.score(x_train, y_train)
print "Train score : ", perceptron_score_train
print "=" *40
perceptron_score_test = perceptron.score(x_test, y_test)
print "Test score : ", perceptron_score_test

perceptron = Perceptron(n_iter = 150, eta0 = 0.3, random_state= 1).fit(x,y)
perceptron_score_train = perceptron.score(x_train, y_train)
print "Train score : ", perceptron_score_train
print "=" *40
perceptron_score_test = perceptron.score(x_test, y_test)
print "Test score : ", perceptron_score_test

"""
Explanation: Percetron

iter에 따라 달라지는 것
End of explanation
"""


from sklearn.svm import SVC
svm = SVC() # defalt 값은 rbf 이다
svm.fit(x_train, y_train)
svm_score_train = svm.score(x_train, y_train)
print "Train Score : ", svm_score_train
svm_score_test = svm.score(x_test, y_test)
print "-" * 40
print "Test Score : ", svm_score_test

from sklearn.svm import SVC
svm = SVC(kernel = 'linear')
svm.fit(x_train, y_train)
svm_score_train = svm.score(x_train, y_train)
print "Train Score : ", svm_score_train
svm_score_test = svm.score(x_test, y_test)
print "-" * 40
print "Test Score : ", svm_score_test

from sklearn.svm import SVC
svm = SVC(kernel = 'sigmoid')
svm.fit(x_train, y_train)
svm_score_train = svm.score(x_train, y_train)
print "Train Score : ", svm_score_train
svm_score_test = svm.score(x_test, y_test)
print "-" * 40
print "Test Score : ", svm_score_test

from sklearn.svm import SVC
svm = SVC(kernel = 'poly')
svm.fit(x_train, y_train)
svm_score_train = svm.score(x_train, y_train)
print "Train Score : ", svm_score_train
svm_score_test = svm.score(x_test, y_test)
print "-" * 40
print "Test Score : ", svm_score_test

"""
Explanation: SVM
End of explanation
"""


models = pd.DataFrame({
        'Model'          : ['QDA', 'LDA', 'GausianNB','MultinomialNB', 'DecisionTree','SVM'],
        'Train_Score' : [qda_score_train, LDA_score_train, NB_score_train, MNB_score_train, decision_score_train, svm_score_train],
        'Test_Score'  : [qda_score_test, LDA_score_test, NB_score_test, MNB_score_test, decision_score_test, svm_score_test]
    })
models.sort_values(by='Test_Score', ascending=True)

"""
Explanation: model comparing
End of explanation
"""


