{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"2020-10-22 - class #6 - activities","provenance":[{"file_id":"1_Ilr68Clb29cPfSbDE5wD3Z8z5pdYFI2","timestamp":1603399684048}],"collapsed_sections":["hiHep6EutK8S","p0RInmIJtkRg","aOBW74EitPe-","7c4Q8lUb_Lz4"],"authorship_tag":"ABX9TyPRWMYaNLxz+k2ZBVLcNAK9"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"0_zaBrGRtUgw"},"source":["# Class \\#6 notebook and activities"]},{"cell_type":"markdown","metadata":{"id":"hiHep6EutK8S"},"source":["## Review: multidimensional arrays"]},{"cell_type":"code","metadata":{"id":"nohB9CrAszIQ","executionInfo":{"status":"ok","timestamp":1603405121810,"user_tz":420,"elapsed":646,"user":{"displayName":"Katy Christensen","photoUrl":"","userId":"13309436073132227481"}},"outputId":"72d7f3b2-03ee-4eaa-f809-2200b587c8d0","colab":{"base_uri":"https://localhost:8080/","height":68}},"source":["import numpy as np\n","\n","sample_array = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,15,17,18]])\n","print(sample_array)\n"],"execution_count":1,"outputs":[{"output_type":"stream","text":["[[ 1 2 3 4 5 6]\n"," [ 7 8 9 10 11 12]\n"," [13 14 15 15 17 18]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"VOx3Ik6bs5Co","executionInfo":{"status":"ok","timestamp":1603405172938,"user_tz":420,"elapsed":406,"user":{"displayName":"Katy Christensen","photoUrl":"","userId":"13309436073132227481"}},"outputId":"3e41d121-0ac9-4416-8d54-3b562e441c60","colab":{"base_uri":"https://localhost:8080/","height":323}},"source":["# Shape\n","print(sample_array.shape)\n","\n","# Size\n","print(sample_array.size)\n","\n","# Replacing values\n","sample_array[2,3] = 16\n","print(sample_array)\n","\n","# Means - axes\n","avg = sample_array.mean()\n","# avg = np.mean(sample_array)\n","print(avg)\n","\n","avg_row = sample_array.mean(axis=0)\n","print(avg_row)\n","\n","avg_col = sample_array.mean(axis=1)\n","print(avg_col)\n","\n","avg_avg = avg_col.mean()\n","print(avg_avg)\n","\n","avg_last_half = sample_array[:,3:].mean(axis=0)\n","# avg_last_half = np.mean(sample_array[:,3:], axis=0)\n","print(avg_last_half)\n","\n","# Reshape\n","new_shape = sample_array.reshape((2,9))\n","print(new_shape)\n","\n","# new_shape2 = sample_array.reshape((2,10))\n","\n","# Transpose\n","turned_array = sample_array.transpose()\n","print(turned_array)\n","\n","\n","# sample_array_3d = np.array([[[1,2,3,4,5,6],[7,8,9,10,11,12]],[[13,14,15,16,17,18],[19,20,21,22,23,24]]])\n","# print(sample_array_3d)\n","# print(sample_array_3d.shape)\n","\n","# # axes = (0,1,2)\n","\n","# turned_3d = sample_array_3d.transpose((1,2,0))\n","# print(turned_3d)\n","# print(turned_3d.shape)"],"execution_count":3,"outputs":[{"output_type":"stream","text":["(3, 6)\n","18\n","[[ 1 2 3 4 5 6]\n"," [ 7 8 9 10 11 12]\n"," [13 14 15 16 17 18]]\n","9.5\n","[ 7. 8. 9. 10. 11. 12.]\n","[ 3.5 9.5 15.5]\n","9.5\n","[10. 11. 12.]\n","[[ 1 2 3 4 5 6 7 8 9]\n"," [10 11 12 13 14 15 16 17 18]]\n","[[ 1 7 13]\n"," [ 2 8 14]\n"," [ 3 9 15]\n"," [ 4 10 16]\n"," [ 5 11 17]\n"," [ 6 12 18]]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"p0RInmIJtkRg"},"source":["## Review: datetime and timedelta"]},{"cell_type":"code","metadata":{"id":"lw40vmW_tpGB","executionInfo":{"status":"ok","timestamp":1603407787114,"user_tz":420,"elapsed":297,"user":{"displayName":"Katy Christensen","photoUrl":"","userId":"13309436073132227481"}},"outputId":"f97e95f1-47db-4a0c-c10f-25f656ab94fd","colab":{"base_uri":"https://localhost:8080/","height":204}},"source":["# import statement(s)\n","from datetime import datetime\n","from datetime import timedelta\n","\n","# from datetime import datetime, timedelta\n","\n","# Now\n","now = datetime.now()\n","print(now)\n","\n","# Separating values\n","print(now.hour)\n","print(now.year)\n","print(now.month)\n","print(now.day)\n","\n","# Creating a datetime object at a specific time\n","some_time = datetime(2020,5,22)\n","print(some_time)\n","\n","# Changing to/from string\n","charlies_bday = datetime.strftime(some_time,'%a %d %B, %Y')\n","print(charlies_bday)\n","\n","c_bday_dt = datetime.strptime(charlies_bday, '%a %d %B, %Y')\n","print(c_bday_dt)\n","print(type(c_bday_dt))\n","\n","# timedelta objects\n","dt = timedelta(seconds=3600)\n","print(dt)\n","\n","print(now - dt)"],"execution_count":27,"outputs":[{"output_type":"stream","text":["2020-10-22 23:03:06.896787\n","23\n","2020\n","10\n","22\n","2020-05-22 00:00:00\n","Fri 22 May, 2020\n","2020-05-22 00:00:00\n","\n","1:00:00\n","2020-10-22 22:03:06.896787\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"tQdqc94lfHHg","executionInfo":{"status":"ok","timestamp":1603406692388,"user_tz":420,"elapsed":478,"user":{"displayName":"Katy Christensen","photoUrl":"","userId":"13309436073132227481"}}},"source":[""],"execution_count":13,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"aOBW74EitPe-"},"source":["## Activity: datetime"]},{"cell_type":"markdown","metadata":{"id":"jhk6Vl7h394y"},"source":["![picture](https://drive.google.com/uc?id=1PZmZNovVe76ADiJnAHt7GM6EtHEkRyZl)"]},{"cell_type":"code","metadata":{"id":"gzU45e9qsxWP","executionInfo":{"status":"ok","timestamp":1603408656284,"user_tz":420,"elapsed":403,"user":{"displayName":"Katy Christensen","photoUrl":"","userId":"13309436073132227481"}},"outputId":"034d8fb3-2dd2-4d9f-b3df-9a3c304790b7","colab":{"base_uri":"https://localhost:8080/","height":68}},"source":["from datetime import datetime, timedelta\n","\n","# Use this string for the following activities:\n","datestring = 'October222020'\n","\n","# 1) Change datestring into a datetime object\n","time = datetime.strptime(datestring,'%B%d%Y')\n","print(time)\n","\n","# 2) Get the datetime object for 71 days later than datestring\n","time_71 = time + timedelta(days=71)\n","print(time_71)\n","\n","# 3) Create a new string for this future date with the format Year-month-day\n","# (Use the datetime formatting table above)\n","datestring_71 = datetime.strftime(time_71,'%Y-%m-%d')\n","print(datestring_71)\n","\n","# 4) Create a list of datetimes starting on datestring up to (and including) 71 days in the future with a frequency of 1 day\n"],"execution_count":29,"outputs":[{"output_type":"stream","text":["2020-10-22 00:00:00\n","2021-01-01 00:00:00\n","2021-01-01\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"7c4Q8lUb_Lz4"},"source":["## Activity: datetime 2 (if there is time)"]},{"cell_type":"code","metadata":{"id":"vtYCNJGl_Ppc"},"source":["# 1) Make your birthday (or another day of significance) into a datetime object\n","\n","# 2) Use today's date and subtract your birthday to find the timedelta object representing how long you’ve been alive.\n","\n","# 3) Find out approximately how many seconds you have been alive."],"execution_count":null,"outputs":[]}]}