Machine Learning News Hubb
Advertisement Banner
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us
Machine Learning News Hubb
No Result
View All Result
Home Machine Learning

NumPy — A Python Library for Machine Learning ( A Beginner to PRO Trainer module) | by Aishwarya Venkat | Sep, 2022

admin by admin
September 8, 2022
in Machine Learning


Do you want to learn about Machine learning libraries used in python and don’t know where to start, then yea you have come to the right place, to begin with, come let’s dive deep into the NumPy library which is used for machine learning with some hands-on training. Here we will begin with the basics of this library following various examples and eventually train you to a pro level.

NumPy is a Python library used for working with arrays. It also has functions for working in the domain of linear algebra, Fourier transform, and matrices. NumPy aims to provide an array object that is up to 50x faster than traditional Python lists. NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.

In machine learning, we encounter large amounts of datasets that are handled with the help of the NumPy library.

I am using google Collaboratory to run the codes and show the outputs.

To use the library, first, we have to import the NumPy library using the below command. We are importing the library in the name of “np”.

NumPy is imported and is ready to use

List vs NumPy — Time taken to complete a task

Here we are creating an array of numbers to which a constant number say 5 is being added to each element of the array and the time taken to perform this task while using list and NumPy is performed and compared. The results show that NumPy accomplishes the task more quickly than lists hence it is more feasible to use NumPy while handling a larger amount of data.

List vs NumPy — Time taken to complete a task

Declaring a NumPy array and printing the array and its type

Here in the below code, ‘np_array’ is the variable that stores the declared NumPy array. The array is printed using the ‘print’ command and its data type is printed using the ‘type’ command which is found to be numpy.nd array (n-dimensional array).

Creating 1D and 2D array of elements

Here, 1D and a 2D array of elements are created and its shape is printed out.

The “.shape” command gives the no. of rows and columns of the input array.

Initial Place holders and matrix creation using NumPy

Initial values are present in the NumPy array. We are creating a 4×5 matrix with all the values as zero. Also, a 3×3 matrix is created with all the values as one. Following that, we create a 4×4 matrix with an array of desired values say 8 using ‘np.full()’ function which takes in two parameters out of which one is row and column size and another one is the desired value.

All values in the matrix is set zero
All values in the matrix are set ones
All values in the matrix are set the same (say here 8)

Create Identity Matrix

Creating an identity matrix where all the diagonal values will be 1 and other values will be zero. We use the ‘np.eye()’ function to create an identity matrix using the NumPy library. It takes in the shape of the matrix as one parameter but you should not enter the rows and column value as the identity matrix has an equal no of rows and columns.

Identity matrix 5×5

Create a NumPy array with Random values

We use the ‘np.random.random()’ function to create a NumPy array of random values. Here in the below example, you can see the array is created with the values generated randomly within the range of 0 and 1.

In order to create an array with random integer values within a specific range we use ‘np.random.randint()’ where it takes the input ranges as a parameter followed by the shape of the matrix as another parameter.

How to create an array of evenly spaced values?

To create an array of evenly spaced values we use the function ‘np.linspace()’ which takes in the starting value and the ending values of the range as the first two parameters followed by the no of values required in the specified range as the third parameter in the function.

An array of evenly spaced values by specifying the no of values required

Another method is to use the function “np.arange()” which takes in the range and the step value for counting between the range to give the evenly spaced values as the output from the specified range. using the step value the jump count is known.

An array of evenly spaced values by specifying the step value

Conversion of list/tuple to NumPy array

Using ‘np.asarray()’ function list of array elements is converted into NumPy array elements.

List to NumPy conversion
Tuple to NumPy array

Analyzing a NumPy array

Here we are analyzing the shape, dimensions, size, and data type of the values present in the NumPy array. The appropriate functions used are shown in the below image of the code.

Analyzing a NumPy array

Mathematical Operations on a NumPy array

When two lists are added using “+” operator mere concatenation of the lists takes place. But in the case of NumPy array addition operation is performed element-wise on the array elements. All the mathematical operations like addition, subtraction, multiplication, and division are performed by using the appropriate operators. Look on to the below code for more clarity.

Also, this can be performed using inbuilt mathematical functions as shown below.

Array Manipulation

The Transpose of an array can be found by two methods.

Method 1 is to use “np.transpose()” function and method 2 is to use array.T to find the transpose of the matrix. The code illustrates the two different ways to find the transpose of the matrix.

Methods to find transpose of a matrix

Reshaping an array

The below code demonstrates how to reshape a NumPy array.

Reshaping an array

If you are interested to learn more about the NumPy library functions and commands you can have a look at these websites for more content:

https://numpy.org/doc/

https://www.w3schools.com/python/numpy/default.asp



Source link

Previous Post

Transformers in Action: Attention Is All You Need | by Soran Ghaderi | Sep, 2022

Next Post

5 dbt Data Model Hacks to Save You Precious Time | by Madison Schott | Sep, 2022

Next Post

5 dbt Data Model Hacks to Save You Precious Time | by Madison Schott | Sep, 2022

Top Programming Languages | 2022 Rating | by Elizaveta Gorelova | Sep, 2022

A Comprehensive Tutorial on Stereo Geometry and Stereo Rectification with Python | by Neeraj Krishna | Sep, 2022

Related Post

Artificial Intelligence

Exploring TensorFlow Model Prediction Issues | by Adam Brownell | Feb, 2023

by admin
February 2, 2023
Machine Learning

Different Loss Functions used in Regression | by Iqra Bismi | Feb, 2023

by admin
February 2, 2023
Machine Learning

How to organize bills? – 3 ways to track bills

by admin
February 2, 2023
Artificial Intelligence

How to decide between Amazon Rekognition image and video API for video moderation

by admin
February 2, 2023
Artificial Intelligence

The Future of AI: GPT-3 vs GPT-4: A Comparative Analysis | by Mohd Saqib | Jan, 2023

by admin
February 2, 2023
Deep Learning

6 Ways To Streamline Tech Hiring With A Recruitment Automation Platform

by admin
February 2, 2023

© 2023 Machine Learning News Hubb All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy and Terms & Conditions.

Navigate Site

  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us

Newsletter Sign Up.

No Result
View All Result
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.