# 1D `Numpy`

in Python

**Welcome!** This notebook will teach you about using `Numpy`

in the Python Programming Language. By the end of this lab, you'll know what `Numpy`

is and the `Numpy`

operations.

## Table of Contents

## Preparation

Create a Python List as follows:

We can access the data via an index:

We can access each element using a square bracket as follows:

## What is Numpy?

A numpy array is similar to a list. It's usually fixed in size and each element is of the same type. We can cast a list to a numpy array by first importing numpy:

We then cast the list as follows:

Each element is of the same type, in this case integers:

As with lists, we can access each element via a square bracket:

### Type

If we check the type of the array we get **numpy.ndarray**:

As numpy arrays contain data of the same type, we can use the attribute "dtype" to obtain the Data-type of the array’s elements. In this case a 64-bit integer:

We can create a numpy array with real numbers:

When we check the type of the array we get **numpy.ndarray**:

If we examine the attribute `dtype`

we see float 64, as the elements are not integers:

### Assign value

We can change the value of the array, consider the array `c`

:

We can change the first element of the array to 100 as follows:

We can change the 5th element of the array to 0 as follows:

### Slicing

Like lists, we can slice the numpy array, and we can select the elements from 1 to 3 and assign it to a new numpy array `d`

as follows:

We can assign the corresponding indexes to new values as follows:

### Assign Value with List

Similarly, we can use a list to select a specific index. The list ' select ' contains several values:

We can use the list as an argument in the brackets. The output is the elements corresponding to the particular index:

We can assign the specified elements to a new value. For example, we can assign the values to 100 000 as follows:

### Other Attributes

Let's review some basic array attributes using the array `a`

:

The attribute `size`

is the number of elements in the array:

The next two attributes will make more sense when we get to higher dimensions but let's review them. The attribute `ndim`

represents the number of array dimensions or the rank of the array, in this case, one:

The attribute `shape`

is a tuple of integers indicating the size of the array in each dimension:

## Numpy Array Operations

### Array Addition

Consider the numpy array `u`

:

Consider the numpy array `v`

:

We can add the two arrays and assign it to z:

The operation is equivalent to vector addition:

### Array Multiplication

Consider the vector numpy array `y`

:

We can multiply every element in the array by 2:

This is equivalent to multiplying a vector by a scaler:

### Product of Two Numpy Arrays

Consider the following array `u`

:

Consider the following array `v`

:

The product of the two numpy arrays `u`

and `v`

is given by:

### Dot Product

The dot product of the two numpy arrays `u`

and `v`

is given by:

### Adding Constant to a Numpy Array

Consider the following array:

Adding the constant 1 to each element in the array:

The process is summarised in the following animation:

## Mathematical Functions

We can access the value of pie in numpy as follows :

We can create the following numpy array in Radians:

We can apply the function `sin`

to the array `x`

and assign the values to the array `y`

; this applies the sine function to each element in the array:

## Linspace

A useful function for plotting mathematical functions is "linespace". Linespace returns evenly spaced numbers over a specified interval. We specify the starting point of the sequence and the ending point of the sequence. The parameter "num" indicates the Number of samples to generate, in this case 5:

If we change the parameter `num`

to 9, we get 9 evenly spaced numbers over the interval from -2 to 2:

We can use the function line space to generate 100 evenly spaced samples from the interval 0 to 2π:

We can apply the sine function to each element in the array `x`

and assign it to the array `y`

:

## Quiz on 1D Numpy Array

Implement the following vector subtraction in numpy: u-v

Double-click **here** for the solution.

Multiply the numpy array z with -2:

Double-click **here** for the solution.

Consider the list `1, 2, 3, 4, 5`

and `1, 0, 1, 0, 1`

, and cast both lists to a numpy array then multiply them together:

Double-click **here** for the solution.

Double-click **here** for the solution.

Double-click **here** for the solution.

Double-click **here** for the solution.

Double-click **here** for the solution.

## The last exercise!

Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow this article to learn how to share your work.

### About the Authors:

Joseph Santarcangelo is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

Other contributors: Mavis Zhou

Copyright © 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the MIT License.