Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # nachfolgend Ihre Nebenrechnungen
- import numpy as np
- x = [[ 8.6, -9.71, -2.1, -6.07, 5.62, -13.95, 4.31,
- 3.39, 1.46, -11.37, -8.93, -3.55, 17.21, 7.82,
- 18.33, 16.77, 13.15, 16.91, 10.52, 20.74, -8.99,
- 6.28, -13.24, 7.52, -15.17, 13.27, -7.35, 7.41,
- -9.98, 8.62, -16.13, 1.16, -3.52, 12.76, 1.98,
- -2.8, 8.56, 6.48, 4.4 ],
- [ 28.1, -25.94, -2.15, -16.83, 21.83, -38.05, 16.29,
- 13.61, 9.74, -34.68, -28.27, -13.0, 47.69, 20.33,
- 54.12, 48.53, 36.8, 46.49, 28.13, 61.36, -28.61,
- 16.37, -43.61, 19.38, -46.38, 38.03, -24.7, 17.99,
- -33.37, 31.18, -43.37, 8.09, -6.48, 41.99, 9.32,
- -5.25, 28.19, 23.27, 16.85 ],
- [ 8.84, -8.96, -0.9, -6.34, 7.3, -12.9, 5.3,
- 4.62, 3.32, -10.92, -8.78, -3.76, 16.46, 7.34,
- 17.88, 16.02, 12.04, 15.26, 9.14, 21.04, -9.14,
- 5.62, -14.74, 6.44, -16.52, 11.62, -9.36, 4.86,
- -12.26, 10.72, -13.94, 3.38, -1.36, 14.98, 4.2,
- -0.46, 10.78, 8.34, 5.9 ]]
- label = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
- x_array = np.asarray(x)
- y_array = np.asarray(label)
- x_mean = np.mean(x_array, axis=1).reshape(3,1)
- S_B = None
- S_W = None
- for cls in range(4):
- x_cls = x_array[:,y_array==cls]
- x_cls_mean = np.mean(x_cls, axis=1).reshape(3,1)
- x_cls_minus = x_cls_mean - x_mean
- if type(S_B) == type(None):
- S_B = x_cls_minus * x_cls_minus.transpose()
- else:
- S_B += x_cls_minus * x_cls_minus.transpose()
- for k in range(len(x_cls)):
- x_k = x_cls[:,k].reshape(3,1)
- x_cls_k_minus = x_k - x_cls_mean
- if type(S_W) == type(None):
- S_W = x_cls_k_minus * x_cls_k_minus.transpose()
- else:
- S_W += x_cls_k_minus * x_cls_k_minus.transpose()
- # print(S_B)
- # print(S_W)
- S_B_sqrt = np.sqrt(S_B)
- S_W_inv = np.linalg.inv(S_W)
- # print(S_B_sqrt)
- # print(S_W_inv)
- A = np.dot(S_B_sqrt, np.dot(S_W_inv, S_B_sqrt))
- # print(A)
- ew, ev = np.linalg.eig(A)
- # print(ev)
- S_B_sqrt_inv = np.linalg.inv(S_B_sqrt)
- W = S_B_sqrt_inv * ev
- W_T = W.transpose()
- # print(W_T)
- p = np.dot(W_T, x_array)
- # print(p)
- p_best = p[:2,:]
- # print(p_best)
- p_best_T = p_best.transpose()
- # print(p_best_T)
- %matplotlib inline
- import numpy
- import scipy
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D # used in 3D-plot: projection='3d'
- def plot_2D(x, label):
- """Visualisierung des Ausgangsproblems"""
- x_2D = numpy.matrix(x)
- color = ['b', 'g', 'r', 'c']
- fig = plt.figure()
- ax = fig.add_subplot(111)
- for i in range(len(label)):
- c = color[label[i]]
- ax.scatter(x_2D[0, i], x_2D[1, i], c=c, marker='o')
- ax.set_xlabel('$x_0$')
- ax.set_ylabel('$x_1$')
- plt.title('Klassifikationsproblem im $R^2$ mit 4 Klassen')
- plt.show()
- plot_2D(p_best, label)
- y_9_transformiert = p_best_T[9-1] # Zahlen ersetzen durch Ihre berechneten Werte
- y_10_transformiert = p_best_T[10-1]
- y_13_transformiert = p_best_T[13-1]
- y_14_transformiert = p_best_T[14-1]
- y_18_transformiert = p_best_T[18-1]
- y_20_transformiert = p_best_T[20-1]
- # Ergebnisse überprüfen
- import test_z_ip
- result = [[y_9_transformiert[0], y_10_transformiert[0], y_13_transformiert[0],
- y_14_transformiert[0], y_18_transformiert[0], y_20_transformiert[0]],
- [y_9_transformiert[1], y_10_transformiert[1], y_13_transformiert[1],
- y_14_transformiert[1], y_18_transformiert[1], y_20_transformiert[1]]]
- test_z_ip.loesung_z4(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement