Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

python - how to generate a list within a list delimited by a space

how do i replicate the structure of result of itertools.product?

so as you know itertools.product gives us an object and we need to put them in a list so we can print it .. something like this.. right?

import itertools
import numpy as np

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

i want to be able to make something like that but i want the data to be from a csv file.. so i want to make something like this

#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)

so the result will be something like this

[[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]]

how can i do that?

EDIT: i am trying to learn how to do recursive feature elimination and i saw in some codes in google that they use the iris data set..

from sklearn import datasets

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

and when printed it looked something like this

[[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]]

how could i make my dataset something like that so i can use this RFE template ?

# 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_)
question from:https://stackoverflow.com/questions/65860326/how-to-generate-a-list-within-a-list-delimited-by-a-space

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't have to. If you want to use rfe.fit function, you need to feed features and target seperately.

So if your df is like:

     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

you can use:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...