<jupyter_start><jupyter_text>SeabornSeaborn is a visualization library that sits on top of matplotlib, making it nicer to look at and adding some extra capabilities too.Let's start by loading up a real dataset on 2019 model-year vehicles, and plotting a histogram just using matplotlib to see the distribution by number of gears.<jupyter_code>%matplotlib inline

import pandas as pd

df = pd.read_csv("http://media.sundog-soft.com/SelfDriving/FuelEfficiency.csv")

gear_counts = df['# Gears'].value_counts()

gear_counts.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>We can load up Seaborn, and just call set() on it to change matplotlib's default settings to something more visually pleasing.<jupyter_code>import seaborn as sns
sns.set()<jupyter_output><empty_output><jupyter_text>Now if we do the same plot command, it's a little more modern looking. Matplotlib is based on Matplot, and its visualization defaults are frankly showing their age.<jupyter_code>gear_counts.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>Let's take a closer look at the data we're dealing with.<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Seaborn includes many types of plots that matplotlib doens't offer. For example, "distplot" can be used to plot a histogram together with a smooth distribution of that histogram overlaid on it. Let's plot the distribution of MPG values on the vehicles in this database as an example:<jupyter_code>sns.distplot(df['CombMPG'])<jupyter_output><empty_output><jupyter_text>Something you encounter pretty often is a "pair plot" from Seaborn. This lets you visualize plots of every combination of various attributes together, so you can look for interesting patterns between features.As an example, let's classify cars by how many cylinders are in their engines, and look for relationships between cylinders, city MPG rating, Highway MPG rating, and combined MPG rating.<jupyter_code>df2 = df[['Cylinders', 'CityMPG', 'HwyMPG', 'CombMPG']]
df2.head()
sns.pairplot(df2, height=2.5);  #Seaborn currently has a bug with the hue parameter so we've omitted it<jupyter_output><empty_output><jupyter_text>By studying the results above, you can see there is a relationship between number of cylinders and MPG, but MPG for 4-cylinder vehicles ranges really widely. There also appears to be a good linear relationship between the different ways of measuring MPG values, until you get into the higher MPG ratings. Seaborn 1.9 also includes "scatterplot", which is exactly what it sounds like. It plots individual data points across two axes of your choosing, so you can see how your data is distributed across those dimensions.<jupyter_code>sns.scatterplot(x="Eng Displ", y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>Seaborn also offers a "jointplot", which combines a scatterplot with histograms on both axes. This lets you visualize both the individual data points and the distribution across both dimensions at the same time.<jupyter_code>sns.jointplot(x="Eng Displ", y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>The "lmplot" is a scatterplot, but with a linear regression line computed and overlaid onto the data.<jupyter_code>sns.lmplot(x="Eng Displ", y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>Next, let's look at a "box plot." This is what's called a "box and whiskers" plot, which is useful for visualizing typical values for a given category without getting distracted by outliers. Each box represents the range between the first and third quartiles of the data, with a line representing the median value. The "whiskers" that extend from the box represent the spread of the remainder of the data, apart from clear outliers that are plotted as individual points outside of the whiskers.As an example, let's look at box plots for each vehicle manufacturer, visualizing the miles-per-gallon ratings across the vehicles they produce. This lets us look at the spread of MPG ratings across all the vehicles each manufacturer offers.There are a lot of manufacturers, so to make the resulting graph readable we'll increase Seaborn's default figure size, and also use set_xticklabels to rotate the labels 45 degrees.<jupyter_code>sns.set(rc={'figure.figsize':(15,5)})
ax=sns.boxplot(x='Mfr Name', y='CombMPG', data=df)
ax.set_xticklabels(ax.get_xticklabels(),rotation=35)<jupyter_output><empty_output><jupyter_text>Another way to visualize the same data is the "swarm plot." Instead of boxes and whiskers, it plots each individual data point - but does so in such way that groups them together based on their distribution. It makes more sense when you look at it:<jupyter_code>ax=sns.swarmplot(x='Mfr Name', y='CombMPG', data=df)
ax.set_xticklabels(ax.get_xticklabels(),rotation=45)<jupyter_output><empty_output><jupyter_text>Another tool is the "count plot." This is basically the same thing as a histogram, but for categorical data. It lets you count up how many times each given category on the X axis occurs in your data, and plot it. So for example, we can see that General Motors offers more vehicle models than anyone else, with BMW not far behind.<jupyter_code>ax=sns.countplot(x='Mfr Name', data=df)
ax.set_xticklabels(ax.get_xticklabels(),rotation=45)<jupyter_output><empty_output><jupyter_text>Finally, let's look at a heat-map in Seaborn. A heat map allows you to plot tabular, 2D data of some sort, with colors representing the individual values in each cell of the 2D table.In this example, we'll create a pivot table from our original dataframe, to create a 2D table that contains the average MPG ratings for every combination of number of cylinders and engine displacement.The resulting heatmap shows all of the engine displacement values along the X axis, and all of the cylinder values along the Y axis. For each cell of the table, the actual average MPG rating for that combination of cylinders and engine displacement is represented not as a number, but as a color that ranges from dark for small values, and light for larger values.And, this does allow you visualize a clear trend where things get progressively darker as we move from the top-left of the graph to the bottom-right. Which makes sense; higher MPG ratings are associated with lower numbers of cylinders, and lower engine displacment values. By the time we get to an 8-liter 16-cylinder engine, the average MPG is at its worst of about 12, represented by the color black.This particular graph has a lot of missing data, but the heatmap deals with that gracefully. A 3-cylinder 8-liter engine simply does not exist!<jupyter_code>df2 = df.pivot_table(index='Cylinders', columns='Eng Displ', values='CombMPG', aggfunc='mean')
sns.heatmap(df2)<jupyter_output><empty_output><jupyter_text>ExerciseExplore the relationship between the number of gears a car has, and its combined MPG rating. Visualize these two dimensions using a scatter plot, lmplot, jointplot, boxplot, and swarmplot. What conclusions can you draw?<jupyter_code>%matplotlib inline

import pandas as pd

df = pd.read_csv("http://media.sundog-soft.com/SelfDriving/FuelEfficiency.csv")

gear_counts = df['# Gears'].value_counts()

gear_counts.plot(kind='bar')
df.head()
import seaborn as sns
sns.set()
sns.distplot(df['CombMPG'])
df.head()
df2=df[['Cylinders', 'CityMPG', 'HwyMPG', 'CombMPG']]
df2.head()
sns.pairplot(df2,height=2.6)
sns.jointplot(x='Eng Displ', y ='CombMPG', data=df)
sns.lmplot(x="Eng Displ", y="Com",data=df)<jupyter_output><empty_output><jupyter_text>My solution is below - no peeking ahead of time!<jupyter_code>sns.scatterplot(x='# Gears', y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>A scatterplot arranges itself into columns when you have ordinal data like the number of gears, but it tells us that there's a pretty wide range of MPG values for each type of gear box, although if you look at where the data points are clustered, you can sort of see a downward trend in MPG as the number of gears increases. But it's subtle.We also see that there's such a thing as a single gear car. These are "continuously variable transmission" cars, and we can see they typically have high MPG ratings and are therefore quite efficient.<jupyter_code>sns.lmplot(x='# Gears', y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>The "lmplot" gives us a linear regression of our data overlaid on the graph, and this makes the overall trend of lower MPG's with more gears apparent. More gears isn't better when it comes to efficiency, it seems. We also see the error bars on that regression line, which tells us this trend is probably real and not just the result of randomness in the samples.<jupyter_code>sns.jointplot(x='# Gears', y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>The jointplot gives us histograms on each axis, which provides some interesting insights. The most common gear configuration seems to be 8, with 6 closely behind. And MPG ratings seem to roughly follow a bell curve centered at around 22 or so.<jupyter_code>sns.boxplot(x='# Gears', y="CombMPG", data=df)<jupyter_output><empty_output><jupyter_text>The box plot shows us that the range of MPG values we see on each gearbox type aren't as crazily distributed as it seemed at first glance; many of the extreme values are in fact outliers that are best discarded when analyzing the trends. This makes the real relationships easier to see; those continuously variable transmissions with a single gear are really quite good at fuel efficiency (higher MPG's are more efficient). Between 5 and 8 things are roughly the same, but from 8-10 things decline markedly.<jupyter_code>sns.swarmplot(x='# Gears', y="CombMPG", data=df)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07309311159018582, 'fraction_numerical': 0.005030284365054922, 'mean_word_length': 5.943692088382038, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '39060', 'license': 'MIT', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2742, 'n_words': 1257, 'path': 'Seaborn.ipynb', 'repo_name': 'AronAyub/MACHINE-LEARNING-', 'size': '9741'}