import numpy as np
sample_array = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,15,17,18]])
print(sample_array)
[[ 1 2 3 4 5 6] [ 7 8 9 10 11 12] [13 14 15 15 17 18]]
# Shape
print(sample_array.shape)
# Size
print(sample_array.size)
# Replacing values
sample_array[2,3] = 16
print(sample_array)
# Means - axes
avg = sample_array.mean()
# avg = np.mean(sample_array)
print(avg)
avg_row = sample_array.mean(axis=0)
print(avg_row)
avg_col = sample_array.mean(axis=1)
print(avg_col)
avg_avg = avg_col.mean()
print(avg_avg)
avg_last_half = sample_array[:,3:].mean(axis=0)
# avg_last_half = np.mean(sample_array[:,3:], axis=0)
print(avg_last_half)
# Reshape
new_shape = sample_array.reshape((2,9))
print(new_shape)
# new_shape2 = sample_array.reshape((2,10))
# Transpose
turned_array = sample_array.transpose()
print(turned_array)
# 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]]])
# print(sample_array_3d)
# print(sample_array_3d.shape)
# # axes = (0,1,2)
# turned_3d = sample_array_3d.transpose((1,2,0))
# print(turned_3d)
# print(turned_3d.shape)
(3, 6) 18 [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12] [13 14 15 16 17 18]] 9.5 [ 7. 8. 9. 10. 11. 12.] [ 3.5 9.5 15.5] 9.5 [10. 11. 12.] [[ 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18]] [[ 1 7 13] [ 2 8 14] [ 3 9 15] [ 4 10 16] [ 5 11 17] [ 6 12 18]]
# import statement(s)
from datetime import datetime
from datetime import timedelta
# from datetime import datetime, timedelta
# Now
now = datetime.now()
print(now)
# Separating values
print(now.hour)
print(now.year)
print(now.month)
print(now.day)
# Creating a datetime object at a specific time
some_time = datetime(2020,5,22)
print(some_time)
# Changing to/from string
charlies_bday = datetime.strftime(some_time,'%a %d %B, %Y')
print(charlies_bday)
c_bday_dt = datetime.strptime(charlies_bday, '%a %d %B, %Y')
print(c_bday_dt)
print(type(c_bday_dt))
# timedelta objects
dt = timedelta(seconds=3600)
print(dt)
print(now - dt)
2020-10-22 23:03:06.896787 23 2020 10 22 2020-05-22 00:00:00 Fri 22 May, 2020 2020-05-22 00:00:00 <class 'datetime.datetime'> 1:00:00 2020-10-22 22:03:06.896787
from datetime import datetime, timedelta
# Use this string for the following activities:
datestring = 'October222020'
# 1) Change datestring into a datetime object
time = datetime.strptime(datestring,'%B%d%Y')
print(time)
# 2) Get the datetime object for 71 days later than datestring
time_71 = time + timedelta(days=71)
print(time_71)
# 3) Create a new string for this future date with the format Year-month-day
# (Use the datetime formatting table above)
datestring_71 = datetime.strftime(time_71,'%Y-%m-%d')
print(datestring_71)
# 4) Create a list of datetimes starting on datestring up to (and including) 71 days in the future with a frequency of 1 day
2020-10-22 00:00:00 2021-01-01 00:00:00 2021-01-01
# 1) Make your birthday (or another day of significance) into a datetime object
# 2) Use today's date and subtract your birthday to find the timedelta object representing how long you’ve been alive.
# 3) Find out approximately how many seconds you have been alive.