降维算法:主成分分析 VS 自动编码器(三)

简介: 降维算法:主成分分析 VS 自动编码器(三)

单层的以线性函数作为激活函数的自动编码器

# Standarise the Data
X_org = image_matrix.copy()
sc = StandardScaler()
X = sc.fit_transform(X_org)
# this is the size of our encoded representations
encoding_dim = reduced_pixel
# this is our input placeholder
input_img = Input(shape=(img.width,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='linear')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(img.width, activation=None)(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
#Encoder
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')
autoencoder.fit(X, X,
                 epochs=500,
                 batch_size=16,
                 shuffle=True)
encoded_imgs = encoder.predict(X)
decoded_imgs = decoder.predict(encoded_imgs)

image.png


自动编码器结构

检查各维度的相关性:

df_ae = pd.DataFrame(data = encoded_imgs,columns=list(range(encoded_imgs.shape[1])))
figure = plt.figure(figsize=(10,6))
corrMatrix = df_ae.corr()
sns.heatmap(corrMatrix, annot=False)
plt.show()

image.png

自动编码器降维后各维度相关性

相关矩阵表明新的变换特征具有一定的相关性。皮尔逊相关系数与0有很大的偏差。

接下来,我们通过降维后的数据来重构原始数据:

X_decoded_ae = sc.inverse_transform(decoded_imgs)
reconstructed_image_ae = Image.fromarray(np.uint8(X_decoded_ae))
plt.figure(figsize=(8,12))
plt.imshow(reconstructed_image_ae,cmap = plt.cm.gray)

image.png

自动编码器重构后的图像

计算重构后图像的均方根误差:

error_ae = my_rmse(image_matrix,X_decoded_ae)

计算可知,均方根误差为12.15。单层线性激活的自动编码器和 PCA 性能几乎一致。

三层的以非线性函数为激活函数的自动编码器

input_img = Input(shape=(img.width,))
encoded1 = Dense(128, activation='relu')(input_img)
encoded2 = Dense(reduced_pixel, activation='relu')(encoded1)
decoded1 = Dense(128, activation='relu')(encoded2)
decoded2 = Dense(img.width, activation=None)(decoded1)
autoencoder = Model(input_img, decoded2)
autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')
autoencoder.fit(X,X,
                 epochs=500,
                 batch_size=16,
                 shuffle=True)
# Encoder
encoder = Model(input_img, encoded2)
# Decoder
decoder = Model(input_img, decoded2)
encoded_imgs = encoder.predict(X)
decoded_imgs = decoder.predict(X)

image.png

自动编码器模型结构

图像重构:

X_decoded_deep_ae = sc.inverse_transform(decoded_imgs)
reconstructed_image_deep_ae = Image.fromarray(np.uint8(X_decoded_deep_ae))
plt.figure(figsize=(8,12))
plt.imshow(reconstructed_image_deep_ae,cmap = plt.cm.gray)

image.png

计算均方误差:

error_dae = my_rmse(image_matrix,X_decoded_deep_ae)

多层自动编码器的均方误差为 8.57,性能优于 PCA,提升了 28%。

具有非线性激活的附加层的自动编码器能够更好地捕获图像中的非线性特征。它能够比PCA更好地捕捉复杂的模式和像素值的突然变化。但是它需要花费相对较高的训练时间和资源。

总结

本文主要介绍了主成分分析以及自动编码器两种方法,具体分析两者的优缺点,并且通过一个生动的示例进行详解。

目录
相关文章
|
5天前
|
JSON 监控 算法
员工上网行为监控:利用Scala编写数据处理和分析算法
企业在数字化时代利用Scala进行员工上网行为监控,以确保合规和网络安全。通过Scala的数据处理和分析能力,读取CSV日志数据转换为DataFrame,分析员工行为,如统计最常访问网站。此外,还展示了将监控数据以JSON格式提交至公司网站的函数,实现实时信息更新与安全防护。
28 5
|
1天前
|
算法 搜索推荐 数据挖掘
MATLAB模糊C均值聚类FCM改进的推荐系统协同过滤算法分析MovieLens电影数据集
MATLAB模糊C均值聚类FCM改进的推荐系统协同过滤算法分析MovieLens电影数据集
|
1天前
|
算法 数据可视化 数据挖掘
数据分享|R语言改进的K-MEANS(K-均值)聚类算法分析股票盈利能力和可视化
数据分享|R语言改进的K-MEANS(K-均值)聚类算法分析股票盈利能力和可视化
|
1天前
|
数据采集 存储 算法
数据分享|Weka数据挖掘Apriori关联规则算法分析用户网购数据
数据分享|Weka数据挖掘Apriori关联规则算法分析用户网购数据
|
4天前
|
机器学习/深度学习 数据采集 算法
共享单车需求量数据用CART决策树、随机森林以及XGBOOST算法登记分类及影响因素分析
共享单车需求量数据用CART决策树、随机森林以及XGBOOST算法登记分类及影响因素分析
11 0
|
5天前
|
移动开发 算法 数据可视化
数据分享|Spss Modeler关联规则Apriori模型、Carma算法分析超市顾客购买商品数据挖掘实例
数据分享|Spss Modeler关联规则Apriori模型、Carma算法分析超市顾客购买商品数据挖掘实例
|
6天前
|
算法 数据可视化 搜索推荐
数据分享|Python用Apriori算法关联规则分析亚马逊购买书籍关联推荐客户和网络图可视化
数据分享|Python用Apriori算法关联规则分析亚马逊购买书籍关联推荐客户和网络图可视化
28 11
|
6天前
|
算法 数据可视化 大数据
圆堆图circle packing算法可视化分析电商平台网红零食销量采集数据
圆堆图circle packing算法可视化分析电商平台网红零食销量采集数据
34 13
|
7天前
|
算法 数据可视化 Python
R语言中使用多重聚合预测算法(MAPA)进行时间序列分析
R语言中使用多重聚合预测算法(MAPA)进行时间序列分析
13 0
|
12天前
|
算法
Copula 算法建模相依性分析股票收益率时间序列案例
Copula 算法建模相依性分析股票收益率时间序列案例
12 0