MACHINE LEARNING RECIPES

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()
```

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