在进行kmeans聚类后找到特征重要性的python代码

问题描述 投票:0回答:1

[我研究了找到特征重要性的方法(我的数据集只有9个特征。以下是两种方法,但是我很难编写python代码。

我希望对影响聚类形成的每个特征进行排名。

  1. 计算每个维的质心的方差。差异最大的维度对于区分聚类最为重要。

  2. 如果变量很少,则可以进行某种留一法测试(删除1个var并重做群集)。还请记住,k-means取决于初始化,因此在重做群集时,您希望保持固定不变。

是否有任何python代码可以做到这一点?

python scikit-learn cluster-analysis k-means feature-selection
1个回答
2
投票

考虑进行这样的特征选择。

import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

# UNIVARIATE SELECTION

data = pd.read_csv('C:\\Users\\Excel\\Desktop\\Briefcase\\PDFs\\1-ALL PYTHON & R CODE SAMPLES\\Feature Selection - Machine Learning\\train.csv')
X = data.iloc[:,0:20]  #independent columns
y = data.iloc[:,-1]    #target column i.e price range

#apply SelectKBest class to extract top 10 best features
bestfeatures = SelectKBest(score_func=chi2, k=10)
fit = bestfeatures.fit(X,y)
dfscores = pd.DataFrame(fit.scores_)
dfcolumns = pd.DataFrame(X.columns)
#concat two dataframes for better visualization 
featureScores = pd.concat([dfcolumns,dfscores],axis=1)
featureScores.columns = ['Specs','Score']  #naming the dataframe columns
print(featureScores.nlargest(10,'Score'))  #print 10 best features


# FEATURE IMPORTANCE
data = pd.read_csv('C:\\your_path\\train.csv')
X = data.iloc[:,0:20]  #independent columns
y = data.iloc[:,-1]    #target column i.e price range
from sklearn.ensemble import ExtraTreesClassifier
import matplotlib.pyplot as plt
model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.show()

enter image description here

# Correlation Matrix with Heatmap
data = pd.read_csv('C:\\your_path\\train.csv')
X = data.iloc[:,0:20]  #independent columns
y = data.iloc[:,-1]    #target column i.e price range
#get correlations of each features in dataset
corrmat = data.corr()
top_corr_features = corrmat.index
plt.figure(figsize=(20,20))
#plot heat map
g=sns.heatmap(data[top_corr_features].corr(),annot=True,cmap="RdYlGn")

enter image description here

Dataset is available here:

https://www.kaggle.com/iabhishekofficial/mobile-price-classification#train.csv
© www.soinside.com 2019 - 2024. All rights reserved.