Advertisement
Guest User

Untitled

a guest
May 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from sklearn import svm
  4. import numpy as np
  5. from sklearn.preprocessing import StandardScaler
  6. from sklearn.neural_network import MLPClassifier
  7. from sklearn.metrics import classification_report,confusion_matrix
  8.  
  9. data =pd.read_csv( 'titanic_800.csv' , sep = ',' , header = 0,
  10. usecols=['Survived','Sex','Pclass','Age','SibSp','Parch','Fare','Embarked'])
  11. yvalues = pd.DataFrame( dict (Survived =[]), dtype = int )
  12.  
  13. yvalues["Survived"] = data["Survived"].copy()
  14. X = data.drop( 'Survived' , axis = 1 , inplace = True )
  15.  
  16.  
  17. data["Embarked"] = data["Embarked"].replace('C',0)
  18. data["Embarked"] = data["Embarked"].replace('S',1)
  19. data["Embarked"] = data["Embarked"].replace('Q',2)
  20.  
  21. data["Sex"] = data["Sex"].replace('male',0)
  22. data["Sex"] = data["Sex"].replace('female',1)
  23.  
  24. x = data["Age"]
  25. y = data["Pclass"]
  26. a = data["SibSp"]
  27. b = data["Parch"]
  28. c = data["Fare"]
  29. d = data["Embarked"]
  30. e = data["Sex"]
  31.  
  32.  
  33.  
  34. plt.figure()
  35. plt.scatter(x.values, yvalues.values, color = 'black' , s = 30 )
  36. plt.show()
  37.  
  38. plt.figure()
  39. plt.scatter(y.values, yvalues.values, color = 'green' , s = 30 )
  40. plt.show()
  41.  
  42. plt.figure()
  43. plt.scatter(a.values, yvalues.values, color = 'red' , s = 30 )
  44. plt.show()
  45.  
  46. plt.figure()
  47. plt.scatter(b.values, yvalues.values, color = 'black' , s = 30 )
  48. plt.show()
  49.  
  50. plt.figure()
  51. plt.scatter(c.values, yvalues.values, color = 'green' , s = 30 )
  52. plt.show()
  53.  
  54. plt.figure()
  55. plt.scatter(d.values, yvalues.values, color = 'red' , s = 30 )
  56. plt.show()
  57.  
  58. plt.figure()
  59. plt.scatter(e.values, yvalues.values, color = 'black' , s = 30 )
  60. plt.show()
  61.  
  62.  
  63.  
  64. newdata = data.fillna(0.0)
  65. print(str(newdata))
  66. xtrain = newdata.head(700)
  67. ytrain = yvalues.head(700)
  68. xtest = newdata.tail(100)
  69. ytest = yvalues.tail(100)
  70.  
  71.  
  72. scaler = StandardScaler().fit(xtrain)
  73. xtrain = scaler.transform(xtrain)
  74. xtest= scaler.transform(xtest)
  75.  
  76. mlp = MLPClassifier(hidden_layer_sizes= (20,17,13),max_iter = 1000,random_state = 0, alpha = 0.1)
  77. mlp.fit(xtrain,ytrain.values.ravel())
  78. predictions = mlp.predict(xtest)
  79.  
  80. matrix = confusion_matrix(ytest,predictions)
  81. print ('Confusion Matrix for MLP classifier')
  82. print (matrix)
  83. print()
  84.  
  85. print()
  86. print('Classification report for MLP classifier')
  87. print(classification_report(ytest,predictions))
  88.  
  89.  
  90. clf = svm.SVC(C=1.0, kernel='poly', gamma = 0.1, random_state=None)
  91. clf.fit(xtrain, np.ravel(ytrain))
  92.  
  93. predictions1 = clf.predict(xtest)
  94. matrix1 = confusion_matrix(ytest,predictions1)
  95. print ('Confusion Matrix for SVM')
  96. print(matrix1)
  97. print()
  98. print('Classification report for SVM')
  99. print(classification_report(ytest,predictions1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement