sk4_learning_pattern.py 819 B

12345678910111213141516171819202122232425262728293031
  1. # View more python learning tutorial on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. """
  5. Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
  6. """
  7. from __future__ import print_function
  8. from sklearn import datasets
  9. from sklearn.model_selection import train_test_split
  10. from sklearn.neighbors import KNeighborsClassifier
  11. iris = datasets.load_iris()
  12. iris_X = iris.data
  13. iris_y = iris.target
  14. ##print(iris_X[:2, :])
  15. ##print(iris_y)
  16. X_train, X_test, y_train, y_test = train_test_split(
  17. iris_X, iris_y, test_size=0.3)
  18. ##print(y_train)
  19. knn = KNeighborsClassifier()
  20. knn.fit(X_train, y_train)
  21. print(knn.predict(X_test))
  22. print(y_test)