What is Numpy | How to use it Python Tutorial 20

Python Tutorial numpy 

What is numpy in python with Example

 NumPy is a powerful numerical computing library for Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPy is widely used in scientific computing, data analysis, machine learning, and other fields where numerical operations on large datasets are common.

Here’s a brief overview of some key features of NumPy, along with an example:

Creating Arrays:

NumPy provides functions to create arrays easily. You can create arrays from Python lists or use functions like numpy.zeros, numpy.ones, or numpy.arange:

import numpy as np

 Creating a 1D array

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

 Creating a 2D array

arr2d = np.array([[1, 2, 3], [4, 5, 6]])

 Creating a 3D array with zeros

arr3d = np.zeros((2, 3, 4))

 Creating a range of values

arr_range = np.arange(0, 10, 2)

Array Operations:

NumPy allows you to perform various operations on arrays, such as element-wise addition, multiplication, and more:

import numpy as np

 Element-wise addition

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

arr2 = np.array([4, 5, 6])

result_addition = arr1 + arr2

 Element-wise multiplication

result_multiplication = arr1 * arr2

Mathematical Functions:

NumPy provides a wide range of mathematical functions to operate on arrays, such as numpy.sin, numpy.cos, numpy.exp, and more:

import numpy as np

 Applying mathematical functions

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

result_sin = np.sin(arr)

result_exp = np.exp(arr)

Array Indexing and Slicing:

NumPy supports powerful indexing and slicing operations for accessing and manipulating array elements:

import numpy as np

 Array indexing and slicing

arr = np.array([[1, 2, 3], [4, 5, 6]])

element_at_0_1 = arr[0, 1]

subarray = arr[:, 1:]

These are just a few examples of what NumPy can do. Its efficient handling of arrays, broadcasting capabilities, and a rich set of functions make it a fundamental library for numerical computing in Python.

It is often used in combination with other libraries, such as SciPy, Matplotlib, and scikit-learn, to form a comprehensive ecosystem for scientific computing and data analysis.

Numpy Linspace How to use in Python with example

NumPy’s linspace function is used to create an array of evenly spaced values over a specified range. The syntax for numpy.linspace is as follows:

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

Here’s an explanation of the parameters:

start: The starting value of the sequence.

stop: The end value of the sequence.

num: The number of evenly spaced samples to generate. (Default is 50.)

endpoint: If True, stop is the last value in the range. If False, stop is not included. (Default is True.)

retstep: If True, return the step size between values as well. (Default is False.)

dtype: The data type of the output array. If not specified, it will be inferred from the other input arguments.

axis: The axis in the result along which the linspace samples are stored. The default is 0.
%0