```python
%matplotlib inline
import numpy as np
import pylab as plt
import pandas as pd
from sklearn import svm
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
```


```python
np.random.seed(0)
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = [0] * 20 + [1] * 20
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.coolwarm, s=25)
```


```python
fig, ax = plt.subplots()
clf2 = svm.LinearSVC(C=1).fit(X, Y)

# get the separating hyperplane
w = clf2.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf2.intercept_[0]) / w[1]

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .2),
                     np.arange(y_min, y_max, .2))
Z = clf2.predict(np.c_[xx2.ravel(), yy2.ravel()])

Z = Z.reshape(xx2.shape)
ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.3)
ax.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.coolwarm, s=25)
ax.plot(xx,yy)

ax.axis([x_min, x_max,y_min, y_max])
plt.show()
```

**Supervised learning**:

  - Linear models (Ridge, Lasso, Elastic Net, ...)
  - Support Vector Machines
  - Tree-based methods (Random Forests, Bagging, GBRT, ...)
  - Nearest neighbors 
  - Neural networks (basics)

**Unsupervised learning**:

  - Clustering (KMeans, Ward, ...)
  - Outlier detection

## Accuracy and precision

1. precision: 
The fraction of relevant instances among the retrieved instances, 
1. recall:
The fraction of relevant instances that have been retrieved over the total amount of relevant instances. 
1. F-score


* The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.

* The recall is the ratio tp / (tp + fn) where tp is the number of true positives and fn the number of false negatives. The recall is intuitively the ability of the classifier to find all the positive samples.

* The F-beta score can be interpreted as a weighted harmonic mean of the precision and recall, where an F-beta score reaches its best value at 1 and worst score at 0. The F-beta score weights recall more than precision by a factor of beta. beta == 1.0 means recall and precision are equally important.

* The support is the number of occurrences of each class in y_true.

Take a look at [HERE](https://en.wikipedia.org/wiki/F1_score) or [HERE](https://en.wikipedia.org/wiki/Precision_and_recall).


```python
# Generate data
from sklearn.datasets import make_blobs,make_circles,make_moons
# X, y = make_blobs(n_samples=1000, centers=2,
#                   cluster_std=1.5,
#                   center_box=(-4.0, 4.0))
# X, y = make_circles(n_samples=1000, noise=.1, factor=.5)
X,y = make_moons(n_samples=1000, noise=.2)

plt.scatter(X[:,0],X[:,1],c=y,)

```


```python
print(X[:3]) 
```

    [[-0.36188439  1.18939692]
     [ 1.00567821  0.91527408]
     [-1.13965547  0.29097234]]



```python
print(y[:3])
```

    [0 0 0]



```python
# X is a 2 dimensional array, with 1000 rows and 2 columns
print(X.shape)
 
# y is a vector of 1000 elements
print(y.shape)
```

    (1000, 2)
    (1000,)



```python
X_train, y_train = X[:700], y[:700]
X_test, y_test = X[700:], y[700:]
```


```python
fig,(ax1,ax2) = plt.subplots(1,2,figsize=(14,7))

ax1.scatter(X_train[:,0],X_train[:,1],c=y_train,)
ax2.scatter(X_test[:,0],X_test[:,1],c=y_test,)

```

## K-Nearest Neighbours


```python
# K-Nearest Neighbours
from sklearn.neighbors import KNeighborsClassifier

Model = KNeighborsClassifier(n_neighbors=8)
Model.fit(X_train, y_train)

y_pred = Model.predict(X_test)

# Summary of the predictions made by the classifier
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
# Accuracy score

print('accuracy is',accuracy_score(y_pred,y_test))
```

                  precision    recall  f1-score   support
    
               0       0.96      0.97      0.97       151
               1       0.97      0.96      0.97       149
    
       micro avg       0.97      0.97      0.97       300
       macro avg       0.97      0.97      0.97       300
    weighted avg       0.97      0.97      0.97       300
    
    [[147   4]
     [  6 143]]
    accuracy is 0.9666666666666667



```python
# Compute (approximate) class probabilities
print(Model.predict_proba(X_test[:5]))
```

    [[1. 0.]
     [1. 0.]
     [0. 1.]
     [0. 1.]
     [1. 0.]]


##  Radius Neighbors Classifier


```python
from sklearn.neighbors import  RadiusNeighborsClassifier
Model=RadiusNeighborsClassifier(radius=8.0)
Model.fit(X_train,y_train)
y_pred=Model.predict(X_test)
#summary of the predictions made by the classifier
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
#Accouracy score
print('accuracy is ', accuracy_score(y_test,y_pred))
```

                  precision    recall  f1-score   support
    
               0       1.00      0.32      0.48       158
               1       0.57      1.00      0.72       142
    
       micro avg       0.64      0.64      0.64       300
       macro avg       0.78      0.66      0.60       300
    weighted avg       0.80      0.64      0.60       300
    
    [[ 50 108]
     [  0 142]]
    accuracy is  0.64


## Naive Bayes

\begin{align}\begin{aligned}P(y \mid x_1, \dots, x_n) \propto P(y) \prod_{i=1}^{n} P(x_i \mid y)\\\Downarrow\\\hat{y} = \arg\max_y P(y) \prod_{i=1}^{n} P(x_i \mid y),\end{aligned}\end{align}


```python
# Naive Bayes
from sklearn.naive_bayes import GaussianNB
Model = GaussianNB()
Model.fit(X_train, y_train)

y_pred = Model.predict(X_test)

# Summary of the predictions made by the classifier
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
# Accuracy score
print('accuracy is',accuracy_score(y_pred,y_test))
```

                  precision    recall  f1-score   support
    
               0       0.93      0.93      0.93       158
               1       0.92      0.92      0.92       142
    
       micro avg       0.93      0.93      0.93       300
       macro avg       0.93      0.93      0.93       300
    weighted avg       0.93      0.93      0.93       300
    
    [[147  11]
     [ 11 131]]
    accuracy is 0.9266666666666666


## SVM


```python
# Support Vector Machine
from sklearn.svm import SVC

Model = SVC(kernel='linear')
# Model = svm.LinearSVC(C=1)

Model.fit(X_train, y_train)

y_pred = Model.predict(X_test)

# Summary of the predictions made by the classifier
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
# Accuracy score

print('accuracy is',accuracy_score(y_pred,y_test))
```

                  precision    recall  f1-score   support
    
               0       0.93      0.94      0.94       158
               1       0.94      0.92      0.93       142
    
       micro avg       0.93      0.93      0.93       300
       macro avg       0.93      0.93      0.93       300
    weighted avg       0.93      0.93      0.93       300
    
    [[149   9]
     [ 11 131]]
    accuracy is 0.9333333333333333



```python
plt.scatter(X_test[:,0],X_test[:,1],c=y_pred,)
```

## Decision Tree


```python
# Decision Tree's
from sklearn.tree import DecisionTreeClassifier

Model = DecisionTreeClassifier()

Model.fit(X_train, y_train)

y_pred = Model.predict(X_test)

# Summary of the predictions made by the classifier
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
# Accuracy score
print('accuracy is',accuracy_score(y_pred,y_test))
```

                  precision    recall  f1-score   support
    
               0       0.88      0.87      0.88       158
               1       0.86      0.87      0.87       142
    
       micro avg       0.87      0.87      0.87       300
       macro avg       0.87      0.87      0.87       300
    weighted avg       0.87      0.87      0.87       300
    
    [[138  20]
     [ 18 124]]
    accuracy is 0.8733333333333333


## RandomForest


```python
from sklearn.ensemble import RandomForestClassifier
Model=RandomForestClassifier(max_depth=2)
Model.fit(X_train,y_train)
y_pred=Model.predict(X_test)
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_pred,y_test))
#Accuracy Score
print('accuracy is ',accuracy_score(y_pred,y_test))
```

                  precision    recall  f1-score   support
    
               0       0.93      0.94      0.93       158
               1       0.93      0.92      0.93       142
    
       micro avg       0.93      0.93      0.93       300
       macro avg       0.93      0.93      0.93       300
    weighted avg       0.93      0.93      0.93       300
    
    [[148  11]
     [ 10 131]]
    accuracy is  0.93


    /home/gf/packages/anaconda3/lib/python3.6/site-packages/sklearn/ensemble/forest.py:246: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
      "10 in version 0.20 to 100 in 0.22.", FutureWarning)


## Neural network


```python
from sklearn.neural_network import MLPClassifier
Model=MLPClassifier()
Model.fit(X_train,y_train)
y_pred=Model.predict(X_test)
# Summary of the predictions
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
#Accuracy Score
print('accuracy is ',accuracy_score(y_pred,y_test))
```

                  precision    recall  f1-score   support
    
               0       0.93      0.93      0.93       158
               1       0.92      0.92      0.92       142
    
       micro avg       0.93      0.93      0.93       300
       macro avg       0.93      0.93      0.93       300
    weighted avg       0.93      0.93      0.93       300
    
    [[147  11]
     [ 11 131]]
    accuracy is  0.9266666666666666



```python
help(MLPClassifier)
```

    Help on class MLPClassifier in module sklearn.neural_network.multilayer_perceptron:
    
    class MLPClassifier(BaseMultilayerPerceptron, sklearn.base.ClassifierMixin)
     |  Multi-layer Perceptron classifier.
     |  
     |  This model optimizes the log-loss function using LBFGS or stochastic
     |  gradient descent.
     |  
     |  .. versionadded:: 0.18
     |  
     |  Parameters
     |  ----------
     |  hidden_layer_sizes : tuple, length = n_layers - 2, default (100,)
     |      The ith element represents the number of neurons in the ith
     |      hidden layer.
     |  
     |  activation : {'identity', 'logistic', 'tanh', 'relu'}, default 'relu'
     |      Activation function for the hidden layer.
     |  
     |      - 'identity', no-op activation, useful to implement linear bottleneck,
     |        returns f(x) = x
     |  
     |      - 'logistic', the logistic sigmoid function,
     |        returns f(x) = 1 / (1 + exp(-x)).
     |  
     |      - 'tanh', the hyperbolic tan function,
     |        returns f(x) = tanh(x).
     |  
     |      - 'relu', the rectified linear unit function,
     |        returns f(x) = max(0, x)
     |  
     |  solver : {'lbfgs', 'sgd', 'adam'}, default 'adam'
     |      The solver for weight optimization.
     |  
     |      - 'lbfgs' is an optimizer in the family of quasi-Newton methods.
     |  
     |      - 'sgd' refers to stochastic gradient descent.
     |  
     |      - 'adam' refers to a stochastic gradient-based optimizer proposed
     |        by Kingma, Diederik, and Jimmy Ba
     |  
     |      Note: The default solver 'adam' works pretty well on relatively
     |      large datasets (with thousands of training samples or more) in terms of
     |      both training time and validation score.
     |      For small datasets, however, 'lbfgs' can converge faster and perform
     |      better.
     |  
     |  alpha : float, optional, default 0.0001
     |      L2 penalty (regularization term) parameter.
     |  
     |  batch_size : int, optional, default 'auto'
     |      Size of minibatches for stochastic optimizers.
     |      If the solver is 'lbfgs', the classifier will not use minibatch.
     |      When set to "auto", `batch_size=min(200, n_samples)`
     |  
     |  learning_rate : {'constant', 'invscaling', 'adaptive'}, default 'constant'
     |      Learning rate schedule for weight updates.
     |  
     |      - 'constant' is a constant learning rate given by
     |        'learning_rate_init'.
     |  
     |      - 'invscaling' gradually decreases the learning rate at each
     |        time step 't' using an inverse scaling exponent of 'power_t'.
     |        effective_learning_rate = learning_rate_init / pow(t, power_t)
     |  
     |      - 'adaptive' keeps the learning rate constant to
     |        'learning_rate_init' as long as training loss keeps decreasing.
     |        Each time two consecutive epochs fail to decrease training loss by at
     |        least tol, or fail to increase validation score by at least tol if
     |        'early_stopping' is on, the current learning rate is divided by 5.
     |  
     |      Only used when ``solver='sgd'``.
     |  
     |  learning_rate_init : double, optional, default 0.001
     |      The initial learning rate used. It controls the step-size
     |      in updating the weights. Only used when solver='sgd' or 'adam'.
     |  
     |  power_t : double, optional, default 0.5
     |      The exponent for inverse scaling learning rate.
     |      It is used in updating effective learning rate when the learning_rate
     |      is set to 'invscaling'. Only used when solver='sgd'.
     |  
     |  max_iter : int, optional, default 200
     |      Maximum number of iterations. The solver iterates until convergence
     |      (determined by 'tol') or this number of iterations. For stochastic
     |      solvers ('sgd', 'adam'), note that this determines the number of epochs
     |      (how many times each data point will be used), not the number of
     |      gradient steps.
     |  
     |  shuffle : bool, optional, default True
     |      Whether to shuffle samples in each iteration. Only used when
     |      solver='sgd' or 'adam'.
     |  
     |  random_state : int, RandomState instance or None, optional, default None
     |      If int, random_state is the seed used by the random number generator;
     |      If RandomState instance, random_state is the random number generator;
     |      If None, the random number generator is the RandomState instance used
     |      by `np.random`.
     |  
     |  tol : float, optional, default 1e-4
     |      Tolerance for the optimization. When the loss or score is not improving
     |      by at least ``tol`` for ``n_iter_no_change`` consecutive iterations,
     |      unless ``learning_rate`` is set to 'adaptive', convergence is
     |      considered to be reached and training stops.
     |  
     |  verbose : bool, optional, default False
     |      Whether to print progress messages to stdout.
     |  
     |  warm_start : bool, optional, default False
     |      When set to True, reuse the solution of the previous
     |      call to fit as initialization, otherwise, just erase the
     |      previous solution. See :term:`the Glossary <warm_start>`.
     |  
     |  momentum : float, default 0.9
     |      Momentum for gradient descent update. Should be between 0 and 1. Only
     |      used when solver='sgd'.
     |  
     |  nesterovs_momentum : boolean, default True
     |      Whether to use Nesterov's momentum. Only used when solver='sgd' and
     |      momentum > 0.
     |  
     |  early_stopping : bool, default False
     |      Whether to use early stopping to terminate training when validation
     |      score is not improving. If set to true, it will automatically set
     |      aside 10% of training data as validation and terminate training when
     |      validation score is not improving by at least tol for
     |      ``n_iter_no_change`` consecutive epochs.
     |      Only effective when solver='sgd' or 'adam'
     |  
     |  validation_fraction : float, optional, default 0.1
     |      The proportion of training data to set aside as validation set for
     |      early stopping. Must be between 0 and 1.
     |      Only used if early_stopping is True
     |  
     |  beta_1 : float, optional, default 0.9
     |      Exponential decay rate for estimates of first moment vector in adam,
     |      should be in [0, 1). Only used when solver='adam'
     |  
     |  beta_2 : float, optional, default 0.999
     |      Exponential decay rate for estimates of second moment vector in adam,
     |      should be in [0, 1). Only used when solver='adam'
     |  
     |  epsilon : float, optional, default 1e-8
     |      Value for numerical stability in adam. Only used when solver='adam'
     |  
     |  n_iter_no_change : int, optional, default 10
     |      Maximum number of epochs to not meet ``tol`` improvement.
     |      Only effective when solver='sgd' or 'adam'
     |  
     |      .. versionadded:: 0.20
     |  
     |  Attributes
     |  ----------
     |  classes_ : array or list of array of shape (n_classes,)
     |      Class labels for each output.
     |  
     |  loss_ : float
     |      The current loss computed with the loss function.
     |  
     |  coefs_ : list, length n_layers - 1
     |      The ith element in the list represents the weight matrix corresponding
     |      to layer i.
     |  
     |  intercepts_ : list, length n_layers - 1
     |      The ith element in the list represents the bias vector corresponding to
     |      layer i + 1.
     |  
     |  n_iter_ : int,
     |      The number of iterations the solver has ran.
     |  
     |  n_layers_ : int
     |      Number of layers.
     |  
     |  n_outputs_ : int
     |      Number of outputs.
     |  
     |  out_activation_ : string
     |      Name of the output activation function.
     |  
     |  Notes
     |  -----
     |  MLPClassifier trains iteratively since at each time step
     |  the partial derivatives of the loss function with respect to the model
     |  parameters are computed to update the parameters.
     |  
     |  It can also have a regularization term added to the loss function
     |  that shrinks model parameters to prevent overfitting.
     |  
     |  This implementation works with data represented as dense numpy arrays or
     |  sparse scipy arrays of floating point values.
     |  
     |  References
     |  ----------
     |  Hinton, Geoffrey E.
     |      "Connectionist learning procedures." Artificial intelligence 40.1
     |      (1989): 185-234.
     |  
     |  Glorot, Xavier, and Yoshua Bengio. "Understanding the difficulty of
     |      training deep feedforward neural networks." International Conference
     |      on Artificial Intelligence and Statistics. 2010.
     |  
     |  He, Kaiming, et al. "Delving deep into rectifiers: Surpassing human-level
     |      performance on imagenet classification." arXiv preprint
     |      arXiv:1502.01852 (2015).
     |  
     |  Kingma, Diederik, and Jimmy Ba. "Adam: A method for stochastic
     |      optimization." arXiv preprint arXiv:1412.6980 (2014).
     |  
     |  Method resolution order:
     |      MLPClassifier
     |      BaseMultilayerPerceptron
     |      abc.NewBase
     |      sklearn.base.BaseEstimator
     |      sklearn.base.ClassifierMixin
     |      builtins.object
     |  
     |  Methods defined here:
     |  
     |  __init__(self, hidden_layer_sizes=(100,), activation='relu', solver='adam', alpha=0.0001, batch_size='auto', learning_rate='constant', learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  fit(self, X, y)
     |      Fit the model to data matrix X and target(s) y.
     |      
     |      Parameters
     |      ----------
     |      X : array-like or sparse matrix, shape (n_samples, n_features)
     |          The input data.
     |      
     |      y : array-like, shape (n_samples,) or (n_samples, n_outputs)
     |          The target values (class labels in classification, real numbers in
     |          regression).
     |      
     |      Returns
     |      -------
     |      self : returns a trained MLP model.
     |  
     |  predict(self, X)
     |      Predict using the multi-layer perceptron classifier
     |      
     |      Parameters
     |      ----------
     |      X : {array-like, sparse matrix}, shape (n_samples, n_features)
     |          The input data.
     |      
     |      Returns
     |      -------
     |      y : array-like, shape (n_samples,) or (n_samples, n_classes)
     |          The predicted classes.
     |  
     |  predict_log_proba(self, X)
     |      Return the log of probability estimates.
     |      
     |      Parameters
     |      ----------
     |      X : array-like, shape (n_samples, n_features)
     |          The input data.
     |      
     |      Returns
     |      -------
     |      log_y_prob : array-like, shape (n_samples, n_classes)
     |          The predicted log-probability of the sample for each class
     |          in the model, where classes are ordered as they are in
     |          `self.classes_`. Equivalent to log(predict_proba(X))
     |  
     |  predict_proba(self, X)
     |      Probability estimates.
     |      
     |      Parameters
     |      ----------
     |      X : {array-like, sparse matrix}, shape (n_samples, n_features)
     |          The input data.
     |      
     |      Returns
     |      -------
     |      y_prob : array-like, shape (n_samples, n_classes)
     |          The predicted probability of the sample for each class in the
     |          model, where classes are ordered as they are in `self.classes_`.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  partial_fit
     |      Update the model with a single iteration over the given data.
     |      
     |      Parameters
     |      ----------
     |      X : {array-like, sparse matrix}, shape (n_samples, n_features)
     |          The input data.
     |      
     |      y : array-like, shape (n_samples,)
     |          The target values.
     |      
     |      classes : array, shape (n_classes), default None
     |          Classes across all calls to partial_fit.
     |          Can be obtained via `np.unique(y_all)`, where y_all is the
     |          target vector of the entire dataset.
     |          This argument is required for the first call to partial_fit
     |          and can be omitted in the subsequent calls.
     |          Note that y doesn't need to contain all labels in `classes`.
     |      
     |      Returns
     |      -------
     |      self : returns a trained MLP model.
     |  
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |  
     |  __abstractmethods__ = frozenset()
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from sklearn.base.BaseEstimator:
     |  
     |  __getstate__(self)
     |  
     |  __repr__(self)
     |      Return repr(self).
     |  
     |  __setstate__(self, state)
     |  
     |  get_params(self, deep=True)
     |      Get parameters for this estimator.
     |      
     |      Parameters
     |      ----------
     |      deep : boolean, optional
     |          If True, will return the parameters for this estimator and
     |          contained subobjects that are estimators.
     |      
     |      Returns
     |      -------
     |      params : mapping of string to any
     |          Parameter names mapped to their values.
     |  
     |  set_params(self, **params)
     |      Set the parameters of this estimator.
     |      
     |      The method works on simple estimators as well as on nested objects
     |      (such as pipelines). The latter have parameters of the form
     |      ``<component>__<parameter>`` so that it's possible to update each
     |      component of a nested object.
     |      
     |      Returns
     |      -------
     |      self
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from sklearn.base.BaseEstimator:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from sklearn.base.ClassifierMixin:
     |  
     |  score(self, X, y, sample_weight=None)
     |      Returns the mean accuracy on the given test data and labels.
     |      
     |      In multi-label classification, this is the subset accuracy
     |      which is a harsh metric since you require for each sample that
     |      each label set be correctly predicted.
     |      
     |      Parameters
     |      ----------
     |      X : array-like, shape = (n_samples, n_features)
     |          Test samples.
     |      
     |      y : array-like, shape = (n_samples) or (n_samples, n_outputs)
     |          True labels for X.
     |      
     |      sample_weight : array-like, shape = [n_samples], optional
     |          Sample weights.
     |      
     |      Returns
     |      -------
     |      score : float
     |          Mean accuracy of self.predict(X) wrt. y.
    



```python

```


```python
df = pd.read_csv('datasets//H`itters.csv').dropna()
df.info()
```

    <class 'pandas.core.frame.DataFrame'>
    Int64Index: 263 entries, 1 to 321
    Data columns (total 21 columns):
    Unnamed: 0    263 non-null object
    AtBat         263 non-null int64
    Hits          263 non-null int64
    HmRun         263 non-null int64
    Runs          263 non-null int64
    RBI           263 non-null int64
    Walks         263 non-null int64
    Years         263 non-null int64
    CAtBat        263 non-null int64
    CHits         263 non-null int64
    CHmRun        263 non-null int64
    CRuns         263 non-null int64
    CRBI          263 non-null int64
    CWalks        263 non-null int64
    League        263 non-null object
    Division      263 non-null object
    PutOuts       263 non-null int64
    Assists       263 non-null int64
    Errors        263 non-null int64
    Salary        263 non-null float64
    NewLeague     263 non-null object
    dtypes: float64(1), int64(16), object(4)
    memory usage: 45.2+ KB



```python
X = df[['Years', 'Hits']].as_matrix()
y = np.log(df.Salary.as_matrix())
```

    /home/gf/packages/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
      """Entry point for launching an IPython kernel.
    /home/gf/packages/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:2: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
      



```python
from sklearn.externals.six import StringIO  
import pydot
from IPython.display import Image
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier, export_graphviz
regr = DecisionTreeRegressor(max_leaf_nodes=3)
regr.fit(X, y)
```




    DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None,
               max_leaf_nodes=3, min_impurity_decrease=0.0,
               min_impurity_split=None, min_samples_leaf=1,
               min_samples_split=2, min_weight_fraction_leaf=0.0,
               presort=False, random_state=None, splitter='best')




```python
# This function creates images of tree models using pydot
def print_tree(estimator, features, class_names=None, filled=True):
    tree = estimator
    names = features
    color = filled
    classn = class_names
    
    dot_data = StringIO()
    export_graphviz(estimator, out_file=dot_data, feature_names=features, class_names=classn, filled=filled)
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    return(graph)
```


```python
graph, = print_tree(regr, features=['Years', 'Hits'])
Image(graph.create_png())
```


```python

```

4e74317428e45c4a1ffc18b774a99b7c11498ade
ipynb
Jupyter Notebook
SII/ML/4-sklearn.ipynb
vafaei-ar/alzahra-workshop-2019
ebcc293e53de5a81379c97fff95fe24f7c67ba39
MIT
2019-11-24T13:55:16.000Z
2021-01-08T20:09:55.000Z
SII/ML/4-sklearn.ipynb
vafaei-ar/alzahra-workshop-2019
ebcc293e53de5a81379c97fff95fe24f7c67ba39
MIT
SII/ML/4-sklearn.ipynb
vafaei-ar/alzahra-workshop-2019
ebcc293e53de5a81379c97fff95fe24f7c67ba39
MIT
Qwen/Qwen-72B
1. YES
2. YES