Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import matplotlib.pyplot as plt
- from sklearn import svm
- import numpy as np
- from sklearn.preprocessing import StandardScaler
- from sklearn.neural_network import MLPClassifier
- from sklearn.metrics import classification_report,confusion_matrix
- data =pd.read_csv( 'titanic_800.csv' , sep = ',' , header = 0,
- usecols=['Survived','Sex','Pclass','Age','SibSp','Parch','Fare','Embarked'])
- yvalues = pd.DataFrame( dict (Survived =[]), dtype = int )
- yvalues["Survived"] = data["Survived"].copy()
- X = data.drop( 'Survived' , axis = 1 , inplace = True )
- data["Embarked"] = data["Embarked"].replace('C',0)
- data["Embarked"] = data["Embarked"].replace('S',1)
- data["Embarked"] = data["Embarked"].replace('Q',2)
- data["Sex"] = data["Sex"].replace('male',0)
- data["Sex"] = data["Sex"].replace('female',1)
- x = data["Age"]
- y = data["Pclass"]
- a = data["SibSp"]
- b = data["Parch"]
- c = data["Fare"]
- d = data["Embarked"]
- e = data["Sex"]
- plt.figure()
- plt.scatter(x.values, yvalues.values, color = 'black' , s = 30 )
- plt.show()
- plt.figure()
- plt.scatter(y.values, yvalues.values, color = 'green' , s = 30 )
- plt.show()
- plt.figure()
- plt.scatter(a.values, yvalues.values, color = 'red' , s = 30 )
- plt.show()
- plt.figure()
- plt.scatter(b.values, yvalues.values, color = 'black' , s = 30 )
- plt.show()
- plt.figure()
- plt.scatter(c.values, yvalues.values, color = 'green' , s = 30 )
- plt.show()
- plt.figure()
- plt.scatter(d.values, yvalues.values, color = 'red' , s = 30 )
- plt.show()
- plt.figure()
- plt.scatter(e.values, yvalues.values, color = 'black' , s = 30 )
- plt.show()
- newdata = data.fillna(0.0)
- print(str(newdata))
- xtrain = newdata.head(700)
- ytrain = yvalues.head(700)
- xtest = newdata.tail(100)
- ytest = yvalues.tail(100)
- scaler = StandardScaler().fit(xtrain)
- xtrain = scaler.transform(xtrain)
- xtest= scaler.transform(xtest)
- mlp = MLPClassifier(hidden_layer_sizes= (20,17,13),max_iter = 1000,random_state = 0, alpha = 0.1)
- mlp.fit(xtrain,ytrain.values.ravel())
- predictions = mlp.predict(xtest)
- matrix = confusion_matrix(ytest,predictions)
- print ('Confusion Matrix for MLP classifier')
- print (matrix)
- print()
- print()
- print('Classification report for MLP classifier')
- print(classification_report(ytest,predictions))
- clf = svm.SVC(C=1.0, kernel='poly', gamma = 0.1, random_state=None)
- clf.fit(xtrain, np.ravel(ytrain))
- predictions1 = clf.predict(xtest)
- matrix1 = confusion_matrix(ytest,predictions1)
- print ('Confusion Matrix for SVM')
- print(matrix1)
- print()
- print('Classification report for SVM')
- print(classification_report(ytest,predictions1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement