<jupyter_start><jupyter_text>Diamond Case Study<jupyter_code># Import dependencies
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# Read the csv file, and check its shape and head
path = 'https://github.com/lustraka/Data_Analysis_Workouts/raw/main/Communicate_Data_Findings/SandBox/'

# Load in the dataset into a pandas dataframe
diamonds = pd.read_csv(path + 'diamonds.csv')


# Convert `cut`, `color`, `clarity` to ordered categorical dtype
cato = {
    'cut': ['Fair', 'Good', 'Very Good', 'Premium', 'Ideal'],
    'color' : ['J', 'I', 'H', 'G', 'F', 'E', 'D'],
    'clarity' : ['I1', 'SI2', 'SI1', 'VS2', 'VS1', 'VVS2', 'VVS1', 'IF']
}
for k, v in cato.items():
  diamonds[k] = diamonds[k].astype(pd.CategoricalDtype(categories=v, ordered=True))<jupyter_output><empty_output><jupyter_text>Explanatory PolishingThrough the 'Explore' workbook, you've performed an exploration of a dataset relating the price of diamonds to their four 'C' attributes: carat, cut, color, and clarity. During that exploration, you found that the primary driver of price was carat weight, where the log of price was approximately linearly related to the cube root of carat weight, analogous to the diamond's length, width, or depth. The price was then modified by the diamond's cut, color, and clarity grades.In this workbook, you'll polish two plots from the earlier exploration so that they can be presented to others in an understandable form.The first plot to polish depicts the base relationship between price and carat. Make additions and revisions to the code below to refine the given plot.<jupyter_code>def cuberoot_trans(x, inverse = False):
    """ quick function for computing cube root and cube operations """
    if not inverse:
        return x ** (1/3)
    else:
        return x ** 3
# scatter plot of price vs. carat, with log transform on price axis and
# cube-root transform on price
diamonds['cr_carat'] = diamonds['carat'].apply(cuberoot_trans)

plt.figure(figsize = [8, 6])

plt.scatter(data = diamonds.sample(2500), x = 'cr_carat', y = 'price', alpha=0.2)

plt.xlabel('Carat')
xtics = cuberoot_trans(np.array([0.2, 0.3, 0.5, 0.7, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0]))
xticklabels = [f'{t:.1f}' for t in cuberoot_trans(xtics, inverse=True)]
plt.xticks(xtics, xticklabels)

plt.yscale('log')
plt.ylabel('Price ($)')
yticks = [500, 1000, 2000, 5000, 10000, 20000]
yticklables = [f'{tick/1000:0.1f}K' for tick in yticks]
plt.yticks(yticks, yticklables)
plt.grid()
plt.title('Diamond Price (log scale) vs Carat (cube root scale)')
plt.show()<jupyter_output><empty_output><jupyter_text>Price vs. Carat Polishing comment 1: Use the following list to make sure you've checked the major points of revision in this plot.- Does the plot has an informative title?- Are the axis labels informative?- Are the tick values interpretable?- Do we need to worry about any of the plot aesthetics?Price vs. Carat Polishing comment 2: Here are some specific comments on the polishing of the plot.- The original plot's vertical axis only has two tick marks, and they're in scientific notation. Not only is it not clear that the data is on a power scale, it's also difficult to parse. We can solve both of these problems by specifying the tick marks. Take advantage of the 1-2-5-10 sequence to set which tick marks to show.- As for the horizontal axis, the values are in terms of the cube-root transformed values, rather than in raw carat values. This presents an obstacle to interpretability. Once again, we should specify tick locations: the peaks observed in the univariate distribution might be good choices. Note that we also need to make use of the cuberoot_trans() function since the transformation is non-standard.- There's a lot of overplotting in the scatterplot as given. Try playing with the alpha parameter to try and make the main trend stand out more.The second plot that you will polish is a multivariate plot of price against levels of color and cut, for diamonds of about 1 carat weight. Make additions and revisions to the code below to refine the given plot.<jupyter_code># select diamonds of approximately 1 carat
diamond_flag = (diamonds['carat'] >= 0.99) & (diamonds['carat'] <= 1.03)
diamonds_1c = diamonds.loc[diamond_flag,:]
plt.figure(figsize = [8,6])

ax = sns.pointplot(data = diamonds_1c, x = 'color', y = 'price', hue = 'cut',
                   dodge=True, linestyles='', palette='viridis')

# special handling of tick marks following seaborn function - you might try
# doing a log transform on the data itself instead of this workaround.
plt.yscale('log')
plt.yticks([3000, 5000, 8000], ['3k', '5k', '8k'])
ax.set_yticklabels([], minor = True)

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], title='Cut')
plt.title('Diamond Price vs. Color and Cut')
plt.xlabel('Color')
plt.ylabel('Price ($)')
plt.show();<jupyter_output><empty_output><jupyter_text>Price vs. Color, Cut: Polishing comment 1:  Don't forget to provide an informative title and axis labels.Price vs. Color, Cut: Polishing comment 2:  There's a few things with the pointplot function that we can revise. First of all, the default color palette used is qualitative, but we should probably choose a sequential palette instead. We could also stand to add a dodge parameter so that the points aren't stacked directly on top of one another and to make individual points easier to read. One other possible modification we could add is to remove the line connecting points on each level of cut: with the other changes made, removing the connecting line can help reduce the ink used, cleaning up the presentation.> If you want to check your work on the case study against a reference, take a look at the example project in the [Resources](https://video.udacity-data.com/topher/2021/May/60a8045e_communicate-data-example-project/communicate-data-example-project.zip)! The example project is based on the same diamonds dataset, and all of the plots that you've worked on in this lesson are also covered there.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09686377000980072, 'fraction_numerical': 0.019274746814766416, 'mean_word_length': 4.825880114176974, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '317257', 'license': 'CC0-1.0', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1764, 'n_words': 807, 'path': 'Communicate_Data_Findings/SandBox/Diamonds-Case-Study-Explain.ipynb', 'repo_name': 'lustraka/Data_Analysis_Workouts', 'size': '6122'}