参数
import numpy as np
import pandas as pd
from sklearn.model_selection import cross_val_score, train_test_split, KFold
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import LabelEncoder
import itertools
import tensorflow
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.layers import Dense, Activation, Flatten, Convolution1D, Dropout, MaxPooling1D, BatchNormalization
from tensorflow.keras.models import model_from_json
from tensorflow.keras.utils import plot_model
from tensorflow.python.keras.utils import np_utils
from tensorflow.keras.models import Sequential
from sklearn.manifold import TSNE
"""
一维卷积神经网络 输入为信号片段;输出为轴承的故障类别;
做的t-sne可视化demo程序
点靠颜色和形状区分 x o
"""
# 载入数据
df = pd.read_csv(r'...\TF2_t_SNE\14改.csv')
#####################################################################
# 定义神经网络
def baseline_model():
model = Sequential()
model.add(Convolution1D(16, 128, strides=1, input_shape=(1024, 1), padding="same"))
model.add(Activation('tanh'))
model.add(MaxPooling1D(2, strides=2, padding='same'))
# model.add(BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None))
model.add(Convolution1D(32, 3, padding='same'))
model.add(
BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros',
gamma_initializer='ones', moving_mean_initializer='zeros',
moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None))
model.add(Activation('tanh'))
model.add(MaxPooling1D(2, strides=2, padding='same'))
model.add(Flatten())
model.add(Dropout(0.3))
model.add(Dense(60, activation='tanh'))
model.add(Dense(2, activation='softmax'))
print(model.summary())
sgd = SGD(lr=0.01, nesterov=True, decay=1e-6, momentum=0.9)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# 模型训练
nb_epoch = 10
model = baseline_model()
estimator = model.fit(X_train, Y_train, epochs=nb_epoch, validation_data=(X_test, Y_test), batch_size=32)
num_layer=1
layer = tensorflow.keras.backend.function([model.layers[0].input], [model.layers[num_layer].output])
f1 = layer([X_train])[0]
np.set_printoptions(threshold=np.inf)
print(f"f1-shape: {f1.shape}")
# print(f1)
f2 = f1.reshape(f1.shape[0]*f1.shape[2], f1.shape[1])
# print(f2)
num = f1.shape[-1]
print(num)
def get_data(f1):
# digits = datasets.load_digits(n_class=10)
digits = 2
data = f2 # digits.data # 图片特征
label = K # digits.target # 图片标签
n_samples = f1.shape[0] * f1.shape[2]
n_features = f1.shape[1] # data.shape # 数据集的形状
return data, label, n_samples, n_features
data, label, n_samples, n_features = get_data(f1) # 调用函数,获取数据集信息
print('Starting compute t-SNE Embedding...')
ts = TSNE(n_components=2, init='pca', random_state=0)
# t-SNE降维
result = ts.fit_transform(data)
# 数据归一化与可视化
x_min, x_max = np.min(result, 0), np.max(result, 0)
data = (result - x_min) / (x_max - x_min) # 对数据进行归一化处理
#####################################################################
# 把标签读出来--因为就是增倍,且序号顺序未变
label2 = np.repeat(label, 16)
map_marker = {0.0: 'o', 1.0: 'x'}
markers = list(map(lambda x: map_marker[x], label2))
map_color = {0.0: 'b', 1.0: 'g'}
colors = list(map(lambda x: map_color[x], label2))
mscatter(data[:, 0], data[:, 1], s=20, c=colors, cmap='tab10', m=markers, alpha=0.3)
plt.legend()
plt.show()
[1] https://blog.csdn.net/m0_47410750/article/details/123119544?spm=1001.2014.3001.5502
[2] https://blog.csdn.net/u014571489/article/details/102667570