import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
df = pd.read_csv('../input/red-wine-quality-cortez-et-al-2009/winequality-red.csv')
df.head()
df.tail()
df.shape
df.describe().T
df.info()
"""
## EDA
"""
fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(18, 14))
sns.histplot(data=df, x="fixed acidity", kde=True,ax=axes[0][0])
sns.histplot(data=df, x="volatile acidity", kde=True,ax=axes[0][1])
sns.histplot(data=df, x="citric acid", kde=True,ax=axes[0][2])
sns.histplot(data=df, x="residual sugar", kde=True,ax=axes[1][0])
sns.histplot(data=df, x="chlorides", kde=True,ax=axes[1][1])
sns.histplot(data=df, x="free sulfur dioxide", kde=True,ax=axes[1][2])
sns.histplot(data=df, x="total sulfur dioxide", kde=True,ax=axes[2][0])
sns.histplot(data=df, x="density", kde=True,ax=axes[2][1])
sns.histplot(data=df, x="pH", kde=True,ax=axes[2][2])
sns.histplot(data=df, x="sulphates", kde=True,ax=axes[3][0])
sns.histplot(data=df, x="alcohol", kde=True,ax=axes[3][1])
sns.histplot(data=df, x="quality", kde=True,ax=axes[3][2])
labels =df['quality'].value_counts(sort = True).index
sizes = df['quality'].value_counts(sort = True)
colors = ["lightblue","yellow"]
plt.figure(figsize=(16,12))
plt.pie(sizes,labels=labels, colors=colors, autopct='%1.1f%%', startangle=20,)
plt.title('quality pie')
plt.show()
PP = sns.pairplot(df, diag_kind="kde",hue="quality")

plt.figure(figsize=(16,8))
sns.heatmap(df.corr(),annot=True)
df.columns
"""
#### fixed acidity
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["fixed acidity"],color='lightblue')
df=df[(np.abs(stats.zscore(df["fixed acidity"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["fixed acidity"],color='lightblue')
"""
#### volatile acidity
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["volatile acidity"],color='lightblue')
df=df[(np.abs(stats.zscore(df["volatile acidity"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["volatile acidity"],color='lightblue')
"""
#### citric acid
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["citric acid"],color='lightblue')
df=df[(np.abs(stats.zscore(df["citric acid"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["citric acid"],color='lightblue')
"""
#### residual sugar
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["residual sugar"],color='lightblue')
df=df[(np.abs(stats.zscore(df["residual sugar"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["residual sugar"],color='lightblue')
"""
#### chlorides
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["chlorides"],color='lightblue')
df=df[(np.abs(stats.zscore(df["chlorides"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["chlorides"],color='lightblue')
"""
#### free sulfur dioxide
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["free sulfur dioxide"],color='lightblue')
df=df[(np.abs(stats.zscore(df["free sulfur dioxide"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["free sulfur dioxide"],color='lightblue')
"""
#### total sulfur dioxide
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["total sulfur dioxide"],color='lightblue')
df=df[(np.abs(stats.zscore(df["total sulfur dioxide"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["total sulfur dioxide"],color='lightblue')
"""
#### density
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["density"],color='lightblue')
df=df[(np.abs(stats.zscore(df["density"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["density"],color='lightblue')
"""
#### pH
"""
df.columns
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["pH"],color='lightblue')
df=df[(np.abs(stats.zscore(df["pH"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["pH"],color='lightblue')
"""
#### sulphates
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["sulphates"],color='lightblue')
df=df[(np.abs(stats.zscore(df["sulphates"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["sulphates"],color='lightblue')
"""
#### alcohol
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["alcohol"],color='lightblue')
df=df[(np.abs(stats.zscore(df["alcohol"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["alcohol"],color='lightblue')
"""
#### quality
"""
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["quality"],color='lightblue')
df=df[(np.abs(stats.zscore(df["quality"])) < 3)]
plt.figure(figsize=(8,4))
sns.boxplot(data=df,x=df["quality"],color='lightblue')
df.head()
df.columns
from sklearn import preprocessing
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score
from sklearn.metrics import roc_auc_score
from collections import Counter
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from imblearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
robust_sc = preprocessing.RobustScaler()
standard_sc = preprocessing.StandardScaler() 
minmax_sc = preprocessing.MinMaxScaler() 
reviews = []
for i in df['quality']:
    if i >= 1 and i <= 5:
        reviews.append('1')
    elif i >= 6 and i <= 7:
        reviews.append('2')
    elif i >= 8 and i <= 10:
        reviews.append('3')
df['Reviews'] = reviews
X = df.drop(["quality",'Reviews'],axis=1)
y = df["Reviews"]
resultado = []
for x in [robust_sc,standard_sc,minmax_sc]:
    %time
    scaler = x.fit(X)
    X_new = x.transform(X)
    tree = DecisionTreeClassifier(random_state=42)
    tree.fit(X_new,y)
    y_pred = tree.predict(X_new)
    f1sc=f1_score(y, y_pred, average='weighted')
    rauc=(y, y_pred)
    resultado.append(f1sc)
    print("El escalado Utilizado--->",x)
    print("f1 segun el tipo de estrategia:",f1sc)
    print("----------------------------------------")
Counter(df['Reviews'])
over = SMOTE()
under = RandomUnderSampler()
steps = [('o', over), ('u', under)]
pipeline = Pipeline(steps=steps)
X, y = pipeline.fit_resample(X, y)
Counter(y)
X_train,X_test,y_train,y_test= train_test_split(X,y,test_size=0.3,random_state=42,stratify=y)
X_train=standard_sc.fit_transform(X_train)
X_test=standard_sc.transform(X_test)
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
"""
### DecisionTreeClassifier
"""
dt = DecisionTreeClassifier()
dt.fit(X_train,y_train)
dt_predict = dt.predict(X_test)
print(classification_report(y_test, dt_predict))
scores = cross_val_score(dt, X_train, y_train, cv=5)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))
"""
### RandomForestClassifier
"""
rfc = RandomForestClassifier(n_estimators=250)
rfc.fit(X_train, y_train)
pred_rfc = rfc.predict(X_test)
print(classification_report(y_test, pred_rfc))
scores = cross_val_score(rfc, X_train, y_train, cv=5)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))
"""
### SGDClassifier
"""
sgd = SGDClassifier(penalty=None)
sgd.fit(X_train, y_train)
pred_sgd = sgd.predict(X_test)
print(classification_report(y_test, pred_sgd))
scores = cross_val_score(sgd, X_train, y_train, cv=5)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))
"""
### SVC
"""
svc = SVC()
svc.fit(X_train, y_train)
pred_svc = svc.predict(X_test)
print(classification_report(y_test, pred_svc))
scores = cross_val_score(svc, X_train, y_train, cv=5)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))
rbf_svc = SVC(kernel='linear')
rbf_svc.fit(X_train, y_train)
rbf_svc_res=rbf_svc.predict(X_test)
print(classification_report(y_test, rbf_svc_res))
scores = cross_val_score(rbf_svc, X_train, y_train, cv=5)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))
from xgboost import XGBClassifier
from sklearn.neural_network import MLPClassifier
"""
### XGB
"""
gbm = XGBClassifier(verbosity=1)
params_xgb = {
        "n_estimators":[500,1000,1500],
        "learning_rate":[0.1,0.3,0.6],
        'gpu_id': [0],
        "predictor":["gpu_predictor"],
        'tree_method': ['gpu_hist'],
        "updater":["grow_gpu_hist"],
        "sampling_method":["gradient_based"],
        "updater":["grow_gpu_hist"]
}
model_xgb = GridSearchCV(gbm,param_grid=params_xgb, cv=3,n_jobs=-1)
model_xgb.fit(X_train,y_train)
print("Best params: "+str(model_xgb.best_params_))
print("Best Score: "+str(model_xgb.best_score_)+'\n')
scores = pd.DataFrame(model_xgb.cv_results_)
scores.sort_values(by="rank_test_score")
y_train_pred_xgb = model_xgb.predict(X_train)
y_test_pred_xgb = model_xgb.predict(X_test)
print(classification_report(y_test, y_test_pred_xgb))
"""
### MLP
"""
clf = MLPClassifier(random_state=42)
params_MLP = {
        "hidden_layer_sizes":[64,128,256],
        "activation":["identity", "logistic", "tanh", "relu"],
        'solver': ["lbfgs", "sgd", "adam"],
        "learning_rate":["constant", "invscaling", "adaptive"],
        'max_iter': [100,200],
        "warm_start":[True]
}
model_MLP = GridSearchCV(clf,param_grid=params_MLP, cv=3,n_jobs=-1)
model_MLP.fit(X_train,y_train)
print("Best params: "+str(model_MLP.best_params_))
print("Best Score: "+str(model_MLP.best_score_)+'\n')

scores = pd.DataFrame(model_MLP.cv_results_)
scores.sort_values(by="rank_test_score")
y_train_pred_MLP = model_MLP.predict(X_train)
y_test_pred_MLP = model_MLP.predict(X_test)

print(classification_report(y_test, y_test_pred_MLP))
"""
### RandomForest CV
"""
clf = RandomForestClassifier(random_state=42)
params_RF = {
        "max_depth":[250,500,1000],
        "criterion":["gini", "entropy"],
        'min_samples_split': [2,4,6],
        "min_samples_leaf":[1,2,3],
        "max_features":['auto', 'sqrt', 'log2'],
        'warm_start':[True],
        'class_weight':['balanced', 'balanced_subsample']
}
model_RF = GridSearchCV(clf,param_grid=params_RF, cv=3,n_jobs=-1)
model_RF.fit(X_train,y_train)
print("Best params: "+str(model_RF.best_params_))
print("Best Score: "+str(model_RF.best_score_)+'\n')
scores = pd.DataFrame(model_RF.cv_results_)
scores.sort_values(by="rank_test_score")
y_train_pred_RF = model_RF.predict(X_train)
y_test_pred_RF = model_RF.predict(X_test)
print(classification_report(y_test, y_test_pred_RF))
"""
## Conclusion:  Random Forest Wins
"""