Simple classification of deep learning

Simple binary classification

Manufacturing data

from sklearn.model_selection import train_test_split
from sklearn import datasets
import matplotlib.pyplot as plt
from tensorflow import keras

X,y = datasets.make_blobs(n_samples=1000,random_state=8,centers=2)

plt.scatter(X[:,0],X[:,1],c=y)
plt.show()

Build models and train

model = keras.models.Sequential([ keras.layers.Dense(32,input_shape=X.shape[1:]), keras.layers.Dense(1,activation=keras.activations.sigmoid)] ) model.summary() model.compile(loss = Keras. Losses. Binary_crossentropy, optimizer = keras. Optimizers. RMSprop (learning_rate = 0.1), the metrics = [keras. Metrics. Accuracy ()]). The model fit (X, y, validation_split = 0.25, epochs = 20)

Review test data and forecast data

print(y[0:10])
y_pre = model.predict(X[0:10])
print(y_pre)


[0 1 1 0 0 1 0 1 1 1]

[[0. 1. 1. 0. 0. 1. 0. 1. 1. 1.]]

Many classification

Manufacturing data

from sklearn.model_selection import train_test_split
from sklearn import datasets
import matplotlib.pyplot as plt
from tensorflow import keras

X,y = datasets.make_blobs(n_samples=1000,random_state=8,centers=3)

plt.scatter(X[:,0],X[:,1],c=y)
plt.show()

Build models and train

model = keras.models.Sequential([ keras.layers.Dense(32,input_shape=X.shape[1:], activation='relu'), keras.layers.Dense(3,activation=keras.activations.softmax)] ) model.summary() model.compile(loss = keras.losses.sparse_categorical_crossentropy, optimizer = keras.optimizers.Adam(), The metrics = [' accuracy ']) model. The fit (X, y, validation_split = 0.25, epochs = 20)

View the data

Print (y[0:10]) y_pre = model.predict(X[0:10]) import numpy as np print(np.reshape(y_pre,[10,3])) [1 1 2 2 1 1 2 2 2 1 1] [[4.50088549E-03 9.95355964E-01 1.43211320E-04] [3.52771860E-03 2.17666663E-03 9.94295657E-01] [5.39137749E-04] 9.99391794E-01 6.91057867E-05] [3.10646836E-03 9.93669093E-01 3.22450022E-03] [1.59081508E-04 9.99381661E-01 4.59307077E-04] [3.76076205E-04 2.09796475E-03 9.97525990E-01] [1.03477845E-02 9.88485038E-01 1.16714649E-03] [8.82121618E-04 1.39025709E-04 9.98978853E-01] [2.29390264E-02 9.75332916E-01 1.72808918E-03] [4.69710241E-04 9.99316335 2.13949577 e-04 e-01]]