2017-12-26 3 views
0

の各列の分散行き方:そのためは、私は電車に保存されている機能の分散を計算し、テストの後を提出したいパンダ

col1 Feature0 Feature1  Feature2 Feature3 Feature4 Feature5 Feature6 Feature7  Feature8  Feature9 
col2  26658  40253.5 3.22115e+09 0.0277727 5.95939 266.56 734.248 307.364 0.000566779 0.000520574 
col3  2658 4053.5  3.25e+09 0.0277 5.95939 266.56 734.248 307.364 0.000566779 0.000520574 
.... 

を私は次のことを書いてきました:

import numpy as np 
from sklearn.decomposition import PCA 
import pandas as pd 
#from sklearn.preprocessing import StandardScaler 
from sklearn import preprocessing 
from matplotlib import pyplot as plt 

# Reading csv file 
training_file = 'Training.csv' 
testing_file = 'Test.csv' 
Training_Frame = pd.read_csv(training_file) 
Testing_Frame = pd.read_csv(testing_file) 
Training_Frame.shape 
# Now we have the feature values saved we start 
# with the standardisation of the those values 
stdsc = preprocessing.MinMaxScaler() 
np_scaled_train = stdsc.fit_transform(Training_Frame.iloc[:,:-2]) 

sel = VarianceThreshold(threshold=(.2 * (1 - .2))) 
sel.fit_transform(np_scaled_train) 
pd_scaled_train = pd.DataFrame(data=np_scaled_train) 
pd_scaled_train.to_csv('variance_result.csv',header=False, index=False) 

これは明らかに機能しません。 variance_result.csvの結果は列車行列が正規化されたものに過ぎません。 私の質問はどのように分散が20%のベローズを持つ列(機能)のインデックスを取得できますか?ありがとうございます。

私は分散の問題をこのように解決してきました

更新:

import numpy as np 
from sklearn.decomposition import PCA 
import pandas as pd 
#from sklearn.preprocessing import StandardScaler 
from sklearn import preprocessing 
from matplotlib import pyplot as plt 
from sklearn.feature_selection import VarianceThreshold 

# Reading csv file 
training_file = 'Training.csv' 
testing_file = 'Test.csv' 
Training_Frame = pd.read_csv(training_file) 
Testing_Frame = pd.read_csv(testing_file) 

Training_Frame.shape 
# Now we have the feature values saved we start 
# with the standardisation of the those values 
stdsc = preprocessing.MinMaxScaler() 
np_scaled_train = stdsc.fit_transform(Training_Frame.iloc[:,:-2]) 
pd_scaled_train = pd.DataFrame(data=np_scaled_train) 
variance =pd_scaled_train.apply(np.var,axis=0) 
pd_scaled_train.to_csv('variance_result.csv',header=False, index=False) 
temp_df = pd.DataFrame(variance.values,Training_Frame.columns.values[:-2]) 
temp_df.T.to_csv('Training_features_variance.csv',index=False) 

いいえ、私はまだ分散して機能のindecesを取得する方法がわからないがvarianceから0.2よりも大きいと言います他のおかげでループを実行する!

答えて

1

しきい値を0.0に設定してから、VarianceThresholdオブジェクトのvariances_属性を使用して、すべての機能の差異を取得するだけで、分散の差が小さいことを特定できます。

from sklearn.feature_selection import VarianceThreshold 
X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]] 
selector = VarianceThreshold() 
selector.fit_transform(X) 

selector.variances_ 
#Output: array([ 0.  , 0.22222222, 2.88888889, 0.  ]) 
関連する問題