Python For Data Science: From Scratch(Part III)
By: Richa Yadav
Tags: Machine Learning, Data Visualization, Data Science, Matplotlib, Python

Python For Data Science: From Scratch(Part III)

Iris!

After learning about python 101, slicing and dicing data structures, it is time to add creativity to our projects. In this article, we shall be looking into data visualization and how it can reveal the data’s deepest and darkest secrets. Next, we are going to build our first machine learning model to get an idea of what the hell is it anyway!

I. Data Visualization

Imagine your favorite bedtime storybook when you were an innocent little monster. You remember it used to have small sentences but really captivating pictures, right? Let’s change that thing for a while and make our favorite colorful bedtime storybook into a white page with lines and lines of the story written in black ink. You be like ~

I can’t do it! It’s torture.

In data science, all you have is a thousand rows of data and an overwhelming number of columns to look after. Just imagine the horror if you had to go through every record and understand the significance that it holds in that dataset. But, if you visualize or plot your data into best-suited charts, you get the significance of each data point in a second.

Data Visualization is so bad-ass it has a different dedicated job profile for itself. So here we are with an understanding of what is visualization without even reading a single boring definition! Haha! 🤫😉

Matplotlib

Matplotlib is a data visualization library available for python platform. It provides 2D representation in the form of bar charts, histograms, box plot, and several other basic charts.To get started you should know that you’ll never know enough of data visualization, you’ll always keep learning and growing. There are multiple other great libraries like Plotly, Bokeh that gives you enticing representations. But understand the basics in matplotlib and then you can master the other ones gradually.

Line chart:

I thought visualizing on actual data would make more sense that any dummy data points. For this purpose, I have used the Crime in India dataset from Kaggle. A line chart is a 2D representation that shows the trend of one variable with respect to another variable.

Fig.1. Line Chart

Hmm...The number of burglary cases goes low very steeply around the year 2003! I wonder what happened then? Maybe the burglars grew rich enough and decided to retire?! Then they spent their fortunes gambling so they had to steal voraciously again in around 2004?

Matplotlib.pyplot library stores methods that look like Matlab. In matplotlib.pyplot, certain states are preserved about the plot figure(here,plt) and the plotting area. Whatever further methods might be used are assumed to be for this particular plot itself.

Anyways, the plt.plot() function plots the points on the X-Y axis.

The x-axis and y-axis label have been given.

And finally, we use the show() function that displays the chart on the console and returns the command back to the IPython shell.

2. Bar chart

Fig. 2. Bar Chart

Bar charts are used to display the frequency or measure of the categorical variables.

In matplotlib.pyplot, we use the bar() method to build a chart. the bar method needs two mandatory parameters, one is the x-axis labels and the other is the height of those labels.

3. Piechart:

Fig.3 .Piechart

The above bar chart is great but we can’t figure out how much proportion of the cases recorded comes under which state. For that matter, to get a clear conception of what is the percentage of each state we build a pie chart.

Here, the pie() method mandatorily needs two arguments, one is the label and the second is the size or angle that will be assigned to that particular label. The autopct attribute is used to give labels to the pie slices.

4. Scatterplot:

Fig.4. Scatter Plot

Scatterplots are used to check the distribution of variables or how variables are related to each other. Hence we need at least 2 fields to be plotted on x-axis and y-axis. I have used a random number generator that simply assigns to a color.

5. Histogram:

Histograms are a great way to see the distribution of a variable. The concept of bins or buckets is very important is a histogram. The bin size defines the extent to which data can fall into that “bin”. Say the bin size is 10, so the representation would be 0–10,10–20,20–30 and so on. Now say the bin size is 50, the representation would be 0–50,50–100, etc.

Fig.5. Histogram

We can interpret that the majority of the cases were a robbery of fewer than ~2000 items.

6. Boxplot:

Boxplot is used to plot the distribution of a variable and detect its outliers. All the outliers are pointed out in circles. While the rectangular box states where the maximum concentration of data lies, it also states the min, max and the inter-quartile range along with a line that is actually a median.

Fig.6. Boxplot

This boxplot conveys the data is heavily skewed along with the presence of multiple outliers. This is exactly what the histogram conveys but in a different representation. Here, we got a clear sense of outliers and their quantity but it wasn’t the same clear picture in a histogram.

According to me, both boxplot and histogram can be used to consider the distribution of a variable. It depends on you to decide which one to choose!

For now, knowing these handful charts are enough to go ahead. Trust me you will improvise as you proceed!