【问题标题】:how to generate a list within a list delimited by a space如何在由空格分隔的列表中生成列表
【发布时间】:2021-04-27 19:34:30
【问题描述】:

如何复制 itertools.product 的结果结构?

所以你知道 itertools.product 给了我们一个对象,我们需要把它们放在一个列表中以便我们可以打印它 .. 像这样的.. 对吧?

import itertools
import numpy as np

CN=np.asarray((itertools.product([0,1], repeat=5)))
print(CN)

我希望能够制作这样的东西,但我希望数据来自 csv 文件..所以我想做这样的东西

#PSEUDOCODE
import pandas as pd

df = pd.read_csv('csv here')

#a b c d are the columns that i want to get
x = list(df['a'] df['c'] df['c'] df['d'])

print(x)

所以结果会是这样的

[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]
 [5.  3.4 1.5 0.2]
 [4.4 2.9 1.4 0.2]
 [4.9 3.1 1.5 0.1]]

我该怎么做?

编辑: 我正在尝试学习如何进行递归特征消除,我在谷歌的一些代码中看到他们使用虹膜数据集..

from sklearn import datasets

dataset = datasets.load_iris()
x = dataset.data
print(x)

打印出来的样子是这样的

[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]
 [5.  3.4 1.5 0.2]
 [4.4 2.9 1.4 0.2]
 [4.9 3.1 1.5 0.1]]

我怎样才能使我的数据集像这样,以便我可以使用这个 RFE 模板?

# Recursive Feature Elimination
from sklearn import datasets
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
# load the iris datasets
dataset = datasets.load_iris()
# create a base classifier used to evaluate a subset of attributes
model = LogisticRegression()
# create the RFE model and select 3 attributes
rfe = RFE(model, 3)
print(rfe)
rfe = rfe.fit(dataset.data, dataset.target)
print("features:",dataset.data)
print("target:",dataset.target)
print(rfe)
# summarize the selection of the attributes
print(rfe.support_)
print(rfe.ranking_)

【问题讨论】:

  • 你能解释一下吗,你想达到什么目的?将 df 转换为列表?
  • 我正在尝试从 datasets.load_iris() 复制数据集。我编辑了问题以添加我想要实现的目标
  • 您不必这样做。我已经编辑了下面的答案
  • 带有[] 和空格的显示由numpy 数组生成。 Python 列表(列表)使用逗号分隔符。尝试了解代码中的各种对象 - 数组、列表等。这比显示细节更重要。

标签: python pandas numpy scikit-learn dataset


【解决方案1】:

您不必这样做。如果你想使用rfe.fit函数,你需要单独喂特征和目标。

所以如果你的 df 是这样的:

     a    b    c    d  target
0  5.1  3.5  1.4  0.2       1
1  4.9  3.0  1.4  0.2       1
2  4.7  3.2  1.3  0.2       0
3  4.6  3.1  1.5  0.2       0
4  5.0  3.6  1.4  0.2       1
5  5.4  3.9  1.7  0.4       1
6  4.6  3.4  1.4  0.3       0
7  5.0  3.4  1.5  0.2       0
8  4.4  2.9  1.4  0.2       1
9  4.9  3.1  1.5  0.1       1

你可以使用:

...
rfe = rfe.fit(df[['a', 'b', 'c', 'd']], df['target'])
...

【讨论】:

    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多