Python: populate the bottom matrix of a triangle from a list

I have discrete data on the availability of air tickets in csv format. This is used to represent ticket availability for a combination of departure windows and arrival times. 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 the availability of tickets for this combination of departure and arrival, 0 otherwise. In this example, we can say that the ticket is available for all combinations of arrival and arrival, 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 (note that some of the combinations here become zero because 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 several days. I read this data as a dictionary with a date being a key and accessibility combinations as a list. Data processing is performed in Python 2.7 . On a certain day, I can get an availability list using a date key.

Now I have 2 questions:

  • How to convert data to a data type matrix. Essentially, this involves transforming the list into a lower triangular matrix plus diagonal elements. I tried using change formula in numpy , but this does not achieve this result.

  • As soon as I transform the matrix - I want to visualize accessibility as a thematic grid - with all 1s in the form of green squares and 0s as red squares. Is this possible in Python? How?

I suggested that reading in csv as a dictionary and then storing accessibility elements in a list is a way that seems pretty straightforward. Change the approach if you feel that there are smarter ways to do this.

Any thoughts of people?!?

+4
source share
1 answer
 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

+1
source

Source: https://habr.com/ru/post/1490191/


All Articles