text_plain_output_1.png
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)
    print('Max leaf nodes: %d  \t\t Mean Absolute Error:  %.5f' % (max_leaf_nodes, mae))