**********************
Train and Score Models
**********************

.. _installing-docdir:

Train Models
-------------
The following methods are used to train models
 - Linear Regression 
 - Decision Tree 
 - Random Forest

Python module sklearn is imported for regression::

      from sklearn.ensemble import RandomForestRegressor
      from sklearn.linear_model import LinearRegression
      from sklearn.tree import DecisionTreeRegressor

Load the housing.csv dataset to train the model

k-fold Cross validation
-----------------------
Cross validation creates and evaluate multiple models on 
multiple subsets of datasets. 10-fold cross validation is used
to evaluate each algorithms and same random seed to ensure
same splits of training data.

Regression Metrics
------------------
Mean squared error is one of the most used regression metrics.
It gives an idea of the magnitude of error. The model with least
RSE value is prefered over others, since it gives accurate results
and less errors

The models can be trained and scored by::

    models = []
    models.append(("Linear Regression", LinearRegression()))
    models.append(("Decision Tree Regressor", DecisionTreeRegressor(random_state=42)))
    models.append(("Random Forest regressor",
        RandomForestRegressor(max_features=6, n_estimators=30, random_state=42),))
    seed = 7
    results = []
    for name, model in models:
        kfold = model_selection.KFold(n_splits=10, random_state=seed, shuffle=True)
        scoring = "neg_mean_squared_error"
        result = model_selection.cross_val_score(model, x, y, scoring=scoring, cv=kfold)
        results.append(result)msg = "%s: %f (%f)" % (name, result.mean(), result.std())
        print(msg)

 


