Machine Learning

https://d33wubrfki0l68.cloudfront.net/e2de4e3466f5d5bd753ac42697153d6d01cddfeb/b41c7/_images/34018729885_002ced9b54_k_d.jpg
Python has a vast number of libraries for data analysis, statistics, and Machine Learning itself, making it a language of choice for many data scientists.

Some widely used packages for Machine Learning and other data science applications are listed below.

SciPy Stack

The SciPy stack consists of a bunch of core helper packages used in data science for statistical analysis and visualising data. Because of its huge number of functionalities and ease of use, the Stack is considered a must-have for most data science applications.

The Stack consists of the following packages (link to documentation given):

Installation

For installing the full stack, or individual packages, you can refer to the instructions given here.

NB:Anaconda is highly preferred and recommended for installing and maintaining data science packages seamlessly.

scikit-learn

Scikit is a free and open source machine learning library for Python. It offers off-the-shelf functions to implement many algorithms like linear regression, classifiers, SVMs, k-means, Neural Networks, etc. It also has a few sample datasets which can be directly used for training and testing.

Because of its speed, robustness, and ease of, it’s one of the most widely-used libraries for many Machine Learning applications.

Installation

Through PyPI:

  1. pip install -U scikit-learn

Through conda:

  1. conda install scikit-learn

scikit-learn also comes shipped with Anaconda (mentioned above). For more installation instructions, refer to this link.

Example

For this example, we train a simple classifier on the Iris dataset, which comes bundled in with scikit-learn.

The dataset takes four features of flowers: sepal length, sepal width, petal length, and petal width, and classifies them into three flower species (labels): setosa, versicolor, or virginica. The labels have been represented as numbers in the dataset: 0 (setosa), 1 (versicolor), and 2 (virginica).

We shuffle the Iris dataset and divide it into separate training and testing sets, keeping the last 10 data points for testing and rest for training. We then train the classifier on the training set and predict on the testing set.

  1. from sklearn.datasets import load_iris
  2. from sklearn import tree
  3. from sklearn.metrics import accuracy_score
  4. import numpy as np
  5.  
  6. #loading the iris dataset
  7. iris = load_iris()
  8.  
  9. x = iris.data #array of the data
  10. y = iris.target #array of labels (i.e answers) of each data entry
  11.  
  12. #getting label names i.e the three flower species
  13. y_names = iris.target_names
  14.  
  15. #taking random indices to split the dataset into train and test
  16. test_ids = np.random.permutation(len(x))
  17.  
  18. #splitting data and labels into train and test
  19. #keeping last 10 entries for testing, rest for training
  20.  
  21. x_train = x[test_ids[:-10]]
  22. x_test = x[test_ids[-10:]]
  23.  
  24. y_train = y[test_ids[:-10]]
  25. y_test = y[test_ids[-10:]]
  26.  
  27. #classifying using decision tree
  28. clf = tree.DecisionTreeClassifier()
  29.  
  30. #training (fitting) the classifier with the training set
  31. clf.fit(x_train, y_train)
  32.  
  33. #predictions on the test dataset
  34. pred = clf.predict(x_test)
  35.  
  36. print pred #predicted labels i.e flower species
  37. print y_test #actual labels
  38. print (accuracy_score(pred, y_test))*100 #prediction accuracy

Since we’re splitting randomly and the classifier trains on every iteration, the accuracy may vary. Running the above code gives:

  1. [0 1 1 1 0 2 0 2 2 2]
  2. [0 1 1 1 0 2 0 2 2 2]
  3. 100.0

The first line contains the labels (i.e. flower species) of the testing data as predicted by our classifier, and the second line contains the actual flower species as given in the dataset. We thus get an accuracy of 100% this time.

More on scikit-learn can be read in the documentation.

原文: https://docs.python-guide.org/scenarios/ml/