Recipe: How to use Regression Metrics in Python?
MACHINE LEARNING RECIPES

How to use Regression Metrics in Python?

This recipe helps you use Regression Metrics in Python
In [1]:
## How to use Regression Metrics in Python
## DataSet: skleran.load_boston()
def Snippet_182():
    print()
    print(format('How to use Regression Metrics in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import datasets
    from sklearn import tree, model_selection
    from sklearn.model_selection import train_test_split
    import matplotlib.pyplot as plt

    plt.style.use('ggplot')

    # load the iris datasets
    seed = 42
    dataset = datasets.load_boston()
    X = dataset.data; y = dataset.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    # fit a tree.DecisionTreeClassifier() model to the data
    model = tree.DecisionTreeRegressor()

    # metrics -> Mean Absolute Error
    scoring = 'neg_mean_absolute_error'
    results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
    print(); print("Mean Absolute Error: ", results.mean()); print("Standard Deviation: ", results.std())

    # metrics -> Mean Squred Error
    scoring = 'neg_mean_squared_error'
    results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
    print(); print("Mean Squared Error: ", results.mean()); print("Standard Deviation: ", results.std())

    # metrics -> R squared
    scoring = 'r2'
    results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
    print(); print("R squared val: ", results.mean()); print("Standard Deviation: ", results.std())
Snippet_182()
*********************How to use Regression Metrics in Python**********************

Mean Absolute Error:  -3.0952560455192035
Standard Deviation:  0.4539146116435922

Mean Squared Error:  -21.0677652916074
Standard Deviation:  9.255250927975112

R squared val:  0.7561802285707946
Standard Deviation:  0.1692887494698974


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe