示例#1
0
def test_svm():
    clf = svm.SVC(C=c, kernel='linear', tol=1e-3)
    clf.fit(X, y)
    pd.plot_data(X, y)
    vb.visualize_boundary(clf, X, 0, 4.5, 1.5, 5)
    plt.show()
    input('Program paused. Press ENTER to continue')
示例#2
0
# ===================== Part 2: Training Linear SVM =====================
# The following code will train a linear SVM on the dataset and plot the
# decision boundary learned
#

print('Training Linear SVM')

# You should try to change the C value below and see how the decision
# boundary varies (e.g., try C = 1000)

c = 1000
clf = svm.SVC(c, kernel='linear', tol=1e-3)
clf.fit(X, y)

pd.plot_data(X, y)
vb.visualize_boundary(clf, X, 0, 4.5, 1.5, 5)

input('Program paused. Press ENTER to continue')

# ===================== Part 3: Implementing Gaussian Kernel =====================
# You will now implement the Gaussian kernel to use
# with the SVM. You should now complete the code in gaussianKernel.py
#

print('Evaluating the Gaussian Kernel')

x1 = np.array([1, 2, 1])
x2 = np.array([0, 4, -1])
sigma = 2
sim = gk.gaussian_kernel(x1, x2, sigma)
示例#3
0
plt.show()

input('Program paused. Press enter to continue.\n')

# ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========
print('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\n')

# SVM parameters
C = 1
sigma = 0.1
gamma = 1.0 / (2.0 * sigma ** 2)

model = svm.SVC(C, kernel='rbf', gamma=gamma)
model.fit(X, y)

visualize_boundary(X, y, model)
plt.show()

input('Program paused. Press enter to continue.\n')

# =============== Part 6: Visualizing Dataset 3 ================
print('Loading and Visualizing Data ...\n')

data = sio.loadmat('ex6data3.mat')
X, y = data['X'], data['y'].flatten()
Xval = data['Xval']
yval = data['yval'].flatten()

plot_data(X, y)
plt.show()
data = scio.loadmat('data/ex6data1.mat')
# 分别取出特征数据 X 和对应的输出 Y,都是narray格式
X = data['X']
Y = data['y'].flatten()
# 绘制散点图
plot_data(X,Y)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')

# ===================== 2.训练SVM--使用线性核函数 =====================
# 使用sklearn自带的svm函数进行训练
C =100; # 异常点的权重
clf = svm.SVC(C, kernel='linear', tol=1e-3)
clf.fit(X, Y)
plot_data(X,Y)
vb.visualize_boundary(clf, X, 0, 4.5, 1.5, 5)


# ===================== 3.验证高斯核函数 =====================
x1 = np.array([1, 2, 1])
x2 = np.array([0, 4, -1])
sigma = 2
sim = gk.gaussian_kernel(x1, x2, sigma)



print('Gaussian kernel between x1 = [1, 2, 1], x2 = [0, 4, -1], sigma = {} : {:0.6f}\n'
      '(for sigma = 2, this value should be about 0.324652'.format(sigma, sim))
'''

# ===================== Part 4: Visualizing Dataset 2 =====================