Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import sklearn
  2.  
  3. file = open('train.txt', 'r')
  4. text = file.read()
  5. result = []
  6. for line in text.splitlines():
  7. result.append(list(line.split()))
  8.  
  9. X=[]
  10. Y=[]
  11. for value in result:
  12. X.append(list(map(float, value[:176])))
  13. Y.append(value[176])
  14.  
  15. Y = [float(y) for y in Y]
  16.  
  17. Z=[]
  18. testFile=open('dev_no_label.txt')
  19. text = testFile.read()
  20. result=[]
  21. for line in text.splitlines():
  22. result.append(list(line.split()))
  23. for value in result:
  24. Z.append(list(map(float, value)))
  25.  
  26.  
  27. def save(predictions):
  28. outFile = open('predictions.txt', 'w')
  29. for y in predictions:
  30. print(int(y), file=outFile)
  31. outFile.close()
  32.  
  33.  
  34. from sklearn import svm, cross_validation, tree, linear_model
  35. X_train,X_test,y_train,y_test=cross_validation.train_test_split(X,Y,test_size=0.4, random_state=0)
  36.  
  37. from sklearn.cross_validation import train_test_split
  38. from sklearn.preprocessing import StandardScaler
  39. from sklearn.datasets import make_moons, make_circles, make_classification
  40. from sklearn.neighbors import KNeighborsClassifier
  41. from sklearn.svm import SVC
  42. from sklearn.tree import DecisionTreeClassifier
  43. from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
  44. from sklearn.naive_bayes import GaussianNB
  45. from sklearn.lda import LDA
  46. from sklearn.qda import QDA
  47. from sklearn.pipeline import Pipeline
  48. from sklearn.svm import LinearSVC
  49.  
  50. def tryAll():
  51. classifiers = [
  52. KNeighborsClassifier(3),
  53. DecisionTreeClassifier(max_depth=5),
  54. RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
  55. AdaBoostClassifier(),
  56. GaussianNB(),
  57. LDA(),
  58. QDA()]
  59. for clf in classifiers:
  60. clf=clf.fit(X_train, y_train)
  61. print(clf.score(X_test,y_test), clf)
  62.  
  63. clf=Pipeline([('feature_selection', LinearSVC(penalty='l1', dual=False)),('classification', RandomForestClassifier())])
  64. clf.fit(X_train,y_train)
  65. print(clf.score(X_test,y_test), clf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement