In [ ]:
In [ ]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
#from sklearn.preprocessing import StandardScaler
In [ ]:
df= pd.read_csv('winequality.csv')
df.head()
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | quality | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 | 5 |
| 1 | 7.8 | 0.88 | 0.00 | 2.6 | 0.098 | 25.0 | 67.0 | 0.9968 | 3.20 | 0.68 | 9.8 | 5 |
| 2 | 7.8 | 0.76 | 0.04 | 2.3 | 0.092 | 15.0 | 54.0 | 0.9970 | 3.26 | 0.65 | 9.8 | 5 |
| 3 | 11.2 | 0.28 | 0.56 | 1.9 | 0.075 | 17.0 | 60.0 | 0.9980 | 3.16 | 0.58 | 9.8 | 6 |
| 4 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 | 5 |
In [ ]:
#task1
features= df.iloc[:, 0:11]
features.head()
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 |
| 1 | 7.8 | 0.88 | 0.00 | 2.6 | 0.098 | 25.0 | 67.0 | 0.9968 | 3.20 | 0.68 | 9.8 |
| 2 | 7.8 | 0.76 | 0.04 | 2.3 | 0.092 | 15.0 | 54.0 | 0.9970 | 3.26 | 0.65 | 9.8 |
| 3 | 11.2 | 0.28 | 0.56 | 1.9 | 0.075 | 17.0 | 60.0 | 0.9980 | 3.16 | 0.58 | 9.8 |
| 4 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 |
In [ ]:
label= df[['quality']]
label.head()
Out[ ]:
| quality | |
|---|---|
| 0 | 5 |
| 1 | 5 |
| 2 | 5 |
| 3 | 6 |
| 4 | 5 |
In [ ]:
features.columns.values
Out[ ]:
array(['fixed acidity', 'volatile acidity', 'citric acid',
'residual sugar', 'chlorides', 'free sulfur dioxide',
'total sulfur dioxide', 'density', 'pH', 'sulphates', 'alcohol'],
dtype=object)
In [ ]:
#task2
plt.figure(figsize=(5,3))
sns.countplot(data=df, x='quality', hue='quality', palette='Set1')
plt.show()
here are our observations:¶
*The qualities are not evenly distributed *Qualities 5 and 6 are the most common in our dataset. in other words, most of the wines are median level quality.
In [ ]:
#task 3
plt.figure(figsize=(4,4))
sns.jointplot(data=df, x= 'sulphates', y='fixed acidity', kind= 'reg', color='steelblue')
plt.show()
<Figure size 400x400 with 0 Axes>
In [ ]:
#task4
len(df)*0.8
Out[ ]:
1279.2
In [ ]:
#first 80% of the data as the training set, knn
#features for model training
train_x= df.iloc[0:1280,0:11]
train_x.head()
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 |
| 1 | 7.8 | 0.88 | 0.00 | 2.6 | 0.098 | 25.0 | 67.0 | 0.9968 | 3.20 | 0.68 | 9.8 |
| 2 | 7.8 | 0.76 | 0.04 | 2.3 | 0.092 | 15.0 | 54.0 | 0.9970 | 3.26 | 0.65 | 9.8 |
| 3 | 11.2 | 0.28 | 0.56 | 1.9 | 0.075 | 17.0 | 60.0 | 0.9980 | 3.16 | 0.58 | 9.8 |
| 4 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 |
In [ ]:
#target vairable for model training
train_y= df.iloc[0:1280, -1]
train_y.head()
Out[ ]:
| quality | |
|---|---|
| 0 | 5 |
| 1 | 5 |
| 2 | 5 |
| 3 | 6 |
| 4 | 5 |
In [ ]:
#model construction
model= KNeighborsClassifier(n_neighbors=5)
model.fit(train_x, train_y)
Out[ ]:
KNeighborsClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
KNeighborsClassifier()
In [ ]:
lafite = [[4, 0, 0, 2, 0.1, 2, 6, 1, 3.5, 0.7, 8]]
In [ ]:
prediction= model.predict(lafite)
print(prediction)
[4]
/usr/local/lib/python3.12/dist-packages/sklearn/utils/validation.py:2739: UserWarning: X does not have valid feature names, but KNeighborsClassifier was fitted with feature names warnings.warn(
In [ ]:
#rescale your feature set and then redefine your KNN
#standard scaler
scaler= StandardScaler()
norm_features= scaler.fit_transform(features)
norm_features_df= pd.DataFrame(norm_features, columns=features.columns)
norm_features_df.head()
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -0.528360 | 0.961877 | -1.391472 | -0.453218 | -0.243707 | -0.466193 | -0.379133 | 0.558274 | 1.288643 | -0.579207 | -0.960246 |
| 1 | -0.298547 | 1.967442 | -1.391472 | 0.043416 | 0.223875 | 0.872638 | 0.624363 | 0.028261 | -0.719933 | 0.128950 | -0.584777 |
| 2 | -0.298547 | 1.297065 | -1.186070 | -0.169427 | 0.096353 | -0.083669 | 0.229047 | 0.134264 | -0.331177 | -0.048089 | -0.584777 |
| 3 | 1.654856 | -1.384443 | 1.484154 | -0.453218 | -0.264960 | 0.107592 | 0.411500 | 0.664277 | -0.979104 | -0.461180 | -0.584777 |
| 4 | -0.528360 | 0.961877 | -1.391472 | -0.453218 | -0.243707 | -0.466193 | -0.379133 | 0.558274 | 1.288643 | -0.579207 | -0.960246 |
In [ ]:
# new train_x and train_y
train_norm_x= norm_features_df.iloc[0:1280, 0:11]
train_norm_y= df.iloc[0:1280, -1]
train_norm_x.head()
train_norm_y.head()
Out[ ]:
| quality | |
|---|---|
| 0 | 5 |
| 1 | 5 |
| 2 | 5 |
| 3 | 6 |
| 4 | 5 |
In [ ]:
model_norm= KNeighborsClassifier(n_neighbors=5)
model_norm.fit(train_norm_x, train_norm_y)
Out[ ]:
KNeighborsClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
KNeighborsClassifier()
In [ ]: