Python 如何索引(切片)数据帧
我正在尝试用预处理的数据集实现k-最近邻算法。我将数据导入为pandas dataframe,然后将其转换为numpy数组,但出现了以下错误-Python 如何索引(切片)数据帧,python,pandas,numpy,Python,Pandas,Numpy,我正在尝试用预处理的数据集实现k-最近邻算法。我将数据导入为pandas dataframe,然后将其转换为numpy数组,但出现了以下错误- File "/home/user/Documents/Mooc_implementation.py", line 8, in <module> x = num_data[:,:10] File "/usr/lib/python2.7/dist-packages/numpy/core/records.py", line 499,
File "/home/user/Documents/Mooc_implementation.py", line 8, in <module>
x = num_data[:,:10]
File "/usr/lib/python2.7/dist-packages/numpy/core/records.py", line 499, in __getitem__
obj = super(recarray, self).__getitem__(indx)
IndexError: too many indices for array
我该怎么办
dataset.head()的输出
在您的情况下,为什么要执行
dataset.to_记录(index=False)
它将转换为数组,您不能像num_数据[:,:10]
那样使用它。而且无需将数据集转换为列车测试拆分的记录(index=False)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
num_data = pd.read_csv('/home/user/Documents/MOOC dataset cleaned/student_reg_vle_info_assessment.csv')
# num_data = dataset.to_records(index=False)
x = num_data.iloc[:,:10]
y = num_data.iloc[:,10:11]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.20)
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=4)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
Q:为什么会出现此错误?
A:当您有一个pandas数据集并且需要对其进行索引时,您需要使用.iloc方法。为问题中的数据集建立索引的方式对于numpy数组索引非常有效。
使用以下命令:
当我这样做时,这个错误正在发生-类型错误:不可损坏类型我已经更新了答案,我忘了写
num\u数据。iloc
你能添加数据吗?在定义num_数据后也使用num_data.iloc
?是。使用这些:num\u data=pd.read\u csv('/home/user/Documents/MOOC dataset cleaned/student\reg\u vle\u info\u assessment.csv')
x=num\u data.iloc[:,:10]y=num\u data.iloc[:,10:11]是它的工作原理。谢谢
date_submitted date_registration date_unregistration sum_click \
0 18 -159 445 16
1 22 -53 445 4
2 30 -92 12 3
3 17 -52 445 1
4 26 -176 445 5
num_of_prev_attempts age_band region highest_education studied_credits \
0 0 0 0 0 240
1 0 1 1 0 60
2 0 1 2 1 60
3 0 1 3 1 60
4 0 2 4 2 60
score final_result
0 78 0
1 70 0
2 87 2
3 72 0
4 69 0
[Finished in 0.274s]
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
num_data = pd.read_csv('/home/user/Documents/MOOC dataset cleaned/student_reg_vle_info_assessment.csv')
# num_data = dataset.to_records(index=False)
x = num_data.iloc[:,:10]
y = num_data.iloc[:,10:11]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.20)
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=4)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('/home/user/Documents/MOOC dataset cleaned/student_reg_vle_info_assessment.csv')
x = dataset.iloc[:,:10]
y = dataset.iloc[:,10:11]