Project 2
1. Display the data of white wine and red wine:
-
Assuming the dataset in a pandas DataFrame (
df), filter the white and red wines as follows:white_wine = df[df['type'] == 'white'] red_wine = df[df['type'] == 'red'] print(white_wine) print(red_wine)
2. Get the size of both white wine and red wine:
-
get the size of each dataset by:
white_size = white_wine.shape[0] red_size = red_wine.shape[0] print(f"White wine size: {white_size}") print(f"Red wine size: {red_size}")
3. Get the number of nulls in both types of wines:
-
To find the number of null values:
white_nulls = white_wine.isnull().sum() red_nulls = red_wine.isnull().sum() print(f"White wine nulls:\n{white_nulls}") print(f"Red wine nulls:\n{red_nulls}")
4. Check for duplicates and remove them in both types of wine data:
-
To check for and remove duplicates:
white_wine = white_wine.drop_duplicates() red_wine = red_wine.drop_duplicates()
5. Get the number of unique values in all features and print them:
-
To check unique values:
white_uniques = white_wine.nunique() red_uniques = red_wine.nunique() print(f"Unique values in white wine:\n{white_uniques}") print(f"Unique values in red wine:\n{red_uniques}")
6. Print the mean density of both the sets:
-
To calculate and print the mean density:
mean_density_white = white_wine['density'].mean() mean_density_red = red_wine['density'].mean() print(f"Mean density of white wine: {mean_density_white}") print(f"Mean density of red wine: {mean_density_red}")
7. Combine both datasets with the color of the wine in a column:
-
To combine the datasets and add a 'color' column:
white_wine['color'] = 'white' red_wine['color'] = 'red' combined_wine = pd.concat([white_wine, red_wine])
8. Remove the null values if any:
-
To remove rows with null values:
combined_wine = combined_wine.dropna()
9. Remove the outliers if any:
-
You can use the Z-score method or IQR method to detect and remove outliers. For simplicity, here's an IQR-based method:
Q1 = combined_wine['density'].quantile(0.25) Q3 = combined_wine['density'].quantile(0.75) IQR = Q3 - Q1 combined_wine = combined_wine[(combined_wine['density'] >= (Q1 - 1.5 * IQR)) & (combined_wine['density'] <= (Q3 + 1.5 * IQR))]
10. Do wines with higher alcohol content receive better ratings?
-
Use a correlation between alcohol content and quality:
correlation = combined_wine['alcohol'].corr(combined_wine['quality']) print(f"Correlation between alcohol content and quality: {correlation}")
11. Find the correlation between citric acid content and pH:
-
To find the correlation:
correlation_citric_ph = combined_wine['citric acid'].corr(combined_wine['pH']) print(f"Correlation between citric acid and pH: {correlation_citric_ph}")
12. Find regression of pH on citric acid:
-
To perform regression:
from sklearn.linear_model import LinearRegression X = combined_wine[['citric acid']] y = combined_wine['pH'] model = LinearRegression() model.fit(X, y) print(f"Regression Coefficients: {model.coef_}, Intercept: {model.intercept_}")
13. Find how many variables are independent:
-
To check multicollinearity and find independent variables, calculate the Variance Inflation Factor (VIF) for each feature:
from statsmodels.stats.outliers_influence import variance_inflation_factor X = combined_wine.drop(columns='quality') vif = pd.DataFrame() vif['Variable'] = X.columns vif['VIF'] = [variance_inflation_factor(X.values, i) for i in range(len(X.columns))] print(vif)
14. Create a new column as the acidity level. Divide the existing data into 5 groups based on the acidity level. The levels are to be named ['High', 'Moderately_High', 'Medium', 'Low']. Acidity may be obtained from the pH.
-
To create the acidity level:
bins = [0, 3.2, 3.5, 3.8, 4.0, 4.5] labels = ['High', 'Moderately_High', 'Medium', 'Low', 'Very_Low'] combined_wine['acidity_level'] = pd.cut(combined_wine['pH'], bins=bins, labels=labels)
15. Find the mean quality of each acidity level with groupby. Use groupby:
-
To calculate mean quality for each acidity level:
mean_quality_acidity = combined_wine.groupby('acidity_level')['quality'].mean() print(f"Mean quality for each acidity level:\n{mean_quality_acidity}")
16. Is a certain type of wine (red or white) associated with higher quality?
-
To check the average quality of each wine type:
mean_quality_wine_type = combined_wine.groupby('color')['quality'].mean() print(f"Mean quality for each wine type:\n{mean_quality_wine_type}")
17. Select samples with alcohol content less than the median:
-
To filter the samples:
median_alcohol = combined_wine['alcohol'].median() low_alcohol = combined_wine[combined_wine['alcohol'] < median_alcohol] print(f"Samples with alcohol content less than the median:\n{low_alcohol}")
18. Get the quality rating for the low alcohol and high alcohol groups:
-
To separate by alcohol content:
high_alcohol = combined_wine[combined_wine['alcohol'] >= median_alcohol] print(f"Low alcohol quality ratings:\n{low_alcohol['quality']}") print(f"High alcohol quality ratings:\n{high_alcohol['quality']}")
19. Do sweeter wines get more ratings?
-
Check if residual sugar correlates with quality:
correlation_sugar_quality = combined_wine['residual sugar'].corr(combined_wine['quality']) print(f"Correlation between residual sugar and quality: {correlation_sugar_quality}")
20. Get the number of counts based on ratings and color of wine:
-
To count by wine color and ratings:
counts_by_rating_color = combined_wine.groupby(['color', 'quality']).size() print(f"Counts based on ratings and wine color:\n{counts_by_rating_color}")
21. Is the density of red wine and white wine similar?
-
To compare density of red and white wines:
density_comparison = combined_wine.groupby('color')['density'].mean() print(f"Average density for each wine type:\n{density_comparison}")
22. Is the confidence interval for the differences of the proportion of white wine with rating 3 and the proportion of red wine with rating 3?
-
Perform a Z-test for proportions to compare the proportions of white and red wines with a rating of 3:
from statsmodels.stats.proportion import proportions_ztest white_rating_3 = (white_wine['quality'] == 3).sum() red_rating_3 = (red_wine['quality'] == 3).sum() n_white = white_wine.shape[0] n_red = red_wine.shape[0] z_stat, p_value = proportions_ztest([white_rating_3, red_rating_3], [n_white, n_red]) print(f"Z-statistic: {z_stat}, p-value: {p_value}")