首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用数组列训练ML模型

使用数组列训练ML模型
EN

Stack Overflow用户
提问于 2020-01-15 00:49:22
回答 1查看 340关注 0票数 0

我正在尝试训练一个包含序列化的值列表的列的模型。但是我遇到了数据类型的错误。在拟合模型之前,我需要执行哪种预处理?

代码语言:javascript
复制
TypeError: float() argument must be a string or a number, not 'list'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 192, in <module>
    regression = train_audio_model()
  File "main.py", line 184, in train_audio_model
    regression.fit(X_train, Y_train)
  File "/Users/colton/code/audio-analysis/env/lib/python3.6/site-packages/sklearn/linear_model/_logistic.py", line 1527, in fit
    accept_large_sparse=solver != 'liblinear')
  File "/Users/colton/code/audio-analysis/env/lib/python3.6/site-packages/sklearn/utils/validation.py", line 755, in check_X_y
    estimator=estimator)
  File "/Users/colton/code/audio-analysis/env/lib/python3.6/site-packages/sklearn/utils/validation.py", line 531, in check_array
    array = np.asarray(array, order=order, dtype=dtype)
  File "/Users/colton/code/audio-analysis/env/lib/python3.6/site-packages/numpy/core/_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

data.csv

代码语言:javascript
复制
Col1 | Col2
-----------
1    | 1.2,-1.3
0    | -2.5,0.9

model.py

代码语言:javascript
复制
data = pd.read_csv('data.csv', converters={'Col2': lambda x: x.split(',')})
X_train, X_test, Y_train, Y_test = train_test_split(data.drop('Col1', axis=1), data['Col1'])

regression = LogisticRegression()
regression.fit(X_train, Y_train)
return regression

data.head(2) output

代码语言:javascript
复制
                 filename                                                                                spectrogram  beep
0  ./samples/nonbeep1.wav  [-315.49462890625, 138.87547302246094, -52.60832977294922, 29.540002822875977, -2.4793...     0
1  ./samples/nonbeep2.wav  [-368.6966552734375, 167.4494171142578, -23.79843521118164, 46.0974006652832, -1.74239...     0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-15 03:31:47

您需要将列表拆分为单独的列。这里有一个最小的例子来解释这个想法:

代码语言:javascript
复制
# sample df
df = pd.DataFrame({'col':[[1,2,3],[4,5,6]], 'target': [0,1]})

print(df)

         col  target
0  [1, 2, 3]       0
1  [4, 5, 6]       1

# convert column with list into separate column
df = pd.concat([df.pop('col').apply(pd.Series), df['target']], axis=1)

print(df)

   0  1  2  target
0  1  2  3       0
1  4  5  6       1

要训练模型,现在可以执行以下操作:

代码语言:javascript
复制
X_train, X_test, Y_train, Y_test = train_test_split(df.drop('target', axis=1), df['target'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59738322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档