2017-07-21 3 views
1

Scikit-learnを使ってランダムフォレスト回帰をしようとしています。 Pandasを使用してデータをロードした後の最初のステップは、データをテストセットとトレーニングセットに分割することです。しかし、私はエラーを取得:私はGoogleで検索して、このエラーの様々な例を見つけたScikit-learn: "yの人口が最も少ないクラスは1人のメンバーしかいません"

The least populated class in y has only 1 member

を、私はまだ、このエラーが何を意味するのかの理解を得るように見えることはできません。

training_file = "training_data.txt" 
data = pd.read_csv(training_file, sep='\t') 

y = data.Result 
X = data.drop('Result', axis=1) 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123, stratify=y) 

pipeline = make_pipeline(preprocessing.StandardScaler(), RandomForestRegressor(n_estimators=100)) 

hyperparameters = { 'randomforestregressor__max_features' : ['auto', 'sqrt', 'log2'], 
       'randomforestregressor__max_depth' : [None, 5, 3, 1] } 

model = GridSearchCV(pipeline, hyperparameters, cv=10) 

model.fit(X_train, y_train) 

prediction = model.predict(X_test) 

joblib.dump(model, 'ms5000.pkl') 

train_test_split方法は、このスタックトレースが得られます。

Traceback (most recent call last): 
    File "/Users/justin.shapiro/Desktop/IPML_Model/model_definition.py", line 18, in <module> 
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.22, random_state=123, stratify=y) 
    File "/Library/Python/2.7/site-packages/sklearn/model_selection/_split.py", line 1700, in train_test_split 
train, test = next(cv.split(X=arrays[0], y=stratify)) 
    File "/Library/Python/2.7/site-packages/sklearn/model_selection/_split.py", line 953, in split 
for train, test in self._iter_indices(X, y, groups): 
    File "/Library/Python/2.7/site-packages/sklearn/model_selection/_split.py", line 1259, in _iter_indices 
raise ValueError("The least populated class in y has only 1" 
ValueError: The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2. 

これは私のデータセットのサンプルです:

var1 var2 var3 var4 var5 var6 var7 var8 Result 
high 5000.0 0  60  1000 75  0.23 0.75 17912.0 
mid  5000.0 0  60  1000 50  0.23 0.75 18707.0 
low  5000.0 0  60  1000 25  0.23 0.75 17912.0 
high 5000.0 5  60  1000 75  0.23 0.75 18577.0 
mid  5000.0 5  60  1000 50  0.23 0.75 19407.0 
low  5000.0 5  60  1000 25  0.23 0.75 18577.0 

は何、このエラーであり、どのように私はそれを取り除くことができますか?この行に上げ

答えて

2

エラー:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.22, random_state=123, stratify=y) 

がjshapy8説明@stratify=y

+0

を削除してください:これは回帰問題であり、階層化がある(付属ラベルに基づいて電車やテストを分割するために使用されます分類のために)。したがって、ここで層別化を使用することはできません。 –

関連する問題