Advertisement
ZergRushA

RT-AI-3

Oct 4th, 2022
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. '''
  2. Task 1.3.1
  3. idk, something like that maybe
  4. !
  5. !
  6. !
  7. !
  8. V
  9.  
  10. from matplotlib import pyplot as plt
  11. from mpl_toolkits.mplot3d import Axes3D
  12. import numpy as np
  13.  
  14. dot1 = np.array([1.6,1.6,1.6])
  15. dot2 = np.array([2.1,2.1,2.1])
  16. dot3 = np.array([3.4,3.4,3.4])
  17. dot4 = np.array([5.2,5.2,5.2])
  18.  
  19. print(np.linalg.norm(dot1-dot2-dot3-dot4))
  20.  
  21. print(np.linalg.norm(dot1-dot2-dot3-dot4)**2)
  22.  
  23. print(np.linalg.norm(dot1-dot2-dot3-dot4, ord=np.inf))
  24.  
  25. print(np.linalg.norm(dot1-dot2-dot3-dot4, ord=1))
  26.  
  27.  
  28. fig = plt.figure()
  29. ax = fig.add_subplot(111, projection="3d")
  30.  
  31.  
  32. ax.scatter(1.6,1.6,1.6)
  33. ax.scatter(2.1,2.1,2.1)
  34. ax.scatter(3.4,3.4,3.4)
  35. ax.scatter(5.2,5.2,5.2)
  36.  
  37. plt.show()
  38.  
  39.  
  40. #Task 2.3.1
  41. from matplotlib import pyplot as plt
  42. from sklearn.neighbors import KNeighborsClassifier
  43. from sklearn.model_selection import train_test_split
  44. from sklearn.metrics import accuracy_score
  45. import numpy as np
  46. import seaborn as sns
  47.  
  48. iris = sns.load_dataset('iris')
  49. x_train, x_test, y_train, y_test = train_test_split(iris.iloc[:, :-1],
  50.                                                     iris.iloc[:, -1],
  51.                                                     test_size = 0.20) # просто поменять 0.20 на 0.15 (см второй пункт)
  52.  
  53. model = KNeighborsClassifier(n_neighbors=3) # поменять 3 на 1/5/10
  54. model.fit(x_train, y_train)
  55. y_pred = model.predict(x_test)
  56.  
  57. plt.figure(figsize=(10, 7))
  58. sns.scatterplot(x="petal_width",
  59.                 y = "petal_length",
  60.                 data=iris,
  61.                 hue='species',
  62.                 s=70)
  63.  
  64. plt.xlabel("Длина лепестка, см")
  65. plt.ylabel("Ширина лепестка, см")
  66. plt.legend(loc=2)
  67. plt.grid()
  68.  
  69. for i in range(len(y_test)):
  70.     if np.array(y_test)[i] != y_pred[i]:
  71.         plt.scatter(x_test.iloc[i, 3], x_test.iloc[i, 2], color="red", s=150)
  72.  
  73. print(y_pred)
  74. print(f'accuracy: {accuracy_score(y_test, y_pred) :.3}')
  75.  
  76.  
  77. plt.show()
  78.  
  79.  
  80.  
  81. Task 3.3.2
  82. from sklearn.feature_extraction import DictVectorizer
  83.  
  84. traits = [{'adult': 3, 'child': 2},
  85.         {'adult': 6, 'child': 4},
  86.         {'adult': 1, 'teen': 3},
  87.         {'adult': 2, 'teen': 1},
  88.         ]
  89.  
  90.  
  91. dictvect = DictVectorizer(sparse=False)
  92.  
  93. traits_matrix = dictvect.fit_transform(traits)
  94.  
  95. print(traits_matrix)
  96.  
  97. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement