Пример #1
0
def Error(X_train,Y_train,alpha,bias,X_test,Y_test,sigma):
	''' Error for the test data'''

	Y_predict =np.zeros(shape=(Y_test.shape[0],1)) #predicted by svm
	Y_t_predict = np.zeros(shape=(Y_train.shape[0],1))
	for i in range(X_test.shape[0]):
		if (smo.predict(X_train,Y_train,alpha,bias,X_test[i,:],sigma) >= 0 ):
			Y_predict[i]=1.0
		else:
			Y_predict[i]=-1.0

	for i in range(X_train.shape[0]):
		if (smo.predict(X_train,Y_train,alpha,bias,X_train[i,:],sigma) >= 0 ):
			Y_t_predict[i]=1.0
		else:
			Y_t_predict[i]=-1.0

	test_error=0.0
	train_error=0.0
	
	for i in range(Y_predict.shape[0]):
		if (Y_predict[i]!=Y_test[i]):
			test_error+=1.0

	
	for i in range(Y_t_predict.shape[0]):
		if (Y_t_predict[i]!=Y_train[i]):
			train_error+=1.0

	return (1-(train_error/Y_train.shape[0]))*100.0,(1-(test_error/Y_test.shape[0]))*100.0
def decision_function(alpha, y, X, x_test, bias, sigma):
    '''returns which class it belongs to'''

    fun_value = smo.predict(X, y, alpha, bias, x_test, sigma)

    if (fun_value >= 0.0):
        return 1.0
    else:
        return -1.0