san*_*oni 6 python numpy pandas
如何以以下格式创建熊猫数据框:
A B C D
0 [1,2,3,4] [2,3,4,5] [4,5,5,6] [6,3,4,5]
1 [2,3,5,6] [3,4,6,6] [3,4,5,7] [2,6,3,4]
2 [8,9,6,7] [5,7,9,5] [3,7,9,5] [5,7,9,8]
Run Code Online (Sandbox Code Playgroud)
基本上每一行都有一个列表作为元素。我正在尝试使用机器学习对数据进行分类。每个数据点有 40 x 6 个值。是否有任何其他格式适合输入分类器。
编辑:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plot
from sklearn.neighbors import KNeighborsClassifier
# Read csv data into pandas data frame
data_frame = pd.read_csv('data.csv')
extract_columns = ['LinearAccX', 'LinearAccY', 'LinearAccZ', 'Roll', 'pitch', 'compass']
# Number of sample in one shot
samples_per_shot = 40
# Calculate number of shots in dataframe
count_of_shots = len(data_frame.index)/samples_per_shot
# Initialize Empty data frame
training_index = range(count_of_shots)
training_data_list = []
# flag for backward compatibility
make_old_data_compatible_with_new = 0
if make_old_data_compatible_with_new:
# Convert 40 shot data to 25 shot data
# New logic takes 25 samples/shot
# old logic takes 40 samples/shot
start_shot_sample_index = 9
end_shot_sample_index = 34
else:
# Start index from 1 and continue till lets say 40
start_shot_sample_index = 1
end_shot_sample_index = samples_per_shot
# Extract each shot into pandas series
for shot in range(count_of_shots):
# Extract current shot
current_shot_data = data_frame[data_frame['shot_no']==(shot+1)]
# Select only the following column
selected_columns_from_shot = current_shot_data[extract_columns]
# Select columns from selected rows
# Find start and end row indexes
current_shot_data_start_index = shot * samples_per_shot + start_shot_sample_index
current_shot_data_end_index = shot * samples_per_shot + end_shot_sample_index
selected_rows_from_shot = selected_columns_from_shot.ix[current_shot_data_start_index:current_shot_data_end_index]
# Append to list of lists
# Convert selected short into multi-dimensional array
training_data_list.append([selected_columns_from_shot[extract_columns[index]].values.tolist() for index in range(len(extract_c olumns))])
# Append each sliced shot into training data
training_data = pd.DataFrame(training_data_list, columns=extract_columns)
training_features = [1 for i in range(count_of_shots)]
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(training_data, training_features)
Run Code Online (Sandbox Code Playgroud)
简单的
pd.DataFrame(
[[[1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 5, 6], [6, 3, 4, 5]],
[[2, 3, 5, 6], [3, 4, 6, 6], [3, 4, 5, 7], [2, 6, 3, 4]],
[[8, 9, 6, 7], [5, 7, 9, 5], [3, 7, 9, 5], [5, 7, 9, 8]]],
columns=list('ABCD')
)
Run Code Online (Sandbox Code Playgroud)
或者
Series用一个MultiIndex和建立一个unstack
lst = [
[1, 2, 3, 4],
[2, 3, 4, 5],
[4, 5, 5, 6],
[6, 3, 4, 5],
[2, 3, 5, 6],
[3, 4, 6, 6],
[3, 4, 5, 7],
[2, 6, 3, 4],
[8, 9, 6, 7],
[5, 7, 9, 5],
[3, 7, 9, 5],
[5, 7, 9, 8]]
pd.Series(lst, pd.MultiIndex.from_product([[0, 1, 2], list('ABCD')])).unstack()
A B C D
0 [1, 2, 3, 4] [2, 3, 4, 5] [4, 5, 5, 6] [6, 3, 4, 5]
1 [2, 3, 5, 6] [3, 4, 6, 6] [3, 4, 5, 7] [2, 6, 3, 4]
2 [8, 9, 6, 7] [5, 7, 9, 5] [3, 7, 9, 5] [5, 7, 9, 8]
Run Code Online (Sandbox Code Playgroud)