concept descriptive statistic in category dask

appears as: descriptive statistics, descriptive statistics, The descriptive statistics
Data Science with Python and Dask

This is an excerpt from Manning's book Data Science with Python and Dask.

Figure 1.6 represents the full directed acyclic graph for the complete recipe. As you can see, the graph fully represents the process from start to finish. You can start at any of the red nodes (medium gray in the print version of this book) since they do not have dependencies, and you will eventually reach the terminal node labeled “Buon appetito!” While looking at this graph, it might be easy to spot some bottlenecks, and potentially reorder some nodes to produce a more optimal or time-efficient way of preparing the dish. For instance, if the pasta water takes 20 minutes to come to a rolling boil, perhaps you could draw a graph with a single starting node of putting the water on to boil. Then you wouldn’t have to wait for the water to heat up after already preparing the rest of the dish. These are great examples of optimizations that either an intelligent task scheduler or you, the designer of the workload, may come up with. And now that you have the foundational understanding of how directed acyclic graphs work, you should be able to read and understand any arbitrary graph—from cooking pasta to calculating descriptive statistics on a big data set. Next, we’ll look at why DAGs are so useful for scalable computing.

You’ve undoubtedly heard some of these terms before, because these concepts are typically taught as the foundation of any basic statistics course. These descriptive statistics, while simple, are very powerful ways to describe all kinds of data and tell us important things about our data. As a refresher, here’s a visual guide to descriptive statistics.

Figure 6.2 A visual guide to descriptive statistics

c06_02.eps

Figure 6.2 shows a histogram of a hypothetical variable in which 100,000 observations were made. The values that were observed are along the X axis, and the relative frequency (how often each value was observed as a percent of all observations) is plotted along the Y axis. What you can take away from this is that we observed a range of values. Sometimes we observed a value of 0, sometimes 5.2, sometimes –3.48, and many more in between. Since we know that the observed value of this hypothetical variable doesn’t always stay the same, we call this a randomly distributed variable. In order to cope with this variable’s randomness, it would be useful to set some expectations about the range of possibilities that we could observe and the value that we’ll most likely observe. This is precisely what descriptive statistics aim to achieve!

We’re now ready to calculate the descriptive statistics! However, we should address one thing before we run the calculations. Each of these calculations (such as mean and standard deviation) requires fully scanning over the entire dataset, so they can take a long time to complete. For example, the mean requires summing all values in the DataFrame, then dividing the sum by the number of rows in the DataFrame. The calculation to produce the vehicle’s age is also reasonably complex because of the object manipulation we need to do with the datetime column (datetime operations are typically slow). This would be a good opportunity to use the persist method to hold the results of this expensive computation in memory. However, we will instead save the intermediate result as a Parquet file, because we will use this data again in a later chapter. By saving the data to disk, you can come back to the data later without needing to recalculate it, and you won’t need to keep your Jupyter notebook server up indefinitely until you need the data again. As a brief reminder, the two parameters we need to pass to the to_parquet method are the filename and the Parquet library we want to use to write the data. As with other examples, we’ll stick to using PyArrow.

Listing 6.5 Saving the intermediate results to Parquet
with ProgressBar():
   files = nyc_data_with_vehicle_age.to_parquet('nyc_data_vehicleAge', engine='pyarrow')

nyc_data_with_vehicle_age = dd.read_parquet('nyc_data_vehicleAge', engine='pyarrow') 

Once these two lines finish executing (which took about 45 minutes on my system), we’ll be in good shape to calculate the descriptive statistics more quickly and efficiently. For convenience, Dask provides built-in descriptive statistics functions so you don’t have to write your own algorithms. We’ll take a look at the five descriptive statistics we covered earlier in the section: mean, standard deviation, minimum, maximum, and skewness.

Listing 6.6 Calculating descriptive statistics
from dask.array import stats as dask_stats
with ProgressBar():
    mean = nyc_data_with_vehicle_age['Vehicle Age'].mean().compute()
    stdev = nyc_data_with_vehicle_age['Vehicle Age'].std().compute()
    minimum = nyc_data_with_vehicle_age['Vehicle Age'].min().compute()
    maximum = nyc_data_with_vehicle_age['Vehicle Age'].max().compute()
    skewness = float(dask_stats.skew(nyc_data_with_vehicle_age['Vehicle Age'].values).compute())    #1  

#1   Compute the skewness by feeding the Vehicle Age column into the dask_stats.skew function, and cast the result to a float.