def test_bin_dataII(self): ###### # Same as above, extra points OUTSIDE THE GRID ###### # define a small 2x3 grid gx = 0.0 gy = 0.0 gwidx = 1.0 gwidy = 1.0 gnumx = 3 gnumy = 2 # define points # First point as above lon = [0.5] lat = [1.5] # but add extra points at all external edges and quadrants # # * * * # +---+---+---+ # | | | | # * +---+---+---+ * # | | | | # +---+---+---+ # # * * * # lon.extend([-0.5, -0.5, -0.5, 1.5, 1.5, 3.5, 3.5, 3.5]) lat.extend([1.0, 2.5, -0.5, 2.5, -0.5, 2.5, 1.0, -0.5]) # expected result for above data expected_res = [ [{'index': [], 'mid_lat_lon': scipy.array([0.5, 0.5])}, {'index': [0], 'mid_lat_lon': scipy.array([1.5, 0.5])}], [{'index': [], 'mid_lat_lon': scipy.array([0.5, 1.5])}, {'index': [], 'mid_lat_lon': scipy.array([1.5, 1.5])}], [{'index': [], 'mid_lat_lon': scipy.array([0.5, 2.5])}, {'index': [], 'mid_lat_lon': scipy.array([1.5, 2.5])}] ] res = util.bin_data(lat, lon, gx, gy, gwidx, gwidy, gnumx, gnumy) for expected, actual in map(None, expected_res, res): for expected_dic, act_dic in map(None, expected, actual): self.failUnlessEqual(expected_dic['index'], act_dic['index']) self.failUnless(scipy.allclose(expected_dic['mid_lat_lon'], act_dic['mid_lat_lon']))
def test_bin_data(self): ###### # First test is small grid, all points inside, one cell empty ###### # define a small 2x3 grid gx = 0.0 gy = 0.0 gwidx = 1.0 gwidy = 1.0 gnumx = 3 gnumy = 2 # define points # ^Y # 2.0 +-----+-----+-----+ # | 0 | 1 | 5| # | | | 4 | # | | 2 |3 | # 1.0 +-----+-----+-----+ # | 6 7 |10 | | # | | | | # | 8 9 | | | # 0.0 +-----+-----+-----+->X # 0.0 1.0 2.0 3.0 # # So expected python array (actually, lists of lists) will be: # [[[6,7,8,9],[0]], [[10,11,12,13,14], ...]] lon = [0.5, 1.3, 1.7, 2.3, 2.5, 2.7, 0.3, 0.7, 0.3, 0.7, 1.3] lat = [1.5, 1.7, 1.3, 1.3, 1.5, 1.7, 0.7, 0.7, 0.3, 0.3, 0.7] # expected result for above data expected_res = [[{'index': [6, 7, 8, 9], 'mid_lat_lon': (0.5, 0.5)}, {'index': [0], 'mid_lat_lon': (1.5, 0.5)}], [{'index': [10], 'mid_lat_lon': (0.5, 1.5)}, {'index': [1, 2], 'mid_lat_lon': (1.5, 1.5)}], [{'index': [], 'mid_lat_lon': (0.5, 2.5)}, {'index': [3, 4, 5], 'mid_lat_lon': (1.5, 2.5)}]] res = util.bin_data(lat, lon, gx, gy, gwidx, gwidy, gnumx, gnumy) for expected, actual in map(None, expected_res, res): for expected_dic, act_dic in map(None, expected, actual): self.failUnlessEqual(expected_dic['index'], act_dic['index']) self.failUnless(scipy.allclose( scipy.array(expected_dic['mid_lat_lon']), act_dic['mid_lat_lon']))
def predict(self, data): results = [] binned_data = bin_data(data, self.categories) for point in binned_data: results.append(self._traverse_tree(self.tree, point)) return results
def fit(self, data, target): # Check if we need to bin data here? binned_data = bin_data(data, self.categories) # Make a copy of data and target here, then pass to create tree self._create_tree(binned_data, target) print("Tree: {}".format(self.tree))
def test_bin_dataII(self): ###### # Same as above, extra points OUTSIDE THE GRID ###### # define a small 2x3 grid gx = 0.0 gy = 0.0 gwidx = 1.0 gwidy = 1.0 gnumx = 3 gnumy = 2 # define points # First point as above lon = [0.5] lat = [1.5] # but add extra points at all external edges and quadrants # # * * * # +---+---+---+ # | | | | # * +---+---+---+ * # | | | | # +---+---+---+ # # * * * # lon.extend([-0.5, -0.5, -0.5, 1.5, 1.5, 3.5, 3.5, 3.5]) lat.extend([1.0, 2.5, -0.5, 2.5, -0.5, 2.5, 1.0, -0.5]) # expected result for above data expected_res = [[{ 'index': [], 'mid_lat_lon': scipy.array([0.5, 0.5]) }, { 'index': [0], 'mid_lat_lon': scipy.array([1.5, 0.5]) }], [{ 'index': [], 'mid_lat_lon': scipy.array([0.5, 1.5]) }, { 'index': [], 'mid_lat_lon': scipy.array([1.5, 1.5]) }], [{ 'index': [], 'mid_lat_lon': scipy.array([0.5, 2.5]) }, { 'index': [], 'mid_lat_lon': scipy.array([1.5, 2.5]) }]] res = util.bin_data(lat, lon, gx, gy, gwidx, gwidy, gnumx, gnumy) for expected, actual in map(None, expected_res, res): for expected_dic, act_dic in map(None, expected, actual): self.failUnlessEqual(expected_dic['index'], act_dic['index']) self.failUnless( scipy.allclose(expected_dic['mid_lat_lon'], act_dic['mid_lat_lon']))
def test_bin_data(self): ###### # First test is small grid, all points inside, one cell empty ###### # define a small 2x3 grid gx = 0.0 gy = 0.0 gwidx = 1.0 gwidy = 1.0 gnumx = 3 gnumy = 2 # define points # ^Y # 2.0 +-----+-----+-----+ # | 0 | 1 | 5| # | | | 4 | # | | 2 |3 | # 1.0 +-----+-----+-----+ # | 6 7 |10 | | # | | | | # | 8 9 | | | # 0.0 +-----+-----+-----+->X # 0.0 1.0 2.0 3.0 # # So expected python array (actually, lists of lists) will be: # [[[6,7,8,9],[0]], [[10,11,12,13,14], ...]] lon = [0.5, 1.3, 1.7, 2.3, 2.5, 2.7, 0.3, 0.7, 0.3, 0.7, 1.3] lat = [1.5, 1.7, 1.3, 1.3, 1.5, 1.7, 0.7, 0.7, 0.3, 0.3, 0.7] # expected result for above data expected_res = [[{ 'index': [6, 7, 8, 9], 'mid_lat_lon': (0.5, 0.5) }, { 'index': [0], 'mid_lat_lon': (1.5, 0.5) }], [{ 'index': [10], 'mid_lat_lon': (0.5, 1.5) }, { 'index': [1, 2], 'mid_lat_lon': (1.5, 1.5) }], [{ 'index': [], 'mid_lat_lon': (0.5, 2.5) }, { 'index': [3, 4, 5], 'mid_lat_lon': (1.5, 2.5) }]] res = util.bin_data(lat, lon, gx, gy, gwidx, gwidy, gnumx, gnumy) for expected, actual in map(None, expected_res, res): for expected_dic, act_dic in map(None, expected, actual): self.failUnlessEqual(expected_dic['index'], act_dic['index']) self.failUnless( scipy.allclose(scipy.array(expected_dic['mid_lat_lon']), act_dic['mid_lat_lon']))