Dataconomy
  • News
  • AI
  • Big Data
  • Machine Learning
  • Trends
    • Blockchain
    • Cybersecurity
    • FinTech
    • Gaming
    • Internet of Things
    • Startups
    • Whitepapers
  • Industry
    • Energy & Environment
    • Finance
    • Healthcare
    • Industrial Goods & Services
    • Marketing & Sales
    • Retail & Consumer
    • Technology & IT
    • Transportation & Logistics
  • Events
  • About
    • About Us
    • Contact
    • Imprint
    • Legal & Privacy
    • Newsletter
    • Partner With Us
    • Writers wanted
Subscribe
No Result
View All Result
Dataconomy
  • News
  • AI
  • Big Data
  • Machine Learning
  • Trends
    • Blockchain
    • Cybersecurity
    • FinTech
    • Gaming
    • Internet of Things
    • Startups
    • Whitepapers
  • Industry
    • Energy & Environment
    • Finance
    • Healthcare
    • Industrial Goods & Services
    • Marketing & Sales
    • Retail & Consumer
    • Technology & IT
    • Transportation & Logistics
  • Events
  • About
    • About Us
    • Contact
    • Imprint
    • Legal & Privacy
    • Newsletter
    • Partner With Us
    • Writers wanted
Subscribe
No Result
View All Result
Dataconomy
No Result
View All Result

Frequency Distribution Analysis using Python Data Stack – Part 2

by Ernest Bonat, Ph.D.
June 7, 2017
in Data Science, Resources
Home Topics Data Science
Share on FacebookShare on TwitterShare on LinkedInShare on WhatsAppShare on e-mail

This is the continuation of the Frequency Distribution Analysis using Python Data Stack – Part 1 article. Here we’ll be analyzing real production business surveys for your review.

Table of Contents

  • Application Configuration File
  • Frequency Distribution Subclass
  • Example 1. TV Networks Frequency Distribution Analysis
  • Example 2. Hotel Ratings Frequency Distribution Analysis
  • Recommendations
  • Conclusions

Application Configuration File

The configuration (config) file config.py is shown in Code Listing 3. This config file includes the general settings for Priority network server activities, TV Network selection and Hotel Ratings survey. It’s important to know and understand that using config file is an excellent tool to store local and global application settings without hardcoding them inside in the application code. While google searching you may find bad practices of hardcoding in Python programs.

Many Data Science programs require the default value of the algorithm parameters. Python Developer has been hardcoded these parameters even in the constructors (__in__()) of the class objects. This is a very bad Continuous Integration and implementation practices.

[codesyntax lang=”python” lines=”normal”]


Join the Partisia Blockchain Hackathon, design the future, gain new skills, and win!


# network activities log file settings
network_activities_file = "network_activities.csv"
# network activities log file settings
data_file_network_activities = "network_activities.csv"
column_name_priority = "Priority"
column_name_message = "Message"
network_activities_image1 = "network_activities1.png"
network_activities_image2 = "network_activities2.png"
network_activities_image3 = "network_activities3.png"
info_class = "Info"
error_class = "Error"
warning_class = "Warning"
alert_class = "Alert"
plot_style = "ggplot"

# priority column settings
plot_x_label_priority = "Priority"
plot_y_label_priority = "Percent Frequency"
plot_title_priority = "Priority Percent Frequency Distribution"
plot_legend_priority = "Priority Amount of Messages"

# error column settings
plot_x_label_error = "Percent Frequency"
plot_y_label_error = "Error Message"
plot_title_error = "Error Percent Frequency Distribution"
plot_legend_error = "Error Amount of Messages"

# warning column settings
plot_x_label_warning = "Percent Frequency"
plot_y_label_warning = "Warning Message"
plot_title_warning = "Warning Percent Frequency Distribution"
plot_legend_warning = "Warning Amount of Messages"

# tv network settings
data_file_tv_networks = "tv_networks.csv"
image_file_tv_networks = "tv_networks_image.png"
column_name_tv_networks = "Network"
plot_x_label_tv_networks = "TV Networks"
plot_y_label_tv_networks = "Percent Frequency"
plot_title_tv_networks = "TV Network Percent Frequency Distribution"
plot_legend_tv_networks = "Amount of TV Networks"

# hotel ratings settings
data_file_hotel_ratings = "hotel_ratings.csv"
image_file_hotel_ratings = "hotel_ratings_image.png"
column_name_hotel_ratings = "Rating"
plot_x_label_hotel_ratings = "Hotel Rating"
plot_y_label_hotel_ratings = "Percent Frequency"
plot_title_hotel_ratings = "Hotel Rating Percent Frequency Distribution"
plot_legend_hotel_ratings = "Amount of Hotel Rating"

[/codesyntax]

Code Listing 3. Program configuration file (config.py).

Frequency Distribution Subclass

The frequency_distribution_subclass.py module contains the FrequencyDistribution(FrequencyDistributionLibrary) subclass object inherited from FrequencyDistributionLibrary(object). The code is provided in Code Listing 4. As you can see the constructor initialize frequency distribution in the superclass library, make sure to get a project directory path and set a data file path. The frequency_distribution_target() function prints frequency summary tables, builds and saves vertical bar chart images for any target columns. The frequency_distribution_message() function does the same but builds and saves a horizontal bar chart image.

[codesyntax lang=”python” lines=”normal”]

import os
from frequency_distribution_superclass import FrequencyDistributionLibrary

class FrequencyDistribution(FrequencyDistributionLibrary):
"""
frequency distribution subclass inhered from frequency distribution superclass library
"""
def __init__(self, data_file):
"""
frequency distribution class constructor
:param data_file: data file name
"""
# initialize frequency distribution superclass library
FrequencyDistributionLibrary.__init__(self)
# get project directory path
self._project_directory_path = self.get_project_directory_path()
# set data file path
self._data_file_path = os.path.join(self._project_directory_path, data_file)

def frequency_distribution_target(self, image_file, column_target, plot_x_label, plot_y_label, plot_title, plot_legend):
"""
print frequency summary table, build and save vertical bar chart image
:param image_file: image file name
:param column_target: column target name
:param plot_x_label: plot x-axis label name
:param plot_y_label: plot y-axis label name
:param plot_title: plot title
:param plot_legend: plot legend
:return none
"""
# set image file path
image_file_path = os.path.join(self._project_directory_path, image_file)
# get x and y axises list data
x_axis, y_axis = self.load_x_y_axis_data(self._data_file_path, column_target)
# print frequency summary table
self.print_frequency_summary_table(plot_x_label, plot_y_label, x_axis, y_axis)
# build and save vertical bar chart image
self.build_bar_chart_vertical(x_axis, y_axis, image_file_path, plot_x_label, plot_y_label, plot_title, plot_legend)

def frequency_distribution_message(self, image_file, column_message, column_target, column_target_class, plot_x_label, plot_y_label, plot_title, plot_legend):
"""
print frequency summary table, build and save horizontal bar chart image
:param image_file: image file name
:param column_message: column message name
:param column_target: column target name
:param column_target_class: column target class name
:param plot_x_label: plot x-axis label name
:param plot_y_label: plot y-axis label name
:param plot_title: plot title
:param plot_legend: plot legend
:return none
"""
# set image file path
image_file_path = os.path.join(self._project_directory_path, image_file)
# get x and y axises list error message data
x_axis, y_axis = self.load_x_y_axis_data(self._data_file_path, column_message, column_target, column_target_class)
# print error message summary table
self.print_frequency_summary_table(plot_x_label, plot_y_label, x_axis, y_axis)
# build and save error message vertical bar chart image
self.build_bar_chart_horizontal(x_axis, y_axis, image_file_path, plot_x_label, plot_y_label, plot_title, plot_legend)

[/codesyntax]

Code Listing 4. Frequency distribution subclass FrequencyDistribution(FrequencyDistributionLibrary).

Now that we have defined and organized the superclass and subclass objects, we are ready to create the client program to do the Frequency Distribution Analysis for the network server activities log file. The code shown in Code Listing 5 provides a complete analysis including:

  • Print priority frequency summary table, build and save vertical bar chart image for priority column (Table 2 and Figure 1)
  • Print error frequency summary table, build and save horizontal bar chart image for error column (Table 3 and Figure 2)
  • Print warning frequency summary table, build and save horizontal bar chart image for warning column (Table 4 and Figure 3)

You can see how clean this code is. All the work is done by the subclass FrequencyDistribution(FrequencyDistributionLibrary), and of course nothing has been hardcoded. Any necessary change made to the config file parameters will not affect the program production code. Moving default algorithm parameter values to the config file is necessary for any Data Analytics program deployment and maintenance.

[codesyntax lang=”python” lines=”normal”]

import time
from frequency_distribution_subclass import FrequencyDistribution
import config

def main():
# create frequency distribution class object
frequency_distribution = FrequencyDistribution(config.data_file_network_activities)
# print frequency summary table, build and save vertical bar chart image for priority column
frequency_distribution.frequency_distribution_target(config.network_activities_image1, config.column_name_priority, config.plot_x_label_priority, config.plot_y_label_priority, config.plot_title_priority, config.plot_legend_priority)
# print frequency summary table, build and save horizontal bar chart image for error column
frequency_distribution.frequency_distribution_message(config.network_activities_image2, config.column_name_message, config.column_name_priority, config.error_class, config.plot_x_label_error, config.plot_y_label_error, config.plot_title_error, config.plot_legend_error)
# print frequency summary table, build and save horizontal bar chart image for warning column
frequency_distribution.frequency_distribution_message(config.network_activities_image3, config.column_name_message, config.column_name_priority, config.warning_class, config.plot_x_label_warning, config.plot_y_label_warning, config.plot_title_warning, config.plot_legend_warning)

if __name__ == '__main__':
start_time = time.time()
main()
end_time = time.time()
print("Program Runtime: " + str(round(end_time - start_time, 1)) + " seconds" + "\n")

[/codesyntax]

Code Listing 5. Program code for Network Server Activities Frequency Distribution Analysis.

Table 2 shows the Priority frequency summary table. The Priority contains four classes: Info, Error, Warning and Alert.

PriorityPercent Frequency
Info33.90%
Error30.70%
Warning26.80%
Alert8.70%


Table 2. Priority frequency summary table.

The bar chart of the Priority Percent Frequency Distribution is shown below in Figure 1. This figure shows 30.7% of occurred Errors (red light) and 26.8% of Warning (yellow light) messages. The system admin team would like to know these messages for network server maintenances and optimization.

Frequency Distribution Analysis using Python Data Stack - Part 2

Figure 1. Priority Percent Frequency Distribution.

Table 3 shows the Error Message frequency summary table. Nine classes has been defined.

Error MessagePercent Frequency
Interface X0 Link Is Down30.80%
Interface X8 Link Is Down20.50%
Interface X5 Link Is Down17.90%
Interface X4 Link Is Down15.40%
Interface X9 Link Is Down5.10%
Interface X2 Link Is Down2.60%
Interface X3 Link Is Down2.60%
Interface X6 Link Is Down2.60%
Interface X7 Link Is Down2.60%


Table 3. Error Message frequency summary table.

The horizontal bar chart of the Error Percent Frequency Distribution is shown in Figure 2. The figure shows that the most occurred Error messages are: Interface X0 Link Is Down (30%), Interface X8 Link Is Down (20%), Interface X5 Link Is Down (17%) and Interface X4 Link Is Down (15%). This information is very important for the network admin team to start looking for the right solutions to fix these server occurred errors.

Frequency Distribution Analysis using Python Data Stack - Part 2

Figure 2. Error Percent Frequency Distribution.

Table 4 shows the Warning Message frequency summary table. Seven classes has been defined.

Warning MessagePercent Frequency
Interface X0 Link Is Up32.40%
Interface X5 Link Is Up20.60%
Interface X8 Link Is Up20.60%
Interface X4 Link Is Up17.60%
Interface X1 Link Is Up2.90%
Interface X9 Link Is Up2.90%
Wan IP Changed2.90%


Table 4. Warning Message frequency summary table.

The horizontal bar chart of the Warning Message Frequency Distribution is shown in Figure 3. As the figure shows the most occurred Warning messages are: Interface X0 Link Is Up (32%), Interface X8 Link Is Up (20%), Interface X5 Link Is Up (20%) and Interface X4 Link Is Up (17%). The message Interface X0 Link Is Up, is critical and requires some attention by the network admin team.

Frequency Distribution Analysis using Python Data Stack - Part 2

Figure 3. Warning Percent Frequency Distribution.

The Network Server Activities Frequency Distribution Analysis can provided to the admin team the following conclusions:

  1. The server Error and Warning messages are the second and third occurred with 30.7% and 26.8% respectively.
  2. The Error messages occurred in the following percent order:
  • Interface X0 Link Is Down (30%)
  • Interface X8 Link Is Down (20%)
  • Interface X5 Link Is Down (17%)
  • Interface X4 Link Is Down (15%)
  1. The Warning messages occurred in the following percent order:
  • Interface X0 Link Is Up (32%)
  • Interface X8 Link Is Up (20%)
  • Interface X5 Link Is Up (20%)
  • Interface X4 Link Is Up (17%).
  1. The Error messages have the same class sequence as the Warning messages.
  2. The Warning messages Interface X0 Link Is Up (32%), Interface X8 Link Is Up (20%) and Interface X5 Link Is Up (20%) are most likely to occur at this point.

Let’s apply this logic to real business examples so you can see how quickly to you can develop and analyze any frequency business data.

Example 1. TV Networks Frequency Distribution Analysis

Table 5 shows ten rows of the TV Network data file (tv_networks.csv). This file contains the most-watched television networks for a period of time.

Network
CBS
CBS
ABC
CBS
FOX
NBC
CBS
NBC
FOX
NBC


Table 5. Ten rows of the TV Network data file.

The config file session ‘tv network settings’ (Code Listing 6.) includes the required seven parameters settings.

[codesyntax lang=”python” lines=”normal”]

# tv network settings
data_file_tv_networks = "tv_networks.csv"
image_file_tv_networks = "tv_networks_image.png"
column_name_tv_networks = "Network"
plot_x_label_tv_networks = "TV Networks"
plot_y_label_tv_networks = "Percent Frequency"
plot_title_tv_networks = "TV Network Percent Frequency Distribution"
plot_legend_tv_networks = "Amount of TV Networks"

[/codesyntax]

Code Listing 6. Config file session for ‘tv network settings’.

The main program is shown in Code Listing 7 and provides the complete Frequency Distribution Analysis for the TV Network data file. This code includes:

  • Create frequency distribution class object
  • Print frequency summary table, build and save vertical bar chart image for TV Networks column

[codesyntax lang=”python” lines=”normal”]

import time
from frequency_distribution_subclass import FrequencyDistribution
import config

def main():
# create frequency distribution class object
frequency_distribution = FrequencyDistribution(config.data_file_tv_networks)
# print frequency summary table, build and save vertical bar chart image for tv networks column
frequency_distribution.frequency_distribution_target(config.image_file_tv_networks, config.column_name_tv_networks, config.plot_x_label_tv_networks, config.plot_y_label_tv_networks, config.plot_title_tv_networks, config.plot_legend_tv_networks)

if __name__ == '__main__':
start_time = time.time()
main()
end_time = time.time()
print("Program Runtime: " + str(round(end_time - start_time, 1)) + " seconds" + "\n")

[/codesyntax]

Code Listing 7. Main program for TV Network Frequency Distribution Analysis.

The program generates the TV Network frequency summary table (Table 6) and the percent frequency distribution vertical bar chart (Figure 4).

TV NetworkPercent Frequency
CBS30.00%
NBC27.00%
FOX24.00%
ABC19.00%


Table 6. TV Network frequency summary table.

As you can see based on the TV Network data file the most-watched is CBS with 30%, following with NBC with 27%.

Frequency Distribution Analysis using Python Data Stack - Part 2

Figure 4. TV Network Percent Frequency Distribution.

Example 2. Hotel Ratings Frequency Distribution Analysis

This example shows ten records of a Hotel Rating survey from the data file hotel_ratings.csv (Table 7)

Rating
Poor
Very Good
Excellent
Poor
Poor
Excellent
Average
Very Good
Average
Very Good


Table 7. Ten rows of the Hotel Rating survey data file.

The config file session ‘hotel rating settings’ (Code Listing 8.) includes the required seven parameters settings.

[codesyntax lang=”python” lines=”normal”]

# hotel ratings settings
data_file_hotel_ratings = "hotel_ratings.csv"
image_file_hotel_ratings = "hotel_ratings_image.png"
column_name_hotel_ratings = "Rating"
plot_x_label_hotel_ratings = "Hotel Rating"
plot_y_label_hotel_ratings = "Percent Frequency"
plot_title_hotel_ratings = "Hotel Rating Percent Frequency Distribution"
plot_legend_hotel_ratings = "Amount of Hotel Rating"

[/codesyntax]

Code Listing 8. Main program for Hotel Ratting Frequency Distribution Analysis.

The main program shown in Code Listing 9 provides the complete Frequency Distribution Analysis for the Hotel Ratting survey data file. This code includes:

  • Create frequency distribution class object
  • Print frequency summary table, build and save vertical bar chart image for Hotel Ratting column

[codesyntax lang=”python” lines=”normal”]

import time
from frequency_distribution_subclass import FrequencyDistribution
import config

def main():
# create frequency distribution class object
frequency_distribution = FrequencyDistribution(config.data_file_hotel_ratings)
# print frequency summary table, build and save vertical bar chart image for hotel ratings column
frequency_distribution.frequency_distribution_target(config.image_file_hotel_ratings, config.column_name_hotel_ratings, config.plot_x_label_hotel_ratings, config.plot_y_label_hotel_ratings, config.plot_title_hotel_ratings, config.plot_legend_hotel_ratings)

if __name__ == '__main__':
start_time = time.time()
main()
end_time = time.time()
print("Program Runtime: " + str(round(end_time - start_time, 1)) + " seconds" + "\n")

[/codesyntax]

Code Listing 9. Main program for Hotel Ratting Frequency Distribution Analysis.

The program generates the Hotel Ratting frequency summary table (Table 8) and the percent frequency distribution vertical bar chart (Figure 4).

Hotel RatingPercent Frequency
Very Good 38.80%
Excellent28.80%
Average16.50%
Poor9.60%
Terrible6.30%


Table 8. Hotel Ratting frequency summary table.

As you can see based on the Hotel Ratting survey data file 38.8% of the guests rated ‘Very Good’ and 28.8% rated ‘Excellent’. The guest messages for the ‘Poor’ and ‘Terrible’ rates were not provided.

Frequency Distribution Analysis using Python Data Stack - Part 2

 

Figure 4. Hotel Ratting Percent Frequency Distribution.

Recommendations

I would like to recommend reading the following OOP materials? applied to Data Analytics projects:

  • An Introduction to Object Oriented Data Science in Python. By Sev Leonard, Data Scientist – The Data Scout
  • Raschka, Sebastian. Python Machine Learning. Birmingham, UK: Packt Publishing, 2015

Based on the info provided in this paper we can summarize the following two conclusions:

Conclusions

  1. Applying Object-Oriented programming to your Data Analytics make your code more organize, reusable and robust. It will allow an easy implementation of the required Continuous Integration and Unit Test standards methodologies.
  2. Store local and global Data Analytics program settings in a configuration file is a good designed and developed practice. Required default value of the algorithm parameters should be stored in the configuration file. Hardcoding these values in the program code is a bad programming and deployment practices.

Feel free to send to Ernest any questions about his paper.

Like this article? Subscribe to our weekly newsletter to never miss out!

Follow @DataconomyMedia

Tags: Frequency Distribution AnalysisMatplotlibNumpyPandaspython

Related Posts

BuzzFeed ChatGPT integration: Buzzfeed stock surges in enthusiasm over OpenAI

BuzzFeed ChatGPT integration: Buzzfeed stock surges after the OpenAI deal

January 31, 2023
Adversarial machine learning 101: A new frontier in cybersecurity

Adversarial machine learning 101: A new cybersecurity frontier

January 31, 2023
What is the Nvidia Eye Contact AI feature? Learn how to get and use the new Nvidia Broadcast feature. Zoom meetings and streams get easier.

Nvidia Eye Contact AI can be the savior of your online meetings

January 30, 2023
How did ChatGPT passed an MBA exam

How did ChatGPT passed an MBA exam?

January 27, 2023
What is AI prompt engineering? Learn how to write a prompt with examples. ChatGPT prompt engineering and more explained in this article.

AI prompt engineering is the key to limitless worlds

January 27, 2023
What is Analytics as a Service (AaaS): Examples

Transform your data into a competitive advantage with AaaS

January 26, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

LATEST ARTICLES

BuzzFeed ChatGPT integration: Buzzfeed stock surges after the OpenAI deal

Adversarial machine learning 101: A new cybersecurity frontier

Fostering a culture of innovation through digital maturity

Nvidia Eye Contact AI can be the savior of your online meetings

How did ChatGPT passed an MBA exam?

AI prompt engineering is the key to limitless worlds

Dataconomy

COPYRIGHT © DATACONOMY MEDIA GMBH, ALL RIGHTS RESERVED.

  • About
  • Imprint
  • Contact
  • Legal & Privacy
  • Partnership
  • Writers wanted

Follow Us

  • News
  • AI
  • Big Data
  • Machine Learning
  • Trends
    • Blockchain
    • Cybersecurity
    • FinTech
    • Gaming
    • Internet of Things
    • Startups
    • Whitepapers
  • Industry
    • Energy & Environment
    • Finance
    • Healthcare
    • Industrial Goods & Services
    • Marketing & Sales
    • Retail & Consumer
    • Technology & IT
    • Transportation & Logistics
  • Events
  • About
    • About Us
    • Contact
    • Imprint
    • Legal & Privacy
    • Newsletter
    • Partner With Us
    • Writers wanted
No Result
View All Result
Subscribe

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy Policy.