3 min read

NMF矩阵变换的理解

来源于 Unsupervised Learning in Python 学习笔记 - A Hugo website , 要改一起改。

Non-negative matrix factorization (NMF) | Python

interpretable 真的PCA,看不懂啊。完全是梦幻走步,瞎给idea啊。 但是,NMF必须都要非负啊,啊啊啊啊。

最后的NMF特征,可以看成是原特征的线形相加。

x~=β1x1+β2x2+beta3x3 其中,x,x1,x2,x3都是向量。

fit() / transform()都可以用。

NumPy arrays and with csr_matrix都可以用。

NMF(n_components=2)设定好,和主成分分析很像。

from sklearn.decomposition import NMF引入。

model = NMF(n_components=2)设定好特征变量数量。

model.fit(samples)跑模型。

nmf_features = model.transform(samples)矩阵转置成功。

NMF=

但是我还是没弄懂怎么个相乘方法。

NMF applied to Wikipedia articles | Python

# Import NMF
from sklearn.decomposition import NMF

# Create an NMF instance: model
model = NMF(n_components = 6)

# Fit the model to articles
model.fit(articles)

# Transform the articles: nmf_features
nmf_features = model.transform(articles)

# Print the NMF features
print(nmf_features)

NMF features of the Wikipedia articles | Python

# Import pandas
import pandas as pd

# Create a pandas DataFrame: df
df = pd.DataFrame(nmf_features, index=titles)

# Print the row for 'Anne Hathaway'
print(df.loc['Anne Hathaway'])

# Print the row for 'Denzel Washington'
print(df.loc['Denzel Washington'])
In [2]: print(df.loc['Denzel Washington'])
0    0.000000
1    0.005601
2    0.000000
3    0.422380
4    0.000000
5    0.000000
Name: Denzel Washington, dtype: float64

In [3]: print(df.loc['Denzel Washington'])
0    0.000000
1    0.005601
2    0.000000
3    0.422380
4    0.000000
5    0.000000
Name: Denzel Washington, dtype: float64

第三个特征非常屌啊。

终于看懂了。哈哈。

Sm×n=Fm×n^×Cn^×n

其中 原样本是S,二维矩阵,m表示ID数,n表示特征变量数, 原样本是F,二维矩阵,m表示ID数,n^表示NMF特征变量数,这是降维、变密集的结果,其中n^n, 原样本是C,二维矩阵,n^表示NMF特征变量数,n表示特征变量数。

其中 Sm×n是高维度的、容易过拟合的,且稀疏、容易不显著的。 Fm×n^是低维度的、不容易过拟合的,且密集、容易显著的。

n^nn^+noise=n

因此我们的假设认为,我们的降维是剔除了噪音的。噪音是过拟合的本质,因此NMF剔除了噪音,因此避免了过拟合。

为了方便理解,做一个简单的计算。

假设原样本的某一行[0.12,0.18,0.32,0.14], 表示某个用户的四个特征。 产生S1×4, 假设NMF特征向量F1×2计算结果为[0.15,0.12],表示用户的两个NMF特征。 假设NMF权重C2×4[0.0102.130.540.991.4700.5]

所以结果满足

[0.15,0.12]×[0.0102.130.540.991.4700.5]=[0.1203,0.1764,0.3195,0.141]=[0.12,0.18,0.32,0.14]F1×2×C2×4=S1×4

这里的优势在于:

相当于逻辑回归的模块化, 针对每一个用户的C2×4都是不一样的, 因此可以做到每个用户的精细化转化变量。

相当于主成分分析, 针对每一个用户的C2×4都是都反映了F1×2中不同变量的权重, 因此转化结果可以给业务部门解释。

C2×4=[V1V2V3V4w11w12w13w14w21w22w23w24]

其中wij衡量了某用户第j个原始变量在第i个NMF变量中的权重。 对于每个NMF变量,我们都可以做个排序,看看哪个原始变量最强! 因此NMF权重矩阵,衡量我们变量优良中差顺序。

NMF learns interpretable parts | Python

没有看懂这个地方图片能干嘛!!!

# Import pandas
import pandas as pd

# Create a DataFrame: components_df
components_df = pd.DataFrame(model.components_,columns=words)

# Print the shape of the DataFrame
print(components_df.shape)

# Select row 3: component
component = components_df.iloc[3]

# Print result of nlargest
print(component.nlargest())
<script.py> output:
    (6, 13125)
    film       0.627877
    award      0.253131
    starred    0.245284
    role       0.211451
    actress    0.186398
    Name: 3, dtype: float64
    

Explore the LED digits dataset | Python

# Import pyplot
from matplotlib import pyplot as plt

# Select the 0th row: digit
digit = samples[0]

# Print digit
print(digit)

# Reshape digit to a 13x8 array: bitmap
bitmap = digit.reshape(13,8)

# Print bitmap
print(bitmap)

# Use plt.imshow to display bitmap
plt.imshow(bitmap, cmap='gray', interpolation='nearest')
plt.colorbar()
plt.show()
In [16]: print(bitmap)
[[ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]]

这就是稀疏矩阵。