Ich verwende from sklearn.linear_model import Lasso
in Python 2.7.6
Ich habe ein Skript geschrieben, mit dem ich eine Lasso-Regression für meine Features (X) und meine Ziele (y) durchgeführt habe. Ich habe es schon einmal verwendet und es funktioniert. Ich verwende es für einen neuen Datensatz (völlig andere Art von Daten) und erhalte alle 0 Koeffizienten.
Was bedeutet das? Kann ich irgendetwas ändern oder optimieren, um Daten zu erhalten?
Ich habe verschiedene Alpha-Parameter ausprobiert. Hier ist meine Funktion unten. Ich benutze dieses Klassensystem, um meine Modelle und Sachen zu speichern. Lassen Sie mich wissen, ob es verwirrend ist oder verallgemeinert werden muss. Ich denke, es ist ziemlich einfach. Meine Notation ist DF_
= DataFrame, D_
= Dictionary, SR_
= Series
#Create the models for store them
from sklearn.cross_validation import LeavePOut
from sklearn.linear_model import Lasso
import time
from collections import defaultdict
class Models:
def __init__(self,target=None,description=None,models=[],duration=0.0):
self.target = target; self.models = models; self.duration = duration; self.description = description
def summation(self):
return(float(sum([q[1] for q in self.models])))
def score(self):
return(self.summation()/len(self.models))
def synthesis(description, query_targets, D_target_Models, DF_attributes, DF_targets,alpha = 1):
"""
Updates Model object
Parameters:
[description] key for dictionary of D_target_Models that stores instances of class
[query_targets] list of targets to make models for in DF_targets
[D_target_Models] dictionary of dictionaries:
Outer dict: {description:targets};
Inner dict: {target:model_instance}
[DF_attributes] Pandas DataFrame of attributes (index = sample, column = attribute)
[DF_targets] Pandas DataFrame of targets (index = sample, column = targets)
[alpha] lambda for regression method
"""
lpo = LeavePOut(len(DF_attributes.index)/1000, p=2)
#Check order of indices
if (list(DF_attributes.index) == list(DF_targets.index)) == True:
# X.index = Y.index = range(len(X.index))
for target in query_targets:
#Create target instance
D_target_Models[description][target] = Models(target=target)
#Get query column for target
SR_target = DF_targets[target]
#Create and train models
models = []
for train_indices,test_indices in lpo:
#Check if all test sets have values
#NOTE!(These conditionsaren't essential for understanding the script. It's how I ensured there were no NAs)
condition_1 = all([(pd.isnull(SR_target.iloc[test_i]) == False) for test_i in test_indices])
condition_2 = DF_attributes.iloc[test_i].isnull().values.any() == False
condition_3 = None #Impute missing data on DF_attributes
conditions = [condition_1,condition_2]
if all(conditions) == True: #Assumes data is present for all features
#Create model
duration_start = time.time() #So I can time the modeling, not essential
model = Lasso(alpha=alpha)
#Update training indices with non-null target/sensitivity indices
train_indices = [train_i for train_i in train_indices if pd.isnull(SR_target.iloc[train_i]) == False]
#Assign X and y
train_X = DF_attributes.iloc[train_indices,:]
test_X = DF_attributes.iloc[test_indices,:]
train_y = SR_target.iloc[train_indices]
test_y = SR_target.iloc[test_indices]
#Fit model
model.fit(train_X,train_y)
#Predict
predicted_values = model.predict(test_X)
correct_values = test_y
accuracy = int((predicted_values[0] > predicted_values[1]) == (correct_values[0] > correct_values[1]))
if accuracy == 1:
if len(set(model.coef_)) > 1:
print(set(model.coef_)) #ALL COEFFICIENTS ARE 0.0
#Store models
models.append((model,accuracy,test_indices))
#Store time for models
D_target_Models[description][target].models = models
D_target_Models[description][target].duration = float(time.time() - duration_start)
return(D_target_Models)
else:
return("DF_attributes.index != DF_target.index")