lancernik

Untitled

May 14th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue May 14 14:55:14 2019
  5.  
  6. @author: student
  7. """
  8. #
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. from sklearn.datasets.samples_generator import make_blobs
  25. from sklearn.cluster import KMeans
  26. import matplotlib.pyplot as plt
  27. from pandas import DataFrame
  28.  
  29. X,y= make_blobs(n_samples=1000, centers=4
  30. ,random_state=0)
  31.  
  32. kmeans = KMeans(n_clusters = 4)
  33. kmeans.fit(X)
  34. y_kmeans = kmeans.predict(X)
  35.  
  36. plt.figure()
  37. plt.scatter(X[:,0],X[:,1],c=y_kmeans)
  38. plt.show
  39.  
  40.  
  41. from sklearn.datasets import make_moons
  42.  
  43. X, y = make_moons(200,noise=.05)
  44.  
  45.  
  46. kmeans = KMeans(n_clusters = 2)
  47. kmeans.fit(X)
  48. y_kmeans = kmeans.predict(X)
  49.  
  50. plt.figure()
  51. plt.scatter(X[:,0],X[:,1],c=y_kmeans)
  52. plt.show
  53.  
  54.  
  55.  
  56.  
  57. # generate 2d classification dataset
  58. X, y = make_moons(n_samples=100, noise=0.1)
  59. # scatter plot, dots colored by class value
  60. df = DataFrame(dict(x=X[:,0], y=X[:,1], label=y))
  61. colors = {0:'red', 1:'blue'}
  62. fig, ax = plt.subplots()
  63. grouped = df.groupby('label')
  64. for key, group in grouped:
  65. group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])
  66. plt.show()
  67.  
  68.  
  69.  
  70.  
  71. from scipy.cluster import hierarchy
  72. import matplotlib.pyplot as plt
  73.  
  74. Z = hierarchy.linkage(X, 'ward')
  75. plt.figure()
  76. dn = hierarchy.dendrogram(Z, p=12, truncate_mode='lastp')
  77.  
  78.  
  79.  
  80. #
  81. # Zadanie 4
  82. #
  83. #import matplotlib.pyplot as plt
  84. #from mpl_toolkits.mplot3d import Axes3D
  85. #from sklearn import datasets
  86. #from sklearn.decomposition import PCA
  87. #
  88. ## import some data to play with
  89. #iris = datasets.load_iris()
  90. #X = iris.data[:, :2] # we only take the first two features.
  91. #y = iris.target
  92. #
  93. #x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
  94. #y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
  95. #
  96. #plt.figure(2, figsize=(8, 6))
  97. #plt.clf()
  98. #
  99. ## Plot the training points
  100. #plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Set1,
  101. # edgecolor='k')
  102. #plt.xlabel('Sepal length')
  103. #plt.ylabel('Sepal width')
  104. #
  105. #plt.xlim(x_min, x_max)
  106. #plt.ylim(y_min, y_max)
  107. #plt.xticks(())
  108. #plt.yticks(())
  109. #
  110. ## To getter a better understanding of interaction of the dimensions
  111. ## plot the first three PCA dimensions
  112. #fig = plt.figure(1, figsize=(8, 6))
  113. #ax = Axes3D(fig, elev=-150, azim=110)
  114. #X_reduced = PCA(n_components=3).fit_transform(iris.data)
  115. #ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=y,
  116. # cmap=plt.cm.Set1, edgecolor='k', s=40)
  117. #ax.set_title("First three PCA directions")
  118. #ax.set_xlabel("1st eigenvector")
  119. #ax.w_xaxis.set_ticklabels([])
  120. #ax.set_ylabel("2nd eigenvector")
  121. #ax.w_yaxis.set_ticklabels([])
  122. #ax.set_zlabel("3rd eigenvector")
  123. #ax.w_zaxis.set_ticklabels([])
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. # Zadanie 5
Add Comment
Please, Sign In to add comment