Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- #generator danych liniowo separowalnych
- def gen_lin_sep(ile_danych, zakresy, wektor_kierunkowy):
- zak = np.array(zakresy)
- X = np.random.sample((ile_danych, len(wektor_kierunkowy)-1))
- X *= (zak[:,1]-zak[:,0])
- X += zak[:,0]
- Y = np.zeros(ile_danych)
- for idx, x in enumerate(X):
- suma = np.dot(np.append(x, 1), wektor_kierunkowy)
- if(suma>0):
- Y[idx] = 1
- else:
- Y[idx] = -1
- return X, Y
- ##ustalenie atrybutów i wygenerowanie danych
- #ile = 100
- #zakres = [[-10,10], [-2,5]]
- #wek = [1,1,1]
- #dane, klasy = gen_lin_sep(ile, zakres, wek)
- ##wyswietlenie granicy decyzyjnej i danych
- #os_x = np.linspace(-10,10,101)
- #os_y = os_x*(wek[0]/-wek[1]) + (wek[2]/-wek[1])
- #plt.plot(os_x, os_y)
- #plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
- #generator danych "koło w prostokącie"
- def gen_sphere(ile_danych, zakresy, srodek, promien):
- zak = np.array(zakresy)
- X = np.random.sample((ile_danych, len(zakresy)))
- X *= (zak[:,1]-zak[:,0])
- X += zak[:,0]
- Y = np.zeros(ile_danych)
- for idx, x in enumerate(X):
- odleglosc = np.sqrt(np.power(x-srodek, 2).sum())
- if(odleglosc < promien):
- Y[idx] = 1
- else:
- Y[idx] = -1
- return X, Y
- ##ustalenie atrybutów i wygenerowanie danych
- #ile = 1000
- #zakres = [[10,20], [10,20]]
- #sr = [15, 15]
- #rad = 3
- #dane, klasy = gen_sphere(ile, zakres, sr, rad)
- ##wyswietlenie granicy decyzyjnej i danych
- #os_x = np.linspace(sr[0]-rad, sr[0]+rad, 101)
- #os_y1 = np.power((rad**2 -np.power(os_x-sr[0], 2)), 0.5) + sr[1]
- #os_y2 = -np.power((rad**2 -np.power(os_x-sr[0], 2)), 0.5) + sr[1]
- #plt.plot(os_x, os_y1, c='r')
- #plt.plot(os_x, os_y2, c='r')
- #plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
- #generator danych "dwa koła w prostokącie"
- def gen_two_sphere(ile_danych, zakresy, srodek1, promien1, srodek2, promien2):
- zak = np.array(zakresy)
- X = np.random.sample((ile_danych, len(zakresy)))
- X *= (zak[:,1]-zak[:,0])
- X += zak[:,0]
- Y = np.zeros(ile_danych)
- for idx, x in enumerate(X):
- odleglosc1 = np.sqrt(np.power(x-srodek1, 2).sum())
- if(odleglosc1 < promien1):
- Y[idx] = 1
- else:
- odleglosc2 = np.sqrt(np.power(x-srodek2, 2).sum())
- if(odleglosc2 < promien2):
- Y[idx] = 2
- else:
- Y[idx] = 3
- return X, Y
- ##ustalenie atrybutów i wygenerowanie danych
- #ile = 1000
- #zakres = [[10,20], [10,20]]
- #sr1 = [17, 17]
- #sr2 = [13, 13]
- #rad1 = 2
- #rad2 = 1
- #dane, klasy = gen_two_sphere(ile, zakres, sr1, rad1, sr2, rad2)
- #klasy
- ##wyswietlenie granicy decyzyjnej i danych
- #os_x1 = np.linspace(sr1[0]-rad1, sr1[0]+rad1, 101)
- #os_y11 = np.power((rad1**2 -np.power(os_x1-sr1[0], 2)), 0.5) + sr1[1]
- #os_y12 = -np.power((rad1**2 -np.power(os_x1-sr1[0], 2)), 0.5) + sr1[1]
- #os_x2 = np.linspace(sr2[0]-rad2, sr2[0]+rad2, 101)
- #os_y21 = np.power((rad2**2 -np.power(os_x2-sr2[0], 2)), 0.5) + sr2[1]
- #os_y22 = -np.power((rad2**2 -np.power(os_x2-sr2[0], 2)), 0.5) + sr2[1]
- #plt.plot(os_x1, os_y11, c='r')
- #plt.plot(os_x1, os_y12, c='r')
- #plt.plot(os_x2, os_y21, c='b')
- #plt.plot(os_x2, os_y22, c='b')
- #plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement