wlk = Walker(lattice = ltc, start = start_simulation, dest_threshold = 1)
    
    # Simulate a random walk and record the destination
    destinations.append(wlk.simulateWalker())

  # Use pandas facilities
  table = pd.DataFrame({"DEST": pd.Series(destinations)})
  table["COUNT"] = 1
  
  prob_table = table.groupby(["DEST"], as_index = False).aggregate({"COUNT": "sum"})
  prob_table.COUNT = prob_table.COUNT / np.sum(prob_table.COUNT)
  
  # Store the probabilities in a DestinationGrid object
  posterior = DestinationGrid(N, M)
  
  for idx, row in prob_table.iterrows():
    posterior.setProb(row.DEST, row.COUNT)
    
    
  # Plot the new distribution
  plt.subplot(1,2,1)
  plt.imshow(np.log(posterior.as_array() + trip_to_array(partial_trip.GRID_POLYLINE, N, M)), interpolation = "nearest")
  plt.title("Complete trip superimposed")
  
  plt.subplot(1,2,2)
  plt.imshow(np.log(posterior.as_array() + trip_to_array(partial_trip.TRUNC_GRID_POLYLINE, N, M)), interpolation = "nearest")
  plt.title("Partial trip superimposed")

  
  
  
Beispiel #2
0
        # Simulate a random walk and record the destination
        destinations.append(wlk.simulateWalker())

    # Use pandas facilities
    table = pd.DataFrame({"DEST": pd.Series(destinations)})
    table["COUNT"] = 1

    prob_table = table.groupby(["DEST"],
                               as_index=False).aggregate({"COUNT": "sum"})
    prob_table.COUNT = prob_table.COUNT / np.sum(prob_table.COUNT)

    # Store the probabilities in a DestinationGrid object
    posterior = DestinationGrid(N, M)

    for idx, row in prob_table.iterrows():
        posterior.setProb(row.DEST, row.COUNT)

    # Plot the new distribution
    plt.subplot(1, 2, 1)
    plt.imshow(np.log(posterior.as_array() +
                      trip_to_array(partial_trip.GRID_POLYLINE, N, M)),
               interpolation="nearest")
    plt.title("Complete trip superimposed")

    plt.subplot(1, 2, 2)
    plt.imshow(np.log(posterior.as_array() +
                      trip_to_array(partial_trip.TRUNC_GRID_POLYLINE, N, M)),
               interpolation="nearest")
    plt.title("Partial trip superimposed")
    partial_trip = test.loc[0]

    # Select the last segment of the partial trip
    end = partial_trip.TRUNC_GRID_POLYLINE[-k:]

    # Create a DestinationGrid object
    grid = DestinationGrid(N, M)

    # Match this segment with trips in the training set
    for i, chunk in enumerate(trips):
        for idx, row in chunk.iterrows():
            if match(end, row.GRID_POLYLINE, k):
                dest = row.GRID_POLYLINE[-1]
                grid._table[dest] += 1

        print "Processed chunk %d" % i

    grid.normalizeProbs()

    # Plot the new distribution
    plt.subplot(1, 2, 1)
    plt.imshow(np.log(grid.as_array() +
                      trip_to_array(partial_trip.GRID_POLYLINE, N, M)),
               interpolation="nearest")
    plt.title("Complete trip superimposed")

    plt.subplot(1, 2, 2)
    plt.imshow(np.log(grid.as_array() +
                      trip_to_array(partial_trip.TRUNC_GRID_POLYLINE, N, M)),
               interpolation="nearest")
    plt.title("Partial trip superimposed")