Python Numpy Tutorial – Starting With Arrays

5Shares
Reading Time: 3 minutes

Python NumPy Tutorial – Arrays

You’re probably wondering, “What is NumPy”? NumPy is a numerical library for Python that allows for extremely fast data generation and handling. It utilizes arrays which can efficiently store data a lot better than the built in python list(arrays) we’re all used to using.

Getting started – Installing NumPy

First we need to install the NumPy library. It can be installed using Python’s standard pip package manager. You can install the packages typing:

python -m pip install numpy

If you’re on windows and are uncomfortable working with commands, check out these pre-built Windows installers.

NumPy Arrays

Alright so now that we have successfully installed numpy, first thing we want to do is import it into our new project.

import numpy as np

NumPy Arrays come in two flavors, Vectors and Matrices. Vectors are one-dimensional arrays and a Matrix is two-dimensional. Lets create our first numpy array.

Array example #1 (With a variable)

list = [1,2,3,4]
np.array(list)

#Output 
[0, 1, 2, 3, 4]

Array example #2

np.array([1,2,3,4])

#Output 
[0, 1, 2, 3, 4]

Both lines will return the same results. We can also work with ranges. For example these two lines return the same output.

Range Array example(Without NumPy)

list(range(0,5))

Range Array example(With NumPy)

np.arange(0,5)

 

NumPy also provides many different functions to create arrays.

np.zeros(4)

#Output:
[0. 0. 0. 0.]

This will create a 1 dimensional array of 4 zeros.

 

np.zeros((3,5))

#Output:
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]

This will create a multidimensional array of 3 rows and 5 columns.

 

There is also np.ones() that will do the same as above. Instead of zeros it will display 1’s.

np.ones((3,5))

#Output:
[[1. 1. 1. 1. 1.] 
[1. 1. 1. 1. 1.] 
[1. 1. 1. 1. 1.]]

 

If for whatever reason you’d like to make an array list with another number, we can use np.full

np.full((3,3), 5)

#Output:
[[5 5 5]
[5 5 5]
[5 5 5]]

 

Np.eye will return a 2D array with ones on the diagonal and zeros elsewhere. Example:

np.eye(6)

#Output:
[[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1.]]

 

Np.linspace will return evenly spaced numbers over a specific interval. Example:

#np.linspace(starting number, stop number, numbers returned)
np.linspace(0,10,5)

#Output: [ 0. 2.5 5. 7.5 10. ]

 

Np.random.random will create an array filled with random values between 0-1.

np.random.rand(2,2)

#Output: 
[[0.88918011 0.38452529]
[0.20915531 0.63032049]]

 

Ok so now lets say you have a huge array and it looks like crap on your screen. A common method we can use is reshape.

For example:

array = np.arange(0,50)
print(array)

#Output: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49]

That looks terrible. Lets reshape it to a 10×5 matrix

array = np.aragne(0,50)
print(array.reshape(10,5))

#Output:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]
[35 36 37 38 39]
[40 41 42 43 44]
[45 46 47 48 49]]

 

If you want to grab the largest value in an array we can user the max() method. We can use min() to return the smallest value

array = np.arange(10)
print(array.max())
# Returns the largest value in the array Output: 9
print(array.argmax())
# Returns the position of the largest value Output: 9
print(array.min())
# Returns the smallest value in the array Output: 0
print(array.argmin())
# Returns the position of the smallest value Output: 0

Thats all the for basics of NumPy arrays.

Thanks for reading and once again feel free to leave a comment below with any suggestions or questions.


Want to get better in Python?

Well come on over and take a look at “Automate the Boring Stuff with Python: Practical Programming for Total Beginners“. If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?

In Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Once you’ve mastered the basics of programming, you’ll create Python programs that effortlessly perform useful and impressive feats of automation to:
–Search for text in a file or across multiple files
–Create, update, move, and rename files and folders
–Search the Web and download online content
–Update and format data in Excel spreadsheets of any size
–Split, merge, watermark, and encrypt PDFs
–Send reminder emails and text notifications
–Fill out online forms

Disclaimer: The programs in this book are written to run on Python 3.

This post “Python Numpy Tutorial – Starting With Arrays” is located under the Programming Category

Learn how to automate logging into facebook using python and selenium!

 

5Shares

admin

Leave a Reply

Your email address will not be published.