assignment.rar
Untitled69.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"Unnamed: 0 | \n",
"id | \n",
"x | \n",
"y | \n",
"frame | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0 | \n",
"1 | \n",
"362.26 | \n",
"21.68 | \n",
"1 | \n",
"
\n",
"\n",
"1 | \n",
"1 | \n",
"1 | \n",
"363.73 | \n",
"21.68 | \n",
"2 | \n",
"
\n",
"\n",
"2 | \n",
"2 | \n",
"1 | \n",
"365.27 | \n",
"21.68 | \n",
"3 | \n",
"
\n",
"\n",
"3 | \n",
"3 | \n",
"1 | \n",
"366.83 | \n",
"21.68 | \n",
"4 | \n",
"
\n",
"\n",
"4 | \n",
"4 | \n",
"1 | \n",
"368.42 | \n",
"21.68 | \n",
"5 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 id x y frame\n",
"0 0 1 362.26 21.68 1\n",
"1 1 1 363.73 21.68 2\n",
"2 2 1 365.27 21.68 3\n",
"3 3 1 366.83 21.68 4\n",
"4 4 1 368.42 21.68 5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('01tracks.csv')\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df = df.interpolate(method='linear', limit_direction='forward', axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv('result.csv') "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"mylist = [16.43, 21.00]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import statistics\n",
"number = statistics.median(mylist)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"df['direction'] = [1 if x <= number else 0 for x in df['y']] "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"Unnamed: 0 | \n",
"id | \n",
"x | \n",
"y | \n",
"frame | \n",
"direction | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0 | \n",
"1 | \n",
"362.26 | \n",
"21.68 | \n",
"1 | \n",
"0 | \n",
"
\n",
"\n",
"1 | \n",
"1 | \n",
"1 | \n",
"363.73 | \n",
"21.68 | \n",
"2 | \n",
"0 | \n",
"
\n",
"\n",
"2 | \n",
"2 | \n",
"1 | \n",
"365.27 | \n",
"21.68 | \n",
"3 | \n",
"0 | \n",
"
\n",
"\n",
"3 | \n",
"3 | \n",
"1 | \n",
"366.83 | \n",
"21.68 | \n",
"4 | \n",
"0 | \n",
"
\n",
"\n",
"4 | \n",
"4 | \n",
"1 | \n",
"368.42 | \n",
"21.68 | \n",
"5 | \n",
"0 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 id x y frame direction\n",
"0 0 1 362.26 21.68 1 0\n",
"1 1 1 363.73 21.68 2 0\n",
"2 2 1 365.27 21.68 3 0\n",
"3 3 1 366.83 21.68 4 0\n",
"4 4 1 368.42 21.68 5 0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"l1 = [0]\n",
"l2 = [0]\n",
"for i in range (1, len(df)):\n",
" l1.append((df['x'][i]-df['x'][i-1])/0.04)\n",
" l2.append((df['y'][i]-df['y'][i-1])/0.04)\n",
"df['x_velocity'] = l1\n",
"df['y_velocity'] = l2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"Unnamed: 0 | \n",
"id | \n",
"x | \n",
"y | \n",
"frame | \n",
"direction | \n",
"x_velocity | \n",
"y_velocity | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0 | \n",
"1 | \n",
"362.26 | \n",
"21.68 | \n",
"1 | \n",
"0 | \n",
"0.00 | \n",
"0.0 | \n",
"
\n",
"\n",
"1 | \n",
"1 | \n",
"1 | \n",
"363.73 | \n",
"21.68 | \n",
"2 | \n",
"0 | \n",
"36.75 | \n",
"0.0 | \n",
"
\n",
"\n",
"2 | \n",
"2 | \n",
"1 | \n",
"365.27 | \n",
"21.68 | \n",
"3 | \n",
"0 | \n",
"38.50 | \n",
"0.0 | \n",
"
\n",
"\n",
"3 | \n",
"3 | \n",
"1 | \n",
"366.83 | \n",
"21.68 | \n",
"4 | \n",
"0 | \n",
"39.00 | \n",
"0.0 | \n",
"
\n",
"\n",
"4 | \n",
"4 | \n",
"1 | \n",
"368.42 | \n",
"21.68 | \n",
"5 | \n",
"0 | \n",
"39.75 | \n",
"0.0 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 id x y frame direction x_velocity y_velocity\n",
"0 0 1 362.26 21.68 1 0 0.00 0.0\n",
"1 1 1 363.73 21.68 2 0 36.75 0.0\n",
"2 2 1 365.27 21.68 3 0 38.50 0.0\n",
"3 3 1 366.83 21.68 4 0 39.00 0.0\n",
"4 4 1 368.42 21.68 5 0 39.75 0.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"group = df.groupby('frame')\n",
"df2 = group.apply(lambda x: x['id'].unique())"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"frame\n",
"1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n",
"2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"3 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"4 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"5 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"6 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"7 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"8 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"9 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
"dtype: object"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2[:10]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"df['neighbor_50']= np.zeros(len(df))\n",
"df['neighbor_100'] = np.zeros(len(df))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"for i in range(0, len(df2)):\n",
" for j in range(0, len(df2[i])):\n",
" if df.loc[(df['id']==df2[i][j]) & (df['direction']==0)] :\n",
" \n",
" df['neighbor_50'][i] = df['neighbor_50'][i] + 1\n",
" df[['x','y']].loc[df['id']==df2[i][j]]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"Unnamed: 0 | \n",
"id | \n",
"width | \n",
"height | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0 | \n",
"1 | \n",
"4.85 | \n",
"2.12 | \n",
"
\n",
"\n",
"1 | \n",
"1 | \n",
"2 | \n",
"4.24 | \n",
"1.92 | \n",
"
\n",
"\n",
"2 | \n",
"2 | \n",
"3 | \n",
"3.94 | \n",
"1.92 | \n",
"
\n",
"\n",
"3 | \n",
"3 | \n",
"4 | \n",
"5.05 | \n",
"2.22 | \n",
"
\n",
"\n",
"4 | \n",
"4 | \n",
"5 | \n",
"4.24 | \n",
"1.82 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 id width height\n",
"0 0 1 4.85 2.12\n",
"1 1 2 4.24 1.92\n",
"2 2 3 3.94 1.92\n",
"3 3 4 5.05 2.22\n",
"4 4 5 4.24 1.82"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3 = pd.read_csv('01tracksmeta.csv')\n",
"df3.head()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"df = pd.merge(df, df3[['width','height','id']], on='id')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"Unnamed: 0 | \n",
"id | \n",
"x | \n",
"y | \n",
"frame | \n",
"direction | \n",
"x_velocity | \n",
"y_velocity | \n",
"neighbor_50 | \n",
"neighbor_100 | \n",
"width | \n",
"height | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0 | \n",
"1 | \n",
"362.26 | \n",
"21.68 | \n",
"1 | \n",
"0 | \n",
"0.00 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"
\n",
"\n",
"1 | \n",
"1 | \n",
"1 | \n",
"363.73 | \n",
"21.68 | \n",
"2 | \n",
"0 | \n",
"36.75 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"
\n",
"\n",
"2 | \n",
"2 | \n",
"1 | \n",
"365.27 | \n",
"21.68 | \n",
"3 | \n",
"0 | \n",
"38.50 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"
\n",
"\n",
"3 | \n",
"3 | \n",
"1 | \n",
"366.83 | \n",
"21.68 | \n",
"4 | \n",
"0 | \n",
"39.00 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"
\n",
"\n",
"4 | \n",
"4 | \n",
"1 | \n",
"368.42 | \n",
"21.68 | \n",
"5 | \n",
"0 | \n",
"39.75 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 id x y frame direction x_velocity y_velocity \\\n",
"0 0 1 362.26 21.68 1 0 0.00 0.0 \n",
"1 1 1 363.73 21.68 2 0 36.75 0.0 \n",
"2 2 1 365.27 21.68 3 0 38.50 0.0 \n",
"3 3 1 366.83 21.68 4 0 39.00 0.0 \n",
"4 4 1 368.42 21.68 5 0 39.75 0.0 \n",
"\n",
" neighbor_50 neighbor_100 width height \n",
"0 0.0 0.0 4.85 2.12 \n",
"1 0.0 0.0 4.85 2.12 \n",
"2 0.0 0.0 4.85 2.12 \n",
"3 0.0 0.0 4.85 2.12 \n",
"4 0.0 0.0 4.85 2.12 "
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"lol = []"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"df.loc[(df['y'] >= 8.51) & (df['y'] < 12.59), 'lane'] = 0\n",
"df.loc[(df['y'] >= 12.59) & (df['y'] < 16.43), 'lane'] = 1\n",
"df.loc[(df['y'] >= 21.00) & (df['y'] < 24.96), 'lane'] = 2\n",
"df.loc[(df['y'] >= 24.96) & (df['y'] <= 28.80), 'lane'] = 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"print(((df['lane'][df['id'][] == 30]).diff() != 0).sum(axis=0))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"x = 0\n",
"lol.append(x)\n",
"for i in range(1, len(df)):\n",
" x = x + ((df['lane'][i]- df['lane'][i-1] != 0).sum(axis=0))\n",
" lol.append(x)\n",
" if (((df['id'][i]- df['id'][i-1] != 0).sum(axis=0) ) == 1):\n",
" x = 0"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"348750"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(lol)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"df['lane_changes'] = lol"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"df4['id'] = list(range(1, (len(uq_id)+1)))\n",
"df = pd.merge(df, df4, on='id')"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"Unnamed: 0 | \n",
"id | \n",
"x | \n",
"y | \n",
"frame | \n",
"direction | \n",
"x_velocity | \n",
"y_velocity | \n",
"neighbor_50 | \n",
"neighbor_100 | \n",
"width | \n",
"height | \n",
"lane_changes | \n",
"vehicle | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0 | \n",
"1 | \n",
"362.26 | \n",
"21.68 | \n",
"1 | \n",
"0 | \n",
"0.00 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"0 | \n",
"Van | \n",
"
\n",
"\n",
"1 | \n",
"1 | \n",
"1 | \n",
"363.73 | \n",
"21.68 | \n",
"2 | \n",
"0 | \n",
"36.75 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"0 | \n",
"Van | \n",
"
\n",
"\n",
"2 | \n",
"2 | \n",
"1 | \n",
"365.27 | \n",
"21.68 | \n",
"3 | \n",
"0 | \n",
"38.50 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"0 | \n",
"Van | \n",
"
\n",
"\n",
"3 | \n",
"3 | \n",
"1 | \n",
"366.83 | \n",
"21.68 | \n",
"4 | \n",
"0 | \n",
"39.00 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"0 | \n",
"Van | \n",
"
\n",
"\n",
"4 | \n",
"4 | \n",
"1 | \n",
"368.42 | \n",
"21.68 | \n",
"5 | \n",
"0 | \n",
"39.75 | \n",
"0.0 | \n",
"0.0 | \n",
"0.0 | \n",
"4.85 | \n",
"2.12 | \n",
"0 | \n",
"Van | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 id x y frame direction x_velocity y_velocity \\\n",
"0 0 1 362.26 21.68 1 0 0.00 0.0 \n",
"1 1 1 363.73 21.68 2 0 36.75 0.0 \n",
"2 2 1 365.27 21.68 3 0 38.50 0.0 \n",
"3 3 1 366.83 21.68 4 0 39.00 0.0 \n",
"4 4 1 368.42 21.68 5 0 39.75 0.0 \n",
"\n",
" neighbor_50 neighbor_100 width height lane_changes vehicle \n",
"0 0.0 0.0 4.85 2.12 0 Van \n",
"1 0.0 0.0 4.85 2.12 0 Van \n",
"2 0.0 0.0 4.85 2.12 0 Van \n",
"3 0.0 0.0 4.85 2.12 0 Van \n",
"4 0.0 0.0 4.85 2.12 0 Van "
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = df.copy()\n",
"data = data.drop(['lane'], axis=1)\n",
"data.head()"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"data.to_csv('result.csv')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"df.loc[(df['width'] <= 4.7) , 'vehicle'] = 'Car' \n",
"df.loc[(df['width'] > 4.7) & (df['width'] <= 6.7), 'vehicle'] = 'Van' \n",
"df.loc[(df['width'] > 6.7) , 'vehicle'] = 'Truck' "
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"from collections import * \n",
"import matplotlib.pyplot as plt\n",
"col_count = Counter(df['vehicle']) \n",
"col_count.keys\n",
"\n",
"class OrderedCounter(Counter, OrderedDict):\n",
" pass\n",
"\n",
"counterlist = OrderedCounter(col_count)\n",
"\n",
"values = counterlist.values()\n",
"keys = counterlist.keys()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"image/png":...