質問

I have discrete-time data of airline ticket availability in csv format. This is used to represent ticket availability for a combination of departure and arrival time windows. Say my day is divided into 4 time periods -

12:01 AM to 6:00 AM, 
6:01 AM to 12:00 PM, 
12:01 PM to 6:00 PM,
6:01 PM to 12:00 AM

1 implies that the there are tickets available for that combination of departure and arrival, 0 otherwise. For this example, lets say that the ticket is available for all departure-arrival combinations, the csv file will have the following data:

1,1,1,1,1,1,1,1,1,1

This data is used to represent this matrix (please note that some of the combinations become zero here as they are illogical time combinations for a 24-hour period):

                    Departure time period           
                    12:01 AM to 6:00 AM | 6:01 AM to 12:00 PM  | 12:01 PM to 6:00 PM |  6:01 PM to 12:00 AM|
Arrival time period ------------------- | ---------------------|---------------------|---------------------|
12:01 AM to 6:00 AM                   1 |                     0|                    0|                    0|
6:01 AM to 12:00 PM                   1 |                     1|                    0|                    0|
12:01 PM to 6:00 PM                   1 |                     1|                    1|                    0|
6:01 PM to 12:00 AM                   1 |                     1|                    1|                    1|

The csv file has this data for multiple days. I have read in this data as a dictionary with the date being the key and the availability combinations as a list. The data processing is being done in Python 2.7. For a particular day I am now able to retrieve the list of availability by using the date key.

Now, I have 2 questions:

  • How can I convert the data into the matrix type data structure. In essence this involves converting the list into a lower triangular matrix plus the diagonal elements. I have tried using the reshape function in numpy but that is not achieving this result.

  • Once I have converted the matrix - I want to pictorially represent availability as a thematic grid - with all 1s as green squares and 0s as red squares. Is this achievable in Python? How?

I assumed that reading in the csv as a dictionary and then storing the availability elements in a list is the way to go as it seemed fairly straightforward. Modify the approach if you feel there are more clever ways to do this.

Any thoughts folks?!?

役に立ちましたか?

解決

import numpy as np
import matplotlib.pyplot as plt

data = [1,1,1,1,1,1,1,1,1,1]
arr = np.zeros((4,4))
indices = np.tril_indices(4)
arr[indices] = data
print(arr)

# array([[ 1.,  0.,  0.,  0.],
#        [ 1.,  1.,  0.,  0.],
#        [ 1.,  1.,  1.,  0.],
#        [ 1.,  1.,  1.,  1.]])


plt.imshow(arr, interpolation='nearest', cmap=plt.get_cmap('RdYlGn'))
plt.show()

plots

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top