如何将Python列表列表转换为sklearn.preprocessing的2D numpy数组

时间:2022-10-29 18:10:52

I currently have a list which contains all of my input for an sklearn classifier. Each element in that list is a list of features, where each element represents a song in my dataset.

我目前有一个列表,其中包含我对sklearn分类器的所有输入。该列表中的每个元素都是一个功能列表,其中每个元素代表我的数据集中的一首歌曲。

I need to convert this structure to a 2D numpy array so I can scale my data via sklearn's preprocessing. This is proving to be very difficult.

我需要将此结构转换为2D numpy数组,以便通过sklearn的预处理来扩展我的数据。事实证明这非常困难。

y = [] all_feats = [] for song in data: mfccs_in_song = song[0] oned_mfccs_in_song = [] for frame in mfccs_in_song: for m in frame: oned_mfccs_in_song.append(m) all_feats.append(oned_mfccs_in_song) label = song[-1] y.append(label)

y = [] all_feats = []用于数据中的歌曲:mfccs_in_song = song [0] oned_mfccs_in_song = []用于mfccs_in_song中的帧:for m in frame:oned_mfccs_in_song.append(m)all_feats.append(oned_mfccs_in_song)label = song [ - 1] y.append(标签)

Long story short, all_feats is that list of lists. It has a length of 600. How can I convert this to a numpy array for preprocessing? I have tried numerous things, including simply all_feats = np.array(all_feats), however that does not work.

长话短说,all_feats就是列表清单。它的长度为600.如何将其转换为numpy数组进行预处理?我尝试了很多东西,包括简单的all_feats = np.array(all_feats),但这不起作用。

1 个解决方案

#1


1  

That error suggests that all_feats may not have sublists of the same size. Take a look at its contents, and once you figure out what's the right length for the sublists, and how to prune the extra elements out, you can run all_feats = np.array(all_feats) and it should work!

该错误表明all_feats可能没有相同大小的子列表。看看它的内容,一旦你弄清楚子列表的长度是多少,以及如何修剪额外的元素,你可以运行all_feats = np.array(all_feats),它应该可以工作!

Take a look at the answers in this link for more explanation.

请查看此链接中的答案以获取更多说明。

#1


1  

That error suggests that all_feats may not have sublists of the same size. Take a look at its contents, and once you figure out what's the right length for the sublists, and how to prune the extra elements out, you can run all_feats = np.array(all_feats) and it should work!

该错误表明all_feats可能没有相同大小的子列表。看看它的内容,一旦你弄清楚子列表的长度是多少,以及如何修剪额外的元素,你可以运行all_feats = np.array(all_feats),它应该可以工作!

Take a look at the answers in this link for more explanation.

请查看此链接中的答案以获取更多说明。