NGSIM数据集特征提取-对数据添加特征,筛选出换道车辆

1.对数据添加特征

对数据添加特征:前后帧localx,localy之差,以及除以0.1,得到车辆的侧向加速度

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import csv
import pandas as pd
f=pd.read_csv("./trajectories-0750.csv")


# In[2]:


local_x=f[["Vehicle_ID","Local_X","Local_Y"]]
local_x


# In[3]:


x=[]
y=[]
x_v=[]
y_v=[]
for i in range(len(local_x)):
    if i==0:
        x.append('none')
        x_v.append('none')
        y.append('none')
        y_v.append('none')
    else:
        if local_x.Vehicle_ID[i]==local_x.Vehicle_ID[i-1]: 
            x.append(local_x.Local_X[i]-local_x.Local_X[i-1])
            x_v.append((local_x.Local_X[i]-local_x.Local_X[i-1])/0.1)
            y.append(local_x.Local_Y[i]-local_x.Local_Y[i-1])
            y_v.append((local_x.Local_Y[i]-local_x.Local_Y[i-1])/0.1)
        else:
            x.append('none')
            x_v.append('none')
            y.append('none')
            y_v.append('none')


# In[4]:


dic={'x':x,'x_v':x_v,'y':y,'y_v':y_v}
df = pd.DataFrame(data=dic)
df


# In[5]:


new_f=pd.concat([f,df],axis=1)
new_f


# In[6]:


new_f=new_f[new_f["x_v"]!='none']
new_f=new_f.rename(columns={'x_v':'x_a'})
new_f


# In[7]:


new_f.to_csv("./算法.csv",index=False)


# In[8]:


train_data=new_f[['Vehicle_ID','Local_X','Local_Y','v_Class','v_Vel','v_Acc','x_a','Lane_ID']]
train_data


# In[ ]:


2.数据筛选-筛选出换道以及直行车辆

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import csv
import pandas as pd
f=pd.read_csv("./算法.csv")
f


# In[2]:


Vehicle_ID=f['Vehicle_ID']
Vehicle_ID_LIST=[]
for i in range(len(f)):
    if Vehicle_ID[i] not in Vehicle_ID_LIST:
        Vehicle_ID_LIST.append(Vehicle_ID[i])
Vehicle_ID_LIST


# In[3]:


len(Vehicle_ID_LIST)


# In[4]:


ID=f[['Vehicle_ID','Local_X','Local_Y','Lane_ID','x','x_a','y','y_v']]
ID


# In[5]:


change_vehicle_id=[]
change_vehicle_row=[]
# change_vehicle_row40=[]
TR_row=[]
TL_row=[]
TR_vehicle_id=[]
TL_vehicle_id=[]
for i in range(len(f)):
    if i==0:
        continue
    else:
        if ID['Vehicle_ID'][i]==ID['Vehicle_ID'][i-1] and ID['Lane_ID'][i]!=ID['Lane_ID'][i-1]:
            if ID['Vehicle_ID'][i] not in change_vehicle_id:
                change_vehicle_id.append(ID['Vehicle_ID'][i])
#             change_vehicle_row40.extend(a for a in range(i-39:i-1))
            change_vehicle_row.append(i-1)
            change_vehicle_row.append(i)
        if ID['Vehicle_ID'][i]==ID['Vehicle_ID'][i-1] and ID['Lane_ID'][i]==ID['Lane_ID'][i-1]+1:
            if ID['Vehicle_ID'][i] not in TR_vehicle_id:
                TR_vehicle_id.append(ID['Vehicle_ID'][i])
            TR_row.append(i-1)
            TR_row.append(i)
        if ID['Vehicle_ID'][i]==ID['Vehicle_ID'][i-1] and ID['Lane_ID'][i]==ID['Lane_ID'][i-1]-1:
            if ID['Vehicle_ID'][i] not in TL_vehicle_id:
                TL_vehicle_id.append(ID['Vehicle_ID'][i])
            TL_row.append(i-1)
            TL_row.append(i)
    
#         change_vehicle_row.append(i-1)
#         change_vehicle_row.append(i)


# In[6]:


change_vehicle_row


# In[7]:


len(change_vehicle_id)


# In[8]:


LK_list=[]
for i in Vehicle_ID_LIST:
    if i not in change_vehicle_id:
        LK_list.append(i)
LK_ID40=f[f.Vehicle_ID.isin(LK_list)]
LK_ID40=LK_ID40[LK_ID40.Lane_ID.isin([1,2,3,4,5])]
LK_ID40['label']=0
LK_ID40=LK_ID40.reset_index(drop=True)


# In[9]:


import random
keep_lane_row=[]
for i in LK_list:
    one_ve_info=LK_ID40[LK_ID40.Vehicle_ID.isin([i])]
    total_frames=len(one_ve_info)
    if total_frames>=40:
        start_frame=random.randint(0,total_frames)-40
        end_frame=start_frame+40
        one_ve_index=one_ve_info.iloc[start_frame:end_frame,:].index
        keep_lane_row.extend(one_ve_index)
print( keep_lane_row)
    


# In[10]:


len(keep_lane_row)/40


# In[11]:


KL_list=list(set(list(LK_ID40.Vehicle_ID)))
lk_40=[]
Vehicle_ID40=[]
for i in range(len(LK_ID40)):
    if i<=40:
        continue
    else:
        if LK_ID40['Vehicle_ID'][i]!=LK_ID40['Vehicle_ID'][i-1] and LK_ID40['Vehicle_ID'][i-1]==LK_ID40['Vehicle_ID'][i-40] :
            lk_40.extend(a for a in range(i-40,i))
lk_40


# In[12]:


len(lk_40)


# In[13]:


KL_train40_csv=LK_ID40.loc[keep_lane_row]
KL_train40_csv.to_csv("./KL_train40.csv",index=False)


# In[14]:


import random
LK_train=random.sample(LK_list,300)
LK_train


# In[15]:


len(LK_train)


# In[16]:


lane_local = pd.DataFrame(columns = ["Vehicle_ID", "Lane_ID","Local_X"]) 
change_lane_id=ID[ID.Vehicle_ID.isin(change_vehicle_id)]
TR_ID=ID[ID.Vehicle_ID.isin(TR_vehicle_id)]
TL_ID=ID[ID.Vehicle_ID.isin(TL_vehicle_id)]
change_lane_id.to_csv("./change_lane_id.csv",index=False)
change_lane_id


# In[17]:


TR_ID


# In[18]:


TL_ID


# In[19]:


change_num=int(len(change_vehicle_row)/2)
change_row=[]
for i in range(change_num):
    change_row.append(change_vehicle_row[2*i])
one_change_local=change_lane_id.loc[change_row]
Change_local=change_lane_id.loc[change_vehicle_row]
TR_local=change_lane_id.loc[TR_row]
TL_local=change_lane_id.loc[TL_row]
one_change_local.to_csv("./one_change_local.csv",index=0)
Change_local.to_csv("./change_local.csv",index=0)
Change_local=Change_local.reset_index(drop=True)


# In[20]:


Change_local


# In[21]:


lane_local12_row=[]
lane_local21_row=[]
lane_local23_row=[]
lane_local32_row=[]
lane_local34_row=[]
lane_local43_row=[]
lane_local45_row=[]
lane_local54_row=[]

for i in range(len(Change_local)):
    if i ==0:
        continue
    else:
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==2 and Change_local.Lane_ID[i-1]==1:
            lane_local12_row.append(i-1)
            lane_local12_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==1 and Change_local.Lane_ID[i-1]==2:
            lane_local21_row.append(i-1)
            lane_local21_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==3 and Change_local.Lane_ID[i-1]==2:
            lane_local23_row.append(i-1)
            lane_local23_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==2 and Change_local.Lane_ID[i-1]==3:
            lane_local32_row.append(i-1)
            lane_local32_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==4 and Change_local.Lane_ID[i-1]==3:
            lane_local34_row.append(i-1)
            lane_local34_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==3 and Change_local.Lane_ID[i-1]==4:
            lane_local43_row.append(i-1)
            lane_local43_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==5 and Change_local.Lane_ID[i-1]==4:
            lane_local45_row.append(i-1)
            lane_local45_row.append(i)
        if Change_local.Vehicle_ID[i]==Change_local.Vehicle_ID[i-1] and  Change_local.Lane_ID[i]==4 and Change_local.Lane_ID[i-1]==5:
            lane_local54_row.append(i-1)
            lane_local54_row.append(i)


# In[22]:


lane_local12_row


# In[ ]:





# In[23]:


change_lane12_local=Change_local.loc[lane_local12_row]
change_lane21_local=Change_local.loc[lane_local21_row]
change_lane23_local=Change_local.loc[lane_local23_row]
change_lane32_local=Change_local.loc[lane_local32_row]
change_lane34_local=Change_local.loc[lane_local34_row]
change_lane43_local=Change_local.loc[lane_local43_row]
change_lane45_local=Change_local.loc[lane_local45_row]
change_lane54_local=Change_local.loc[lane_local54_row]


# In[24]:


change_lane12_local


# In[25]:


Vehicle_ID12=change_lane12_local['Vehicle_ID']
Vehicle_ID12.tolist()
set(Vehicle_ID12)
Vehicle_ID12=set(Vehicle_ID12)


# In[26]:


change_lane21_local


# In[27]:


Vehicle_ID21=change_lane21_local['Vehicle_ID']
Vehicle_ID21.tolist()
set(Vehicle_ID21)
Vehicle_ID21=set(Vehicle_ID21)


# In[28]:


change_lane23_local


# In[29]:


Vehicle_ID23=change_lane23_local['Vehicle_ID']
Vehicle_ID23.tolist()
set(Vehicle_ID23)
Vehicle_ID23=set(Vehicle_ID23)


# In[30]:


change_lane32_local


# In[31]:


Vehicle_ID32=change_lane32_local['Vehicle_ID']
Vehicle_ID32.tolist()
set(Vehicle_ID32)
Vehicle_ID32=set(Vehicle_ID32)


# In[32]:


change_lane34_local


# In[33]:


Vehicle_ID34=change_lane34_local['Vehicle_ID']
Vehicle_ID34.tolist()
set(Vehicle_ID34)
Vehicle_ID34=set(Vehicle_ID34)


# In[34]:


change_lane43_local


# In[35]:


Vehicle_ID43=change_lane43_local['Vehicle_ID']
Vehicle_ID43.tolist()
set(Vehicle_ID43)
Vehicle_ID43=set(Vehicle_ID43)


# In[36]:


change_lane45_local


# In[37]:


Vehicle_ID45=change_lane45_local['Vehicle_ID']
Vehicle_ID45.tolist()
set(Vehicle_ID45)
Vehicle_ID45=set(Vehicle_ID45)


# In[38]:


change_lane54_local


# In[39]:


Vehicle_ID54=change_lane54_local['Vehicle_ID']
Vehicle_ID54.tolist()
Vehicle_ID54=set(Vehicle_ID54)


# In[40]:


change_lane_ID15=Vehicle_ID12|Vehicle_ID21|Vehicle_ID23|Vehicle_ID32|Vehicle_ID34|Vehicle_ID43|Vehicle_ID45|Vehicle_ID54
change_lane_ID15=list(change_lane_ID15)
change_lane_ID15


# In[41]:


len(change_lane_ID15)


# In[42]:


TR_ID15=Vehicle_ID12|Vehicle_ID23|Vehicle_ID34|Vehicle_ID45
TL_ID15=Vehicle_ID21|Vehicle_ID32|Vehicle_ID43|Vehicle_ID54
TR_ID15=list(TR_ID15)
TL_ID15=list(TL_ID15)
len(TL_ID15)


# In[43]:


len(TL_ID15)


# In[44]:


CL_train=change_lane_ID15
CL_train


# In[45]:


TR_ID15_csv=f[f.Vehicle_ID.isin(TR_ID15)]
TR_ID15_csv['label']=2
TL_ID15_csv=f[f.Vehicle_ID.isin(TL_ID15)]
TL_ID15_csv['label']=1
TR_ID15_csv=TR_ID15_csv.reset_index(drop=True)
TL_ID15_csv=TL_ID15_csv.reset_index(drop=True)
TR_ID15_csv


# In[46]:


TR_row=[]
TL_row=[]
TR_local_row=lane_local12_row+lane_local23_row+lane_local34_row+lane_local45_row
TL_local_row=lane_local21_row+lane_local32_row+lane_local43_row+lane_local54_row
TR_num=int(len(TR_local_row)/2)
for i in range(TR_num):
    TR_row.append(TR_local_row[2*i])
TL_num=int(len(TL_local_row)/2)
for i in range(TL_num):
    TL_row.append(TL_local_row[2*i])
TL_row


# In[47]:


CL_train_csv=f[f.Vehicle_ID.isin(CL_train)]
# CL_train_csv['label']=1
CL_train_csv=CL_train_csv.reset_index(drop=True)
CL_train_csv


# In[48]:


import numpy as np
b=[]
change_vehicle_row40=[]
Vehicle_ID40=[]
for i in range(len(CL_train_csv)):
    if i<=40:
        continue
    else:
        if CL_train_csv['Vehicle_ID'][i]==CL_train_csv['Vehicle_ID'][i-1] ==CL_train_csv['Vehicle_ID'][i-40] and CL_train_csv['Lane_ID'][i]!=CL_train_csv['Lane_ID'][i-1]:
            x=list(CL_train_csv['Lane_ID'][i-40:i])
            lane_num=len(set(x))
            if lane_num==1:
                b.extend(a for a in range(i-40,i+1))
for i in b:
    if i>=0:
        change_vehicle_row40.append(i)
change_vehicle_row40


# In[49]:


len(change_vehicle_row40)


# In[50]:


CL_train40_csv=CL_train_csv.loc[change_vehicle_row40]
CL_train40_csv.to_csv("./CL_train41.csv",index=False)
CL_train40_csv


# In[51]:


train_ID=LK_train+CL_train
train_ID


# In[52]:


train_csv=f[f.Vehicle_ID.isin(train_ID)]
train_csv=train_csv.reset_index(drop=True)
train_csv['label']=1
train_csv


# In[53]:


TR_local


# In[54]:


TL_local


# In[55]:


change_lane12_local.describe()


# In[56]:


change_lane21_local.describe()


# In[57]:


change_lane23_local.describe()


# In[58]:


change_lane32_local.describe()


# In[59]:


change_lane34_local.describe()


# In[60]:


change_lane43_local.describe()


# In[61]:


change_lane45_local.describe()


# In[62]:


change_lane54_local.describe()


# In[63]:


TL_local.describe()


# In[64]:


TR_local.describe()


# In[ ]:





# In[ ]:





你可能感兴趣的:(NGSIM,python,ngsim)