簡體   English   中英

如何將多維列做成單值向量以訓練sklearn熊貓中的數據

[英]How to make a multi-dimensional column into a single valued vector for training data in sklearn pandas

我有一個數據集,其中某些列是幾個獨立值的組合,如下例所示:

id        age        marks
1          5          3,6,7
2          7          1,2
3          4          34,78,2

因此,列本身就是由多個值組成的,因此我需要將向量傳遞到機器學習算法中,我無法真正組合這些值來分配單個值,例如:

3,6,7 => 1
1,2 => 2
34,78,2 => 3

使我的新向量為

id        age        marks
1          5          1
2          7          2
3          4          3

然后將其傳遞給算法,因為這種組合的數量將是無限的,並且也可能無法真正捕獲數據的真實含義。

如何處理單個要素是多個要素的組合的情況。

注意 :

列標記中的值僅是示例,它可以是任何值列表。 它可以是整數列表或字符串列表,字符串由多個用逗號分隔的字符串組成

更新:我認為我們可以在這種情況下使用CountVectorizer

假設我們有以下DF:

In [33]: df
Out[33]:
   id  age        marks
0   1    5    [3, 6, 7]
1   2    7       [1, 2]
2   3    4  [34, 78, 2]
3   4   11    [3, 6, 7]

In [34]: %paste
from sklearn.feature_extraction.text import CountVectorizer
from nltk.tokenize import TreebankWordTokenizer

vect = CountVectorizer(ngram_range=(1,1), stop_words=None, tokenizer=TreebankWordTokenizer().tokenize)

X = vect.fit_transform(df.marks.apply(' '.join))

r = pd.DataFrame(X.toarray(), columns=vect.get_feature_names())
## -- End pasted text --

結果:

In [35]: r
Out[35]:
   1  2  3  34  6  7  78
0  0  0  1   0  1  1   0
1  1  1  0   0  0  0   0
2  0  1  0   1  0  0   1
3  0  0  1   0  1  1   0

舊答案:

您可以先將列表轉換為字符串,然后再對其進行分類

In [119]: df
Out[119]:
   id  age        marks
0   1    5    [3, 6, 7]
1   2    7       [1, 2]
2   3    4  [34, 78, 2]
3   4   11    [3, 6, 7]

In [120]: df['new'] = pd.Categorical(pd.factorize(df.marks.str.join('|'))[0])

In [121]: df
Out[121]:
   id  age        marks new
0   1    5    [3, 6, 7]   0
1   2    7       [1, 2]   1
2   3    4  [34, 78, 2]   2
3   4   11    [3, 6, 7]   0

In [122]: df.dtypes
Out[122]:
id          int64
age         int64
marks      object
new      category
dtype: object

如果marks是一列字符串,這也將起作用:

In [124]: df
Out[124]:
   id  age    marks
0   1    5    3,6,7
1   2    7      1,2
2   3    4  34,78,2
3   4   11    3,6,7

In [125]: df['new'] = pd.Categorical(pd.factorize(df.marks.str.join('|'))[0])

In [126]: df
Out[126]:
   id  age    marks new
0   1    5    3,6,7   0
1   2    7      1,2   1
2   3    4  34,78,2   2
3   4   11    3,6,7   0

Tp以[[x, y, z], [x, y, z]][[x, x], [y, y], [z, z]] (最適合該功能的是您需要致電),然后使用:

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(a=[1, 2, 3, 4], b=[3, 4, 3, 4], c=[[1,2,3], [1,2], [], [2]]))
df.values
zip(*df.values)

where
>>> df

   a  b          c
0  1  3  [1, 2, 3]
1  2  4     [1, 2]
2  3  3         []
3  4  4        [2]
>>> df.values

array([[1, 3, [1, 2, 3]],
       [2, 4, [1, 2]],
       [3, 3, []],
       [4, 4, [2]]], dtype=object)
>>> zip(*df.values)

[(1, 2, 3, 4), (3, 4, 3, 4), ([1, 2, 3], [1, 2], [], [2])]

要轉換列,請嘗試以下操作:

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(a=[1, 2], b=[3, 4], c=[[1,2,3], [1,2]]))
df['c'].apply(lambda x: np.mean(x))

之前:

>>> df
   a  b          c
0  1  3  [1, 2, 3]
1  2  4     [1, 2]

后:

>>> df
   a  b    c
0  1  3  2.0
1  2  4  1.5

您可以pd.factorize tuples
假設marks是一個列表

df

   id  age        marks
0   1    5    [3, 6, 7]
1   2    7       [1, 2]
2   3    4  [34, 78, 2]
3   4    5    [3, 6, 7]

應用tuple並分解

df.assign(new=pd.factorize(df.marks.apply(tuple))[0] + 1)

   id  age        marks  new
0   1    5    [3, 6, 7]    1
1   2    7       [1, 2]    2
2   3    4  [34, 78, 2]    3
3   4    5    [3, 6, 7]    1

設置df

df = pd.DataFrame([
        [1, 5, ['3', '6', '7']],
        [2, 7, ['1', '2']],
        [3, 4, ['34', '78', '2']],
        [4, 5, ['3', '6', '7']]
    ], [0, 1, 2, 3], ['id', 'age', 'marks']
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM