import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df_red = pd.read_csv('winequality-red.csv', sep=';')
# red_df.rename(columns={'total_sulfur-dioxide':'total_sulfur_dioxide'}, inplace=True)
df_red.shape
sum(df_red.duplicated())
df_red['quality'].unique()
df_white = pd.read_csv('winequality-white.csv', sep=';')
df_white.shape
sum(df_white.duplicated())
df_white['quality'].unique()
a = np.random.random((int)(1e8))
import time
start = time.time()
sum(a) / len(a)
print(time.time() - start, 'seconds')
start = time.time()
np.mean(a)
print(time.time() - start, 'seconds')
# create color array for red dataframe
color_red = np.repeat('red', 1599)
# create color array for white dataframe
color_white = np.repeat('white', 4898)
df_red['color'] = color_red
df_red.head()
df_white['color'] = color_white
df_white.head()
# append dataframes
wine_df = df_red.append(df_white, ignore_index= True, sort=False)
# view dataframe to check for success
wine_df.head()
wine_df.tail()
wine_df.to_csv('winequality_edited.csv', index=False)
wine_df.shape
wine_df['fixed acidity'].hist()
wine_df['total sulfur dioxide'].hist()
wine_df['pH'].hist()
wine_df['alcohol'].hist()
df.plot(x="volatile acidity", y="quality", kind="scatter");
wine_df.plot(x='residual sugar', y='quality', kind='scatter');
wine_df.plot(x='pH', y='quality', kind='scatter');
wine_df.plot(x='alcohol', y='quality', kind='scatter');
wine_df.mean()
wine_df.groupby('quality').mean()
wine_df.groupby(['quality', 'color']).mean()
# if only interested in fixed pH
wine_df.groupby(['quality', 'color'], as_index=False)['pH'].mean()
wine_df[wine_df['color'] == 'red']['quality'].mean()
wine_df[wine_df['color'] == 'white']['quality'].mean()
# Find the mean quality of each wine type (red and white) with groupby
wine_df.groupby(['color'], as_index=False)['quality'].mean()
wine_df.describe().pH
# Bin edges that will be used to "cut" the data into groups
bin_edges = [2.72, 3.11, 3.21, 3.32, 4.01] # Fill in this list with five values you just found
# Labels for the four acidity level groups
bin_names = ['Low', 'Medium', 'Moderately High', 'High'] # Name each acidity level category
# Creates acidity_levels column
wine_df['acidity levels'] = pd.cut(df['pH'], bin_edges, labels=bin_names)
# Checks for successful creation of this column
wine_df.head()
# Find the mean quality of each acidity level with groupby
wine_df.groupby(['acidity levels'], as_index=False)['quality'].mean()
wine_df.groupby('acidity levels').mean().quality
# Use groupby to get the mean quality for each acidity level
acidity_level_quality_means = wine_df.groupby('acidity levels').quality.mean()
acidity_level_quality_means
# Create a bar chart with proper labels
locations = [1, 2, 3, 4] # reorder values above to go from low to high
heights = acidity_level_quality_means
labels = ['Low', 'Medium', 'Moderately High', 'High']
# labels = acidity_level_quality_means.index.str.replace('_', ' ').str.title() # alternative to commented out line above
plt.bar(locations, heights, tick_label=labels)
plt.title('Average Quality Ratings by Acidity Level')
plt.xlabel('Acidity Level')
plt.ylabel('Average Quality Rating');
plt.plot(locations, heights)
plt.title('Average Quality Ratings by Acidity Level')
plt.xlabel('Acidity Level')
plt.ylabel('Average Quality Rating');
# Save changes for the next section
wine_df.to_csv('winequality_edited.csv', index=False)
# get the median amount of alcohol content
wine_df['alcohol'].median()
# select samples with alcohol content less than the median
low_alcohol = wine_df.query('alcohol < 10.30')
# select samples with alcohol content greater than or equal to the median
high_alcohol = wine_df.query('alcohol >= 10.30')
# ensure these queries included each sample exactly once
num_samples = wine_df.shape[0]
num_samples == low_alcohol['quality'].count() + high_alcohol['quality'].count() # should be True
# get mean quality rating for the low alcohol and high alcohol groups
low_alcohol.quality.mean(), high_alcohol.quality.mean()
mean_quality_low = low_alcohol['quality'].mean()
mean_quality_high = high_alcohol['quality'].mean()
# Create a bar chart with proper labels
locations = [1, 2]
heights = [mean_quality_low, mean_quality_high]
labels = ['Low', 'High']
plt.bar(locations, heights, tick_label=labels)
plt.title('Average Quality Ratings by Alcohol Content')
plt.xlabel('Alcohol Content')
plt.ylabel('Average Quality Rating');
# get the median amount of residual sugar
wine_df['residual sugar'].median()
# select samples with residual sugar less than the median
low_sugar = wine_df[wine_df['residual sugar'] < 3]
# select samples with residual sugar greater than or equal to the median
high_sugar = wine_df[wine_df['residual sugar'] >= 3]
# ensure these queries included each sample exactly once
num_samples == low_sugar['quality'].count() + high_sugar['quality'].count() # should be True
# get mean quality rating for the low sugar and high sugar groups
low_sugar.quality.mean(), high_sugar.quality.mean()
mean_quality_low = low_sugar['quality'].mean()
mean_quality_high = high_sugar['quality'].mean()
# Create a bar chart with proper labels
locations = [1, 2]
heights = [mean_quality_low, mean_quality_high]
labels = ['Low', 'High']
plt.bar(locations, heights, tick_label=labels)
plt.title('Average Quality Ratings by Residual Sugar')
plt.xlabel('Residual Sugar')
plt.ylabel('Average Quality Rating');
colors = ['red', 'grey']
color_means = wine_df.groupby('color')['quality'].mean()
color_means.plot(kind='bar', title='Average wine quality by color', color = colors, alpha=0.7)
plt.xlabel('Colors', fontsize = 18)
plt.ylabel('Quality', fontsize = 18)
counts = wine_df.groupby(['quality', 'color']).count()
counts
counts = wine_df.groupby(['quality', 'color']).count()['pH']
counts.plot(kind='bar', title='Counts by Wine Color and Quantity', color = colors, alpha=0.7)
plt.xlabel('Quality and Color', fontsize = 18)
plt.ylabel('Count', fontsize = 18)
totals = wine_df.groupby(['quality', 'color']).count()['pH']
proportions = counts / totals
proportions.plot(kind='bar', title='Proportions by Wine Color and Quantity', color = colors, alpha=.7)
plt.xlabel('Quality and Color', fontsize = 18)
plt.ylabel('Proportion', fontsize = 18)