|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class L2SVM:
def __init__(self,X,Y,sigma=0.8,toler=1e-5,gamma=0.8):
'''
:param X: 训练样本
:param Y: 样本标签
:param sigma: 高斯核函数的sigma
:param gamma: 先验概率
:param toler: 松弛变量
'''
self.m, self.n = X.shape # m为样本数(60000),n为特征数(784=28*28)
self.X = X
self.Y = Y # 转化为列向量
self.sigma = sigma
self.gamma = gamma
self.toler = toler
self.alpha = [0 for _ in range(len(Y))] # 每一个样本对应的拉格朗日系数alpha
self.K = self.calKernel() # 高斯核矩阵,存放代替内积的核函数值
self.supportVector = [] # 支持向量的索引,用于预测。因为W只和alpha>0的样本有关。if __name__ == "__main__":
# 获取当前时间
start = time.time()
Data, label = loadData('heart.txt')
X_train, X_test, y_train, y_test = train_test_split(Data, label, test_size=0.2)
print('Init SVM classifier')
svm = L2SVM(X_train,y_train)
print('start to train')
svm.train(100)
print('start to test')
svm.test(X_test, y_test)
# 获取结束时间
end = time.time()
print('run time:', end - start)
一个是我创建的类 在主函数调用时报错Traceback (most recent call last): File "D:/code/pycharm/L2_svm.py", line 16, in <module> class L2SVM: File "D:/code/pycharm/L2_svm.py", line 243, in L2SVM svm = L2SVM(X_train,y_train)NameError: name 'L2SVM' is not defined
该怎么改
|
|