質問編集履歴
4
表現変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,19 @@
|
|
1
|
-
あるサイトでcategory_mapping
|
1
|
+
あるサイトでcategory_mappingで数値変換対応表を作るコードを参考にしておりますが、
|
2
2
|
「cannot unpack non-iterable int object」というエラーが出てしまいます。
|
3
3
|
エラー文を検索してもいいヒントが見つからず困っています。
|
4
4
|
|
5
|
-
カテゴリ
|
5
|
+
カテゴリー列をOrdinal Encoderで数値変換した対応表を作成したいと思っております。
|
6
6
|
他にいい方法があれば、この方法にこだわっている訳ではありません。
|
7
7
|
|
8
|
+
colの出力が
|
9
|
+
{'col': '出身校', 'mapping': 大阪大学 1
|
10
|
+
神戸大学 2
|
11
|
+
岡山大学 3
|
12
|
+
山口大学 4
|
13
|
+
徳島大学 5
|
14
|
+
|
15
|
+
となっており、mapping列にある「〇〇大学」と「数字」を別々の変数(label、ord_um)に
|
8
|
-
|
16
|
+
格納して表示させたいのですが、この記述だと入れられないという事です。
|
9
|
-
問題は一番下の列群です。
|
10
17
|
アドバイスいただけますと幸いです。
|
11
18
|
|
12
19
|
```ここに言語を入力
|
@@ -17,6 +24,7 @@
|
|
17
24
|
df[categorical_cols] = ce_oe.fit_transform(df[categorical_cols])
|
18
25
|
|
19
26
|
#変換項目の対応表を作成(ここの記述に関してのご相談です)
|
27
|
+
|
20
28
|
def get_ordinal_mapping(encoding_instance):
|
21
29
|
map_list = []
|
22
30
|
for col in ce_oe.category_mapping:
|
3
問題点に絞りました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,75 +1,47 @@
|
|
1
|
-
あるサイトでcategory_mappingの対応表を作るのコードを参考にさせていただいておりますが、
|
1
|
+
あるサイトでcategory_mappingの対応表を作るのコードを参考にさせていただいておりますが、
|
2
|
+
「cannot unpack non-iterable int object」というエラーが出てしまいます。
|
3
|
+
エラー文を検索してもいいヒントが見つからず困っています。
|
2
4
|
|
3
|
-
---------------------------------------------------------------------------
|
4
|
-
TypeError Traceback (most recent call last)
|
5
|
-
~\AppData\Local\Temp/ipykernel_7788/3970629349.py in <module>
|
6
|
-
----> 1 get_ordinal_mapping(ce_oe)
|
7
|
-
|
8
|
-
~\AppData\Local\Temp/ipykernel_7788/2653423329.py in get_ordinal_mapping(encoding_instance)
|
9
|
-
48 map_list = []
|
10
|
-
49 for column in encoding_instance.category_mapping:
|
11
|
-
---> 50 for (label, ord_num) in column['mapping']:
|
12
|
-
51 map_list.append([column['col'], label, ord_num])
|
13
|
-
52 map_df = pd.DataFrame(map_list, columns=['column', 'label', 'ord_num'])
|
14
|
-
|
15
|
-
TypeError: cannot unpack non-iterable int object
|
16
|
-
---------------------------------------------------------------------------
|
17
|
-
|
18
|
-
|
5
|
+
カテゴリデータをOrdinal Encoderで数値化して変換データの対応表を取得したいと思っております。
|
19
6
|
他にいい方法があれば、この方法にこだわっている訳ではありません。
|
20
|
-
|
21
|
-
カテゴリ項目の数値化は何十項目もあるのですが、一列を取ってみると下記の様になっており、変数の数は間違っていない様のですが、色々試してみた結果、mappingが二列データを持っているのを上手に記述できていない感じです。
|
22
|
-
初心者で応用力が無いので対応できませんでした。
|
23
|
-
|
24
|
-
{'col': '出身校',
|
25
|
-
'mapping': 〇〇大学 1
|
26
|
-
〇〇大学 2
|
27
|
-
〇〇大学 3
|
28
|
-
..
|
29
|
-
その他大学 85
|
30
|
-
NaN -2
|
31
|
-
Length: 89, dtype: int64,
|
32
|
-
'data_type': dtype('O')}
|
33
7
|
|
34
8
|
下記が関係するところのコードになります。
|
35
9
|
問題は一番下の列群です。
|
36
10
|
アドバイスいただけますと幸いです。
|
37
11
|
|
38
12
|
```ここに言語を入力
|
13
|
+
df = pd.read_csv(url)
|
39
14
|
useful_features = [c for c in df.columns if c not in (id)]
|
40
|
-
#文字変数と数値変数の振り分け
|
41
15
|
categorical_cols = [c for c in useful_features if df[c].dtype == 'object']
|
42
|
-
numerical_cols = [c for c in useful_features if df[c].dtype in ['int64', 'float64']]
|
43
|
-
#標準化したくない数値項目を選択
|
44
|
-
numerical_features = numerical_cols
|
45
|
-
remove_numerical_features = 'graduate', 'Year'
|
46
|
-
for remove_numerical_feature in remove_numerical_features:
|
47
|
-
numerical_features.remove(remove_numerical_feature)
|
48
|
-
#特徴量の比率をそろえるために数値の標準化
|
49
|
-
scaler = StandardScaler()
|
50
|
-
df[numerical_cols] = scaler.fit_transform(df[numerical_cols])
|
51
|
-
|
52
|
-
# カテゴリ値変換
|
53
16
|
ce_oe = ce.OrdinalEncoder(cols=categorical_cols,handle_unknown='impute')
|
54
17
|
df[categorical_cols] = ce_oe.fit_transform(df[categorical_cols])
|
55
|
-
#値を1の始まりから0の始まりにする(LGB用)
|
56
|
-
for i in categorical_cols:
|
57
|
-
df[categorical_cols][i] = df[categorical_cols][i] - 1
|
58
|
-
# カテゴリ項目から外す特徴量
|
59
|
-
categorical_features = categorical_cols
|
60
|
-
remove_categorical_features = reserch_column,'target',
|
61
|
-
for remove_categorical_feature in remove_categorical_features:
|
62
|
-
categorical_features.remove(remove_categorical_feature)
|
63
18
|
|
64
|
-
|
19
|
+
#変換項目の対応表を作成(ここの記述に関してのご相談です)
|
65
20
|
def get_ordinal_mapping(encoding_instance):
|
66
|
-
|
21
|
+
map_list = []
|
67
|
-
for col
|
22
|
+
for col in ce_oe.category_mapping:
|
68
|
-
for (label, ord_num) in col
|
23
|
+
for (label, ord_num) in col['mapping']:
|
69
|
-
map_list.append([col
|
24
|
+
map_list.append([col['col'], label, ord_num])
|
70
25
|
map_df = pd.DataFrame(map_list, columns=['column', 'label', 'ord_num'])
|
71
26
|
return map_df
|
72
27
|
|
73
28
|
get_ordinal_mapping(ce_oe)
|
29
|
+
```
|
74
30
|
|
31
|
+
---------------------------------------------------------------------------
|
32
|
+
TypeError Traceback (most recent call last)
|
33
|
+
~\AppData\Local\Temp\4/ipykernel_55428/1328553831.py in <module>
|
34
|
+
46 # map_df = pd.DataFrame(map_list, columns=['column', 'label', 'ord_num'])
|
75
|
-
|
35
|
+
47
|
36
|
+
---> 48 get_ordinal_mapping(ce_oe)
|
37
|
+
49
|
38
|
+
50 # ce_oe_map = ce_oe.category_mapping
|
39
|
+
|
40
|
+
~\AppData\Local\Temp\4/ipykernel_55428/1328553831.py in get_ordinal_mapping(encoding_instance)
|
41
|
+
34 map_list = []
|
42
|
+
35 for col in ce_oe.category_mapping:
|
43
|
+
---> 36 for (label, ord_num) in col['mapping']:
|
44
|
+
37 map_list.append([col['col'], label, ord_num])
|
45
|
+
38 map_df = pd.DataFrame(map_list, columns=['column', 'label', 'ord_num'])
|
46
|
+
|
47
|
+
TypeError: cannot unpack non-iterable int object
|
2
記述修正です。
test
CHANGED
File without changes
|
test
CHANGED
@@ -55,13 +55,13 @@
|
|
55
55
|
#値を1の始まりから0の始まりにする(LGB用)
|
56
56
|
for i in categorical_cols:
|
57
57
|
df[categorical_cols][i] = df[categorical_cols][i] - 1
|
58
|
-
#
|
58
|
+
# カテゴリ項目から外す特徴量
|
59
|
+
categorical_features = categorical_cols
|
59
|
-
remove_
|
60
|
+
remove_categorical_features = reserch_column,'target',
|
60
|
-
for remove_
|
61
|
+
for remove_categorical_feature in remove_categorical_features:
|
61
|
-
categorical_features.remove(remove_
|
62
|
+
categorical_features.remove(remove_categorical_feature)
|
62
63
|
|
63
64
|
#変換項目の対応表を作成(ここの記述に関してのご相談です)
|
64
|
-
categorical_features = categorical_cols
|
65
65
|
def get_ordinal_mapping(encoding_instance):
|
66
66
|
map_list = []
|
67
67
|
for column in encoding_instance.category_mapping:
|
1
記述が間違っていましたので修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -55,14 +55,13 @@
|
|
55
55
|
#値を1の始まりから0の始まりにする(LGB用)
|
56
56
|
for i in categorical_cols:
|
57
57
|
df[categorical_cols][i] = df[categorical_cols][i] - 1
|
58
|
-
#
|
58
|
+
# 数値項目から外す特徴量
|
59
|
-
categorical_features = categorical_cols
|
60
59
|
remove_numerical_features = reserch_column,'target',
|
61
60
|
for remove_numerical_feature in remove_numerical_features:
|
62
61
|
categorical_features.remove(remove_numerical_feature)
|
63
62
|
|
64
63
|
#変換項目の対応表を作成(ここの記述に関してのご相談です)
|
65
|
-
|
64
|
+
categorical_features = categorical_cols
|
66
65
|
def get_ordinal_mapping(encoding_instance):
|
67
66
|
map_list = []
|
68
67
|
for column in encoding_instance.category_mapping:
|