3-3-2-5. Using Built-in Functions to Create ndarrays

In the last video,

we learnt how to create NumPy arrays by converting

existing array like objects such as Python lists,

using NumPy’s array function.

But one great time saving feature of NumPy is

its ability to generate specific kinds of NumPy arrays from nothing,

using just one line of code.

Here, we will see a few of the most useful builtin functions for generating Numpy arrays.

Let’s start by creating a NumPy array of zeroes with a shape that we specify.

We can do this by using NumPy’s zeros function.

This function takes as an argument the shape of the array you want to create.

Passing in the tuple (3,4) gives us a 3 by 4 array of zeros.

By default, this creates an array with the data type float 64.

If you want to use a different data type,

you can change this with the keyword dtype.

Similarly, we can create a NumPy array of ones using this function,

which also takes shape as an argument.

In addition to ones and zeros,

you can create an array filled with any constant value using the full function.

This takes two arguments,

a shape for the array and the constant you want to fill it with.

The full function by default creates an array

with a data type of the value you inputted as a constant.

Here, since we used the Integer 5 as our constant,

this generated an array with the dtype int64.

Use the key word dtype to specify otherwise.

A fundamental array in Linear Algebra is the identity matrix.

A matrix is just another term used to describe

a two dimensional array with rows and columns.

And, an identity matrix is

just a square shaped matrix that has ones along its main diagonal,

and zeros everywhere else.

NumPy’s eye function can be used to create this.

Since all identity matrices are square,

this only takes a single integer as an argument.

Using 5 gives us a 5 x 5 identity matrix.

And its main diagonal which goes from the top

left to the bottom right, is filled with ones.

We can also use Numpy’s diag function to create a diagonal matrix.

This function takes in as input a sequence of

values to use as the main diagonal of a square matrix,

and fills in the rest with zeros.

NumPy also has useful functions to generate arrays with specific numerical ranges.

One useful one is arange,

which creates a one dimensional array of evenly spaced values within a given interval.

This takes three arguments: start, stop, and step.

But we can still use this whether we want to specify one,

two or three arguments.

Let’s see each case.

When only one integer is specified,

arange uses this as a stop argument,

and generates an array of integers from zero to that integer minus one.

The stop argument is exclusive,

which is why we need to subtract one.

For example, arange 10 gives us an array from zero to 10 minus one which is nine.

When used with two arguments,

arange uses the first as a start argument,

and the second as a stop argument.

The start is inclusive,

and the stop is exclusive.

Arange (4,10) gives us an array from four to nine.

When you use the three arguments,

arange generates an array from the first integer to the second minus one,

evenly spaced by the third.

This third argument, the step,

is the distance between any two values in this array.

When we specified only one or two arguments in arange earlier,

step defaulted to one.

Even though NumPy’s arange function allows for a non integer steps such as 0.3,

the output is usually inconsistent due to finite floating point precision.

For this reason, when we want non integer steps,

it’s usually better to use a different NumPy function, linspace.

This takes three arguments, start, stop,

and n. This returns n evenly spaced numbers from start to stop,

both start and stop being inclusive.

Unlike arange, Linspace requires at least two arguments for start and stop.

If n is not specified,

it defaults to 50.

Let’s see some examples.

Here’s a rank one array that has 10 numbers evenly spaced from zero to 25.

Again, note that both the start and stop points are inclusive.

However, you can let the stop endpoint of the interval be

excluded just like it is in the arange function,

if you set the keyword endpoint to false like this.

As you can see, because we have excluded the endpoint 25,

the spacing between the values had to change in

order to fit 10 evenly spaced numbers in the given interval.

So far, we’ve only used the functions arange and linspace to create rank 1 arrays.

However, we can use these functions to create rank 2 arrays of

any shape by combining them with NumPy’s reshape function.

This function converts any NumPy array into a specified shape.

It’s important to note that the new shape specified here,

should be compatible with a number of elements in the array.

For example, you can convert a rank one array

with 20 elements into a 4 by 5 rank two array,

or a 10 by 2 array,

since both of these rank two arrays still have 20 elements.

However, you can’t reshape this to a 5 by 5 array,

since this rank two array would have 25 elements,

which is greater than the number of elements in the original NumPy array.

One great feature about NumPy is that some functions can also be applied as methods.

This allows us to apply different functions in sequence,

in just one line of code.

NumPy array methods are similar to

its attributes in that they are both applied using dot notation.

Let’s see how we can accomplish the same result as this example using one line of code.

This gives us the same result.

Notice that when using reshape as a method,

we don’t need to pas in the array as an argument.

Similarly, we can also use reshape to create rank two arrays with the linspace function.

Lastly, we are going to create NumPy arrays that contain random numbers.

Often in machine learning,

you need to create random matrices.

For example, when initializing the weights of a neural network,

NumPy offers a variety of

random functions to help us create random NumPy arrays of any shape.

Let’s start by using NumPy’s random function to create an array of a given shape,

with random floats between zero and one,

where zero is inclusive,

and one is exclusive.

The following functions including this random function,

are contained in NumPy’s random module.

So we type np.random to access this module,

and then.random to access the function in the module.

NumPy also let’s us create NumPy arrays

that contain random integers within a particular interval.

We can use the function randint to do this.

This takes three arguments, the lower bound,

inclusive, and the upper bound,

exclusive, and the shape.

In some cases, you may need to create NumPy arrays

with random numbers that satisfy certain statistical properties.

For example, you may want the random numbers in the array to have an average of zero.

NumPy allows you to create random arrays with

numbers drawn from various probability distributions.

The function np.random.normal for example,

creates an array with the given shape that

contains random numbers picked from a normal distribution,

with a given mean and standard deviation.

This creates a 1000 by 1000 array of

random floats drawn from a normal distribution with the mean of zero,

and a standard deviation of 0.1.

As we can see, the average of the random numbers in the array is very close to zeo,

and the standard deviation is also very close to 0.1.

Both the maximum and minimum values next are symmetric about zero, the average.

And we have about the same number of positive and negative integers.

지난 영상에서는

변환하여 NumPy 배열을 만드는 방법을 배웠습니다.

Python 목록과 같은 객체와 같은 기존 배열,

NumPy의 배열 기능을 사용합니다.

그러나 NumPy의 한 가지 훌륭한 시간 절약 기능은

무에서 특정 종류의 NumPy 배열을 생성하는 기능,

단 한 줄의 코드를 사용합니다.

여기에서 Numpy 배열을 생성하는 데 가장 유용한 몇 가지 내장 함수를 볼 수 있습니다.

우리가 지정한 모양으로 0으로 구성된 NumPy 배열을 만드는 것으로 시작하겠습니다.

NumPy의 0 함수를 사용하여 이를 수행할 수 있습니다.

이 함수는 생성하려는 배열의 모양을 인수로 취합니다.

튜플(3,4)을 전달하면 0의 3 x 4 배열이 제공됩니다.

기본적으로 이것은 데이터 유형이 float 64인 배열을 생성합니다.

다른 데이터 유형을 사용하려면

dtype 키워드로 이것을 변경할 수 있습니다.

마찬가지로 이 함수를 사용하여 NumPy 배열을 생성할 수 있습니다.

이것은 또한 인수로 형태를 취합니다.

1과 0 외에도

full 함수를 사용하여 상수 값으로 채워진 배열을 만들 수 있습니다.

이것은 두 가지 인수를 취합니다.

배열의 모양과 채우려는 상수입니다.

전체 기능은 기본적으로 배열을 생성합니다.

입력한 값의 데이터 유형을 상수로 사용합니다.

여기서 정수 5를 상수로 사용했기 때문에

이것은 dtype이 int64인 배열을 생성했습니다.

달리 지정하려면 키워드 dtype을 사용하십시오.

선형 대수학의 기본 배열은 단위 행렬입니다.

행렬은 설명하는 데 사용되는 또 다른 용어일 뿐입니다.

행과 열이 있는 2차원 배열.

그리고, 단위 행렬은

주대각선을 따라 1이 있는 정사각형 모양의 행렬,

다른 곳에서는 0입니다.

NumPy의 눈 기능을 사용하여 이를 생성할 수 있습니다.

모든 항등행렬은 정사각형이므로,

이것은 하나의 정수만 인수로 취합니다.

5를 사용하면 5 x 5 단위 행렬이 생성됩니다.

그리고 상단에서 오는 주요 대각선

왼쪽에서 오른쪽 아래로 채워집니다.

Numpy의 diag 함수를 사용하여 대각 행렬을 만들 수도 있습니다.

이 함수는 다음의 시퀀스를 입력으로 받습니다.

정방 행렬의 주대각선으로 사용할 값,

나머지는 0으로 채웁니다.

NumPy에는 특정 숫자 범위의 배열을 생성하는 유용한 기능도 있습니다.

유용한 것 중 하나는 범위입니다.

주어진 간격 내에서 균일한 간격의 값으로 구성된 1차원 배열을 생성합니다.

시작, 중지 및 단계의 세 가지 인수가 필요합니다.

그러나 우리는 이것을 지정하고 싶은지 여부에 관계없이 이것을 사용할 수 있습니다.

두세 가지 인수.

각각의 경우를 살펴보자.

하나의 정수만 지정된 경우,

범위는 이것을 중지 인수로 사용합니다.

0부터 해당 정수에서 1까지의 정수 배열을 생성합니다.

stop 인수는 배타적이며,

이것이 우리가 하나를 빼야 하는 이유입니다.

예를 들어, arange 10은 0에서 10에서 1을 뺀 9까지의 배열을 제공합니다.

두 개의 인수와 함께 사용할 때,

범위는 첫 번째 인수를 시작 인수로 사용합니다.

두 번째는 stop 인수로 사용됩니다.

시작은 포괄적이고,

그리고 정류장은 배타적입니다.

Arange(4,10)는 4에서 9까지의 배열을 제공합니다.

세 가지 인수를 사용할 때,

범위는 첫 번째 정수에서 두 번째 빼기 1까지의 배열을 생성하고,

세 번째 간격으로 균등하게 배치됩니다.

이 세 번째 인수인 단계,

이 배열의 두 값 사이의 거리입니다.

이전에 범위에서 하나 또는 두 개의 인수만 지정했을 때,

기본값은 1입니다.

NumPy의 범위 함수는 0.3과 같은 정수가 아닌 단계를 허용하지만,

유한 부동 소수점 정밀도로 인해 출력은 일반적으로 일관성이 없습니다.

이러한 이유로 정수가 아닌 단계를 원할 때

일반적으로 다른 NumPy 함수인 linspace를 사용하는 것이 좋습니다.

이것은 세 개의 인수, 시작, 중지,

그리고 n. 이것은 처음부터 끝까지 n개의 균등한 간격의 숫자를 반환합니다.

둘 다 포함되기 시작하고 중지합니다.

범위와 달리 Linspace에는 시작 및 중지에 대해 최소 두 개의 인수가 필요합니다.

n을 지정하지 않으면

기본값은 50입니다.

몇 가지 예를 살펴보겠습니다.

다음은 0에서 25까지 균등한 간격으로 10개의 숫자가 있는 1순위 배열입니다.

다시 말하지만 시작 지점과 중지 지점이 모두 포함되어 있습니다.

그러나 간격의 중지 끝점이

범위 함수와 마찬가지로 제외됩니다.

이와 같이 키워드 endpoint를 false로 설정하면.

보시다시피 끝점 25를 제외했기 때문에

값 사이의 간격은 다음에서 변경되어야 했습니다.

주어진 간격에 10개의 균일한 간격의 숫자를 맞추도록 합니다.

지금까지는 1순위 배열을 생성하기 위해 range 및 linspace 함수만 사용했습니다.

그러나 이러한 함수를 사용하여 2순위 배열을 생성할 수 있습니다.

NumPy의 모양 변경 기능과 결합하여 모든 모양을 만들 수 있습니다.

이 함수는 모든 NumPy 배열을 지정된 모양으로 변환합니다.

여기에 지정된 새 모양은

배열의 여러 요소와 호환되어야 합니다.

예를 들어, 1순위 배열을 변환할 수 있습니다.

20개의 요소를 4 x 5 랭크 2 배열로,

또는 10 x 2 배열,

이 두 순위 배열 모두에는 여전히 20개의 요소가 있기 때문입니다.

그러나 이것을 5×5 배열로 바꿀 수는 없습니다.

이 2순위 배열에는 25개의 요소가 있으므로

이는 원래 NumPy 배열의 요소 수보다 큽니다.

NumPy의 한 가지 훌륭한 기능은 일부 기능을 메서드로 적용할 수도 있다는 것입니다.

이렇게 하면 다른 기능을 순서대로 적용할 수 있습니다.

단 한 줄의 코드로.

NumPy 배열 방법은 다음과 유사합니다.

둘 다 점 표기법을 사용하여 적용된다는 점에서 속성입니다.

한 줄의 코드를 사용하여 이 예제와 동일한 결과를 얻을 수 있는 방법을 살펴보겠습니다.

이것은 우리에게 동일한 결과를 제공합니다.

reshape를 메소드로 사용할 때,

배열을 인수로 전달할 필요가 없습니다.

마찬가지로 reshape를 사용하여 linspace 함수로 2순위 배열을 생성할 수도 있습니다.

마지막으로 난수를 포함하는 NumPy 배열을 만들 것입니다.

흔히 머신러닝에서는

난수 행렬을 생성해야 합니다.

예를 들어, 신경망의 가중치를 초기화할 때,

NumPy는 다양한

임의의 모양의 임의의 NumPy 배열을 생성하는 데 도움이 되는 임의의 함수.

NumPy의 random 함수를 사용하여 주어진 모양의 배열을 만드는 것으로 시작하겠습니다.

0과 1 사이의 임의의 부동 소수점으로,

0이 포함된 경우

그리고 하나는 독점적입니다.

이 랜덤 함수를 포함한 다음 함수는,

NumPy의 random 모듈에 포함되어 있습니다.

따라서 이 모듈에 액세스하기 위해 np.random을 입력합니다.

then.random을 사용하여 모듈의 함수에 액세스합니다.

NumPy는 또한 NumPy 배열을 생성해 보겠습니다.

특정 간격 내에 임의의 정수를 포함합니다.

randint 함수를 사용하여 이를 수행할 수 있습니다.

이것은 세 개의 인수를 취합니다. 하한,

포함하고 상한,

독점, 그리고 모양.

경우에 따라 NumPy 배열을 생성해야 할 수도 있습니다.

특정 통계적 속성을 만족하는 난수.

예를 들어, 배열의 난수가 평균이 0이 되도록 할 수 있습니다.

NumPy를 사용하면 다음을 사용하여 임의의 배열을 만들 수 있습니다.

다양한 확률 분포에서 추출한 숫자.

예를 들어, np.random.normal 함수,

주어진 모양으로 배열을 만듭니다.

정규 분포에서 선택한 난수를 포함하고,

주어진 평균과 표준편차로

이것은 1000 x 1000 배열을 생성합니다.

평균이 0인 정규 분포에서 추출한 임의의 부동 소수점,

및 0.1의 표준 편차.

보시다시피, 배열의 난수의 평균은 zeo에 매우 가깝습니다.

표준 편차도 0.1에 매우 가깝습니다.

다음 최대값과 최소값은 모두 평균인 0에 대해 대칭입니다.

그리고 우리는 거의 같은 수의 양의 정수와 음의 정수를 가지고 있습니다.

We strongly encourage you to type the commands that you have learned in this demo. However, the notebook file demonstrated in the video above, Using Built-in Functions to Create ndarrays.ipynb, is available at the bottom of this page.

One great time-saving feature of NumPy is its ability to create ndarrays using built-in functions. These functions allow us to create certain kinds of ndarrays with just one line of code. Below we will see a few of the most useful built-in functions for creating ndarrays that you will come across when doing AI programming.

Let’s start by creating an ndarray with a specified shape that is full of zeros. We can do this by using the np.zeros() function. The function np.zeros(shape) creates an ndarray full of zeros with the given shape. So, for example, if you wanted to create a rank 2 array with 3 rows and 4 columns, you will pass the shape to the function in the form of (rows, columns), as in the example below:

Example 1. Create a Numpy array of zeros with a desired shape

# We create a 3 x 4 ndarray full of zeros. 
X = np.zeros((3,4))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

X =
[[ 0. 0. 0. 0.]
 [ 0. 0. 0. 0.]
 [ 0. 0. 0. 0.]]

X has dimensions: (3, 4)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: float64

As we can see, the np.zeros() function creates by default an array with dtype float64. If desired, the data type can be changed by using the keyword dtype.

Similarly, we can create an ndarray with a specified shape that is full of ones. We can do this by using the np.ones() function. Just like the np.zeros() function, the np.ones() function takes as an argument the shape of the ndarray you want to make. Let’s see an example:

Example 2. Create a Numpy array of ones

# We create a 3 x 2 ndarray full of ones. 
X = np.ones((3,2))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype) 

X =
[[ 1. 1.]
 [ 1. 1.]
 [ 1. 1.]]

X has dimensions: (3, 2)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: float64

As we can see, thenp.ones() function also creates by default an array with dtype float64. If desired, the data type can be changed by using the keyword dtype.

We can also create an ndarray with a specified shape that is full of any number we want. We can do this by using the np.full() function. The np.full(shape, constant value) function takes two arguments. The first argument is the shape of the ndarray you want to make and the second is the constant value you want to populate the array with. Let’s see an example:

Example 3. Create a Numpy array of constants

# We create a 2 x 3 ndarray full of fives. 
X = np.full((2,3), 5) 

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)  

X =
[[5 5 5]
 [5 5 5]]

X has dimensions: (2, 3)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: int64

The np.full() function creates by default an array with the same data type as the constant value used to fill in the array. If desired, the data type can be changed by using the keyword dtype.

As you will learn later, a fundamental array in Linear Algebra is the Identity Matrix. An Identity matrix is a square matrix that has only 1s in its main diagonal and zeros everywhere else. The function np.eye(N) creates a square N x N ndarray corresponding to the Identity matrix. Since all Identity Matrices are square, the np.eye() function only takes a single integer as an argument. Let’s see an example:

Example 4 a. Create a Numpy array of an Identity matrix

# We create a 5 x 5 Identity matrix. 
X = np.eye(5)

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)  

X =
[[ 1. 0. 0. 0. 0.]
 [ 0. 1. 0. 0. 0.]
 [ 0. 0. 1. 0. 0.]
 [ 0. 0. 0. 1. 0.]
 [ 0. 0. 0. 0. 1.]]

X has dimensions: (5, 5)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: float64

As we can see, the np.eye() function also creates by default an array with dtype float64. If desired, the data type can be changed by using the keyword dtype. You will learn all about Identity Matrices and their use in the Linear Algebra section of this course. We can also create diagonal matrices by using the np.diag() function. A diagonal matrix is a square matrix that only has values in its main diagonal. The np.diag() function creates an ndarray corresponding to a diagonal matrix , as shown in the example below:

Example 4 b. Create a Numpy array of constants

# Create a 4 x 4 diagonal matrix that contains the numbers 10,20,30, and 50
# on its main diagonal
X = np.diag([10,20,30,50])

# We print X
print()
print('X = \n', X)
print()

X =
[[10 0 0 0]
 [ 0 20 0 0]
 [ 0 0 30 0]
 [ 0 0 0 50]]

numpy.arange

Syntax:

numpy.arange([start, ]stop, [step, ]dtype=None)

It returns evenly spaced values within a given interval. Details about the optional arguments are available here.

NumPy also allows you to create ndarrays that have evenly spaced values within a given interval. NumPy’s np.arange() function is very versatile and can be used with either one, two, or three arguments. Below we will see examples of each case and how they are used to create different kinds of ndarrays.

Let’s start by using np.arange() with only one argument. When used with only one argument, np.arange(N) will create a rank 1 ndarray with consecutive integers between 0 and N - 1. Therefore, notice that if I want an array to have integers between 0 and 9, I have to use N = 10, NOT N = 9, as in the example below:

Example 5. Create a Numpy array of evenly spaced values in a given range, using arange(stop_val)

# We create a rank 1 ndarray that has sequential integers from 0 to 9
x = np.arange(10)
​
# We print the ndarray
print()
print('x = ', x)
print()

# We print information about the ndarray
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype) 

x = [0 1 2 3 4 5 6 7 8 9]

x has dimensions: (10,)
x is an object of type: class ‘numpy.ndarray’
The elements in x are of type: int64

When used with two arguments, np.arange(start,stop) will create a rank 1 ndarray with evenly spaced values within the half-open interval [start, stop). This means the evenly spaced numbers will include start but exclude stop. Let’s see an example

Example 6. Create a Numpy array using arange(start_val, stop_val)

# We create a rank 1 ndarray that has sequential integers from 4 to 9. 
x = np.arange(4,10)

# We print the ndarray
print()
print('x = ', x)
print()

# We print information about the ndarray
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype) 

x = [4 5 6 7 8 9]

x has dimensions: (6,)
x is an object of type: class ‘numpy.ndarray’
The elements in x are of type: int64

As we can see, the function np.arange(4,10) generates a sequence of integers with 4 inclusive and 10 exclusive.

Finally, when used with three arguments, np.arange(start,stop,step) will create a rank 1 ndarray with evenly spaced values within the half-open interval [start, stop) with step being the distance between two adjacent values. Let’s see an example:

Example 7. Create a Numpy array using arange(start_val, stop_val, step_size)

# We create a rank 1 ndarray that has evenly spaced integers from 1 to 13 in steps of 3.
x = np.arange(1,14,3)

# We print the ndarray
print()
print('x = ', x)
print()

# We print information about the ndarray
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype) 

x = [ 1 4 7 10 13]

x has dimensions: (5,)
x is an object of type: class ‘numpy.ndarray’
The elements in x are of type: int64

We can see that x has sequential integers between 1 and 13 but the difference between all adjacent values is 3.

numpy.linspace

Syntax:

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

It returns num evenly spaced values calculated over the interval [start, stop]. Details about the optional arguments are available here.

Even though the np.arange() function allows for non-integer steps, such as 0.3, the output is usually inconsistent, due to the finite floating point precision. For this reason, in the cases where non-integer steps are required, it is usually better to use the function np.linspace(). The np.linspace(start, stop, N) function returns N evenly spaced numbers over the closed interval [start, stop]. This means that both the start and thestop values are included. We should also note the np.linspace() function needs to be called with at least two arguments in the form np.linspace(start,stop). In this case, the default number of elements in the specified interval will be N= 50. The reason np.linspace() works better than the np.arange() function, is that np.linspace() uses the number of elements we want in a particular interval, instead of the step between values. Let’s see some examples:

Example 8. Create a Numpy array using linspace(start, stop, n), with stop inclusive.

# We create a rank 1 ndarray that has 10 integers evenly spaced between 0 and 25.
x = np.linspace(0,25,10)

# We print the ndarray
print()
print('x = \n', x)
print()

# We print information about the ndarray
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype) 

x = [ 0. 2.77777778 5.55555556 8.33333333 11.11111111 13.88888889 16.66666667 19.44444444 22.22222222 25. ]

x has dimensions: (10,)
x is an object of type: class ‘numpy.ndarray’
The elements in x are of type: float64

As we can see from the above example, the function np.linspace(0,25,10) returns an ndarray with 10 evenly spaced numbers in the closed interval [0, 25]. We can also see that both the start and end points, 0 and 25 in this case, are included. However, you can let the endpoint of the interval be excluded (just like in the np.arange() function) by setting the keyword endpoint = False in the np.linspace() function. Let’s create the same x ndarray we created above but now with the endpoint excluded:

Example 9. Create a Numpy array using linspace(start, stop, n), with stop excluded.

# We create a rank 1 ndarray that has 10 integers evenly spaced between 0 and 25,
# with 25 excluded.
x = np.linspace(0,25,10, endpoint = False)

# We print the ndarray
print()
print('x = ', x)
print()

# We print information about the ndarray
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype) 

x = [ 0. 2.5 5. 7.5 10. 12.5 15. 17.5 20. 22.5]

x has dimensions: (10,)
x is an object of type: class ‘numpy.ndarray’
The elements in x are of type: float64

As we can see, because we have excluded the endpoint, the spacing between values had to change in order to fit 10 evenly spaced numbers in the given interval.

numpy.reshape – This is a Function.

Syntax:

numpy.reshape(array, newshape, order='C')[source]

It gives a new shape to an array without changing its data. More details about the arguments are available here.

So far, we have only used the built-in functions np.arange() and np.linspace() to create rank 1 ndarrays. However, we can use these functions to create rank 2 ndarrays of any shape by combining them with the np.reshape() function. The np.reshape(ndarray, new_shape) function converts the given ndarray into the specified new_shape. It is important to note that the new_shape should be compatible with the number of elements in the given ndarray. For example, you can convert a rank 1 ndarray with 6 elements, into a 3 x 2 rank 2 ndarray, or a 2 x 3 rank 2 ndarray, since both of these rank 2 arrays will have a total of 6 elements. However, you can’t reshape the rank 1 ndarray with 6 elements into a 3 x 3 rank 2 ndarray, since this rank 2 array will have 9 elements, which is greater than the number of elements in the original ndarray. Let’s see some examples:

Example 10. Create a Numpy array by feeding the output of arange() function as an argument to the reshape() function.

# We create a rank 1 ndarray with sequential integers from 0 to 19
x = np.arange(20)

# We print x
print()
print('Original x = ', x)
print()

# We reshape x into a 4 x 5 ndarray 
x = np.reshape(x, (4,5))

# We print the reshaped x
print()
print('Reshaped x = \n', x)
print()

# We print information about the reshaped x
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype) 

Original x = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

Reshaped x =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

x has dimensions: (4, 5)
x is an object of type: class ‘numpy.ndarray’
The elements in x are of type: int64

numpy.ndarray.reshape – This one is a Method.

Syntax:

ndarray.reshape(shape, order='C')

It returns an array containing the same data with a new shape. More details about the arguments are available here.

Need clarity between a Method versus Function? – Refer to this discussion on StackOverflow

One great feature about NumPy, is that some functions can also be applied as methods. This allows us to apply different functions in sequence in just one line of code. ndarray methods are similar to ndarray attributes in that they are both applied using dot notation (.). Let’s see how we can accomplish the same result as in the above example, but in just one line of code:

Example 11. Create a Numpy array by calling the reshape() function from the output of arange() function.

# We create a a rank 1 ndarray with sequential integers from 0 to 19 and
# reshape it to a 4 x 5 array 
Y = np.arange(20).reshape(4, 5)

# We print Y
print()
print('Y = \n', Y)
print()

# We print information about Y
print('Y has dimensions:', Y.shape)
print('Y is an object of type:', type(Y))
print('The elements in Y are of type:', Y.dtype) 

Y =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

Y has dimensions: (4, 5)
Y is an object of type: class ‘numpy.ndarray’
The elements in Y are of type: int64

As we can see, we get the exact same result as before. Notice that when we use reshape() as a method, it’s applied as ndarray.reshape(new_shape). This converts the ndarray into the specified shape new_shape. As before, it is important to note that the new_shape should be compatible with the number of elements in ndarray. In the example above, the function np.arange(20) creates an ndarray and serves as the ndarray to be reshaped by the reshape() method. Therefore, when using reshape() as a method, we don’t need to pass the ndarray as an argument to the reshape() function, instead we only need to pass the new_shape argument.

In the same manner, we can also combine reshape() with np.linspace() to create rank 2 arrays, as shown in the next example.

Example 12. Create a rank 2 Numpy array by using the reshape() function.

# We create a rank 1 ndarray with 10 integers evenly spaced between 0 and 50,
# with 50 excluded. We then reshape it to a 5 x 2 ndarray
X = np.linspace(0,50,10, endpoint=False).reshape(5,2)

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

X =
 [[ 0. 5.]
 [ 10. 15.]
 [ 20. 25.]
 [ 30. 35.]
 [ 40. 45.]]

X has dimensions: (5, 2)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: float64

The last type of ndarrays we are going to create are random ndarrays. Random ndarrays are arrays that contain random numbers. Often in Machine Learning, you need to create random matrices, for example, when initializing the weights of a Neural Network. NumPy offers a variety of random functions to help us create random ndarrays of any shape.

Let’s start by using the np.random.random(shape) function to create an ndarray of the given shape with random floats in the half-open interval [0.0, 1.0).

Example 13. Create a Numpy array using the numpy.random.random() function.

# We create a 3 x 3 ndarray with random floats in the half-open interval [0.0, 1.0).
X = np.random.random((3,3))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in x are of type:', X.dtype)

X =
[[ 0.12379926 0.52943854 0.3443525 ]
 [ 0.11169547 0.82123909 0.52864397]
 [ 0.58244133 0.21980803 0.69026858]]

X has dimensions: (3, 3)
X is an object of type: class ‘numpy.ndarray’
The elements in x are of type: float64

NumPy also allows us to create ndarrays with random integers within a particular interval. The function np.random.randint(start, stop, size = shape) creates an ndarray of the given shape with random integers in the half-open interval [start, stop). Let’s see an example:

Example 14. Create a Numpy array using the numpy.random.randint() function.

# We create a 3 x 2 ndarray with random integers in the half-open interval [4, 15).
X = np.random.randint(4,15,size=(3,2))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

X =
[[ 7 11]
 [ 9 11]
 [ 6 7]]

X has dimensions: (3, 2)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: int64

In some cases, you may need to create ndarrays with random numbers that satisfy certain statistical properties. For example, you may want the random numbers in the ndarray to have an average of 0. NumPy allows you create random ndarrays with numbers drawn from various probability distributions. The function np.random.normal(mean, standard deviation, size=shape), for example, creates an ndarray with the given shape that contains random numbers picked from a normal (Gaussian) distribution with the given mean and standard deviation. Let’s create a 1,000 x 1,000 ndarray of random floating point numbers drawn from a normal distribution with a mean (average) of zero and a standard deviation of 0.1.

Example 15. Create a Numpy array of “Normal” distributed random numbers, using the numpy.random.normal() function.

# We create a 1000 x 1000 ndarray of random floats drawn from normal (Gaussian) distribution
# with a mean of zero and a standard deviation of 0.1.
X = np.random.normal(0, 0.1, size=(1000,1000))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
print('The elements in X have a mean of:', X.mean())
print('The maximum value in X is:', X.max())
print('The minimum value in X is:', X.min())
print('X has', (X < 0).sum(), 'negative numbers')
print('X has', (X > 0).sum(), 'positive numbers')

X =
[[ 0.04218614 0.03247225 -0.02936003 …, 0.01586796 -0.05599115 -0.03630946]
 [ 0.13879995 -0.01583122 -0.16599967 …, 0.01859617 -0.08241612 0.09684025]
 [ 0.14422252 -0.11635985 -0.04550231 …, -0.09748604 -0.09350044 0.02514799]
 …,
 [-0.10472516 -0.04643974 0.08856722 …, -0.02096011 -0.02946155 0.12930844]
 [-0.26596955 0.0829783 0.11032549 …, -0.14492074 -0.00113646 -0.03566034]
 [-0.12044482 0.20355356 0.13637195 …, 0.06047196 -0.04170031 -0.04957684]]

X has dimensions: (1000, 1000)
X is an object of type: class ‘numpy.ndarray’
The elements in X are of type: float64
The elements in X have a mean of: -0.000121576684405
The maximum value in X is: 0.476673923106
The minimum value in X is: -0.499114224706
X has 500562 negative numbers
X has 499438 positive numbers

As we can see, the average of the random numbers in the ndarray is close to zero, both the maximum and minimum values in X are symmetric about zero (the average), and we have about the same amount of positive and negative numbers.

Additional Resource

Supporting Materials

%d 블로거가 이것을 좋아합니다: