Skip to content

Commit bb6771f

Browse files
committed
update
1 parent f1185af commit bb6771f

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

classic-models/bagging_clf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
import tensorflow as tf
2+
from utils import one_hot
33
from sklearn.tree import DecisionTreeClassifier
44

55

@@ -19,7 +19,7 @@ def fit(self, X, y):
1919

2020
def predict(self, X):
2121
ys = [model.predict(X) for model in self.models]
22-
ys_one_hot = [tf.contrib.keras.utils.to_categorical(y) for y in ys]
22+
ys_one_hot = [one_hot(y) for y in ys]
2323
return np.argmax(np.sum(ys_one_hot, axis=0), axis=1)
2424

2525

classic-models/random_forest_clf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from sklearn.tree import DecisionTreeClassifier
22
import math
33
import numpy as np
4-
import tensorflow as tf
4+
from utils import one_hot
55

66

77
class RandomForestClassifier:
@@ -23,7 +23,7 @@ def fit(self, X, y):
2323

2424
def predict(self, X):
2525
ys = [tree.predict(X[:, self._features[i]]) for i, tree in enumerate(self._forest)]
26-
ys_one_hot = [tf.contrib.keras.utils.to_categorical(y) for y in ys]
26+
ys_one_hot = [one_hot(y) for y in ys]
2727
return np.argmax(np.sum(ys_one_hot, axis=0), axis=1)
2828

2929

classic-models/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ def plot_decision_boundary(X, model):
3333
# Put the result into a color plot
3434
Z = Z.reshape(xx.shape)
3535
plt.contour(xx, yy, Z, cmap=plt.cm.Paired)
36+
37+
38+
def one_hot(y, num_classes=None):
39+
y = np.array(y, dtype='int').ravel()
40+
if not num_classes:
41+
num_classes = np.max(y) + 1
42+
n = y.shape[0]
43+
categorical = np.zeros((n, num_classes))
44+
categorical[np.arange(n), y] = 1
45+
return categorical

nlp-models/tensorflow/word2vec_skipgram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def fit(self, n_epoch=10, batch_size=1000, top_k=5, eval_step=1000):
144144
y = np.array(y)[:, np.newaxis]
145145
_, loss = self.sess.run([self.train_op, self.loss], {self.x: x, self.y: y})
146146
if local_step % 50 == 0:
147-
print ('Epoch %d/%d | Batch %d/%d | train loss: %.4f |' %
147+
print ('Epoch %d/%d | Batch %d/%d | train loss: %.4f' %
148148
(epoch+1, n_epoch, local_step, n_batch, loss))
149149
global_step += 1
150150
if local_step % eval_step == 0:

0 commit comments

Comments
 (0)