{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "torch.set_printoptions(edgeitems=2, threshold=50, linewidth=75)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.0000e+00, 1.0000e+00, ..., 1.3000e+01, 1.6000e+01],\n", " [2.0000e+00, 1.0000e+00, ..., 3.2000e+01, 4.0000e+01],\n", " ...,\n", " [1.7378e+04, 3.1000e+01, ..., 4.8000e+01, 6.1000e+01],\n", " [1.7379e+04, 3.1000e+01, ..., 3.7000e+01, 4.9000e+01]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bikes_numpy = np.loadtxt(\n", " \"../data/p1ch4/bike-sharing-dataset/hour-fixed.csv\", \n", " dtype=np.float32, \n", " delimiter=\",\", \n", " skiprows=1, \n", " converters={1: lambda x: float(x[8:10])}) # <1>\n", "bikes = torch.from_numpy(bikes_numpy)\n", "bikes" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "(torch.Size([17520, 17]), (17, 1))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bikes.shape, bikes.stride()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(torch.Size([730, 24, 17]), (408, 17, 1))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "daily_bikes = bikes.view(-1, 24, bikes.shape[1])\n", "daily_bikes.shape, daily_bikes.stride()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(torch.Size([730, 17, 24]), (408, 1, 17))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "daily_bikes = daily_bikes.transpose(1, 2)\n", "daily_bikes.shape, daily_bikes.stride()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2,\n", " 2, 2])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_day = bikes[:24].long()\n", "weather_onehot = torch.zeros(first_day.shape[0], 4)\n", "first_day[:,9]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1., 0., 0., 0.],\n", " [1., 0., 0., 0.],\n", " ...,\n", " [0., 1., 0., 0.],\n", " [0., 1., 0., 0.]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weather_onehot.scatter_(\n", " dim=1, \n", " index=first_day[:,9].unsqueeze(1).long() - 1, # <1>\n", " value=1.0)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1.0000, 1.0000, 1.0000, 0.0000, 1.0000, 0.0000, 0.0000,\n", " 6.0000, 0.0000, 1.0000, 0.2400, 0.2879, 0.8100, 0.0000,\n", " 3.0000, 13.0000, 16.0000, 1.0000, 0.0000, 0.0000, 0.0000]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.cat((bikes[:24], weather_onehot), 1)[:1]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([730, 4, 24])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "daily_weather_onehot = torch.zeros(daily_bikes.shape[0], 4,\n", " daily_bikes.shape[2])\n", "daily_weather_onehot.shape" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([730, 4, 24])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "daily_weather_onehot.scatter_(\n", " 1, daily_bikes[:,9,:].long().unsqueeze(1) - 1, 1.0)\n", "daily_weather_onehot.shape" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "daily_bikes = torch.cat((daily_bikes, daily_weather_onehot), dim=1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "daily_bikes[:, 9, :] = (daily_bikes[:, 9, :] - 1.0) / 3.0" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "temp = daily_bikes[:, 10, :]\n", "temp_min = torch.min(temp)\n", "temp_max = torch.max(temp)\n", "daily_bikes[:, 10, :] = ((daily_bikes[:, 10, :] - temp_min)\n", " / (temp_max - temp_min))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "temp = daily_bikes[:, 10, :]\n", "daily_bikes[:, 10, :] = ((daily_bikes[:, 10, :] - torch.mean(temp))\n", " / torch.std(temp))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }