【python 走进NLP】如何用Keras对分类问题进行类及其概率的预测

对于分类问题,模型学习的是一个输入特征到输出特征之间的映射,这里的输出即为一个标签。比如“垃圾邮件”和“非垃圾邮件”.下边是Keras中为简单的二分类问题开发的神经网络模型的一个例子.

# -*- encoding=utf-8 -*-


from keras.models import Sequential
from keras.layers import Dense
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import MinMaxScaler

# 生成二分类数据集

X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1)
scalar = MinMaxScaler()
scalar.fit(X)
X = scalar.transform(X)


# 定义并拟合出最终模型
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(X, y, epochs=500, verbose=0)
model.summary()
# 新的未知数据
Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1)
Xnew = scalar.transform(Xnew)

# 做预测概率
ynew = model.predict_proba(Xnew)
# 显示输入输出
for i in range(len(Xnew)):
    print("X=%s, Predicted=%s" % (Xnew[i], ynew[i]))



# 新的未知数据实例
Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1)
Xnew = scalar.transform(Xnew)
# 作出预测类别
ynew = model.predict_classes(Xnew)
# 显示输入和输出
for i in range(len(Xnew)):
    print("X=%s, Predicted=%s" % (Xnew[i], ynew[i]))

运行结果:

Using TensorFlow backend.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 4)                 12        
_________________________________________________________________
dense_2 (Dense)              (None, 4)                 20        
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 5         
=================================================================
Total params: 37
Trainable params: 37
Non-trainable params: 0
_________________________________________________________________
X=[ 0.89337759  0.65864154], Predicted=[ 0.00425744]
X=[ 0.29097707  0.12978982], Predicted=[ 0.98392618]
X=[ 0.78082614  0.75391697], Predicted=[ 0.00636977]
X=[ 0.89337759  0.65864154], Predicted=[0]
X=[ 0.29097707  0.12978982], Predicted=[1]
X=[ 0.78082614  0.75391697], Predicted=[0]

Process finished with exit code 0

一个类别预测会给定最终的模型以及若干数据实例,我们利用模型来判断这些实例的类别。
在Keras中,可以利用predict_class()函数来完成预测新数据样本的类别。

另外一种是对数据实例属于某一类的可能性进行预测。它被称为“概率预测”,
在Keras中,我们可以调用predict_proba()函数来实现

你可能感兴趣的:(深度学习--keras)