Project 2
1.
Certainly! Here's an example of the data for white wine and red wine:
White Wine:
- Color: Pale yellow to golden
- Aroma: Citrus, floral, and fruity notes (e.g., apple, pear, peach)
- Taste: Crisp, light to medium-bodied, with flavors such as lemon, green apple, and tropical fruits
- Serving Temperature: Chilled, typically around 8-10°C (46-50°F)
- Common Varieties: Chardonnay, Sauvignon Blanc, Riesling, Pinot Grigio
Red Wine:
- Color: Ruby red to deep purple
- Aroma: Red and dark fruits (e.g., cherry, blackberry, plum), sometimes with earthy or spicy notes
- Taste: Medium to full-bodied, with flavors like black cherry, blackcurrant, chocolate, and vanilla
- Serving Temperature: Cool, but not as chilled as white wine, usually around 14-18°C (57-64°F)
- Common Varieties: Cabernet Sauvignon, Merlot, Pinot Noir, Syrah/Shiraz
Please note that these descriptions provide a general overview of white and red wines. The specific characteristics can vary depending on the grape variety, winemaking techniques, and region of origin.
2.
The size of white wine and red wine can vary depending on the bottle format and the region. Here are the commonly available sizes for both white and red wine:
White Wine Bottle Sizes:
- Split: 187.5 ml (quarter bottle)
- Half Bottle: 375 ml
- Standard Bottle: 750 ml
- Magnum: 1.5 liters (equivalent to 2 standard bottles)
- Double Magnum: 3 liters (equivalent to 4 standard bottles)
- Jeroboam: 5 liters (equivalent to 6.7 standard bottles)
- Rehoboam: 4.5 liters (equivalent to 6 standard bottles)
- Methuselah: 6 liters (equivalent to 8 standard bottles)
- Salmanazar: 9 liters (equivalent to 12 standard bottles)
- Nebuchadnezzar: 15 liters (equivalent to 20 standard bottles)
Red Wine Bottle Sizes: The bottle sizes for red wine are generally the same as white wine, ranging from the split (187.5 ml) to the Nebuchadnezzar (15 liters).
It's important to note that not all bottle sizes are commonly available for every type of wine. The most commonly found sizes are the half bottle, standard bottle, and magnum.
3.
For the first dataset (Type 1 wine):
- fixed acidity: 0 null values
- volatile acidity: 0 null values
- citric acid: 0 null values
- residual sugar: 0 null values
- chlorides: 0 null values
- free sulfur dioxide: 0 null values
- total sulfur dioxide: 0 null values
- density: 0 null values
- pH: 0 null values
- sulphates: 0 null values
- alcohol: 0 null values
- quality: 0 null values
For the second dataset (Type 2 wine):
- fixed acidity: 0 null values
- volatile acidity: 0 null values
- citric acid: 0 null values
- residual sugar: 0 null values
- chlorides: 0 null values
- free sulfur dioxide: 0 null values
- total sulfur dioxide: 0 null values
- density: 0 null values
- pH: 0 null values
- sulphates: 0 null values
- alcohol: 0 null values
- quality: 0 null values
Based on the provided data, there are no null values in either type of wine in both datasets.
4.
import pandas as pd
# Read the data into pandas dataframes
red_wine_df = pd.read_csv('red_wine_data.csv', delimiter=';')
white_wine_df = pd.read_csv('white_wine_data.csv', delimiter=';')
# Check for duplicates in red wine data
red_wine_duplicates = red_wine_df.duplicated()
red_wine_duplicates_count = red_wine_duplicates.sum()
print("Number of duplicates in red wine data:", red_wine_duplicates_count)
# Remove duplicates from red wine data
red_wine_df = red_wine_df.drop_duplicates()
# Check for duplicates in white wine data
white_wine_duplicates = white_wine_df.duplicated()
white_wine_duplicates_count = white_wine_duplicates.sum()
print("Number of duplicates in white wine data:", white_wine_duplicates_count)
# Remove duplicates from white wine data
white_wine_df = white_wine_df.drop_duplicates()
# Save the cleaned data to new CSV files
red_wine_df.to_csv('cleaned_red_wine_data.csv', index=False)
white_wine_df.to_csv('cleaned_white_wine_data.csv', index=False)
5.
To get the number of unique values in each feature, you can iterate over the columns and calculate the count of unique values. Here's the code to accomplish that:
import pandas as pd
# Define the data
data = [
[7.4, 0.7, 0, 1.9, 0.076, 11, 34, 0.9978, 3.51, 0.56, 9.4, 5],
[7.8, 0.88, 0, 2.6, 0.098, 25, 67, 0.9968, 3.2, 0.68, 9.8, 5],
[7.8, 0.76, 0.04, 2.3, 0.092, 15, 54, 0.997, 3.26, 0.65, 9.8, 5],
[11.2, 0.28, 0.56, 1.9, 0.075, 17, 60, 0.998, 3.16, 0.58, 9.8, 6],
[7.4, 0.7, 0, 1.9, 0.076, 11, 34, 0.9978, 3.51, 0.56, 9.4, 5],
[7.4, 0.66, 0, 1.8, 0.075, 13, 40, 0.9978, 3.51, 0.56, 9.4, 5],
[7.9, 0.6, 0.06, 1.6, 0.069, 15, 59, 0.9964, 3.3, 0.46, 9.4, 5],
[7.3, 0.65, 0, 1.2, 0.065, 15, 21, 0.9946, 3.39, 0.47, 10, 7],
[7.8, 0.58, 0.02, 2, 0.073, 9, 18, 0.9968, 3.36, 0.57, 9.5, 7],
[7.5, 0.5, 0.36, 6.1, 0.071, 17, 102, 0.9978, 3.35, 0.8, 10.5, 5],
[6.7, 0.58, 0.08, 1.8, 0.097, 15, 65, 0.9959, 3.28, 0.54, 9.2, 5],
[7.5, 0.5, 0.36, 6.1, 0.071, 17, 102, 0.9978, 3.35, 0.8, 10.5, 5],
[5.6, 0.615, 0, 1.6, 0.089, 16, 59, 0.9943, 3.58, 0.52, 9.9, 5],
[7.8, 0.61, 0.29, 1.6, 0.114, 9, 29, 0.9974, 3.26, 1.56, 9.1, 5],
[8.9, 0.62, 0.18, 3.8, 0.176, 52, 145, 0.9986, 3.16, 0.88, 9.2, 5],
[8.9, 0.62, 0.19, 3.9, 0.17, 51, 148, 0.9986, 3.17, 0.93, 9.2, 5],
[8.5, 0.28, 0.56, 1.8, 0.092, 35, 103, 0.9969, 3.3, 0.75, 10.5, 7],
[8.1, 0.56, 0.28, 1.7, 0.368, 16, 56, 0.9968, 3.11, 1.28, 9.3, 5]
]
# Create a dataframe
df = pd.DataFrame(data, columns=[
'fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar',
'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density',
'pH', 'sulphates', 'alcohol', 'quality'
])
# Get the number of unique values in each feature
unique_counts = df.nunique()
# Print the number of unique values for each feature
for column, count in unique_counts.items():
print(f"Number of unique values in {column}: {count}")Mean Density - Set 1: 0.9970611111111111 Mean Density - Set 2: 0.9944263157894738
7.
To combine both datasets with the color of the wine in a new column, you can follow these steps:
- Add a new column named "color" to each DataFrame representing the color of the wine. You can assign a value like "red" or "white" to each dataset to differentiate them.
df1['color'] = 'red'
df2['color'] = 'white'
- Concatenate the two DataFrames vertically using the
concat()function from pandas.
combined_df = pd.concat([df1, df2], ignore_index=True)
The resulting combined_df DataFrame will have both datasets combined with the color of the wine indicated in the "color" column.
8.
To remove any null values from the combined DataFrame, you can use the dropna() function in pandas. This function will remove any rows that contain null values.
Here's how you can remove null values from the combined DataFrame:
combined_df = combined_df.dropna()
After executing this code, the combined_df DataFrame will no longer contain any rows with null values.
9.
To remove outliers from the combined DataFrame, you can use various methods depending on your specific requirements. One common approach is to use the Z-score method to identify and remove outliers. Here's how you can do it using the Z-score method:
from scipy import stats
# Calculate the absolute Z-score for each column
z_scores = np.abs(stats.zscore(combined_df))
# Define a threshold for outlier detection
threshold = 3
# Create a boolean mask to identify outliers
outlier_mask = (z_scores > threshold).any(axis=1)
# Remove rows with outliers
combined_df = combined_df[~outlier_mask]
In the code above, we first calculate the Z-scores for each column in the combined DataFrame. Then, we define a threshold value (commonly set to 3) to identify outliers. Any data point with a Z-score greater than the threshold is considered an outlier.
Next, we create a boolean mask outlier_mask that indicates whether each row contains outliers. Rows with at least one outlier will have True in the mask.
Finally, we use the boolean mask to filter the combined DataFrame and keep only the rows that do not have outliers.
10.
The relationship between alcohol content and wine ratings is subjective and can vary depending on personal preferences and the specific context. While some individuals may prefer wines with higher alcohol content and rate them more positively, others may prefer wines with lower alcohol content or have different preferences for different types of wines.
In general, alcohol content is just one of the many factors that can influence wine ratings. Wine ratings are typically based on a combination of factors such as aroma, taste, acidity, balance, complexity, and overall enjoyment. Each individual may have their own preferences and criteria for evaluating wines.
It's important to note that higher alcohol content in wine can affect its taste and mouthfeel. Wines with higher alcohol content may have more pronounced heat and a fuller body, which some people may find appealing, while others may prefer wines with a lower alcohol content that are lighter and more delicate.
Ultimately, the relationship between alcohol content and wine ratings is complex and can vary among individuals and wine experts. It's best to consider alcohol content as just one aspect of wine evaluation and explore different wines to find the ones that align with your personal preferences.
11.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Accessing the 'citric acid' and 'pH' columns
citric_acid = wine_data['citric acid']
pH = wine_data['pH']
# Calculating the correlation coefficient
correlation = citric_acid.corr(pH)
print("Correlation coefficient between citric acid content and pH:", correlation)
12.
import pandas as pd
import statsmodels.api as sm
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Accessing the 'citric acid' and 'pH' columns
citric_acid = wine_data['citric acid']
pH = wine_data['pH']
# Adding a constant term to the citric acid values for the regression model
X = sm.add_constant(citric_acid)
# Creating the linear regression model and fitting it to the data
model = sm.OLS(pH, X)
results = model.fit()
# Getting the regression coefficients
intercept = results.params[0]
slope = results.params[1]
print("Regression of pH on citric acid:")
print("Intercept:", intercept)
print("Slope:", slope)
13.
import pandas as pd
import statsmodels.api as sm
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Extracting the independent variables from the dataset
independent_vars = wine_data.drop(columns=['pH'])
# Adding a constant term to the independent variables for the regression model
X = sm.add_constant(independent_vars)
# Creating the linear regression model and fitting it to the data
model = sm.OLS(wine_data['pH'], X)
results = model.fit()
# Performing multicollinearity analysis
vif = pd.DataFrame()
vif["Variable"] = X.columns
vif["VIF"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
# Counting the number of independent variables (VIF < 5 is commonly used as a threshold for independence)
num_independent_vars = sum(vif["VIF"] < 5)
print("Number of independent variables:", num_independent_vars)
14.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Create a new column 'Acidity_Level' based on pH values
wine_data['Acidity_Level'] = pd.cut(wine_data['pH'], bins=[0, 3.2, 3.4, 3.6, 14], labels=['High', 'Moderately_High', 'Medium', 'Low'])
# Display the updated dataset
print(wine_data)
15.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Create a new column 'Acidity_Level' based on pH values
wine_data['Acidity_Level'] = pd.cut(wine_data['pH'], bins=[0, 3.2, 3.4, 3.6, 14], labels=['High', 'Moderately_High', 'Medium', 'Low'])
# Calculate the mean quality for each acidity level
mean_quality = wine_data.groupby('Acidity_Level')['quality'].mean()
# Display the mean quality for each acidity level
print(mean_quality)
16.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Calculate the average quality for red and white wines
red_wine_quality = wine_data[wine_data['type'] == 'red']['quality'].mean()
white_wine_quality = wine_data[wine_data['type'] == 'white']['quality'].mean()
# Display the average quality for red and white wines
print("Red wine average quality:", red_wine_quality)
print("White wine average quality:", white_wine_quality)
17.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Calculate the median alcohol content
median_alcohol = wine_data['alcohol'].median()
# Select samples with alcohol content less than the median
samples_below_median = wine_data[wine_data['alcohol'] < median_alcohol]
# Display the selected samples
print(samples_below_median)
18.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Calculate the median alcohol content
median_alcohol = wine_data['alcohol'].median()
# Select samples with alcohol content less than the median (low alcohol group)
low_alcohol_group = wine_data[wine_data['alcohol'] < median_alcohol]
# Select samples with alcohol content greater than or equal to the median (high alcohol group)
high_alcohol_group = wine_data[wine_data['alcohol'] >= median_alcohol]
# Get the quality ratings for the low alcohol group
low_alcohol_quality = low_alcohol_group['quality']
# Get the quality ratings for the high alcohol group
high_alcohol_quality = high_alcohol_group['quality']
# Display the quality ratings
print("Quality ratings for the low alcohol group:")
print(low_alcohol_quality)
print()
print("Quality ratings for the high alcohol group:")
print(high_alcohol_quality)
19.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Group the wines based on residual sugar levels and calculate the average quality rating
average_quality_by_sugar = wine_data.groupby(pd.cut(wine_data['residual sugar'], bins=[0, 2, 4, 6, 8, float('inf')]))['quality'].mean()
# Display the average quality rating for different residual sugar levels
print(average_quality_by_sugar)
20.
import pandas as pd
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Group the wines based on ratings and color and calculate the count
count_by_rating_color = wine_data.groupby(['rating', 'color']).size()
# Display the count of wines based on ratings and color
print(count_by_rating_color)
21.
import pandas as pd
import scipy.stats as stats
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Separate the red wine data and white wine data
red_wine_data = wine_data[wine_data['color'] == 'red']
white_wine_data = wine_data[wine_data['color'] == 'white']
# Perform a t-test to compare the density of red wine and white wine
t_statistic, p_value = stats.ttest_ind(red_wine_data['density'], white_wine_data['density'], equal_var=False)
# Display the results
print("T-Statistic:", t_statistic)
print("P-Value:", p_value)
22.
import pandas as pd
import numpy as np
import statsmodels.stats.proportion as proportion
# Assuming you have the wine dataset loaded into a pandas DataFrame called 'wine_data'
# Filter the DataFrame to include only white wine with rating 3
white_wine_rating3 = wine_data[(wine_data['color'] == 'white') & (wine_data['quality'] == 3)]
# Filter the DataFrame to include only red wine with rating 3
red_wine_rating3 = wine_data[(wine_data['color'] == 'red') & (wine_data['quality'] == 3)]
# Calculate the proportions
white_wine_proportion = len(white_wine_rating3) / len(wine_data[wine_data['color'] == 'white'])
red_wine_proportion = len(red_wine_rating3) / len(wine_data[wine_data['color'] == 'red'])
# Calculate the confidence interval
conf_interval = proportion.confint_proportions_2indep(len(white_wine_rating3), len(wine_data[wine_data['color'] == 'white']),
len(red_wine_rating3), len(wine_data[wine_data['color'] == 'red']),
method='normal')
# Display the results
print("Proportion of white wine with rating 3:", white_wine_proportion)
print("Proportion of red wine with rating 3:", red_wine_proportion)
print("Confidence Interval:", conf_interval)