In [0]:
#@title Copyright 2020 Google LLC. Double-click here for license information.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Machine Learning Crash Course uses Colaboratories (Colabs) for all programming exercises. Colab is Google's implementation of Jupyter Notebook. Like all Jupyter Notebooks, a Colab consists of two kinds of components:
You read the text cells and run the code cells.
You must run code cells in order. In other words, you may only run a code cell once all the code cells preceding it have already been run.
To run a code cell:
Alternatively, you may invoke Runtime->Run all. Note, though, that some of the code cells will fail because not all the coding is complete. (You'll complete the coding as part of the exercise.)
The most common reasons for seeing code cell errors are as follows:
In [0]:
import numpy as np
In [0]:
one_dimensional_array = np.array([1.2, 2.4, 3.5, 4.7, 6.1, 7.2, 8.3, 9.5])
print(one_dimensional_array)
You can also use np.array to create a two-dimensional matrix. To create a two-dimensional matrix, specify an extra layer of square brackets. For example, the following call creates a 3x2 matrix:
In [0]:
two_dimensional_array = np.array([[6, 5], [11, 7], [4, 8]])
print(two_dimensional_array)
To populate a matrix with all zeroes, call np.zeros. To populate a matrix with all ones, call np.ones.
In [0]:
sequence_of_integers = np.arange(5, 12)
print(sequence_of_integers)
Notice that np.arange generates a sequence that includes the lower bound (5) but not the upper bound (12).
NumPy provides various functions to populate matrices with random numbers across certain ranges. For example, np.random.randint generates random integers between a low and high value. The following call populates a 6-element vector with random integers between 50 and 100.
In [0]:
random_integers_between_50_and_100 = np.random.randint(low=50, high=101, size=(6))
print(random_integers_between_50_and_100)
Note that the highest generated integer np.random.randint is one less than the high argument.
To create random floating-point values between 0.0 and 1.0, call np.random.random. For example:
In [0]:
random_floats_between_0_and_1 = np.random.random([6])
print(random_floats_between_0_and_1)
If you want to add or subtract two vectors or matrices, linear algebra requires that the two operands have the same dimensions. Furthermore, if you want to multiply two vectors or matrices, linear algebra imposes strict rules on the dimensional compatibility of operands. Fortunately, NumPy uses a trick called broadcasting to virtually expand the smaller operand to dimensions compatible for linear algebra. For example, the following operation uses broadcasting to add 2.0 to the value of every item in the vector created in the previous code cell:
In [0]:
random_floats_between_2_and_3 = random_floats_between_0_and_1 + 2.0
print(random_floats_between_2_and_3)
The following operation also relies on broadcasting to multiply each cell in a vector by 3:
In [0]:
random_integers_between_150_and_300 = random_integers_between_50_and_100 * 3
print(random_integers_between_150_and_300)
Your goal is to create a simple dataset consisting of a single feature and a label as follows:
feature.label such that: label = (3)(feature) + 4
For example, the first value for label should be:
label = (3)(6) + 4 = 22
In [0]:
feature = ? # write your code here
print(feature)
label = ? # write your code here
print(label)
In [0]:
#@title Double-click to see a possible solution to Task 1.
feature = np.arange(6, 21)
print(feature)
label = (feature * 3) + 4
print(label)
To make your dataset a little more realistic, insert a little random noise into each element of the label array you already created. To be more precise, modify each value assigned to label by adding a different random floating-point value between -2 and +2.
Don't rely on broadcasting. Instead, create a noise array having the same dimension as label.
In [0]:
noise = ? # write your code here
print(noise)
label = ? # write your code here
print(label)
In [0]:
#@title Double-click to see a possible solution to Task 2.
noise = (np.random.random([15]) * 4) - 2
print(noise)
label = label + noise
print(label)