Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help; iris-train
#1
I'm a beginner of machine learning and now learning scikit-learn using iris analysis.

My code is here...

import pandas as pd
from sklearn import svm, metrics
from sklearn.model_selection import train_test_split

csv = pd.read_csv('iris.csv')

csv_data = csv[["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"]]
csv_label = csv["Name"]

train_data, test_data, train_label, test_label = train_test_split(csv_data, csv_label)

clf = svm.SVC(gamma = 'auto')
clf.fit(train_data, train_label)
pre = clf.predict(test_data)
But at the end it said

Error:
Traceback (most recent call last): File "iris-train2.py", line 9, in <module> csv_data = csv[["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"]] File "C:\Users\KarinSugiura\Desktop\MachineLearning\learning\lib\site-packages\pandas\core\frame.py", line 2934, in __getitem__ raise_missing=True) File "C:\Users\My name\lib\site-packages\pandas\core\indexing.py", line 1354, in _convert_to_indexer return self._get_listlike_indexer(obj, axis, **kwargs)[1] File "C:\Users\My name\lib\site-packages\pandas\core\indexing.py", line 1161, in _get_listlike_indexer raise_missing=raise_missing) File "C:\Users\My name\lib\site-packages\pandas\core\indexing.py", line 1246, in _validate_read_indexer key=key, axis=self.obj._get_axis_name(axis))) KeyError: "None of [Index(['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'], dtype='object')] are in the [columns]"
anyone can solve it??
Reply
#2
To solve this, we need to know content of the file "iris.csv". It seems that it hasn't been properly loaded or has column names
differ from those you are used in line #7.
It would be better, if you passed numpy arrays to scikit-learn classifiers (not data frames), e.g.

X = csv[[...column names go here...]].values # gets numpy array
y = csv["grouping variable name goes here"].values # gets numpy array

# you need to encode grouping variable y, e.g. using LabelEncoder from scikit-learn.
# you probably need to do column-wise scaling of your data,
# e.g. using StandardScaler or Pandas facilities. 

# do splitting, training and testing with X, y and scikit-learn
Reply
#3
To solve this, we need to know content of the file "iris.csv". It seems that it hasn't been properly loaded or has column names
differ from those you are used in line #7.


Thanks!! I didn't realize the difference of the name. Now I got it.
Sorry, it was too easy question...but great thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is my train and test accuracy so low? python420 0 2,033 Dec-08-2019, 08:51 PM
Last Post: python420
  Partitioning when splitting data into train and test-dataset Den0st 0 1,940 Dec-07-2019, 08:31 PM
Last Post: Den0st
  How to define train set and test set Raj 6 7,815 Mar-08-2018, 01:04 PM
Last Post: Raj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020