2017-01-06 12 views
1

の値としてリストをパンダのデータフレームを作成します。次の形式でパンダのデータフレームを作成する方法行

 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] 

基本的に各列要素としてリストを有しています。私は機械学習を使ってデータを分類しようとしています。各データポイントには40×6の値があります。分類子に入力するのに適した他のフォーマットがありますか?

編集:

0 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:current_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_c olumns))]) 
52 
53 # Append each sliced shot into training data 
54 training_data = pd.DataFrame(training_data_list, columns=extract_columns) 
55 training_features = [1 for i in range(count_of_shots)] 
56 knn = KNeighborsClassifier(n_neighbors=3) 
57 knn.fit(training_data, training_features) 
+0

Seriesを構築しますか?入力形式とは何ですか?あなたの質問にそれらを含めてください。 [mcve] – MYGz

+1

を作成するリストのDataFrameが必要ですか?それはめったに意味がありません。 –

+0

IMUデータを使用してジェスチャを分類しようとしていますが、各ジェスチャにはRoll、Pitch、Yaw、AccelerationX、AccY、AccZの40個の値があります。 40個の値を持つこれらの6つの列はそれぞれ、データポイントを構成します。それを表現するより良い方法はありますか?おかげさまで –

答えて

0

シンプル

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') 
) 

それとも

あなたが何をしようとしなかったMultiIndexunstack

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] 
+0

出来た ! –

+0

私はバイナリ分類を実行しようとしています。上記のデータのラベルリストはちょうど[1,1、... 1]です。上記のデータフレームとトレーニングラベルをKNNクラシファイアに入力すると、「値エラー:配列要素をシーケンスで設定する」というエラーが表示されます。トレーニングデータとトレーニングラベルの両方が同じ行数です。 –

+0

ええ、この形式は非典型です。私はあなたが望むものが分かっていたと信じていましたしかし、私はあなたが必要としているものを、賢明な形式ではわかりません。あなたはそれらの詳細と別の質問を投稿する必要があります。 – piRSquared

0

あなたはこれを試すことができます。

import pandas as pd 

data = [{'A': [1,2,3,4], 'B': [2,3,4,5], 'C': [4,5,5,6], 'D': [6,3,4,5]}, {'A': [2,3,5,6], 'B': [3,4,6,6], 'C': [3,4,5,7], 'D': [2,6,3,4]}, {'A': [8,9,6,7], 'B': [5,7,9,5], 'C': [3,7,9,5], 'D': [5,7,9,8]}] 
df = pd.DataFrame(data) 
print(df) 

# Output 
       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] 
関連する問題