I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Note: in the first version of the book, the loss function was simply the squared error between the target Q-Values (y) and the estimated Q-Values (q_value). However, because the experiences are very noisy, it is better to use a quadratic loss only for small errors (below 1.0) and a linear loss (twice the absolute error) for larger errors, which is what the code above computes. This way large errors don't push the model parameters around as much. Note that we also tweaked some hyperparameters (using a smaller learning rate, and using Nesterov Accelerated Gradients rather than Adam optimization, since adaptive gradient algorithms may sometimes be bad, according to this paper). We also tweaked a few other hyperparameters below (a larger replay memory, longer decay for the $\epsilon$-greedy policy, larger discount rate, less frequent copies of the online DQN to the target DQN, etc.).
We use this ReplayMemory class instead of a deque because it is much faster for random access (thanks to @NileshPS who contributed it). Moreover, we default to sampling with replacement, which is much faster than sampling without replacement for large replay memories.
Can you write Python code for it?

class ReplayMemory:
    def __init__(self, maxlen):
        self.maxlen = maxlen
        self.buf = np.empty(shape=maxlen, dtype=np.object)
        self.index = 0
        self.length = 0

    def append(self, data):
        self.buf[self.index] = data
        self.length = min(self.length + 1, self.maxlen)
        self.index = (self.index + 1) % self.maxlen

    def sample(self, batch_size, with_replacement=True):
        if with_replacement:
            indices = np.random.randint(self.length, size=batch_size) # faster
        else:
            indices = np.random.permutation(self.length)[:batch_size]
        return self.buf[indices]

replay_memory_size = 500000
replay_memory = ReplayMemory(replay_memory_size)

def sample_memories(batch_size):
    cols = [[], [], [], [], []] # state, action, reward, next_state, continue
    for memory in replay_memory.sample(batch_size):
        for col, value in zip(cols, memory):
            col.append(value)
    cols = [np.array(col) for col in cols]
    return cols[0], cols[1], cols[2].reshape(-1, 1), cols[3], cols[4].reshape(-1, 1)

eps_min = 0.1
eps_max = 1.0
eps_decay_steps = 2000000

def epsilon_greedy(q_values, step):
    epsilon = max(eps_min, eps_max - (eps_max-eps_min) * step/eps_decay_steps)
    if np.random.rand() < epsilon:
        return np.random.randint(n_outputs) # random action
    else:
        return np.argmax(q_values) # optimal action

n_steps = 4000000  # total number of training steps
training_start = 10000  # start training after 10,000 game iterations
training_interval = 4  # run a training step every 4 game iterations
save_steps = 1000  # save the model every 1,000 training steps
copy_steps = 10000  # copy online DQN to target DQN every 10,000 training steps
discount_rate = 0.99
skip_start = 90  # Skip the start of every game (it's just waiting time).
batch_size = 50
iteration = 0  # game iterations
checkpoint_path = "./my_dqn.ckpt"
done = True # env needs to be reset


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Note: there may be minor differences between the output of this notebook and the examples shown in the book. You can safely ignore these differences. They are mainly due to the fact that most of the environments provided by OpenAI gym have some randomness.
Introduction to OpenAI gym
In this notebook we will be using OpenAI gym, a great toolkit for developing and comparing Reinforcement Learning algorithms. It provides many environments for your learning agents to interact with. Let's start by importing gym:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Notice that the game is over when the pole tilts too much, not when it actually falls. Now let's reset the environment and push the cart to right instead:
Can you write Python code for it?

obs = env.reset()
while True:
    obs, reward, done, info = env.step(1)
    if done:
        break

plot_cart_pole(env, obs)


obs = env.reset()
while True:
    obs, reward, done, info = env.step(1)
    if done:
        break

plot_environment(env)
plt.show()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Now let's look at how well this randomly initialized policy network performed:
Can you write Python code for it?


video = plot_animation(frames)
plt.show()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Now let's look at the action space:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Now show the animation (it's a bit jittery within Jupyter):
Can you write Python code for it?

def update_scene(num, frames, patch):
    patch.set_data(frames[num])
    return patch,

def plot_animation(frames, repeat=False, interval=40):
    plt.close()  # or else nbagg sometimes plots in the previous cell
    fig = plt.figure()
    patch = plt.imshow(frames[0])
    plt.axis('off')
    return animation.FuncAnimation(fig, update_scene, fargs=(frames, patch), frames=len(frames), repeat=repeat, interval=interval)

video = plot_animation(frames)
plt.show()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Now show the animation:
Can you write Python code for it?

def update_scene(num, frames, patch):
    patch.set_data(frames[num])
    return patch,

def plot_animation(frames, repeat=False, interval=40):
    fig = plt.figure()
    patch = plt.imshow(frames[0])
    plt.axis('off')
    anim = animation.FuncAnimation(
        fig, update_scene, fargs=(frames, patch),
        frames=len(frames), repeat=repeat, interval=interval)
    plt.close()
    return anim

plot_animation(frames)


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Now we are going to build the DQN. Like the DQN for Pac-Man, this model will train 3 convolutional layers, then a hidden fully connected layer, then finally a fully connected layer with 6 neurons, one representing each possible output.
Can you write Python code for it?

reset_graph()

input_width = 80
input_height = 80
input_channels = 1

conv_n_maps = [32, 64, 64]
conv_kernel_sizes = [9, 5, 3]
conv_kernel_strides = [4, 2, 1]
conv_paddings = ['VALID'] * 3
conv_activation = [tf.nn.relu] * 3

n_hidden_in = 5 * 5 * 64
n_hidden = 512
hidden_activation = tf.nn.relu
n_outputs = env.action_space.n

he_init = tf.contrib.layers.variance_scaling_initializer()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Observations vary depending on the environment. In this case it is an RGB image represented as a 3D NumPy array of shape [width, height, channels] (with 3 channels: Red, Green and Blue). In other environments it may return different objects, as we will see later.
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Once you have finished playing with an environment, you should close it to free up resources:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Preprocessing
Preprocessing the images is optional but greatly speeds up training.
Can you write Python code for it?

mspacman_color = np.array([210, 164, 74]).mean()

def preprocess_observation(obs):
    img = obs[1:176:2, ::2] # crop and downsize
    img = img.mean(axis=2) # to greyscale
    img[img==mspacman_color] = 0 # Improve contrast
    img = (img - 128) / 128 - 1 # normalize from -1. to 1.
    return img.reshape(88, 80, 1)

img = preprocess_observation(obs)

plt.figure(figsize=(11, 7))
plt.subplot(121)
plt.title("Original observation (160×210 RGB)")
plt.imshow(obs)
plt.axis("off")
plt.subplot(122)
plt.title("Preprocessed observation (88×80 greyscale)")
plt.imshow(img.reshape(88, 80), interpolation="nearest", cmap="gray")
plt.axis("off")
save_fig("preprocessing_plot")
plt.show()


mspacman_color = 210 + 164 + 74

def preprocess_observation(obs):
    img = obs[1:176:2, ::2] # crop and downsize
    img = img.sum(axis=2) # to greyscale
    img[img==mspacman_color] = 0 # Improve contrast
    img = (img // 3 - 128).astype(np.int8) # normalize from -128 to 127
    return img.reshape(88, 80, 1)

img = preprocess_observation(obs)


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Q-Learning
Q-Learning will learn the optimal policy by watching the random policy play.
Can you write Python code for it?

n_states = 3
n_actions = 3
n_steps = 20000
alpha = 0.01
gamma = 0.99
exploration_policy = policy_random
q_values = np.full((n_states, n_actions), -np.inf)
for state, actions in enumerate(possible_actions):
    q_values[state][actions]=0

env = MDPEnvironment()
for step in range(n_steps):
    action = exploration_policy(env.state)
    state = env.state
    next_state, reward = env.step(action)
    next_value = np.max(q_values[next_state]) # greedy policy
    q_values[state, action] = (1-alpha)*q_values[state, action] + alpha*(reward + gamma * next_value)

def optimal_policy(state):
    return np.argmax(q_values[state])

q_values

all_totals = []
for episode in range(1000):
    all_totals.append(run_episode(optimal_policy, n_steps=100, display=(episode<5)))
print("Summary: mean={:.1f}, std={:1f}, min={}, max={}".format(np.mean(all_totals), np.std(all_totals), np.min(all_totals), np.max(all_totals)))
print()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Q-Learning
Q-Learning works by watching an agent play (e.g., randomly) and gradually improving its estimates of the Q-Values. Once it has accurate Q-Value estimates (or close enough), then the optimal policy consists in choosing the action that has the highest Q-Value (i.e., the greedy policy).
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
The environment also tells the agent how much reward it got during the last step:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
The observation is a 1D NumPy array composed of 4 floats: they represent the cart's horizontal position, its velocity, the angle of the pole (0 = vertical), and the angular velocity. Let's render the environment...
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
The observation is a 1D NumPy array composed of 4 floats: they represent the cart's horizontal position, its velocity, the angle of the pole (0 = vertical), and the angular velocity. Let's render the environment... unfortunately we need to fix an annoying rendering issue first.
Fixing the rendering issue
Some environments (including the Cart-Pole) require access to your display, which opens up a separate window, even if you specify the rgb_array mode. In general you can safely ignore that window. However, if Jupyter is running on a headless server (ie. without a screen) it will raise an exception. One way to avoid this is to install a fake X server like Xvfb. You can start Jupyter using the xvfb-run command:
$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

If Jupyter is running on a headless server but you don't want to worry about Xvfb, then you can just use the following rendering function for the Cart-Pole:
Can you write Python code for it?

from PIL import Image, ImageDraw

try:
    from pyglet.gl import gl_info
    openai_cart_pole_rendering = True   # no problem, let's use OpenAI gym's rendering function
except Exception:
    openai_cart_pole_rendering = False  # probably no X server available, let's use our own rendering function

def render_cart_pole(env, obs):
    if openai_cart_pole_rendering:
        # use OpenAI gym's rendering function
        return env.render(mode="rgb_array")
    else:
        # rendering for the cart pole environment (in case OpenAI gym can't do it)
        img_w = 600
        img_h = 400
        cart_w = img_w // 12
        cart_h = img_h // 15
        pole_len = img_h // 3.5
        pole_w = img_w // 80 + 1
        x_width = 2
        max_ang = 0.2
        bg_col = (255, 255, 255)
        cart_col = 0x000000 # Blue Green Red
        pole_col = 0x669acc # Blue Green Red

        pos, vel, ang, ang_vel = obs
        img = Image.new('RGB', (img_w, img_h), bg_col)
        draw = ImageDraw.Draw(img)
        cart_x = pos * img_w // x_width + img_w // x_width
        cart_y = img_h * 95 // 100
        top_pole_x = cart_x + pole_len * np.sin(ang)
        top_pole_y = cart_y - cart_h // 2 - pole_len * np.cos(ang)
        draw.line((0, cart_y, img_w, cart_y), fill=0)
        draw.rectangle((cart_x - cart_w // 2, cart_y - cart_h // 2, cart_x + cart_w // 2, cart_y + cart_h // 2), fill=cart_col) # draw cart
        draw.line((cart_x, cart_y - cart_h // 2, top_pole_x, top_pole_y), fill=pole_col, width=pole_w) # draw pole
        return np.array(img)

def plot_cart_pole(env, obs):
    plt.close()  # or else nbagg sometimes plots in the previous cell
    img = render_cart_pole(env, obs)
    plt.imshow(img)
    plt.axis("off")
    plt.show()

plot_cart_pole(env, obs)


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
The observation tells the agent what the environment looks like, as discussed earlier. This is a 210x160 RGB image:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
The random policy does not fare very well. So let's try to use the DQN and see if we can do better.
First let's write a preprocessing function to scale down the input state. Since a single observation does not tell us about the ball's velocity, we will also need to combine multiple observations into a single state. Below is the preprocessing code for this environment. The preprocessing algorithm is two-fold:

Convert the image in the observation to an image to only black and white and scale it down to 80x80 pixels.

Combine 3 observations into a single state which depicts the velocity of the paddles and the ball.
Can you write Python code for it?

green_paddle_color = (92, 186, 92)
red_paddle_color = (213, 130, 74)
background_color = (144, 72, 17)
ball_color = (236, 236, 236)

def preprocess_observation(obs):
    img = obs[34:194:2, ::2].reshape(-1, 3)
    tmp = np.full(shape=(80 * 80), fill_value=0.0, dtype=np.float32)
    for i, c in enumerate(img):
        c = tuple(c)
        if c in {green_paddle_color, red_paddle_color, ball_color}:
            tmp[i] = 1.0
        else:
            tmp[i] = 0.0
    return tmp.reshape(80, 80)

obs = env.reset()
for _ in range(25):
    obs, _, _, _ = env.step(0)

plt.figure(figsize=(11, 7))
plt.subplot(121)
plt.title('Original Observation (160 x 210 RGB)')
plt.imshow(obs)
plt.axis('off')
plt.subplot(122)
plt.title('Preprocessed Observation (80 x 80 Grayscale)')
plt.imshow(preprocess_observation(obs), interpolation='nearest', cmap='gray')
plt.axis('off')
plt.show()

def combine_observations(preprocess_observations, dim_factor=0.75):
    dimmed = [obs * (dim_factor ** idx)
              for idx, obs in enumerate(reversed(preprocess_observations))]
    return np.max(np.array(dimmed), axis=0)

n_observations_per_state = 3

obs = env.reset()
for _ in range(20):
    obs, _, _, _ = env.step(0)

preprocess_observations = []
for _ in range(n_observations_per_state):
    obs, _, _, _ = env.step(2)
    preprocess_observations.append(preprocess_observation(obs))

img = combine_observations(preprocess_observations)

plt.figure(figsize=(6, 6))
plt.title('Combined Observations as a Single State')
plt.imshow(img, interpolation='nearest', cmap='gray')
plt.axis('off')
plt.show()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
The step() function actually returns several important objects:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
This is a 4D continuous action space controling each leg's hip torque and knee torque (from -1 to 1). To deal with a continuous action space, one method is to discretize it. For example, let's limit the possible torque values to these 3 values: -1.0, 0.0, and 1.0. This means that we are left with $3^4=81$ possible actions.
Can you write Python code for it?

from itertools import product

possible_torques = np.array([-1.0, 0.0, 1.0])
possible_actions = np.array(list(product(possible_torques, possible_torques, possible_torques, possible_torques)))
possible_actions.shape

tf.reset_default_graph()

# 1. Specify the network architecture
n_inputs = env.observation_space.shape[0]  # == 24
n_hidden = 10
n_outputs = len(possible_actions) # == 625
initializer = tf.variance_scaling_initializer()

# 2. Build the neural network
X = tf.placeholder(tf.float32, shape=[None, n_inputs])

hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.selu,
                         kernel_initializer=initializer)
logits = tf.layers.dense(hidden, n_outputs,
                         kernel_initializer=initializer)
outputs = tf.nn.softmax(logits)

# 3. Select a random action based on the estimated probabilities
action_index = tf.squeeze(tf.multinomial(logits, num_samples=1), axis=-1)

# 4. Training
learning_rate = 0.01

y = tf.one_hot(action_index, depth=len(possible_actions))
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=logits)
optimizer = tf.train.AdamOptimizer(learning_rate)
grads_and_vars = optimizer.compute_gradients(cross_entropy)
gradients = [grad for grad, variable in grads_and_vars]
gradient_placeholders = []
grads_and_vars_feed = []
for grad, variable in grads_and_vars:
    gradient_placeholder = tf.placeholder(tf.float32, shape=grad.get_shape())
    gradient_placeholders.append(gradient_placeholder)
    grads_and_vars_feed.append((gradient_placeholder, variable))
training_op = optimizer.apply_gradients(grads_and_vars_feed)

init = tf.global_variables_initializer()
saver = tf.train.Saver()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
This model will sample past experiences from a Replay Memory, this will hopefully help the model learn what higher level patterns to pay attention to to find the right action. It also reduces the chance that the model's behavior gets too correlated to it's most recent experiences.
The replay memory will store its data in the kernel's memory.
Can you write Python code for it?

class ReplayMemory(object):
    def __init__(self, maxlen):
        self.maxlen = maxlen
        self.buf = np.empty(shape=maxlen, dtype=np.object)
        self.index = 0
        self.length = 0

    def append(self, data):
        self.buf[self.index] = data
        self.index += 1
        self.index %= self.maxlen
        self.length = min(self.length + 1, self.maxlen)

    def sample(self, batch_size):
        return self.buf[np.random.randint(self.length, size=batch_size)]

replay_size = 200000
replay_memory = ReplayMemory(replay_size)

def sample_memories(batch_size):
    cols = [[], [], [], [], []]  # state, action, reward, next_state, continue
    for memory in replay_memory.sample(batch_size):
        for col, value in zip(cols, memory):
            col.append(value)
    cols = [np.array(col) for col in cols]
    return cols[0], cols[1], cols[2].reshape(-1, 1), cols[3], \
         cols[4].reshape(-1, 1)


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
This model will use two DQNs, an online DQN and a target DQN. The online DQN learns new parameters at each training step. The target DQN is used to compute the target Q-Values for the online DQN's loss function during training. The online DQN's parameters are copied to the target DQN at regular intervals.
Can you write Python code for it?

def q_network(X_state, name):
    prev_layer = X_state
    with tf.variable_scope(name) as scope:
        for n_maps, kernel_size, strides, padding, activation in zip(
            conv_n_maps, conv_kernel_sizes, conv_kernel_strides, conv_paddings,
            conv_activation):
            prev_layer = tf.layers.conv2d(prev_layer, filters=n_maps,
                                          kernel_size=kernel_size,
                                          strides=strides, padding=padding,
                                          activation=activation,
                                          kernel_initializer=he_init)
        flattened = tf.reshape(prev_layer, [-1, n_hidden_in])
        hidden = tf.layers.dense(flattened, n_hidden,
                                 activation=hidden_activation,
                                 kernel_initializer=he_init)
        outputs = tf.layers.dense(hidden, n_outputs, kernel_initializer=he_init)
    trainable_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       scope=scope.name)
    trainable_vars_by_name = {var.name[len(scope.name):]: var
                              for var in trainable_vars}
    return outputs, trainable_vars_by_name

# Starting the DQN definition.

X_state = tf.placeholder(tf.float32, shape=(None, input_height, input_width,
                                            input_channels))
online_q_values, online_vars = q_network(X_state, 'q_networks/online')
target_q_values, target_vars = q_network(X_state, 'q_networks/target')
copy_ops = [var.assign(online_vars[name]) for name, var in target_vars.items()]
copy_online_to_target = tf.group(*copy_ops)

# Defining the training objective.

learning_rate = 1e-3
momentum = 0.95

with tf.variable_scope('training') as scope:
    X_action = tf.placeholder(tf.int32, shape=(None,))
    y = tf.placeholder(tf.float32, shape=(None, 1))
    Q_target = tf.reduce_sum(online_q_values * tf.one_hot(X_action, n_outputs),
                             axis=1, keepdims=True)
    error...


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
To code our first learning agent, we will be using a simpler environment: the Cart-Pole.
A simple environment: the Cart-Pole
The Cart-Pole is a very simple environment composed of a cart that can move left or right, and pole placed vertically on top of it. The agent must move the cart left or right to keep the pole upright.
Can you write Python code for it?

env = gym.make("CartPole-v0")

env.seed(42)
obs = env.reset()

obs


env = gym.make("CartPole-v0")

obs = env.reset()

obs


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
We can make the same net play in 10 different environments in parallel, and train for 1000 iterations. We also reset environments when they are done.
Can you write Python code for it?

n_environments = 10
n_iterations = 1000

envs = [gym.make("CartPole-v0") for _ in range(n_environments)]
observations = [env.reset() for env in envs]

with tf.Session() as sess:
    init.run()
    for iteration in range(n_iterations):
        target_probas = np.array([([1.] if obs[2] < 0 else [0.]) for obs in observations]) # if angle<0 we want proba(left)=1., or else proba(left)=0.
        action_val, _ = sess.run([action, training_op], feed_dict={X: np.array(observations), y: target_probas})
        for env_index, env in enumerate(envs):
            obs, reward, done, info = env.step(action_val[env_index][0])
            observations[env_index] = obs if not done else env.reset()
    saver.save(sess, "./my_policy_net_basic.ckpt")

for env in envs:
    env.close()

def render_policy_net(model_path, action, X, n_max_steps=1000):
    frames = []
    env = gym.make("CartPole-v0")
    obs = env.reset()
    with tf.Session() as sess:
        saver.restore(sess, model_path)
        for step in range(n_max_steps):
            img = env.render(mode="rgb_array")
            frames.append(img)
            action_val = action.eval(feed_dict={X: obs.reshape(1, n_inputs)})
            obs, reward, done, info = env.step(action_val[0][0])
            if done:
                break
    env.close()
    return frames

frames = render_policy_net("./my_policy_net_basic.ckpt", action, X)
plot_animation(frames)


n_environments = 10
n_iterations = 1000

envs = [gym.make("CartPole-v0") for _ in range(n_environments)]
observations = [env.reset() for env in envs]

with tf.Session() as sess:
    init.run()
    for iteration in range(n_iterations):
        target_probas = np.array([([1.] if obs[2] < 0 else [0.]) for obs in observations]) # if angle<0 we want proba(left)=1., or else proba(left)=0.
        action_val, _ = sess.run([action, training_op], feed_dict={X: np.array(observations), y: target_probas})
        for env_index, env in enumerate(envs):
            obs, reward, done, info = env.step(action_val[env_index][0])
            observations[env_index] = obs if not done else env.reset()
    saver.save(sess, "./my_policy_net_basic.ckpt")

for env in envs:
    env.close()

def render_policy_net(model_path, action, X, n_max_steps = 1000):
    frames = []
    env = gym.make("CartPole-v0")
    obs = env.reset()
    with tf.Session() as sess:
        saver.restore(sess, model_path)
        for step in range(n_max_steps):
            img = render_cart_pole(env, obs)
            frames.append(img)
            action_val = action.eval(feed_dict={X: obs.reshape(1, n_inputs)})
            obs, reward, done, info = env.step(action_val[0][0])
            if done:
                break
    env.close()
    return frames

frames = render_policy_net("./my_policy_net_basic.ckpt", action, X)
video = plot_animation(frames)
plt.show()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
We see the observation space is a 210x160 RGB image. The action space is a Discrete(6) space with 6 different actions: actions 0 and 1 do nothing, actions 2 and 4 move the paddle up, and finally actions 3 and 5 move the paddle down. The paddle is free to move immediately but the ball does not appear until after 18 steps into the episode.
Let's play a game with a completely random policy and plot the resulting animation.
Can you write Python code for it?

# A helper function to run an episode of Pong. It's first argument should be a
# function which takes the observation of the environment and the current
# iteration and produces an action for the agent to take.

def run_episode(policy, n_max_steps=1000, frames_per_action=1):
    obs = env.reset()
    frames = []
    for i in range(n_max_steps):
        obs, reward, done, info = env.step(policy(obs, i))
        frames.append(env.render(mode='rgb_array'))
        if done:
            break
    return plot_animation(frames)

run_episode(lambda obs, i: np.random.randint(0, 5))


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Welcome back to the 1980s! :)
In this environment, the rendered image is simply equal to the observation (but in many environments this is not the case):
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
When the game is over, the environment returns done=True:
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Where are we now?
Can you write Python code for it?


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Yeah... pretty bad. The neural network will have to learn to do better. First let's see if it is capable of learning the basic policy we used earlier: go left if the pole is tilting left, and go right if it is tilting right. The following code defines the same neural network but we add the target probabilities y, and the training operations (cross_entropy,  optimizer and training_op):
Can you write Python code for it?

import tensorflow as tf

reset_graph()

n_inputs = 4
n_hidden = 4
n_outputs = 1

learning_rate = 0.01

initializer = tf.variance_scaling_initializer()

X = tf.placeholder(tf.float32, shape=[None, n_inputs])
y = tf.placeholder(tf.float32, shape=[None, n_outputs])

hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.elu, kernel_initializer=initializer)
logits = tf.layers.dense(hidden, n_outputs)
outputs = tf.nn.sigmoid(logits) # probability of action 0 (left)
p_left_and_right = tf.concat(axis=1, values=[outputs, 1 - outputs])
action = tf.multinomial(tf.log(p_left_and_right), num_samples=1)

cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=logits)
optimizer = tf.train.AdamOptimizer(learning_rate)
training_op = optimizer.minimize(cross_entropy)

init = tf.global_variables_initializer()
saver = tf.train.Saver()


import tensorflow as tf

reset_graph()

n_inputs = 4
n_hidden = 4
n_outputs = 1

learning_rate = 0.01

initializer = tf.contrib.layers.variance_scaling_initializer()

X = tf.placeholder(tf.float32, shape=[None, n_inputs])
y = tf.placeholder(tf.float32, shape=[None, n_outputs])

hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.elu, kernel_initializer=initializer)
logits = tf.layers.dense(hidden, n_outputs)
outputs = tf.nn.sigmoid(logits) # probability of action 0 (left)
p_left_and_right = tf.concat(axis=1, values=[outputs, 1 - outputs])
action = tf.multinomial(tf.log(p_left_and_right), num_samples=1)

cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=logits)
optimizer = tf.train.AdamOptimizer(learning_rate)
training_op = optimizer.minimize(cross_entropy)

init = tf.global_variables_initializer()
saver = tf.train.Saver()


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
Yep, just two possible actions: accelerate towards the left or towards the right. Let's push the cart left until the pole falls:
Can you write Python code for it?

obs = env.reset()
while True:
    obs, reward, done, info = env.step(0)
    if done:
        break

plt.close()  # or else nbagg sometimes plots in the previous cell
img = render_cart_pole(env, obs)
plt.imshow(img)
plt.axis("off")
save_fig("cart_pole_plot")

img.shape


I am working on the file "16_reinforcement_learning.ipynb".
The first task is:
You can interrupt the cell above at any time to test your agent using the cell below. You can then run the cell above once again, it will load the last parameters saved and resume training.
Can you write Python code for it?

frames = []
n_max_steps = 10000

with tf.Session() as sess:
    saver.restore(sess, checkpoint_path)

    obs = env.reset()
    for step in range(n_max_steps):
        state = preprocess_observation(obs)

        # Online DQN evaluates what to do
        q_values = online_q_values.eval(feed_dict={X_state: [state]})
        action = np.argmax(q_values)

        # Online DQN plays
        obs, reward, done, info = env.step(action)

        img = env.render(mode="rgb_array")
        frames.append(img)

        if done:
            break

plot_animation(frames)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/0_Check_Environment.ipynb".
The first task is:
Python 3.5
The next line should say that you're using Python 3.5.x from Continuum Analytics. At the time of publication it looks like this (Mac/Linux):
3.5.3 |Continuum Analytics, Inc.| (default, Mar  6 2017, 12:15:08) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)

or like this (Windows 10):
3.5.3 |Continuum Analytics, Inc.| (default, May 11 2017, 13:52:01) [MSC v.1900 64 bit (AMD64)]

but date and exact version of GCC may change in the future.
If you see a different version of python, go back to the previous step and make sure you created and activated the environment correctly.
Can you write Python code for it?


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
3) Standard normalization
Can you write Python code for it?

from sklearn.preprocessing import StandardScaler

ss = StandardScaler()
df['Weight_ss'] = ss.fit_transform(df[['Weight']])
df['Height_ss'] = ss.fit_transform(df[['Height']])
df.describe().round(2)

plt.figure(figsize=(15, 5))

for i, feature in enumerate(['Height', 'Height (feet)', 'Height_mms', 'Height_ss']):
    plt.subplot(1, 4, i+1)
    df[feature].plot(kind = 'hist',
                     title = feature)
    plt.xlabel(feature)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Classification
Can you write Python code for it?

df = pd.read_csv('./data/user_visit_duration.csv')

df.head()

df.plot(kind = 'scatter',
        x='Time (min)',
        y='Buy')

model = Sequential()
model.add(Dense(1, input_shape=(1,), activation='sigmoid'))

model.compile(SGD(lr = 0.5),
              loss = 'binary_crossentropy',
              metrics=['accuracy'])

model.summary()

X = df[['Time (min)']].values
y = df['Buy'].values

model.fit(X, y, epochs = 25)

ax = df.plot(kind='scatter',
             x = 'Time (min)',
             y ='Buy',
             title = 'Purchase behavior VS time spent on site')

temp = np.linspace(0, 4)
ax.plot(temp, model.predict(temp), color = 'orange')
plt.legend(['model', 'data'])

temp_class = model.predict(temp) > 0.5

ax = df.plot(kind = 'scatter',
             x = 'Time (min)',
             y = 'Buy',
             title = 'Purchase behavior VS time spent on site')

temp = np.linspace(0, 4)
ax.plot(temp, temp_class, color = 'orange')
plt.legend(['model', 'data'])

y_pred = model.predict(X)
y_class_pred = y_pred > 0.5

from sklearn.metrics import accuracy_score

print("The accuracy score is {:0.3f}".format(accuracy_score(y, y_class_pred)))


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Confusion Matrix
Can you write Python code for it?

from sklearn.metrics import confusion_matrix

confusion_matrix(y, y_class_pred)

def pretty_confusion_matrix(y_true, y_pred, labels = ["False", "True"]):
    cm = confusion_matrix(y_true, y_pred)
    pred_labels = ['Predicted '+ l for l in labels]
    df = pd.DataFrame(cm, index = labels, columns = pred_labels)
    return df

pretty_confusion_matrix(y, y_class_pred, ['Not Buy', 'Buy'])

from sklearn.metrics import precision_score, recall_score, f1_score

print("Precision:\t{:0.3f}".format(precision_score(y, y_class_pred)))
print("Recall:  \t{:0.3f}".format(recall_score(y, y_class_pred)))
print("F1 Score:\t{:0.3f}".format(f1_score(y, y_class_pred)))

from sklearn.metrics import classification_report

print(classification_report(y, y_class_pred))


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Cost Function
Can you write Python code for it?

def mean_squared_error(y_true, y_pred):
    s = (y_true - y_pred) ** 2
    return s.mean()

X = df[['Height']].values
y_true = df['Weight'].values

y_true

y_pred = line(X)

y_pred

mean_squared_error(y_true, y_pred.ravel())


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Lowering the learning rate
Can you write Python code for it?

model = Sequential()
model.add(Dense(1, input_shape = (3, )))
opt = Adam(lr = 0.1)
model.compile(optimizer = opt, loss = 'mean_squared_error')

model.fit(X_train, Y_train, epochs = 20, verbose = 1)

Y_train_predicted = model.predict(X_train)
Y_test_predicted = model.predict(X_test)

train_score = r2_score(Y_train, Y_train_predicted)
test_score = r2_score(Y_test, Y_test_predicted)

print('Train set score: \t{:0.3f}'.format(train_score))
print('Test set score: \t{:0.3f}'.format(test_score))


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Machine Learning Exercises
Exercise 1
You've just been hired at a real estate investment firm and they would like you to build a model for pricing houses. You are given a dataset that contains data for house prices and a few features like number of bedrooms, size in square feet and age of the house. Let's see if you can build a model that is able to predict the price. In this exercise we extend what we have learned about linear regression to a dataset with more than one feature. Here are the steps to complete it:

Load the dataset ../data/housing-data.csv
plot the histograms for each feature
create 2 variables called X and y: X shall be a matrix with 3 columns (sqft,bdrms,age) and y shall be a vector with 1 column (price)
create a linear regression model in Keras with the appropriate number of inputs and output
split the data into train and test with a 20% test size
train the model on the training set and check its accuracy on training and test set
how's your model doing? Is the loss growing smaller?
try to improve your model with these experiments:
normalize the input features with one of the rescaling techniques mentioned above
use a different value for the learning rate of your model
use a different optimizer

once you're satisfied with training, check the R2score on the test set
Can you write Python code for it?

ex1 = pd.read_csv('./data/housing-data.csv')

ex1.head()

ex1.shape

plt.figure(figsize=(20, 5))

for i, feature in enumerate(ex1.columns):
    plt.subplot(1, 4, i + 1)
    ex1[feature].plot(kind = 'hist',
                      title = feature)
    plt.xlabel(feature)
    plt.tight_layout()

X = ex1[['sqft', 'bdrms', 'age']]
X.head()

Y = ex1[['price']]
Y.head()

X = X.values
Y = Y.values

X.shape

Y.shape

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD, Adam

model = Sequential()

model.add(Dense(1, input_shape = (3, )))

opt = Adam(lr = 0.8)

model.compile(optimizer = opt, loss = 'mean_squared_error')

model.summary()

from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2)

model.fit(X_train, Y_train, epochs = 10)

from sklearn.metrics import r2_score

Y_train_predicted = model.predict(X_train)
Y_test_predicted = model.predict(X_test)

train_score = r2_score(Y_train, Y_train_predicted)
test_score = r2_score(Y_test, Y_test_predicted)

print('Train set score: \t{:0.3f}'.format(train_score))
print('Test set score: \t{:0.3f}'.format(test_score))


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Manual exploration of different values of W and b
Can you write Python code for it?

plt.figure(figsize=(10, 5))

# we are going to draw 2 plots in the same figure
# first plot, data and a few lines
ax1 = plt.subplot(121)
df.plot(kind = 'scatter',
        x = 'Height',
        y = 'Weight',
        title = 'Weight and Height in adults', ax=ax1)

# let's explore the cost function for a few values of b between -100 and +150
bbs = np.array([-100, -50, 0, 50, 100, 150])
mses = []  # we will append the values of the cost here, for each line
for b in bbs:
    y_pred = line(X, w = 2, b = b)
    mse = mean_squared_error(y_true, y_pred)
    mses.append(mse)
    plt.plot(X, y_pred)

# second plot: Cost function
ax2 = plt.subplot(122)
plt.plot(bbs, mses, 'o-')
plt.title('Cost as a function of b')
plt.xlabel('b')


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
MinMax normalization
Can you write Python code for it?

from sklearn.preprocessing import MinMaxScaler

mms = MinMaxScaler()
df['Weight_mms'] = mms.fit_transform(df[['Weight']])
df['Height_mms'] = mms.fit_transform(df[['Height']])
df.describe().round(2)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
MinMaxScaler
Can you write Python code for it?

from sklearn.preprocessing import MinMaxScaler

minmax = MinMaxScaler()

X = minmax.fit_transform(X)
Y = minmax.fit_transform(Y)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2)

model = Sequential()
model.add(Dense(1, input_shape = (3, )))
opt = Adam(lr = 0.8)
model.compile(optimizer = opt, loss = 'mean_squared_error')

model.fit(X_train, Y_train, epochs = 20)

Y_train_predicted = model.predict(X_train)
Y_test_predicted = model.predict(X_test)

train_score = r2_score(Y_train, Y_train_predicted)
test_score = r2_score(Y_test, Y_test_predicted)

print('Train set score: \t{:0.3f}'.format(train_score))
print('Test set score: \t{:0.3f}'.format(test_score))


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/3 Machine Learning.ipynb".
The first task is:
Using SGD
Can you write Python code for it?

model = Sequential()
model.add(Dense(1, input_shape = (3, )))
opt = SGD(lr = 0.1)
model.compile(optimizer = opt, loss = 'mean_squared_error')

model.fit(X_train, Y_train, epochs = 20)

Y_train_predicted = model.predict(X_train)
Y_test_predicted = model.predict(X_test)

train_score = r2_score(Y_train, Y_train_predicted)
test_score = r2_score(Y_test, Y_test_predicted)

print('Train set score: \t{:0.3f}'.format(train_score))
print('Test set score: \t{:0.3f}'.format(test_score))


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
1D convolution
Can you write Python code for it?

a = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
             dtype='float32')

b = np.array([-1, 1],
             dtype='float32')

c = np.convolve(a, b)

a

b

c

plt.subplot(211)
plt.plot(a, 'o-')

plt.subplot(212)
plt.plot(c, 'o-')


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
Convolutional neural networks
Can you write Python code for it?

from keras.layers import Conv2D

img.shape

plt.figure(figsize = (7, 7))
plt.imshow(img,
           cmap = 'gray')

img_tensor = img.reshape((1, 512, 512, 1))

model = Sequential()
model.add(Conv2D(filters = 1,
                 kernel_size = (3, 3),
                 strides = (2,1),
                 input_shape = (512, 512, 1)))
model.compile('adam', 'mse')

img_pred_tensor = model.predict(img_tensor)

img_pred_tensor.shape

img_pred = img_pred_tensor[0, :, :, 0]

plt.imshow(img_pred,
           cmap = 'gray')

weights = model.get_weights()

weights[0].shape

plt.imshow(weights[0][:, :, 0, 0],
           cmap = 'gray')

weights[0] = np.ones(weights[0].shape)

model.set_weights(weights)

img_pred_tensor = model.predict(img_tensor)

img_pred = img_pred_tensor[0, :, :, 0]

plt.imshow(img_pred,
           cmap = 'gray')

model = Sequential()
model.add(Conv2D(filters = 1,
                 kernel_size = (3, 3),
                 input_shape = (512, 512, 1),
                 padding='same'))
model.compile('adam', 'mse')

img_pred_tensor = model.predict(img_tensor)

img_pred_tensor.shape

img_pred_tensor = img_pred_tensor[0, :, :, 0]

plt.imshow(img_pred_tensor,
           cmap = 'gray')


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
Exercise 1
You've been hired by a shipping company to overhaul the way they route mail, parcels and packages. They want to build an image recognition system  capable of recognizing the digits in the zipcode on a package, so that it can be automatically routed to the correct location.
You are tasked to build the digit recognition system. Luckily, you can rely on the MNIST dataset for the intial training of your model!
Build a deep convolutional neural network with at least two convolutional and two pooling layers before the fully connected layer.

Start from the network we have just built
Insert a Conv2D layer after the first MaxPool2D, give it 64 filters.
Insert a MaxPool2D after that one
Insert an Activation layer
retrain the model
does performance improve?
how many parameters does this new model have? More or less than the previous model? Why?
how long did this second model take to train? Longer or shorter than the previous model? Why?
did it perform better or worse than the previous model?
Can you write Python code for it?

X_train.shape, X_test.shape, y_train_cat.shape, y_test_cat.shape

from keras.layers import Input, Dense, Conv2D, MaxPool2D, Activation, Flatten
import keras.backend as K
from keras.models import Model
from keras.optimizers import Adam

K.clear_session()
inp = Input(shape = (28, 28, 1 ))
net = Conv2D(filters = 64,
             kernel_size = (2, 2),
             activation = 'relu',
             padding = 'valid')(inp)
net = MaxPool2D(pool_size = (2, 2),
                strides = (2, 2),
                padding = 'valid')(net)
net = Conv2D(filters = 8,
             kernel_size = (2, 2),
             activation = 'relu',
             padding = 'valid')(net)
net = MaxPool2D(pool_size = (2, 2),
                strides = (2, 2),
                padding = 'valid')(net)
net = Activation(activation = 'relu')(net)
net = Flatten()(net)
prediction = Dense(10, activation = 'softmax')(net)

model = Model(inputs = inp, outputs = prediction)
model.compile(optimizer = Adam(),
              loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

model.summary()

model.fit(X_train,
          y_train_cat,
          batch_size = 50,
          validation_split = 0.2,
          epochs = 5,
          verbose = 2)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
Exercise 2
Pleased with your performance with the digits recognition task, your boss decides to challenge you with a harder task. Their online branch allows people to upload images to a website that generates and prints a postcard that is shipped to destination. Your boss would like to know what images people are loading on the site in order to provide targeted advertising on the same page, so he asks you to build an image recognition system capable of recognizing a few objects. Luckily for you, there's a dataset ready made with a collection of labeled images. This is the Cifar 10 Dataset, a very famous dataset that contains images for 10 different categories:

airplane
automobile
bird
cat
deer
dog
frog
horse
ship
truck

In this exercise we will reach the limit of what you can achieve on your laptop and get ready for the next session on cloud GPUs.
Here's what you have to do:
- load the cifar10 dataset using keras.datasets.cifar10.load_data()
- display a few images, see how hard/easy it is for you to recognize an object with such low resolution
- check the shape of X_train, does it need reshape?
- check the scale of X_train, does it need rescaling?
- check the shape of y_train, does it need reshape?
- build a model with the following architecture, and choose the parameters and activation functions for each of the layers:
    - conv2d
    - conv2d
    - maxpool
    - conv2d
    - conv2d
    - maxpool
    - flatten
    - dense
    - output
- compile the model and check the number of parameters
- attempt to train the model with the optimizer of your choice. How fast does training proceed?
- If training is too slow (as expected) stop the execution and move to the next session!
Can you write Python code for it?

from keras.datasets import cifar10

(X_train, y_train), (X_test, y_test) = cifar10.load_data()

X_train.shape

X_train[0, :, :, 0]

X_train[0, :, :, 1]

X_train[0, :, :, 2]

np.max(X_train[0, :, :, :])

np.min(X_train[0, :, :, :])

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train = X_train / 255.0
X_test = X_test / 255.0

from keras.utils import to_categorical
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)

y_train_cat[:5]

print(y_train_cat.shape)

y_test_cat[:5]

print(y_test_cat.shape)

'''conv2d
conv2d
maxpool

conv2d
conv2d
maxpool
flatten
dense
output
'''

from keras.models import Model
from keras.layers import Input, Dense, Conv2D, MaxPool2D, Flatten
from keras.regularizers import l2

# Inputs 32 x 32 x 3
inp = Input(shape = (32, 32, 3))

net = Conv2D(filters = 8,
             kernel_size = (2, 2),
             padding = 'same')(inp)
net = Conv2D(filters = 8,
             kernel_size = (2, 2),
             padding = 'same')(net)
net = MaxPool2D(pool_size = (2, 2), padding = 'valid')(net)

net = Conv2D(filters = 8,
             kernel_size = (2, 2),
             padding = 'same')(net)
net = Conv2D(filters = 8,
             kernel_size = (2, 2),
             padding = 'same')(net)
net = MaxPool2D(pool_size = (2, 2), padding = 'valid')(net)

net = Flatten()(net)
net = Dense(units = 10,
            activation = 'relu')(net)
prediction = Dense(units = 10,
                   activation = 'softmax')(net)

model = Model(inputs = [inp], outputs = [prediction])

from keras.optimizers import Adam

model.compile(optimizer = Adam(),
              loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

model.summary()

model.fit(X_train,
          y_train_cat,
          batch_size = 50,
          validation_split = 0.2,
          epochs = 5,
          verbose = 2)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
MNIST
Can you write Python code for it?

from keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data('/tmp/mnist.npz')

X_train.shape

X_test.shape

X_train[0]

plt.imshow(X_train[0],
           cmap = 'gray')

# Flattening the input
X_train = X_train.reshape(-1, 28 * 28)
X_test = X_test.reshape(-1, 28 * 28)

X_train.shape

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0

X_train[0]

from keras.utils.np_utils import to_categorical

y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)

y_train[0]

# One-hot encoded labels
y_train_cat[0]

y_train_cat.shape

y_test_cat.shape


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
Tensor Math
Can you write Python code for it?

A = np.random.randint(10, size = (2, 3, 4, 5))
B = np.random.randint(10, size = (2, 3))

A

A[0, 1, 0, 3]

B


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/6 Convolutional Neural Networks.ipynb".
The first task is:
Tensor operations
Can you write Python code for it?

2 * A

A + A

A.shape

B.shape

np.tensordot(A,
             B,
             axes = ([0, 1], [0, 1]))

np.tensordot(A,
             B,
             axes = ([0], [0])).shape


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/8 Recurrent Neural Networks.ipynb".
The first task is:
Exercise 2
RNN models can be applied to images too. In general we can apply them to any data where there's a connnection between nearby units. Let's see how we can easily build a model that works with images.

Load the MNIST data, by now you should be able to do it blindfolded :)
reshape it so that an image looks like a long sequence of pixels
create a recurrent model and train it on the training data
how does it perform compared to a fully connected? How does it compare to Convolutional Neural Networks?

(feel free to run this exercise on a cloud GPU if it's too slow on your laptop)
Can you write Python code for it?

from keras.datasets import mnist

(X_train, Y_train), (X_test, Y_test) = mnist.load_data(path = 'mnist.npz')

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

# Regularization
X_train /= 255.0
X_test /= 255.0

from keras.utils import to_categorical

Y_train.shape

Y_train_cat = to_categorical(Y_train)
Y_test_cat = to_categorical(Y_test)

Y_train_cat.shape, Y_test_cat.shape

X_train = X_train.reshape(X_train.shape[0], -1, 1)
X_test = X_test.reshape(X_test.shape[0], -1, 1)

X_train.shape, X_test.shape

from keras.models import Model
from keras.layers import Input, LSTM, Dense
import keras.backend as K

K.clear_session()

inp = Input(shape = X_train.shape[1:])

net = LSTM(units = 32)(inp)
prediction = Dense(units = 10,
                   activation = 'softmax')(net)

model = Model(inputs = inp,
              outputs = prediction)
model.compile(optimizer = 'adam',
              loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

model.summary()

early_stop = EarlyStopping(monitor = 'loss',
                           patience = 1,
                           verbose = 1)

model.fit(X_train,
          Y_train_cat,
          epochs = 2,
          batch_size = 1000,
          validation_split = 0.3,
          verbose = 1,
          callbacks = [early_stop])

model.evaluate(X_test, Y_test_cat)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/8 Recurrent Neural Networks.ipynb".
The first task is:
Fully Connected on Windows
Can you write Python code for it?

K.clear_session()

model = Sequential()
model.add(Dense(12,
                input_dim = 12,
                activation = 'relu'))
model.add(Dense(1))
model.compile(optimizer = 'adam',
              loss = 'mean_squared_error',
              metrics = ['mae'])
model.summary()

early_stop = EarlyStopping(monitor = 'loss',
                           patience = 10,
                           verbose = 1)

model.fit(X_train,
          y_train,
          epochs = 200,
          batch_size = 1,
          verbose = 2,
          callbacks = [early_stop])

y_pred = model.predict(X_test)
plt.plot(y_test)
plt.plot(y_pred)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/8 Recurrent Neural Networks.ipynb".
The first task is:
Fully connected predictor
Can you write Python code for it?

from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
from keras.callbacks import EarlyStopping

K.clear_session()

model = Sequential()
model.add(Dense(12,
                input_dim = 1,
                activation = 'relu'))
model.add(Dense(1))
model.compile(optimizer = 'adam',
              loss = 'mean_squared_error',
              metrics = ['mae'])
model.summary()

early_stop = EarlyStopping(monitor = 'loss',
                           patience = 10,
                           verbose = 1)

model.fit(X_train,
          y_train,
          epochs = 200,
          batch_size = 2,
          verbose = 2,
          callbacks = [early_stop])

y_pred = model.predict(X_test)

plt.plot(y_test)
plt.plot(y_pred)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/8 Recurrent Neural Networks.ipynb".
The first task is:
Recurrent predictor
Can you write Python code for it?

from keras.layers import LSTM

X_train.shape

#3D tensor with shape (batch_size, timesteps, input_dim)
X_train[:, None].shape

X_train_t = X_train[:, None]
X_test_t = X_test[:, None]

K.clear_session()
model = Sequential()

model.add(LSTM(6,
               input_shape = (1, 1)))

model.add(Dense(1))

model.compile(optimizer = 'adam',
              loss = 'mean_squared_error',
              metrics = ['mae'])

model.summary()

early_stop = EarlyStopping(monitor = 'loss',
                           patience = 10,
                           verbose = 1)

model.fit(X_train_t,
          y_train,
          epochs = 100,
          batch_size = 1,
          verbose = 2,
          callbacks=[early_stop])

y_pred = model.predict(X_test_t)
plt.plot(y_test)
plt.plot(y_pred)


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/9 Improving performance.ipynb".
The first task is:
Batch Normalization
Can you write Python code for it?

from keras.layers import BatchNormalization

def repeated_training(X_train,
                      y_train,
                      X_test,
                      y_test,
                      units=512,
                      activation='sigmoid',
                      optimizer='sgd',
                      do_bn=False,
                      epochs=10,
                      repeats=3):
    histories = []

    for repeat in range(repeats):
        K.clear_session()

        model = Sequential()

        # first fully connected layer
        model.add(Dense(units,
                        input_shape=X_train.shape[1:],
                        kernel_initializer='normal',
                        activation=activation))
        if do_bn:
            model.add(BatchNormalization())

        # second fully connected layer
        model.add(Dense(units,
                        kernel_initializer='normal',
                        activation=activation))
        if do_bn:
            model.add(BatchNormalization())

        # third fully connected layer
        model.add(Dense(units,
                        kernel_initializer='normal',
                        activation=activation))
        if do_bn:
            model.add(BatchNormalization())

        # output layer
        model.add(Dense(10, activation='softmax'))

        model.compile(optimizer,
                      'categorical_crossentropy',
                      metrics=['accuracy'])

        h = model.fit(X_train, y_train,
                      validation_data=(X_test, y_test),
                      epochs=epochs,
                      verbose=0)
        histories.append([h.history['acc'], h.history['val_acc']])
        print(repeat, end=' ')

    histories = np.array(histories)

    # calculate mean and standard deviation across repeats:
    mean_acc = histories.mean(axis=0)
    std_acc = histories.std(axis=0)
    print()

    return mean_acc[0], std_acc[0], mean_acc[1], std_acc[1]

mean_acc, std_acc, mean_acc_val, std_acc_val = \
   ...


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/9 Improving performance.ipynb".
The first task is:
Data augmentation
Can you write Python code for it?

from keras.preprocessing.image import ImageDataGenerator

generator = ImageDataGenerator(rescale = 1./255,
                               width_shift_range=0.1,
                               height_shift_range=0.1,
                               rotation_range = 20,
                               shear_range = 0.3,
                               zoom_range = 0.3,
                               horizontal_flip = True)

train = generator.flow_from_directory('../data/generator',
                                      target_size = (128, 128),
                                      batch_size = 32,
                                      class_mode = 'binary')

plt.figure(figsize=(12, 12))
for i in range(16):
    img, label = train.next()
    plt.subplot(4, 4, i+1)
    plt.imshow(img[0])


I am working on the file "17-08-31-Zero-to-Deep-Learning-with-Python-and-Keras/9 Improving performance.ipynb".
The first task is:
Learning curves
Can you write Python code for it?

from sklearn.datasets import load_digits

digits = load_digits()

X, y = digits.data, digits.target

X[0]

X.shape

for i in range(8):
    plt.subplot(1,8,i+1)
    plt.imshow(X.reshape(-1, 8, 8)[i], cmap='gray')

from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
import keras.backend as K
from keras.callbacks import EarlyStopping

K.clear_session()

model = Sequential()
model.add(Dense(16, input_shape=(64,), activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])

# store the initial random weights
initial_weights = model.get_weights()

y_cat = to_categorical(y, 10)

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y_cat,
                                                    test_size=0.3)

X_train.shape

train_sizes = (len(X_train) * np.linspace(0.1, 0.999, 4)).astype(int)
train_sizes

train_scores = []
test_scores = []

for train_size in train_sizes:
    X_train_frac, _, y_train_frac, _ = \
    train_test_split(X_train, y_train, train_size=train_size)

    # at each iteration reset the weights of the model
    # to the initial random weights
    model.set_weights(initial_weights)

    h = model.fit(X_train_frac, y_train_frac,
                  verbose=0,
                  epochs=300,
                  callbacks=[EarlyStopping(monitor='loss', patience=1)])

    r = model.evaluate(X_train_frac, y_train_frac, verbose=0)
    train_scores.append(r[-1])

    e = model.evaluate(X_test, y_test, verbose=0)
    test_scores.append(e[-1])

    print("Done size: ", train_size)

plt.plot(train_sizes, train_scores, 'o-', label="Training score")
plt.plot(train_sizes, test_scores, 'o-', label="Test score")
plt.legend(loc="best")


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/1 - Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #2
Given the string:
stock_index = "SP500"

Grab '500' from the string using indexing.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/1 - Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #3
 Given the variables:
stock_index = "SP500"
price = 300

 Use .format() to print the following string:
The SP500 is at 300 today.
Can you write Python code for it?

stock_index = "SP500"
price = 300

print('The {x} is at {y} today.'.format(x = stock_index, y = price))


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/1 - Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #5
 Given strings with this form where the last source value is always separated by two dashes --
"PRICE:345.324:SOURCE--QUANDL"

Create a function called source_finder() that returns the source. For example, the above string passed into the function would return "QUANDL"
Can you write Python code for it?

def source_finder(inp):
    return (inp.split(sep = '--'))[-1]

source_finder("PRICE:345.324:SOURCE--QUANDL")


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/1 - Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #6
 Create a function called count_price() that counts the number of times the word "price" occurs in a string. Account for capitalization and if the word price is next to punctuation.
Can you write Python code for it?

def count_price(inp):
    return s.lower().count('price')

s = 'Wow that is a nice price, very nice Price! I said price 3 times.'

count_price(s)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/1 - Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #7
Create a function called avg_price that takes in a list of stock price numbers and calculates the average (Sum of the numbers divided by the number of elements in the list). It should return a float.
Can you write Python code for it?

def avg_price(inp):
    return (sum(inp) / len(inp))

avg_price([3,4,5])


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Bonus Question
We worked a lot with random data with numpy, but is there a way we can insure that we always get the same random numbers? Click Here for a Hint
Can you write Python code for it?

# My favourite number is 7
np.random.seed(7)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create a 3x3 identity matrix
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create a 3x3 matrix with values ranging from 0 to 8
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of 10 fives
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of 10 ones
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of 10 zeros
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of all the even integers from 10 to 50
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of the integers from 10 to 50
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Create the following matrix:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Get the standard deviation of the values in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Get the sum of all the columns in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Now do the following
Get the sum of all the values in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Numpy Indexing and Selection
Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:
Can you write Python code for it?

# HERE IS THE GIVEN MATRIX CALLED MAT
# USE IT FOR THE FOLLOWING TASKS
mat = np.arange(1,26).reshape(5,5)
mat

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[2:, ]

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[3, -1]

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[:3, 1].reshape(3, 1)

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[-1, :]

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[-2:, :]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Use NumPy to generate a random number between 0 and 1
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
Bonus: How many banks closed in the year 2008? (this is hard because we technically haven't learned about time series with pandas yet! Feel free to skip this one!
Can you write Python code for it?

# CODE HERE
sum(df['Closing Date'].apply(lambda date: date[-2:] == '08'))


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
How many CERT values are above 20000 ?
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
How many States (ST) are represented in this data set?
Can you write Python code for it?

# CODE HERE
df['ST'].unique().shape[0]
# or
# len(df['ST'].unique())


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
How many bank names consist of just two words? (e.g. "First Bank" , "Bank Georgia" )
Can you write Python code for it?

# CODE HERE
sum(df['Bank Name'].apply(lambda name: len(name.split()) == 2))


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
How many bank names start with the letter 's' ?
Can you write Python code for it?

# CODE HERE
df['Bank Name'].apply(lambda name: name[0].lower() == 's').value_counts()


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
How many banks has the State Bank of Texas acquired? How many of them were actually in Texas?
Can you write Python code for it?

# CODE HERE
df[df['Acquiring Institution'] == 'State Bank of Texas']

len(df[df['Acquiring Institution'] == 'State Bank of Texas'])

len(df[(df['Acquiring Institution'] == 'State Bank of Texas') & (df['ST'] == 'TX')])


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
How many failed banks don't have the word "Bank" in their name?
Can you write Python code for it?

# CODE HERE
len(df[~df['Bank Name'].str.contains('Bank')]['Bank Name'].unique())


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
Show the head of the dataframe
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
What are the column names?
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/3 - Pandas Exercises-checkpoint.ipynb".
The first task is:
What is the most common city in California for a bank to fail in?
Can you write Python code for it?

# CODE HERE
df[df['ST'] == 'CA']['City'].value_counts()[0]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/.ipynb_checkpoints/Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #3
 Given the variables:
stock_index = "SP500"
price = 300

 Use .format() to print the following string:
The SP500 is at 300 today.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/.ipynb_checkpoints/Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #4
 Given the variable of a nested dictionary with nested lists:
stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}

 Use indexing and key calls to grab the following items:

Yesterday's SP500 price (250)
The number 365 nested inside a list nested inside the 'info' key.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/.ipynb_checkpoints/Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #5
 Create a function called price_finder that returns True if the word 'price' is in a string. Your function should work even if 'Price' is capitalized or next to punctuation ('price!')
Can you write Python code for it?

price_finder("What is the price?")

price_finder("DUDE, WHAT IS PRICE!!!")

price_finder("The price is 300")


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/.ipynb_checkpoints/Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #5
 Given strings with this form where the last source value is always separated by two dashes --
"PRICE:345.324:SOURCE--QUANDL"

Create a function called source_finder() that returns the source. For example, the above string passed into the function would return "QUANDL"
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/.ipynb_checkpoints/Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #6
 Create a function called count_price() that counts the number of times the word "price" occurs in a string. Account for capitalization and if the word price is next to punctuation.
Can you write Python code for it?

s = 'Wow that is a nice price, very nice Price! I said price 3 times.'

count_price(s)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/.ipynb_checkpoints/Python Crash Course Exercises -checkpoint.ipynb".
The first task is:
Task #7
Create a function called avg_price that takes in a list of stock price numbers and calculates the average (Sum of the numbers divided by the number of elements in the list). It should return a float.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/Python Crash Course Exercises - Solved .ipynb".
The first task is:
Task #2
Given the string:
stock_index = "SP500"

Grab '500' from the string using indexing.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/Python Crash Course Exercises - Solved .ipynb".
The first task is:
Task #4
 Given the variable of a nested dictionary with nested lists:
stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}

 Use indexing and key calls to grab the following items:

Yesterday's SP500 price (250)
The number 365 nested inside a list nested inside the 'info' key.
Can you write Python code for it?

stock_info = {'sp500':{'today':300,'yesterday': 250}, 'info':['Time',[24,7,365]]}

print(stock_info['sp500']['yesterday'])

print(stock_info['info'][1][2])


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/01-Python-Crash-Course/Python Crash Course Exercises - Solved .ipynb".
The first task is:
Task #5
 Create a function called price_finder that returns True if the word 'price' is in a string. Your function should work even if 'Price' is capitalized or next to punctuation ('price!')
Can you write Python code for it?

def price_finder(inp):
    return 'price' in inp.lower()

price_finder(inp = "What is the price?")

price_finder("What is the price?")

price_finder("DUDE, WHAT IS PRICE!!!")

price_finder("The price is 300")


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create a 3x3 identity matrix
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create a 3x3 matrix with values ranging from 0 to 8
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of 10 fives
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of 10 ones
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of all the even integers from 10 to 50
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create an array of the integers from 10 to 50
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Create the following matrix:
Create an array of 20 linearly spaced points between 0 and 1:
Numpy Indexing and Selection
Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:
Can you write Python code for it?

# HERE IS THE GIVEN MATRIX CALLED MAT
# USE IT FOR THE FOLLOWING TASKS
mat = np.arange(1,26).reshape(5,5)
mat

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Get the sum of all the columns in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Now do the following
Get the sum of all the values in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/.ipynb_checkpoints/Numpy Exercises-checkpoint.ipynb".
The first task is:
Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
Array Attributes and Methods
Let's discuss some useful attributes and methods or an array:
Can you write Python code for it?

arr = np.arange(25)
ranarr = np.random.randint(0, 50, 10)

arr

ranarr


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
Built-in Methods
There are lots of built-in ways to generate Arrays
arange
Return evenly spaced values within a given interval.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
Numpy has many built-in functions and capabilities. We won't cover them all but instead we will focus on some of the most important aspects of Numpy: vectors,arrays,matrices, and number generation. Let's start by discussing arrays.
Numpy Arrays
NumPy arrays are the main way we will use Numpy throughout the course. Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).
Let's begin our introduction by exploring how to create NumPy arrays.
Creating NumPy Arrays
From a Python List
We can create an array by directly converting a list or list of lists:
Can you write Python code for it?

my_list = [1, 2, 3]
my_list

np.array(my_list)

my_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
my_matrix

np.array(my_matrix)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
Random
Numpy also has lots of ways to create random number arrays:
rand
Create an array of the given shape and populate it with
random samples from a uniform distribution
over [0, 1).
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
Reshape
Returns an array containing the same data with a new shape.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
Shape
Shape is an attribute that arrays have (not a method):
Can you write Python code for it?

# Vector
arr.shape

# Notice the two sets of brackets
arr.reshape(1, 25)

arr.reshape(1, 25).shape

arr.reshape(25, 1)

arr.reshape(25, 1).shape


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
dtype
You can also grab the data type of the object in the array:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
linspace
Return evenly spaced numbers over a specified interval.
Can you write Python code for it?

np.linspace(0, 10, 3)

np.linspace(0, 10, 50)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
randint
Return random integers from low (inclusive) to high (exclusive).
Can you write Python code for it?

np.random.randint(1, 100)

np.random.randint(1, 100, 10)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/1-NumPy-Arrays.ipynb".
The first task is:
randn
Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform:
Can you write Python code for it?

np.random.randn(2)

np.random.randn(5, 5)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/2-Numpy-Indexing-and-Selection.ipynb".
The first task is:
Bracket Indexing and Selection
The simplest way to pick one or some elements of an array looks very similar to python lists:
Can you write Python code for it?

#Get a value at an index
arr[8]

#Get values in a range
arr[1:5]

#Get values in a range
arr[0:5]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/2-Numpy-Indexing-and-Selection.ipynb".
The first task is:
Broadcasting
Numpy arrays differ from a normal Python list because of their ability to broadcast:
Can you write Python code for it?

#Setting a value with index range (Broadcasting)
arr[0:5] = 100

#Show
arr

# Reset array, we'll see why I had to reset in  a moment
arr = np.arange(0, 11)

#Show
arr

#Important notes on Slices
slice_of_arr = arr[0:6]

#Show slice
slice_of_arr

#Change Slice
slice_of_arr[:] = 99

#Show Slice again
slice_of_arr


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/2-Numpy-Indexing-and-Selection.ipynb".
The first task is:
Indexing a 2D array (matrices)
The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity.
Can you write Python code for it?

arr_2d = np.array(([5, 10, 15], [20, 25, 30], [35, 40, 45]))

#Show
arr_2d

#Indexing row
arr_2d[1]

# Format is arr_2d[row][col] or arr_2d[row,col]

# Getting individual element value
arr_2d[1][0]

# Getting individual element value
arr_2d[1,0]

# 2D array slicing

#Shape (2,2) from top right corner
arr_2d[:2,1:]

#Shape bottom row
arr_2d[2]

#Shape bottom row
arr_2d[2,:]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/2-Numpy-Indexing-and-Selection.ipynb".
The first task is:
More Indexing Help
Indexing a 2d matrix can be a bit confusing at first, especially when you start to add in step size. Try google image searching NumPy indexing to fins useful images, like this one:
<img src= 'http://memory.osu.edu/classes/python/_images/numpy_indexing.png' width=500/>
Conditional Selection
This is a very fundamental concept that will directly translate to pandas later on, make sure you understand this part!
Let's briefly go over how to use brackets for selection based off of comparison operators.
Can you write Python code for it?

arr = np.arange(1, 11)
arr

arr > 4

bool_arr = arr > 4

bool_arr

arr[bool_arr]

arr[arr > 2]

x = 2
arr[arr > x]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/2-Numpy-Indexing-and-Selection.ipynb".
The first task is:
Now note the changes also occur in our original array!
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/3-Numpy-Operations.ipynb".
The first task is:
Universal Array Functions
Numpy comes with many universal array functions, which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones:
Can you write Python code for it?

#Taking Square Roots
np.sqrt(arr)

#Calcualting exponential (e^)
np.exp(arr)

np.max(arr) #same as arr.max()

np.sin(arr)

np.log(arr)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Bonus Question
We worked a lot with random data with numpy, but is there a way we can insure that we always get the same random numbers? Click Here for a Hint
Can you write Python code for it?

# My favourite number is 7
np.random.seed(7)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create a 3x3 identity matrix
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create an array of 10 ones
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create an array of 10 zeros
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create an array of 20 linearly spaced points between 0 and 1:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create an array of all the even integers from 10 to 50
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create an array of the integers from 10 to 50
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Create the following matrix:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Get the standard deviation of the values in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Get the sum of all the columns in mat
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Numpy Indexing and Selection
Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:
Can you write Python code for it?

# HERE IS THE GIVEN MATRIX CALLED MAT
# USE IT FOR THE FOLLOWING TASKS
mat = np.arange(1,26).reshape(5,5)
mat

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[2:, ]

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[3, -1]

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[:3, 1].reshape(3, 1)

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[-1, :]

# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE

mat[-2:, :]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/02-NumPy/Numpy Exercises - Solved.ipynb".
The first task is:
Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/.ipynb_checkpoints/05-Groupby-checkpoint.ipynb".
The first task is:
And then call aggregate methods off the object:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/.ipynb_checkpoints/05-Groupby-checkpoint.ipynb".
The first task is:
You can save this object as a new variable:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/02-Series.ipynb".
The first task is:
Dictionary
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/02-Series.ipynb".
The first task is:
Operations are then also done based off of index:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/03-DataFrames.ipynb".
The first task is:
Conditional Selection
An important feature of pandas is conditional selection using bracket notation, very similar to numpy:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/03-DataFrames.ipynb".
The first task is:
Creating a new column:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/03-DataFrames.ipynb".
The first task is:
DataFrame Columns are just Series
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/03-DataFrames.ipynb".
The first task is:
More Index Details
Let's discuss some more features of indexing, including resetting the index or setting it something else. We'll also talk about index hierarchy!
Can you write Python code for it?

df

# Reset to default 0,1...n index
df.reset_index()

newind = 'CA NY WY OR CO'.split()

df['States'] = newind

df

df.set_index('States')

df

df.set_index('States',
             inplace = True)

df


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/03-DataFrames.ipynb".
The first task is:
Multi-Index and Index Hierarchy
Let us go over how to work with Multi-Index, first we'll create a quick example of what a Multi-Indexed DataFrame would look like:
Can you write Python code for it?

# Index Levels
outside = ['G1', 'G1', 'G1', 'G2', 'G2', 'G2']
inside = [1, 2 , 3, 1, 2, 3]
hier_index = list(zip(outside,inside))
hier_index = pd.MultiIndex.from_tuples(hier_index)

hier_index

df = pd.DataFrame(np.random.randn(6, 2),
                  index=hier_index,columns = ['A', 'B'])
df


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/03-DataFrames.ipynb".
The first task is:
Or select based off of position instead of label
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/05-Groupby.ipynb".
The first task is:
More examples of aggregate methods:
Can you write Python code for it?

by_comp.std()

by_comp.min()

by_comp.max()

by_comp.count()

by_comp.describe()

by_comp.describe().transpose()

by_comp.describe().transpose()['GOOG']


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/05-Groupby.ipynb".
The first task is:
Now you can use the .groupby() method to group rows together based off of a column name. For instance let's group based off of Company. This will create a DataFrameGroupBy object:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/05-Groupby.ipynb".
The first task is:
You can save this object as a new variable:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/06-Merging-Joining-and-Concatenating.ipynb".
The first task is:
Concatenation
Concatenation basically glues together DataFrames. Keep in mind that dimensions should match along the axis you are concatenating on. You can use pd.concat and pass in a list of DataFrames to concatenate together:
Can you write Python code for it?

pd.concat([df1, df2, df3])

pd.concat([df1, df2, df3],
          axis = 1)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/06-Merging-Joining-and-Concatenating.ipynb".
The first task is:
Joining
Joining is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/06-Merging-Joining-and-Concatenating.ipynb".
The first task is:
Merging
The merge function allows you to merge DataFrames together using a similar logic as merging SQL Tables together. For example:
Can you write Python code for it?

pd.merge(left, right,
         how = 'inner',
         on = 'key')


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/06-Merging-Joining-and-Concatenating.ipynb".
The first task is:
Or to show a more complicated example:
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/08-Data-Input-and-Output.ipynb".
The first task is:
CSV
CSV Input
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/08-Data-Input-and-Output.ipynb".
The first task is:
CSV Output
Can you write Python code for it?

df.to_csv('example',
          index = False)


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/08-Data-Input-and-Output.ipynb".
The first task is:
Excel
Pandas can read and write excel files, keep in mind, this only imports data. Not formulas or images, having images or macros may cause this read_excel method to crash.
Excel Input
Can you write Python code for it?

pd.read_excel('Excel_Sample.xlsx',
              sheetname = 'Sheet1')


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/08-Data-Input-and-Output.ipynb".
The first task is:
Excel Output
Can you write Python code for it?

df.to_excel('Excel_Sample.xlsx',
            sheet_name = 'Sheet1')


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/08-Data-Input-and-Output.ipynb".
The first task is:
HTML
You may need to install htmllib5,lxml, and BeautifulSoup4. In your terminal/command prompt run:
conda install lxml
conda install html5lib
conda install BeautifulSoup4

Then restart Jupyter Notebook.
(or use pip install if you aren't using the Anaconda Distribution)
Pandas can read table tabs off of html. For example:
HTML Input
Pandas read_html function will read tables off of a webpage and return a list of DataFrame objects:
Can you write Python code for it?

df = pd.read_html('http://www.fdic.gov/bank/individual/failed/banklist.html')

df[0]


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/Pandas Exercises - Solved.ipynb".
The first task is:
Bonus: How many banks closed in the year 2008? (this is hard because we technically haven't learned about time series with pandas yet! Feel free to skip this one!
Can you write Python code for it?

# CODE HERE
sum(df['Closing Date'].apply(lambda date: date[-2:] == '08'))


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/Pandas Exercises - Solved.ipynb".
The first task is:
Get a list or array of all the states in the data set.
Can you write Python code for it?


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/Pandas Exercises - Solved.ipynb".
The first task is:
How many States (ST) are represented in this data set?
Can you write Python code for it?

# CODE HERE
df['ST'].unique().shape[0]
# or
# len(df['ST'].unique())


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/Pandas Exercises - Solved.ipynb".
The first task is:
How many bank names consist of just two words? (e.g. "First Bank" , "Bank Georgia" )
Can you write Python code for it?

# CODE HERE
sum(df['Bank Name'].apply(lambda name: len(name.split()) == 2))


I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/03-General Pandas/Pandas Exercises - Solved.ipynb".
The first task is:
How many bank names start with the letter 's' ?
Can you write Python code for it?

# CODE HERE
df['Bank Name'].apply(lambda name: name[0].lower() == 's').value_counts()