質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Kaggle

Kaggleは、機械学習モデルを構築するコンペティションのプラットフォームおよびその運営企業を指します。企業や政府といった組織とデータサイエンティスト・機械学習エンジニアを繋げるプラットフォームであり、単純なマッチングではなくコンペティションが特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

7648閲覧

Pythonのエラー: Unknown label type: 'continuous'

egpt

総合スコア25

Kaggle

Kaggleは、機械学習モデルを構築するコンペティションのプラットフォームおよびその運営企業を指します。企業や政府といった組織とデータサイエンティスト・機械学習エンジニアを繋げるプラットフォームであり、単純なマッチングではなくコンペティションが特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/04/27 14:02

KaggleでPythonを使っているときに起きたエラーです。
Unknown label type: 'continuous'と出ます。

解いている問題はHouse Pricesというチュートリアルの問題です。
何が良くないのでしょうか。

Python

1import numpy as np 2import pandas as pd 3#for visualization 4import matplotlib.pyplot as plt 5import seaborn as sns 6from jupyterthemes import jtplot 7jtplot.style(theme='monokai') 8pd.set_option('display.max_rows', 100) 9pd.set_option('display.max_columns', 100) 10 11#import data 12train = pd.read_csv("C:/Users/owner/Kaggle/HousePrice/train.csv") 13test = pd.read_csv("C:/Users/owner/Kaggle/HousePrice/test.csv") 14from sklearn.preprocessing import LabelEncoder 15 16for i in range(train.shape[1]): 17 if train.iloc[:,i].dtypes == object: 18 lbl = LabelEncoder() 19 lbl.fit(list(train.iloc[:,i].values) + list(test.iloc[:,i].values)) 20 train.iloc[:,i] = lbl.transform(list(train.iloc[:,i].values)) 21 test.iloc[:,i] = lbl.transform(list(test.iloc[:,i].values)) 22from sklearn.preprocessing import LabelEncoder 23 24for i in range(train.shape[1]): 25 if train.iloc[:,i].dtypes == object: 26 lbl = LabelEncoder() 27 lbl.fit(list(train.iloc[:,i].values) + list(test.iloc[:,i].values)) 28 train.iloc[:,i] = lbl.transform(list(train.iloc[:,i].values)) 29 test.iloc[:,i] = lbl.transform(list(test.iloc[:,i].values)) 30target = np.log(target) 31# feature importance using random forest 32from sklearn.ensemble import RandomForestRegressor 33rf = RandomForestRegressor(n_estimators=80, max_features='auto') 34rf.fit(X_train, target) 35 36ranking = np.argsort(-rf.feature_importances_) 37X_train = X_train.iloc[:,ranking[:5]] 38X_test = X_test.iloc[:,ranking[:5]] 39from sklearn import tree 40 41my_tree = tree.DecisionTreeClassifier() 42my_tree = my_tree.fit(X_train, target) 43my_prediction = my_tree.predict(X_test) 44# my_prediction = np.exp(my_prediction) 45print(my_prediction)

エラー内容

Python

1ValueError Traceback (most recent call last) 2<ipython-input-61-05b366c7c9d5> in <module> 3 2 4 3 my_tree = tree.DecisionTreeClassifier() 5----> 4 my_tree = my_tree.fit(X_train, target) 6 5 my_prediction = my_tree.predict(X_test) 7 6 # my_prediction = np.exp(my_prediction) 8 9~\Anaconda3\lib\site-packages\sklearn\tree\_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted) 10 875 sample_weight=sample_weight, 11 876 check_input=check_input, 12--> 877 X_idx_sorted=X_idx_sorted) 13 878 return self 14 879 15 16~\Anaconda3\lib\site-packages\sklearn\tree\_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted) 17 171 18 172 if is_classification: 19--> 173 check_classification_targets(y) 20 174 y = np.copy(y) 21 175 22 23~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y) 24 167 if y_type not in ['binary', 'multiclass', 'multiclass-multioutput', 25 168 'multilabel-indicator', 'multilabel-sequences']: 26--> 169 raise ValueError("Unknown label type: %r" % y_type) 27 170 28 171 29 30ValueError: Unknown label type: 'continuous'

targetのlogを取らなければ正常に動作します。
正規分布にしたいのでlogを取るべきと参考サイトにあり、このまま進めたいのですが、どうすればいいでしょうか。
どうぞよろしくお願いいたします!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

tree.DecisionTreeClassifier()をtree.DecisionTreeRegressor()にすれば解決しました。

投稿2020/04/27 15:07

egpt

総合スコア25

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

tree.DecisionTreeClassifierのfit()のパラメータyは下記の通り整数か文字列でなければなりません。
targetの中身が条件を満たしていないかと思われます。

fit(self, X, y, sample_weight=None, check_input=True, X_idx_sorted=None)

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.

sklearn.tree.DecisionTreeClassifier

投稿2020/04/27 15:00

meg_

総合スコア10579

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

egpt

2020/04/27 15:06

確かにそうみたいですね! tree.DecisionTreeRegressor()にすれば通りました。ありがとうございます(o_ _)o)) 日本語の記事でfitの引数を調べても全然出てこなかったので、これからは英語で検索をかけることにします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問