<jupyter_start><jupyter_text>Problems 1. Shipments of Household Appliances: Line Graphs.The file `ApplianceShipments.csv` contains the series of quarterly shipments (in millions of dollars) of US household appliances between 1985 and 1989.a. Create a well-formatted time plot of the data using Python.b. Does there appear to be a quarterly pattern? For a closer view of the patterns, zoom in to the range of 3500–5000 on the y-axis.c. Using Python, create one chart with four separate lines, one line for each of Q1, Q2, Q3, and Q4. In Python, this can be achieved by add column for quarter and year. Then group the data frame by quarter and then plot shipment versus year for each quarter as a separate series on a line graph. Zoom in to the range of 3500–5000 on the y-axis. Does there appear to be a difference between quarters?d. Using Python, create a line graph of the series at a yearly aggregated level (i.e., the total shipments in each year). a.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
shipments_df = pd.read_csv("../datasets/ApplianceShipments.csv")
shipments_df.head()
quarters = shipments_df.Quarter.str.replace(r"(Q\d)-([0-9]+)", r"\2\1", regex=True)

shipments_df["Date"] = pd.PeriodIndex(quarters, freq="Q")
shipments_ts = pd.Series(shipments_df.Shipments.values, index=shipments_df.Date)
fig, ax = plt.subplots(figsize=(8, 4), tight_layout=True)
shipments_ts.plot(ax=ax, legend=False)
ax.set_ylabel("Shipments (in $MM)")
ax.set_title("Time Plot Quarterly Shipments")
plt.show()<jupyter_output><empty_output><jupyter_text>b.It appears to have a quarterly pattern, let's zoom (3,500 - 5,000 on y-axis):<jupyter_code>fig, ax = plt.subplots(figsize=(8, 4), tight_layout=True)

shipments_ts.plot(ylim=[3500, 5000], ax=ax, legend=False)
ax.set_ylabel("Shipments (in $MM)")
ax.set_title("Time Plot Quarterly Shipments (Zoomed y-axis)")
plt.show()<jupyter_output><empty_output><jupyter_text>c.<jupyter_code>quarter_and_year_df = shipments_df.Quarter.str.split("-", expand=True)
shipments_df = shipments_df.merge(quarter_and_year_df, left_index=True, right_index=True)

shipments_df.rename(columns={0:"Q", 1:"Y"}, inplace=True)
shipments_df.head()
fig, ax = plt.subplots(figsize=(8, 4), tight_layout=True)

for group, ships_by_year in shipments_df.groupby("Q"):
    ships_by_year.plot(x="Y", y="Shipments", ax=ax, label=group)

ax.set_title("Shipment versus Year for each Quarter")
ax.set_ylabel("Shipments (in $MM)")
ax.set_xlabel("Year")

plt.show()<jupyter_output><empty_output><jupyter_text>Q2 and Q3 have more shipments than Q1 and Q4 (this behavior repeats throughout the years). It is also possible to see that the shipments increase as the years advance, independently of the quarter.  d.<jupyter_code>ax = shipments_df.groupby("Y").sum().plot(figsize=(8, 4), legend=False)

ax.set_title("Yearly Aggregated Shipments")
ax.set_ylabel("Shipments (in $MM)")
ax.set_xlabel("Year")

plt.show()<jupyter_output><empty_output><jupyter_text>2. Sales of Riding Mowers: Scatter Plots.A company that manufactures riding mowers wants to identify the best sales prospects for an intensive sales campaign. In particular, the manufacturer is interested in classifying households as prospective owners or nonowners on the basis of Income (in $1000s) and Lot Size (in 1000 ft 2 ). The marketing expert looked at a random sample of 24 households, given in the file `RidingMowers.csv`.a. Using Python, create a scatter plot of Lot Size vs. Income, color-coded by the outcome variable owner/nonowner. Make sure to obtain a well-formatted plot (create legible labels and a legend, etc.).<jupyter_code>mowers_df = pd.read_csv("../datasets/RidingMowers.csv")
mowers_df.head()
# plot first the data points for Ownership equals "Owner" and then of "Nonowner"
# setting color to "none" gives open circles

_, ax = plt.subplots(figsize=(8, 5))
for cat_value, color in [("Owner", "C1"), ("Nonowner", "C0")]:
    subset_df = mowers_df[mowers_df.Ownership == cat_value]
    ax.scatter(subset_df.Lot_Size, subset_df.Income, color="none",
               edgecolor=color, marker="*")
    
ax.set_title("Lot Size vs. Income")
ax.set_xlabel("Lot Size (in 1000 $ft^2$)")
ax.set_ylabel("Income (in $1000s)")
ax.legend(["Owner", "Nonowner"])
plt.show()<jupyter_output><empty_output><jupyter_text>3. Laptop Sales at a London Computer Chain: Bar Charts and Boxplots.The file `LaptopSalesJanuary2008.csv` contains data for all sales of laptops at a computer chain in London in January 2008. This is a subset of the full dataset that includes data for the entire year.a. Create a bar chart, showing the average retail price by store. Which store has the highest average? Which has the lowest?b. To better compare retail prices across stores, create side-by-side boxplots of retail price by store. Now compare the prices in the two stores from (a). Does there seem to be a difference between their price distributions?<jupyter_code>laptops_sales_jan_df = pd.read_csv("../datasets/LaptopSalesJanuary2008.csv")
laptops_sales_jan_df.head()<jupyter_output><empty_output><jupyter_text>a.<jupyter_code># barchart of average retail price by store
laptops_sales_jan_df.groupby("Store Postcode").mean()["Retail Price"].plot(kind="bar", figsize=(8, 4))

plt.title("Average Retail Price by Store")
plt.ylabel("Avg. Retail Price")
plt.xticks(rotation=80)
plt.show()
laptops_sales_jan_df.groupby("Store Postcode").mean()["Retail Price"].sort_values(ascending=False)<jupyter_output><empty_output><jupyter_text>Well, the average retail price is pretty much the same between the stores. The highest average is 494.634146 (store at N17 6QA) and the lowest is 481.006289 (store at W4 3PH).b.<jupyter_code># alternative plot with matplotlib
data_for_plot = [list(laptops_sales_jan_df[laptops_sales_jan_df["Store Postcode"] == "N17 6QA"]["Retail Price"]),
                 list(laptops_sales_jan_df[laptops_sales_jan_df["Store Postcode"] == "W4 3PH"]["Retail Price"])]
fig, ax = plt.subplots(figsize=(8, 6))
ax.boxplot(data_for_plot)

ax.set_title("Side-by-side Boxplots of Retail Price by Store")
ax.set_xticks([1, 2])
ax.set_xticklabels(["N17 6QA", "W4 3PH"])
ax.set_xlabel("Store Postcode")
ax.set_ylabel("Retail Price")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11488543502643807, 'fraction_numerical': 0.022752763980131388, 'mean_word_length': 5.943270300333704, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '102037', 'license': 'MIT', 'n_tokens_mistral': 2204, 'n_tokens_neox': 2058, 'n_words': 655, 'path': 'problems/chapter03_data_visualization.ipynb', 'repo_name': 'jeantardelli/data-mining-for-business-analytics', 'size': '6241'}