text_plain_output_1.png
from sklearn.ensemble import RandomForestRegressor

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_absolute_error

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.tree import DecisionTreeRegressor

import matplotlib.pyplot as plt # pyplot plotting tool

import pandas as pd # data processing

import seaborn as sns # seaborn plotting tool

winedf = pd.read_csv('/kaggle/input/red-wine-quality-cortez-et-al-2009/winequality-red.csv')

winedf.quality.value_counts()

corr = winedf.corr()

X = winedf.iloc[:, :-1]
y = winedf['quality']

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=21, test_size=0.3)
scaler = StandardScaler().fit(X_train)
standardized_X = scaler.transform(X_train)
standardized_X_test = scaler.transform(X_test)

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(standardized_X, y_train)

y_pred = lr.predict(standardized_X_test)

from sklearn.tree import DecisionTreeRegressor
dtree = DecisionTreeRegressor(random_state=22)
dtree.fit(X_train, y_train)

def get_mae(max_leaf_nodes, X_train, X_test, y_train, y_test):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=1)
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    mae = mean_absolute_error(y_test, y_pred)
    return mae
for max_leaf_nodes in [5, 10, 25, 50, 250, 500, 1000, 5000]:
    mae = get_mae(max_leaf_nodes, X_train, X_test, y_train, y_test)

model_1 = RandomForestRegressor(n_estimators=50, random_state=0)
model_2 = RandomForestRegressor(n_estimators=100, random_state=0)
model_3 = RandomForestRegressor(n_estimators=100, criterion='mae', random_state=0)
model_4 = RandomForestRegressor(n_estimators=200, min_samples_split=20, random_state=0)
model_5 = RandomForestRegressor(n_estimators=100, max_depth=7, random_state=0)
models = [model_1, model_2, model_3, model_4, model_5]

def score_model(model, X_t=X_train, X_v=X_test, y_t=y_train, y_v=y_test):
    model.fit(X_t, y_t)
    preds = model.predict(X_v)
    return mean_absolute_error(y_v, preds)
for i in range(0, len(models)):
    mae = score_model(models[i])
    print('Model %d MAE: %f' % (i + 1, mae))