Skip to content

Commit 31c83ba

Browse files
committed
2 parents 0c6d371 + 313c375 commit 31c83ba

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

04_Support_Vector_Machines/02_Working_with_Linear_SVMs/02_linear_svm.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Linear Support Vector Machine: Soft Margin
2-
#----------------------------------
2+
# ----------------------------------
33
#
44
# This function shows how to use TensorFlow to
55
# create a soft margin SVM
@@ -27,10 +27,12 @@
2727
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
2828
iris = datasets.load_iris()
2929
x_vals = np.array([[x[0], x[3]] for x in iris.data])
30-
y_vals = np.array([1 if y==0 else -1 for y in iris.target])
30+
y_vals = np.array([1 if y == 0 else -1 for y in iris.target])
3131

3232
# Split data into train/test sets
33-
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
33+
train_indices = np.random.choice(len(x_vals),
34+
round(len(x_vals)*0.8),
35+
replace=False)
3436
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
3537
x_vals_train = x_vals[train_indices]
3638
x_vals_test = x_vals[test_indices]
@@ -45,8 +47,8 @@
4547
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
4648

4749
# Create variables for linear regression
48-
A = tf.Variable(tf.random_normal(shape=[2,1]))
49-
b = tf.Variable(tf.random_normal(shape=[1,1]))
50+
A = tf.Variable(tf.random_normal(shape=[2, 1]))
51+
b = tf.Variable(tf.random_normal(shape=[1, 1]))
5052

5153
# Declare model operations
5254
model_output = tf.subtract(tf.matmul(x_data, A), b)
@@ -84,18 +86,26 @@
8486
rand_x = x_vals_train[rand_index]
8587
rand_y = np.transpose([y_vals_train[rand_index]])
8688
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
87-
89+
8890
temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
8991
loss_vec.append(temp_loss)
90-
91-
train_acc_temp = sess.run(accuracy, feed_dict={x_data: x_vals_train, y_target: np.transpose([y_vals_train])})
92+
93+
train_acc_temp = sess.run(accuracy, feed_dict={
94+
x_data: x_vals_train,
95+
y_target: np.transpose([y_vals_train])})
9296
train_accuracy.append(train_acc_temp)
93-
94-
test_acc_temp = sess.run(accuracy, feed_dict={x_data: x_vals_test, y_target: np.transpose([y_vals_test])})
97+
98+
test_acc_temp = sess.run(accuracy, feed_dict={
99+
x_data: x_vals_test,
100+
y_target: np.transpose([y_vals_test])})
95101
test_accuracy.append(test_acc_temp)
96-
97-
if (i+1)%100==0:
98-
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))
102+
103+
if (i + 1) % 100 == 0:
104+
print('Step #{} A = {}, b = {}'.format(
105+
str(i+1),
106+
str(sess.run(A)),
107+
str(sess.run(b))
108+
))
99109
print('Loss = ' + str(temp_loss))
100110

101111
# Extract coefficients
@@ -110,13 +120,13 @@
110120
# Get best fit line
111121
best_fit = []
112122
for i in x1_vals:
113-
best_fit.append(slope*i+y_intercept)
123+
best_fit.append(slope*i+y_intercept)
114124

115125
# Separate I. setosa
116-
setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==1]
117-
setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==1]
118-
not_setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==-1]
119-
not_setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==-1]
126+
setosa_x = [d[1] for i, d in enumerate(x_vals) if y_vals[i] == 1]
127+
setosa_y = [d[0] for i, d in enumerate(x_vals) if y_vals[i] == 1]
128+
not_setosa_x = [d[1] for i, d in enumerate(x_vals) if y_vals[i] == -1]
129+
not_setosa_y = [d[0] for i, d in enumerate(x_vals) if y_vals[i] == -1]
120130

121131
# Plot data and line
122132
plt.plot(setosa_x, setosa_y, 'o', label='I. setosa')
@@ -143,4 +153,4 @@
143153
plt.title('Loss per Generation')
144154
plt.xlabel('Generation')
145155
plt.ylabel('Loss')
146-
plt.show()
156+
plt.show()

0 commit comments

Comments
 (0)