A step-by-step beginners guide to use NumPy Library with Python
Python is dynamically typed general purpose programming language and because of its flexible nature it is used extensively in Data Science. And that's where the NumPy Library comes into action. NumPy library is the foundation for most of the Data Analysis and Data Preparation for Machine Learning. It is a core array processing package and it gives us the ability to work with Multi Dimensional array objects.
- Setting Up Environment: We will be using JetBrains PyCharm for programming in python. Install PyCharm on your System and you will be ready to do basic programming in Python. Or you can also use Python Software Foundation's IDLE and install it on your System.
- Installing NumPy on PyCharm:
- Step 1: Launch PyCharm and navigate to Files > Settings or use the shortcut keys Ctrl + Alt + S. It should look like the image below.
- Step 2: Click on the + icon on the right hand side pane to launch the available packages window or simple use the shortcut keys Alt + Insert. It should look like the image below.
- Step 3: Now search for NumPy Package and click on Install Package. This will install the package on your system. It should look like the image below.
- Installing NumPy on IDLE: For installing NumPy on IDLE we will use the pip command from the Command Prompt.
- Step 1: Fire Up your Command Prompt and type the command line pip install numpy and hit enter. This will take some to install NumPy Package for your IDLE.
Creating arrays with NumPy
Now that we are set up with our IDE and NumPy Package, Lets start the fun part Lets Begin Coding...
As NumPy is mainly used for creating homogeneous multidimensional arrays, we will start by learning to create arrays with NumPy.
Methods of creating arrays:
- array() Method: We can create array from regular python Lists or Tuple using the array() method. This will create an array of the type of values specified. We can see in the code below that the datatype of the array created is float because one of our elements is of float type it is also called implicit conversion. Note that all the values in array should be of same datatype, hence implicit conversion occurs.
- linspace() Method: This method creates an array with evenly spaced values within a given interval. In the code below we have specified the starting value 1, the ending value 20 and the total number of values 10. Here, the datatype of array is float because the elements of the resultant array are of float type. The default value to create parts is 50.
- arange() Method: This method also creates an array with evenly spaced values within a given interval but it takes an argument to skip the number of values. In the code below we have specified the starting value 1, the ending value 15 and the number of values to skip 2. This creates an array with a difference of 2 between the consecutive elements.
- logspace() Method: This method also creates an array with evenly spaced values within a given interval. In the code below, we have specified the starting value 1, the ending value 20 and the number of elements 10. This creates an array with 10 elements ranging from 101 to 1020 .
- zeros() Method: This is a very efficient method of creating arrays, this method creates an array of specified size and initializes all the elements with 0. And the array is of float type.
- ones() Method: This method creates an array of specified size and initializes all the elements with 1. And the array is of float type.
Smartcodehub ™ Blog
Place your ads here contact firstname.lastname@example.org now