sk7_normalization.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 preprocessing
  9. import numpy as np
  10. from sklearn.model_selection import train_test_split
  11. from sklearn.datasets.samples_generator import make_classification
  12. from sklearn.svm import SVC
  13. import matplotlib.pyplot as plt
  14. a = np.array([[10, 2.7, 3.6],
  15. [-100, 5, -2],
  16. [120, 20, 40]], dtype=np.float64)
  17. print(a)
  18. print(preprocessing.scale(a))
  19. X, y = make_classification(n_samples=300, n_features=2 , n_redundant=0, n_informative=2,
  20. random_state=22, n_clusters_per_class=1, scale=100)
  21. plt.scatter(X[:, 0], X[:, 1], c=y)
  22. plt.show()
  23. X = preprocessing.scale(X) # normalization step
  24. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3)
  25. clf = SVC()
  26. clf.fit(X_train, y_train)
  27. print(clf.score(X_test, y_test))