팬더를 사용하여 CSV 파일의 항목을 읽은 다음 인코딩하려고합니다.
내 코드는 다음과 같습니다.
import sklearn
from sklearn.utils import shuffle
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
from sklearn import linear_model, preprocessing
data = pd.read_csv("car.data") # import in data
print(data.head()) # show the top few lines of data
le = preprocessing.LabelEncoder() # object to change data into a numerical value
buying = le.fit_transform(list(data["buying"])) # input buying column into object le
maint = le.fit_transform(list(data["maint"])) # input maint column into object le
door = le.fit_transform(list(data["door"])) # input door column into object le
persons = le.fit_transform(list(data["persons"])) # input persons column into object le
lug_boot = le.fit_transform(list(data["lug_boot"])) # input lug_boot column into object le
safety = le.fit_transform(list(data["safety"])) # input safety column into object le
cls = le.fit_transform(list(data["class"])) # input class column into object le
predict = "class" # what will be predicted
x = list(zip(buying, maint, door, persons, lug_boot, safety)) # will put all of the values into one list (x)
y = list(cls) # will convert numpy array (cls) into list
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size = 0.1) # create new data so the machine can't memorise results
print(x_train, y_test) # show variables to test its working
그리고 내 car.data
파일 의 처음 몇 줄은
buying, maint, door, persons, lug_boot, safety, class
vhigh,vhigh,2,2,small,low,unacc
vhigh,vhigh,2,2,small,med,unacc
vhigh,vhigh,2,2,small,high,unacc
vhigh,vhigh,2,2,med,low,unacc
vhigh,vhigh,2,2,med,med,unacc
vhigh,vhigh,2,2,med,high,unacc
vhigh,vhigh,2,2,big,low,unacc
vhigh,vhigh,2,2,big,med,unacc
vhigh,vhigh,2,2,big,high,unacc
나는 모든 것을 올바르게하고 있다고 생각하지만 다음과 같은 오류가 발생합니다.
Traceback (most recent call last):
File "/opt/anaconda3/envs/tensor/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2895, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'maint'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/name/PycharmProjects/Machine_learning/KNN/KNN Working File.py", line 13, in <module>
maint = le.fit_transform(list(data["maint"])) # input maint column into object le
File "/opt/anaconda3/envs/tensor/lib/python3.6/site-packages/pandas/core/frame.py", line 2906, in __getitem__
indexer = self.columns.get_loc(key)
File "/opt/anaconda3/envs/tensor/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2897, in get_loc
raise KeyError(key) from err
KeyError: 'maint'
나는 왜 그것이 maint
변수가 아닌 변수 에 대한 오류를 주 었는지에 대해 가장 혼란 스럽습니다 buying
. 매우 혼란 스럽기 때문에 내가 뭘 잘못하고 있는지 알려주십시오. 감사.
'maint'앞에 공백이 있으므로 실제 키는 'maint'여야합니다.
csv 파일을 수정하거나 다음에서 플래그 skipinitialspace=True
를 지정하십시오 pd.read_csv()
.
data = pd.read_csv("car.data", skipinitialspace=True)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다