Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. import numpy as np
  2. from sklearn import preprocessing
  3. import matplotlib.pyplot as plt
  4. import matplotlib.image as mpimg
  5. import scipy.ndimage.interpolation as compress
  6. from sklearn import linear_model
  7. import pandas as pd
  8. from sklearn.feature_selection import SelectKBest
  9. from sklearn.feature_selection import chi2
  10.  
  11. X_cropped = np.load('/home/evrova/MLTask1/ml-project/data/X_cropped.npy')
  12. y_train = np.genfromtxt('/home/evrova/MLTask1/ml-project/data/y_1.csv')
  13.  
  14. print("Loading done")
  15.  
  16. NO_SAMPLES = X_cropped.shape[0]
  17. #SLICES_AXE_SIZE = 176
  18.  
  19. X_train_3D = np.reshape(X_cropped, (-1, 136, 170, 144))
  20.  
  21. #We need an array of indexes of the non-zero pixels to see which one is the maximum
  22. # non_zero_indexes = np.empty
  23. # maximal_values = np.empty((1, 1))
  24.  
  25.  
  26. #print(type(X_cropped))
  27. # min_x = 176
  28. # max_x = 0
  29. # min_y = 208
  30. # max_y = 0
  31. # min_z = 176
  32. # max_z = 0
  33. #
  34. #
  35. # for i in range(0, NO_SAMPLES):
  36. #     for z in range(0, SLICES_AXE_SIZE):
  37. #         for x in range(0, 176):
  38. #             non_zero_indexes_y = np.argwhere(X_train_3D[i,x,:,z])
  39. #             if non_zero_indexes_y.size > 0:
  40. #                 min_y = min(min_y, np.amin(non_zero_indexes_y))
  41. #                 max_y = max(max_y, np.amax(non_zero_indexes_y))
  42. #
  43. #
  44. # for i in range(0, NO_SAMPLES):
  45. #     for z in range(0, SLICES_AXE_SIZE):
  46. #         for y in range(0, 208):
  47. #             non_zero_indexes_x = np.argwhere(X_train_3D[i,:,y,z])
  48. #             if non_zero_indexes_x.size > 0:
  49. #                 min_x = min(min_x, np.amin(non_zero_indexes_x))
  50. #                 max_x = max(max_x, np.amax(non_zero_indexes_x))
  51. #
  52. # for i in range(0, NO_SAMPLES):
  53. #     for x in range(0, 176):
  54. #         for y in range(0, 208):
  55. #             non_zero_indexes_z = np.argwhere(X_train_3D[i,x,y,:])
  56. #             if non_zero_indexes_z.size > 0:
  57. #                 min_z = min(min_z, np.amin(non_zero_indexes_z))
  58. #                max_z = max(max_z, np.amax(non_zero_indexes_z))
  59. NO_PATCHES = 10000
  60. PATCH_SIZE = 5
  61.  
  62. X_test = np.load('/home/evrova/MLTask1/ml-project/data/X_test.npy')
  63. X_test_3D = np.reshape(X_test, (-1, 176, 208, 176))
  64. X_test_cropped = X_test_3D[:, 18:154, 18:188, 7:151]
  65.  
  66. averages = np.zeros((NO_SAMPLES, NO_PATCHES))
  67. averages_test = np.zeros((X_test_cropped.shape[0], NO_PATCHES))
  68. k = 0
  69. temp_average = 1
  70.  
  71. for r in range(0, NO_SAMPLES):
  72.     np.random.seed(10)  # TO CHECK
  73.     k = 0
  74.     while(k<NO_PATCHES):
  75.         distr_x = np.random.randint(low=5, high=X_cropped.shape[1]-5)
  76.         distr_y = np.random.randint(low=5, high=X_cropped.shape[2]-5)
  77.         distr_z = np.random.randint(low=5, high=X_cropped.shape[3]-5)
  78.  
  79.  
  80.         temp_average = np.average(X_cropped[r,distr_x:distr_x+PATCH_SIZE-1,distr_y:distr_y+PATCH_SIZE-1,distr_z:distr_z+PATCH_SIZE-1])
  81.         if r < X_test_cropped.shape[0]:
  82.             temp_average_test = np.average(X_test_cropped[r, distr_x:distr_x + PATCH_SIZE - 1, distr_y:distr_y + PATCH_SIZE - 1, distr_z:distr_z + PATCH_SIZE - 1])
  83.  
  84.         if temp_average > 1:
  85.             averages[r, k] = temp_average
  86.             if r < X_test_cropped.shape[0]:
  87.                 averages_test[r,k] = temp_average_test
  88.             k = k + 1
  89.  
  90. slct = SelectKBest(k=5000)
  91. X_new = slct.fit_transform(averages, y_train)
  92. X_test_new = slct.transform(averages_test)
  93. #
  94. # NO_BINS = np.sqrt(X_new.shape[1])
  95. # hist, bins = np.histogram(X_new, NO_BINS)
  96. #
  97. # width = 0.7 * (bins[1] - bins[0])
  98. # center = (bins[:-1] + bins[1:]) / 2
  99. # plt.bar(center, hist, align='center', width=width)
  100. # plt.show()
  101.  
  102.  
  103. print("Before fitting")
  104.  
  105. model = linear_model.LassoLarsCV(cv=5)
  106. model.fit(X_new, y_train)
  107.  
  108. print("After fitting")
  109.  
  110.  
  111. err = model.score(X_new, y_train)
  112.  
  113. err1 = model.predict(X_new)
  114.  
  115. error = np.sum((y_train - err1)**2)
  116.  
  117.  
  118. print("Score: ", err)
  119. print("Our score: ", error)
  120.  
  121.  
  122.  
  123. y_test = model.predict(X_test_new)
  124.  
  125. y_test = pd.DataFrame(y_test)
  126. y_test.to_csv('test.csv', sep=',')
  127.  
  128.  
  129. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement