Advertisement
Guest User

ANI Z4 - WTF

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. # nachfolgend Ihre Nebenrechnungen
  2. import numpy as np
  3.  
  4. x = [[  8.6,   -9.71,  -2.1,   -6.07,   5.62, -13.95,   4.31,
  5.         3.39,   1.46, -11.37,  -8.93,  -3.55,  17.21,   7.82,
  6.        18.33,  16.77,  13.15,  16.91,  10.52,  20.74,  -8.99,
  7.         6.28, -13.24,   7.52, -15.17,  13.27,  -7.35,   7.41,
  8.        -9.98,   8.62, -16.13,   1.16,  -3.52,  12.76,   1.98,
  9.        -2.8,    8.56,   6.48,   4.4                           ],
  10.      [ 28.1,  -25.94,  -2.15, -16.83,  21.83, -38.05,  16.29,
  11.        13.61,   9.74, -34.68, -28.27, -13.0,   47.69,  20.33,
  12.        54.12,  48.53,  36.8,   46.49,  28.13,  61.36, -28.61,
  13.        16.37, -43.61,  19.38, -46.38,  38.03, -24.7,   17.99,
  14.       -33.37,  31.18, -43.37,   8.09,  -6.48,  41.99,   9.32,
  15.        -5.25,  28.19,  23.27,  16.85                          ],
  16.      [  8.84,  -8.96,  -0.9,   -6.34,   7.3,  -12.9,    5.3,
  17.         4.62,   3.32, -10.92,  -8.78,  -3.76,  16.46,   7.34,
  18.        17.88,  16.02,  12.04,  15.26,   9.14,  21.04,  -9.14,
  19.         5.62, -14.74,   6.44, -16.52,  11.62,  -9.36,   4.86,
  20.       -12.26,  10.72, -13.94,   3.38,  -1.36,  14.98,   4.2,
  21.        -0.46,  10.78,   8.34,   5.9                           ]]
  22.  
  23. label = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  24.          1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
  25.  
  26. x_array = np.asarray(x)
  27. y_array = np.asarray(label)
  28.  
  29. x_mean = np.mean(x_array, axis=1).reshape(3,1)
  30.  
  31. S_B = None
  32. S_W = None
  33. for cls in range(4):
  34.     x_cls = x_array[:,y_array==cls]
  35.     x_cls_mean = np.mean(x_cls, axis=1).reshape(3,1)
  36.     x_cls_minus = x_cls_mean - x_mean
  37.    
  38.     if type(S_B) == type(None):
  39.         S_B = x_cls_minus * x_cls_minus.transpose()
  40.     else:
  41.         S_B += x_cls_minus * x_cls_minus.transpose()
  42.    
  43.     for k in range(len(x_cls)):
  44.         x_k = x_cls[:,k].reshape(3,1)
  45.         x_cls_k_minus = x_k - x_cls_mean
  46.        
  47.         if type(S_W) == type(None):
  48.             S_W = x_cls_k_minus * x_cls_k_minus.transpose()
  49.         else:
  50.             S_W += x_cls_k_minus * x_cls_k_minus.transpose()
  51.        
  52.        
  53. # print(S_B)
  54. # print(S_W)
  55.  
  56. S_B_sqrt = np.sqrt(S_B)
  57. S_W_inv = np.linalg.inv(S_W)
  58.  
  59. # print(S_B_sqrt)
  60. # print(S_W_inv)
  61.  
  62. A = np.dot(S_B_sqrt, np.dot(S_W_inv, S_B_sqrt))
  63. # print(A)
  64.  
  65. ew, ev = np.linalg.eig(A)
  66. # print(ev)
  67.  
  68. S_B_sqrt_inv = np.linalg.inv(S_B_sqrt)
  69.  
  70. W = S_B_sqrt_inv * ev
  71. W_T = W.transpose()
  72. # print(W_T)
  73.  
  74. p = np.dot(W_T, x_array)
  75.  
  76. # print(p)
  77.  
  78. p_best = p[:2,:]
  79.  
  80. # print(p_best)
  81.  
  82. p_best_T = p_best.transpose()
  83. # print(p_best_T)
  84.  
  85. %matplotlib inline
  86. import numpy
  87. import scipy
  88. import matplotlib.pyplot as plt
  89. from mpl_toolkits.mplot3d import Axes3D # used in 3D-plot: projection='3d'
  90.  
  91. def plot_2D(x, label):
  92.     """Visualisierung des Ausgangsproblems"""
  93.     x_2D = numpy.matrix(x)
  94.     color = ['b', 'g', 'r', 'c']
  95.     fig = plt.figure()
  96.     ax = fig.add_subplot(111)
  97.     for i in range(len(label)):
  98.         c = color[label[i]]
  99.         ax.scatter(x_2D[0, i], x_2D[1, i], c=c, marker='o')
  100.     ax.set_xlabel('$x_0$')
  101.     ax.set_ylabel('$x_1$')
  102.     plt.title('Klassifikationsproblem im $R^2$ mit 4 Klassen')
  103.     plt.show()
  104.  
  105. plot_2D(p_best, label)
  106.  
  107.  
  108. y_9_transformiert  = p_best_T[9-1] # Zahlen ersetzen durch Ihre berechneten Werte
  109. y_10_transformiert = p_best_T[10-1]
  110. y_13_transformiert = p_best_T[13-1]
  111. y_14_transformiert = p_best_T[14-1]
  112. y_18_transformiert = p_best_T[18-1]
  113. y_20_transformiert = p_best_T[20-1]
  114.  
  115.  
  116. # Ergebnisse überprüfen
  117. import test_z_ip
  118. result = [[y_9_transformiert[0], y_10_transformiert[0], y_13_transformiert[0],
  119.            y_14_transformiert[0], y_18_transformiert[0], y_20_transformiert[0]],
  120.           [y_9_transformiert[1], y_10_transformiert[1], y_13_transformiert[1],
  121.            y_14_transformiert[1], y_18_transformiert[1], y_20_transformiert[1]]]
  122. test_z_ip.loesung_z4(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement