예제 #1
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test5():

	verbose = False
	mat = sio.loadmat('../matlab/M.mat')
	n = int(mat['n'])
	r = int(mat['r'])
	nt = int(mat['nt'])
	rcross = int(mat['rcross'])
	d = int(mat['d'])
	num_eval = mat['num_eval']
	init_pt = int(mat['init_pt'])
	Xfull = mat['Xfull']
	
	deg = np.squeeze(mat['deg'])
	Yfull = np.squeeze(mat['Yfull'])
	pi = sum(Yfull)/len(Yfull)
	
	t1 = time.time()
	Xe, b, w, deg = eigenmap(Xfull.T.dot(Xfull), d)
	t2 = time.time()
	f2,_,_ = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Yfull, options={'num_eval':num_eval,'pi':pi,'n_conncomp':b,'init_pt':init_pt}, verbose=verbose)
	t3 = time.time()
	f1,_,_ = AS.kernel_AS (Xfull, Yfull, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose)
	t4 = time.time()

	print "Time taken for eigenmap + computing X.T*X:", t2-t1
	print "Time taken for lreg:", t3-t2
	print "Time taken for kernel:", t4-t3

	f1 = np.squeeze(f1)
	f2 = np.squeeze(f2)
	f3 = np.squeeze(mat['f'])

	import IPython
	IPython.embed()
예제 #2
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test13(): # testing sparse AS

	verbose = True
	#ts_data = ef.load_timestamps (tsfile)
	Xfull = load_sparse_csr('Xfull1.npz')
	print Xfull.shape
	Xfull = Xfull[np.squeeze(np.asarray(np.nonzero(Xfull.sum(axis=1))[0])),:]
	Xfull = Xfull[:,np.squeeze(np.asarray(np.nonzero(Xfull.sum(axis=0))[1]))]
	r,n = Xfull.shape

	nt = int(0.1*n)
	num_eval = 20
	# getting rid of features which are zero for all these elements
	# X = np.array((Xfull[:,:n]).todense())
	# X = X[np.nonzero(X.sum(axis=1))[0],:]
	# X = X[:,np.nonzero(X.sum(axis=0))[1]]
	# import IPython 
	# IPython.embed()
	# X = X[:r,:]
	# X = X[np.nonzero(X.sum(axis=1))[0],:]
	# X = X[:,np.nonzero(X.sum(axis=0))[1]]
	#X = np.load('X11.npy')

	# import IPython
	# IPython.embed()
	num_eval = 20
	# num_eval = nt*2
	Y = np.array([1]*nt + [0]*(n-nt), dtype=int)


	pi = sum(Y)/len(Y)
	init_pt = 100

	t1 = time.time()
	f1,h1,s1,fs1,dtinv1 = AS.kernel_AS (Xfull, Y, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose,all_fs=True,tinv=True,sparse=True)
	t2 = time.time()
	f2,h2,s2,fs2,dtinv2 = AS.kernel_AS (np.array(Xfull.todense()), Y, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose,all_fs=True,tinv=True,sparse=False)
	t3 = time.time()

	import IPython
	IPython.embed()
예제 #3
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test8():

	n = 500
	r = 50
	nt = 100
	#rcross = 50
	d = 50
	hubs = 1

	verbose = False

	num_eval = 100

	#X, Y = createFakeData(n, r, nt, rcross)
	#X, Y = createFakeData2(n, r, nt, hubs)
	X,Y = np.load('t8data.npy')
	# import IPython
	# IPython.embed()

	init_pt = np.nonzero(Y)[0][0]

	ker = True


	pi = sum(Y)/len(Y)
	print "Constructing the similarity matrix:"
	A = X.T.dot(X)
	t1 = time.time()
	if ker:
		print "Performing Kernel AS"
		f1,h1,s1,fs1 = AS.kernel_AS (X, Y, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose,all_fs=True)
	t2 = time.time()
	#print "Performing Eigen decmop"
	#Xe, b, w, deg = eigenmap(A, d)
	#t3 = time.time()
	if ker:
		print "Performing Naive Shari AS"
		f2,h2,s2,fs2 = AS.shari_activesearch_probs_naive(A, labels=Y, pi=pi, w0=None, eta=None, num_eval=num_eval, init_pt=init_pt, verbose=verbose, all_fs=True)
		#f2,h2,s2,fs2 = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Y, options={'num_eval':num_eval,'pi':pi,'n_conncomp':b}, verbose=verbose)
	t4 = time.time()

	print "Time taken for kernel:", t2-t1
	#print "Time taken for eigenmap + computing X.T*X:", t3-t2
	print "Time taken for Shari's method (naive):", t4-t2
	if ker:
		print "h_kernel: %i/%i"%(h1[-1],num_eval)
		print "h_lreg: %i/%i"%(h2[-1],num_eval)

	import IPython
	IPython.embed()
예제 #4
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test2 (n, cc=2, nt=1, d=5):

	# Created banded diagonal kernel matrix
	X = np.eye(n)
	X[range(n-1), range(1,n)] = 1
	Xfull = slg.block_diag(*([X]*cc))
	Yfull = [0]+[1]+[0]*(n-5)+[1]*3+[1]*((nt-1)*n) + [0]*((cc-nt)*n)

	Xe, b, w, deg = eigenmap(Xfull.T.dot(Xfull), d)

	num_eval = 5
	init_pt = 1
	pi = sum(Yfull)/len(Yfull)

	f1 = AS.kernel_AS (Xfull, Yfull, pi=pi, num_eval=num_eval, init_pt=init_pt)
	f2 = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Yfull, options={'num_eval':num_eval,'pi':pi,'init_pt':init_pt,'n_conncomp':b})

	import IPython
	IPython.embed()
예제 #5
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test1 (n, cc=2, nt=1, d=4):
	# Example where you chains of connections in targets
	# and similar chains in non-targets

	num_eval = 5
	init_pt = 1
	# Created banded diagonal kernel matrix
	X = np.eye(n)
	X[range(n-1), range(1,n)] = 1
	Xfull = slg.block_diag(*([X]*cc))
	Yfull = [1]*(nt*n) + [0]*((cc-nt)*n)

	pi = sum(Yfull)/len(Yfull)

	f1,_,_ = AS.kernel_AS (Xfull, Yfull, pi=pi, num_eval=num_eval, init_pt=init_pt)

	Xe, b, w, deg = eigenmap(Xfull.T.dot(Xfull), d)
	f2,_,_ = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Yfull, options={'num_eval':num_eval,'pi':pi,'init_pt':init_pt,'n_conncomp':b})

	import IPython
	IPython.embed()
예제 #6
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test4():

	n = 1000
	r = 100
	nt = 100
	#rcross = 50
	d = 10
	hubs = 1

	verbose = False

	num_eval = 300
	#init_pt = 1

	#X, Y = createFakeData(n, r, nt, rcross)
	X, Y = createFakeData2(n, r, nt, hubs)

	pi = sum(Y)/len(Y)
	print "Constructing the similarity matrix:"
	A = X.T.dot(X)
	t1 = time.time()
	print "Performing Kernel AS"

	f1,h1,s1 = AS.kernel_AS (X, Y, pi=pi, num_eval=num_eval, init_pt=None, verbose=verbose)
	t2 = time.time()
	print "Performing Eigen decmop"
	Xe, b, w, deg = eigenmap(A, d)
	t3 = time.time()
	print "Performing LREG AS"
	f2,h2,s2 = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Y, options={'num_eval':num_eval,'pi':pi,'n_conncomp':b}, verbose=verbose)
	t4 = time.time()

	print "Time taken for kernel:", t2-t1
	print "Time taken for eigenmap + computing X.T*X:", t3-t2
	print "Time taken for lreg:", t4-t3
	print "h_kernel: %i/%i"%(h1[-1],num_eval)
	print "h_lreg: %i/%i"%(h2[-1],num_eval)

	import IPython
	IPython.embed()
예제 #7
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test_interface ():
	verbose = False
	#ts_data = ef.load_timestamps (tsfile)
	Xfull = load_sparse_csr('Xfull1.npz')

	r,n = Xfull.shape

	nt = int(0.05*n)
	num_eval = 1000
	# num_eval = nt*2
	Y = np.array([1]*nt + [0]*(n-nt), dtype=int)

	pi = sum(Y)/len(Y)
	init_pt = 100

	t1 = time.time()

	prms = ASI.Parameters(pi=pi,sparse=True, verbose=verbose)	
	kAS = ASI.kernelAS(prms)
	kAS.initialize(Xfull)
	kAS.firstMessage(init_pt)
	fs2 = [kAS.f]	

	for i in range(num_eval):
		idx = kAS.getNextMessage()
		kAS.setLabelCurrent(Y[idx])
		fs2.append(kAS.f)

	t2 = time.time()

	f1,h1,s1,fs1,dtinv1 = AS.kernel_AS (Xfull, Y, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose,all_fs=True,tinv=True,sparse=True)

	t3 = time.time()

	checks = [np.allclose(fs1[i],fs2[i]) for i in range(len(fs1))]

	import IPython
	IPython.embed()
예제 #8
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def testfake():

	n = 400
	r = 60
	nt = 150
	#rcross = 50
	d = n
	hubs = 1

	verbose = False

	num_eval = 390
	#init_pt = 1

	for i in range(100):
		#X, Y = createFakeData(n, r, nt, rcross)
		X, Y = createFakeData2(n, r, nt, hubs)

		pi = sum(Y)/len(Y)
		print i

		# print "Constructing the similarity matrix:"
		# print "Performing Kernel AS"
		f1,h1,s1 = AS.kernel_AS (X, Y, pi=pi, num_eval=num_eval, init_pt=None, verbose=verbose)
예제 #9
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test3 (hubs=3, followers=10):

	# Creating as follows:
	# Every alternate hub and half its followers are targets.
	# Every hub shares followers with adjacent hubs

	fl0 = int(followers/2)
	fl1 = followers - fl0

	X = np.zeros((hubs*followers, (hubs-1)*followers + hubs))
	Yfull = []
	t = 0
	for i in range(hubs-1):
		idx = i*(followers+1)
		X[i*followers:(i+1)*followers, idx] = 1
		X[xrange(i*followers,(i+1)*followers), xrange(idx+1, idx+followers+1)] = [1]*followers
		X[xrange((i+1)*followers,(i+2)*followers), xrange(idx+1, idx+followers+1)] = [1]*followers
		if t == 0:
			Yfull += [0]*(fl0+1) + [1]*fl1
			t = 1
		else:
			Yfull += [1]*(fl1+1) + [0]*fl0
			t = 0

	X[(hubs-1)*followers:, -1] = 1
	if t == 0: 
		Yfull.append(0)
	else:
		Yfull.append(1)

	Xfull = X

	d = 10


	num_eval = 15
	init_pt = 10
	pi = sum(Yfull)/len(Yfull)

	# t1 = time.time()
	f1,_,_ = AS.kernel_AS (Xfull, Yfull, pi=pi, num_eval=num_eval, init_pt=init_pt)
	# t2 = time.time()
	Xe, b, w, deg = eigenmap(Xfull.T.dot(Xfull), d)
	f2,_,_ = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Yfull, options={'num_eval':num_eval,'pi':pi,'init_pt':init_pt,'n_conncomp':b})
	# t3 = time.time()

	# print "Time 1:", (t2-t1)
	# print "Time 2:", (t3-t2)


	# print f1
	# print f2

	plt.plot(f1, color='r', label='kernel')
	plt.plot(f2, color='b', label='lreg')
	plt.plot(Yfull, color='g', label='true')
	plt.legend()
	plt.show()

	mat = sio.loadmat('../matlab/L.mat')
	Xe2 = mat['Xe']
	deg2 = np.squeeze(mat['deg'])
	Yfull2 = np.squeeze(mat['Yfull'])
	f3,_,_ = AS.lreg_AS (Xe2, deg2, d, alpha=0.0, labels=Yfull2, options={'num_eval':num_eval,'pi':pi,'init_pt':init_pt,'n_conncomp':b})

	import IPython
	IPython.embed()
예제 #10
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test12():

	verbose = True
	#ts_data = ef.load_timestamps (tsfile)
	Xfull = load_sparse_csr('Xfull1.npz')

	r,n = Xfull.shape
		
	nt = int(0.1*n)
	num_eval = nt*2
	X = np.array(Xfull.todense())
	# getting rid of features which are zero for all these elements
	X = X[np.nonzero(X.sum(axis=1))[0],:]
	X = X[:,np.nonzero(X.sum(axis=0))[1]]
	# import IPython 
	# IPython.embed()
	X = X[:r,:]
	X = X[np.nonzero(X.sum(axis=1))[0],:]
	X = X[:,np.nonzero(X.sum(axis=0))[1]]
	#X2 = np.load('X11.npy')

	# import IPython
	# IPython.embed()

	nt = int(0.1*n)
	num_eval = nt*2
	Y = np.array([1]*nt + [0]*(n-nt), dtype=int)

	#rrange =  [10*i for i in range(2,11)] + [100*i for i in range(2,11,2)] + [1500, 2000, 2500, 3000]

	pi = sum(Y)/len(Y)
	init_pt = 100

	#hits = []
	#rtime = []

	#for r in rrange:
	
	t1 = time.time()
	print "Performing the kernel AS"
	f1,h1,s1,fs1 = AS.kernel_AS (X, Y, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose,all_fs=True,sparse=False)
	t2 = time.time()

	# hits.append(h1[-1][0])
	# rtime.append(t2-t1)

	# print "r =", r
	# print "hits =", h1[-1][0]
	print "time =", t2-t1
	# print
	# print

	# plt.figure()
	# plt.plot(rrange, hits)
	# plt.xlabel('d')
	# plt.ylabel('hits')
	# plt.figure()
	# plt.plot(rrange, rtime)
	# plt.xlabel('d')
	# plt.ylabel('time in s')
	# plt.show()

	# Timing
	# Constructing C: 71s
	# Inverse of C: 202s
	# Total time: 3446s
	import IPython
	IPython.embed()
예제 #11
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test11():

	verbose = False
	#ts_data = ef.load_timestamps (tsfile)
	Xfull = load_sparse_csr('Xfull1.npz')

	n = 5000
	nt = int(0.1*n)
	num_eval = nt*2
	# getting rid of features which are zero for all these elements
	# X = np.array((Xfull[:,:n]).todense())
	# X = X[np.nonzero(X.sum(axis=1))[0],:]
	# X = X[:,np.nonzero(X.sum(axis=0))[1]]
	# import IPython 
	# IPython.embed()
	# X = X[:r,:]
	# X = X[np.nonzero(X.sum(axis=1))[0],:]
	# X = X[:,np.nonzero(X.sum(axis=0))[1]]
	X = np.load('X11.npy')

	# import IPython
	# IPython.embed()

	nt = int(0.1*n)
	num_eval = nt*2
	Y = np.array([1]*nt + [0]*(n-nt), dtype=int)

	rrange =  [10*i for i in range(2,11)] + [100*i for i in range(2,11,2)] + [1500, 2000, 2500]

	pi = sum(Y)/len(Y)
	init_pt = 100

	hits = []
	rtime = []

	for r in rrange:
		Xr = X[:r,:]
		Xr = Xr[np.nonzero(Xr.sum(axis=1))[0],:]
		Xr = Xr[:,np.nonzero(Xr.sum(axis=0))[1]]

		t1 = time.time()
		f1,h1,s1,fs1 = AS.kernel_AS (Xr, Y, pi=pi, num_eval=num_eval, init_pt=init_pt, verbose=verbose,all_fs=True)
		t2 = time.time()

		hits.append(h1[-1][0])
		rtime.append(t2-t1)

		print "r =", r
		print "hits =", h1[-1][0]
		print "time =", t2-t1
		print
		print

	# plt.figure()
	# plt.plot(rrange, hits)
	# plt.xlabel('d')
	# plt.ylabel('hits')
	# plt.figure()
	# plt.plot(rrange, rtime)
	# plt.xlabel('d')
	# plt.ylabel('time in s')
	# plt.show()

	# import IPython
	# IPython.embed()
	return rrange, hits, rtime
예제 #12
0
파일: test.py 프로젝트: yifeim/ActiveSearch
def test6():
	n = 10
	start = 1000
	end = 5000
	nt_ratio = 0.6
	r_ratio = 0.1
	ne_ratio = 0.2

	nrange = np.logspace(np.log10(start),np.log10(end), n).tolist()
	nrange = [int(nv) for nv in nrange]
	ntrange = [int(nt_ratio*nv) for nv in nrange]
	rrange = [int(r_ratio*nv) for nv in nrange]
	drange = [r*2 for r in rrange]
	nerange = [int(ne_ratio*nv) for nv in nrange]

	t_kernel = []
	t_eigendecomp = []
	t_lreg = []
	max_error = []

	verbose = False
	for n,nt,r,d,ne in zip(nrange,ntrange,rrange,drange,nerange):

		X, Y = createFakeData(n, r, nt, rcross=0)

		pi = sum(Y)/len(Y)
	
		A = X.T.dot(X) # Don't really want to include timings for this.

		t1 = time.time()
		f1,h1,s1 = AS.kernel_AS (X, Y, pi=pi, num_eval=ne, init_pt=None, verbose=verbose)
		t2 = time.time()
		Xe, b, w, deg = eigenmap(A, d)
		t3 = time.time()
		f2,h2,s2 = AS.lreg_AS (Xe, deg, d, alpha=0.0, labels=Y, options={'num_eval':ne,'pi':pi,'n_conncomp':b}, verbose=verbose)
		t4 = time.time()

		print "Parameters: n=%i, nt=%i, r=%i, d=%i, ne=%i"%(n,nt,2*r,d,ne)
		print "Time taken for kernel:", t2-t1
		print "Time taken for eigenmap + computing X.T*X:", t3-t2
		print "Time taken for lreg:", t4-t3
		print "Max error difference:", np.max(np.abs(f1-f2))
		print

		t_kernel.append(t2-t1)
		t_eigendecomp.append(t3-t2)
		t_lreg.append(t4-t3)
		max_error.append(np.max(np.abs(f1-f2)))

	plt.plot(nrange, t_kernel, label='kernel')
	plt.plot(nrange, t_eigendecomp, label='eigendecomp')
	plt.plot(nrange, t_lreg, label='lreg')

	plt.legend()
	plt.xlabel('Number of data points')
	plt.ylabel('Time')

	plt.show()

	import IPython
	IPython.embed()