Python 对多维数据使用knn时出错
我是机器学习的初学者,我试图将多维数据分为两类。每个数据点的浮点值为40x6。首先,我已经阅读了我的csv文件。在此文件中,快照编号表示数据点 以下是python中的代码:Python 对多维数据使用knn时出错,python,machine-learning,scikit-learn,classification,knn,Python,Machine Learning,Scikit Learn,Classification,Knn,我是机器学习的初学者,我试图将多维数据分为两类。每个数据点的浮点值为40x6。首先,我已经阅读了我的csv文件。在此文件中,快照编号表示数据点 以下是python中的代码: import pandas as pd 1 import numpy as np 2 import matplotlib.pyplot as plot 3 4 from sklearn.neighbors import KNeighborsClassifier 5 6 # Read csv data
import pandas as pd
1 import numpy as np
2 import matplotlib.pyplot as plot
3
4 from sklearn.neighbors import KNeighborsClassifier
5
6 # Read csv data into pandas data frame
7 data_frame = pd.read_csv('data.csv')
8
9 extract_columns = ['LinearAccX', 'LinearAccY', 'LinearAccZ', 'Roll', 'pitch', 'compass']
10
11 # Number of sample in one shot
12 samples_per_shot = 40
13
14 # Calculate number of shots in dataframe
15 count_of_shots = len(data_frame.index)/samples_per_shot
16
17 # Initialize Empty data frame
18 training_index = range(count_of_shots)
19 training_data_list = []
20
21 # flag for backward compatibility
22 make_old_data_compatible_with_new = 0
23
24 if make_old_data_compatible_with_new:
25 # Convert 40 shot data to 25 shot data
26 # New logic takes 25 samples/shot
27 # old logic takes 40 samples/shot
28 start_shot_sample_index = 9
29 end_shot_sample_index = 34
30 else:
31 # Start index from 1 and continue till lets say 40
32 start_shot_sample_index = 1
33 end_shot_sample_index = samples_per_shot
34
35 # Extract each shot into pandas series
36 for shot in range(count_of_shots):
37 # Extract current shot
38 current_shot_data = data_frame[data_frame['shot_no']==(shot+1)]
39
40 # Select only the following column
41 selected_columns_from_shot = current_shot_data[extract_columns]
42
43 # Select columns from selected rows
44 # Find start and end row indexes
45 current_shot_data_start_index = shot * samples_per_shot + start_shot_sample_index
46 current_shot_data_end_index = shot * samples_per_shot + end_shot_sample_index
47 selected_rows_from_shot = selected_columns_from_shot.ix[current_shot_data_start_index:curren t_shot_data_end_index]
48
49 # Append to list of lists
50 # Convert selected short into multi-dimensional array
51
training_data_list.append([selected_columns_from_shot[extract_columns[index]].values.tolist( ) for index in range(len(extract_columns))])
8
7 # Append each sliced shot into training data
6 training_data = pd.DataFrame(training_data_list, columns=extract_columns)
5 training_features = [1 for i in range(count_of_shots)]
4 knn = KNeighborsClassifier(n_neighbors=3)
3 knn.fit(training_data, training_features)
training_data_list.append([selected_columns_from_shot[extract_columns[index]].values.tolist( ) for index in range(len(extract_columns))])
运行上述代码后,我得到一个错误
ValueError:使用序列设置数组元素
排队
knn.fit(training_data, training_features)
你能确认你的训练数据的形状是[n_样本,n_特征]并且训练特征的长度与训练特征的长度相同吗?你能详细说明数据的形状[n_样本,n_特征]吗。在这种情况下,training_功能和training data的长度为16。请确认training_data不是1d列表。