You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

knn_classification.py 1.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # ---
  2. # jupyter:
  3. # jupytext_format_version: '1.2'
  4. # kernelspec:
  5. # display_name: Python 3
  6. # language: python
  7. # name: python3
  8. # language_info:
  9. # codemirror_mode:
  10. # name: ipython
  11. # version: 3
  12. # file_extension: .py
  13. # mimetype: text/x-python
  14. # name: python
  15. # nbconvert_exporter: python
  16. # pygments_lexer: ipython3
  17. # version: 3.5.2
  18. # ---
  19. # # KNN Classification
  20. #
  21. #
  22. #
  23. # +
  24. % matplotlib inline
  25. import matplotlib.pyplot as plt
  26. from sklearn import datasets, neighbors, linear_model
  27. # load data
  28. digits = datasets.load_digits()
  29. X_digits = digits.data
  30. y_digits = digits.target
  31. print("Feature dimensions: ", X_digits.shape)
  32. print("Label dimensions: ", y_digits.shape)
  33. # +
  34. # plot sample images
  35. nplot = 10
  36. fig, axes = plt.subplots(nrows=1, ncols=nplot)
  37. for i in range(nplot):
  38. img = X_digits[i].reshape(8, 8)
  39. axes[i].imshow(img)
  40. axes[i].set_title(y_digits[i])
  41. # +
  42. # split train / test data
  43. n_samples = len(X_digits)
  44. n_train = int(0.4 * n_samples)
  45. X_train = X_digits[:n_train]
  46. y_train = y_digits[:n_train]
  47. X_test = X_digits[n_train:]
  48. y_test = y_digits[n_train:]
  49. # +
  50. # do KNN classification
  51. knn = neighbors.KNeighborsClassifier()
  52. logistic = linear_model.LogisticRegression()
  53. print('KNN score: %f' % knn.fit(X_train, y_train).score(X_test, y_test))
  54. print('LogisticRegression score: %f' % logistic.fit(X_train, y_train).score(X_test, y_test))
  55. # -
  56. # ## References
  57. # * [Digits Classification Exercise](http://scikit-learn.org/stable/auto_examples/exercises/plot_digits_classification_exercise.html)
  58. #

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。

Contributors (1)