基于双月数据集利用感知层进行分类
生活随笔
收集整理的這篇文章主要介紹了
基于双月数据集利用感知层进行分类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、生成數(shù)據(jù)集
class moon_data_class(object):def __init__(self,N,d,r,w):self.N=Nself.w=wself.d=dself.r=rdef sgn(self,x):if(x>0):return 1;else:return -1;def dbmoon(self):N1 = 10*self.Nr = self.rw2 = self.w/2d = self.ddone = Truedata = np.empty(0)while done:#generate Rectangular datatmp_x = 2*(r+w2)*(np.random.random([N1, 1])-0.5)tmp_y = (r+w2)*np.random.random([N1, 1])tmp = np.concatenate((tmp_x, tmp_y), axis=1)tmp_ds = np.sqrt(tmp_x*tmp_x + tmp_y*tmp_y)#generate double moon data ---upperidx = np.logical_and(tmp_ds > (r-w2), tmp_ds < (r+w2))idx = (idx.nonzero())[0]if data.shape[0] == 0:data = tmp.take(idx, axis=0)else:data = np.concatenate((data, tmp.take(idx, axis=0)), axis=0)if data.shape[0] >= N:done = False#print (data)db_moon = data[0:N, :]#print (db_moon)#generate double moon data ----downdata_t = np.empty([N, 2])data_t[:, 0] = data[0:N, 0] + rdata_t[:, 1] = -data[0:N, 1] - ddb_moon = np.concatenate((db_moon, data_t), axis=0)return db_moon N = 1000 d = 3 r = 10 width = 2data_source = moon_data_class(N, d, r, width) data = data_source.dbmoon()2、整理數(shù)據(jù)集,將輸入數(shù)據(jù)定義為感知器的標準輸入,將數(shù)據(jù)集分為1和-1兩類,存入列表d中。
x0 = [1 for x in range(1,2001)]#固定為+1 x = np.array([np.reshape(x0, len(x0)), np.reshape(data[0:2*N, 0], len(data)), np.reshape(data[0:2*N, 1], len(data))]).transpose() w = np.array([1, 0, 0])d_pre = [1 for y in range(1, 1001)] d_pos = [-1 for y in range(1, 1001)] d=d_pre+d_pos3、迭代訓練獲取權(quán)值向量
for ii in range(50):i=0sum = 0for x_n in x:y_n = data_source.sgn(np.dot(w.T, x_n))w = w + a*(d[i] - y_n)*x_ni = i+14、計算決策邊界,繪制訓練結(jié)果。
因為
w [ 0 ] + w [ 1 ] ? x + w [ 2 ] ? y = 0 w[0] + w[1]*x+w[2]*y=0 w[0]+w[1]?x+w[2]?y=0
于是有
y = ? w [ 1 ] / w [ 2 ] ? x ? w [ 0 ] / w [ 2 ] y=-w[1]/w[2] *x-w[0]/w[2] y=?w[1]/w[2]?x?w[0]/w[2]
5、運行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的基于双月数据集利用感知层进行分类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字母顺序排序(C语言)
- 下一篇: 在线生成图标