A Technical Journalist who loves writing about Machine Learning and…
Data science in Python is mostly analytical, so learning it from scratch is a basic thing to start with. The beginner-friendly steps shown here are a kick-start for learning how to program from the basic codes.
One of the main benefits is its extensive set of libraries, a collection of routines and functions that help data scientists perform complex tasks quite effortlessly without writing a single line of code. One such important function is numerical Python aka NumPy which is a fundamental library, well known for high-performance multi-dimensional array and can be used for different mathematical functions like linear algebra, Fourier Transformations, etc. as well as logical operations.
This article takes a lowdown on understanding NumPy and its functions, including steps to create NumPy arrays, indexing, slicing, etc.
Understanding NumPy And How It Works
The easy way to install Python in your system is to download and install a package called Anaconda that contains pre-installed libraries.
Once Anaconda is installed, now you can easily install NumPy on your terminal:
conda install nump
Voila! NumPy is installed. Let’s start by launching Jupyter notebook.
Import NumPy on your Jupyter notebook:
Import numpy as np
Creating a one-dimensional array:
array1 = np.array ( [ 1, 2, 3, 4, 5, 6 ] )
Creating higher dimensional array:
array2 = np.array ( [ [ 1, 2, 3 ] [ 4, 5, 6 ] ] )
Shape of array2:
print ( array2.shape ) # Prints ( 2, 3 )
Changing an element in an array:
array1 [ 0 ] = 9
print ( array1 ) # Prints “ [ 9, 2, 3, 4, 5, 6 ] ”
Using arange( ) built-in function, let’s create NumPy array:
array1 = np.arange ( 0, 10 ) # This generates index value from 0 to 1
We can create Identity Matrix with the given code:
my_matrx = np . eye( 44 ) # here 4 is the number of columns/rows
Generating Random Numbers
An array of random numbers can be generated by using the functions rand(), randn() or randint().
For instance, if we need a one-dimensional array containing 6 objects which are uniformly distributed from 0 to 1, then:
my_rand = np.random.rand (6)
Arithmetic Operations On NumPy: (operating as each element to itself)
array1 = np.arange (1, 12)
array1 * array1
array1 - array1
array1 + array1
array1 / array1
Using Universal Functions In Each Element:
np.sqrt (array1) # Returns square root
np.exp (array1) # Returns the exponential
np.log (array1) # Returns logarithm
np.std (array1) # Returns standard deviation
The array can be indexed and sliced, noted that a slice must be specified for each dimension of the array:
array1 [ 2 ] # Prints 3
array2 [ 1, 1 ] # Prints 5
array2 [ 0, : ] # array ( [ 1, 2, 3 ] ); row 1
array1 ( [ 1 : 3 ] ) # Prints array ( [ 2, 3] )
array1 ( [ 1 : 3 ] ) = [ -2, -3 ] # Prints array ( [ 1, -2, -3, 4, 5, 6 ] )
array1 [ : : ] # array ( [ 1, -2, -3, 4, 5, 6 ] ); lower, upper, step all take the default values
array1 [ : 3 ] # array ( [ 1, -2, -3 ] ); first three elements
array [ : : 2 ] # array ( [ 1, -3, 5 ] ); here the step is 2
We can compute the functions in reverse order using negative numbers:
array1 [ -3 : ] # Prints array ( [4, 5, 6 ] ); the last three numbers
Importing And Exporting Of Files:
np.loadtxt (file.txt) # imports from a text file
np.savetxt (‘file.txt’,arr,delimiter= ’ ’) #writes to a text file
It is always helpful and good to have something extra in the bag. So besides using NumPy, here are some other libraries for Python that you can check out and keep yourself updated.
Register for our upcoming events:
- Meetup: NVIDIA RAPIDS GPU-Accelerated Data Analytics & Machine Learning Workshop, 18th Oct, Bangalore
- Join the Grand Finale of Intel Python HackFury2: 21st Oct, Bangalore
- Machine Learning Developers Summit 2020: 22-23rd Jan, Bangalore | 30-31st Jan, Hyderabad
Enjoyed this story? Join our Telegram group. And be part of an engaging community.
Provide your comments below
What's Your Reaction?
A Technical Journalist who loves writing about Machine Learning and Artificial Intelligence. A lover of music, writing and learning something out of the box. Contact: firstname.lastname@example.org