2017-12-26 14 views
0

ich in einem Zug die Varianz der Merkmale gespeichert berechnen wollen und Test-Datei ein, gefolgt:gibt es die Varianz jeder Spalte in Pandas

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) 
:

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 
.... 

für das ich habe folgendes geschrieben

Dies funktioniert offensichtlich nicht. das Ergebnis in variance_result.csv ist nur die Zugmatrix normalisiert. Also meine Frage, wie bekomme ich den Index der Spalten (Features), die eine Abweichung unter 20% haben. danke im voraus!

aktualisieren

ich die Varianz Problem auf diese Weise gelöst haben:

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) 

Nein, ich weiß noch nicht, wie Indizes von Funktionen mit einer Varianz bekommen sagen größer als 0.2 von variance anderen dank läuft eine Schleife!

Antwort

1

Setzen Sie den Schwellenwert auf 0.0 und verwenden Sie dann das variances_ Attribut des VarianceThreshold-Objekts, um die Varianzen aller Ihrer Features abzurufen. Dann können Sie feststellen, welche von ihnen eine geringere Varianz aufweisen.

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.  ]) 
Verwandte Themen