Reputation: 1
I'm a beginner.I am now writing a feature selection algorithm under sklearn, and I wrote a simple program to try it out. The goal is to take out certain columns in the dataset, but the result is an error. The following is the code and error message. Please help me,Thank you.What should i do?
import numpy as np
from sklearn.datasets import load_iris
data = load_iris()
data=np.array(data)
print(data[:,[0,1]]) #Take the first two columns of the dataset and print
error message:
Traceback (most recent call last):
File "K:/b/f_s_DF.py", line 6, in <module>
print(data[:,[0,1]])#Take the first two columns of the dataset and print
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed
Upvotes: 0
Views: 98
Reputation: 2615
First thing, you need to change the iris type <class 'sklearn.utils.Bunch'>
to pandas dataframe
. then numpy ndarray
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
data1 = np.array(pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target']))
print(data1[:,:2])
while getting the value from ndarray, no need to give the index like 1,2
. you can give :2
.
Upvotes: 1
Reputation: 494
Rather than converting Numpy.ndarray
You con convert these data on pandas Dataframe
and Easily see the Fist two or any Column. Please check out the below code:
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
datasets = pd.DataFrame(data['data'], columns =
data['feature_names'])
target_val = pd.Series(data['target'], name =
'target_values')
datasets['target'] = target_val
datasets[:2]
Output:
sepal length sepal width petal length petal width target
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
Upvotes: 0