# Numpy python

## Numpy is a library in Python

NumPy (Numerical Python) is a fundamental library in Python for numerical computing. It provides support for working with large, multi-dimensional arrays and matrices of data, as well as a wide range of mathematical functions to operate on these arrays. NumPy is a cornerstone of the scientific computing ecosystem in Python and is extensively used in various fields such as data analysis, machine learning, scientific research, and engineering.

### Here are some key features and components of NumPy:

Multi-dimensional Arrays: NumPy's primary data structure is the numpy.ndarray (n-dimensional array), which allows you to store and manipulate data efficiently. These arrays can have any number of dimensions, making them suitable for various applications, from simple vectors and matrices to higher-dimensional data like images and tensors.

Efficiency: NumPy is implemented in C and Fortran, which makes it significantly faster than equivalent operations performed using Python lists. It also provides functions for element-wise operations, broadcasting, and vectorized computations, which further enhance performance.

Mathematical Functions: NumPy offers a rich set of mathematical functions for performing operations like arithmetic, trigonometry, linear algebra, statistics, and more. These functions can be applied element-wise or across arrays, making it easy to perform complex computations on large datasets.

Indexing and Slicing: You can access and manipulate elements of NumPy arrays using indexing and slicing, similar to Python lists. NumPy's indexing capabilities are more versatile and efficient, making it easier to work with specific elements or sections of an array.

Broadcasting: NumPy supports broadcasting, which allows you to perform operations on arrays of different shapes, automatically aligning and extending smaller arrays to match the shape of larger ones. This feature simplifies many operations and makes code more readable.

Random Number Generation: NumPy includes a random number generation module (numpy.random) for generating random data with various distributions. This is particularly useful for simulations, statistical analysis, and machine learning.

Integration with Other Libraries: NumPy seamlessly integrates with other popular Python libraries, such as SciPy (for scientific computing), Matplotlib (for data visualization), and scikit-learn (for machine learning), creating a powerful ecosystem for scientific and data analysis tasks.

Memory Management: NumPy provides efficient memory management for large arrays, allowing you to allocate, resize, and manage memory more effectively than Python lists.

Here's a simple example of how to use NumPy to create an array and perform some basic operations:

python
Copy code
import numpy as np

# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])

# Perform element-wise operations
arr_squared = arr ** 2
arr_sum = np.sum(arr)

# Indexing and slicing
sub_array = arr[2:4]

print(arr_squared)
print(arr_sum)
print(sub_array)
NumPy is an essential tool for numerical and scientific computing in Python, offering high-performance data manipulation and mathematical operations, which are crucial for a wide range of applications.
What is numpy?
import numpy as np
How to create an Arry function in Python?
[6, 7.5, 8, 0, 1]
How to create an equal-length list function in Python?
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
How to create a zero matrix in Python?
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000]],
[[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000]]])
How to see data type in Python?
[16., 25., 36.]])
[0., 0., 0.]])
[0.25 , 0.2 , 0.16666667]])
[2. , 2.23606798, 2.44948974]])
How to see individual elements in Python?
9])
In higher dimensional arrays, you have
many more options.
In a two-dimensional
array, the elements at each index are no longer scalars
but rather one-dimensional arrays
Numpy is the most required library of Python is numpy. Numpy is a core library for computing with Python that provides a foundation for all computational libraries and its usage patterns is a fundamental skill for Python.
import pandas as pd
import matplotlib.pyplot as plt
Describe Numpy arry:
# ARRYS AND VECTORISED COMPUTATION
NumPy, short for Numerical Python, is the fundamental package. required for high-performance scientific computing and data analysis. ndarray, a fast and space-efficient multidimensional array providing vectorized arithmetic operations and sophisticated broadcasting capabilities • Standard mathematical functions for fast operations on entire arrays of data without having to write loops • Tools for reading/writing array data to disk and working with memory-mapped files • Linear algebra, random number generation, and Fourier transform capabilities • Tools for integrating code written in C, C++, and Fortran
# Creating ndarrays
#The easiest way to create an array is
to use the array function
data1 = [67.5801]
data1
data1*10
data1+data1
[6, 7.5, 8, 0, 1, 6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1
array([6. , 7.5, 8. , 0. , 1. ])
#Nested sequences, like a list of equal-length
lists, will be converted
into a multidimensional array:
data2 = [, ]
arr2 = np.array(data2)
arr2
arr2.ndim
2
arr2.shape
(2, 4)
arr1.dtype
dtype('float64')
arr2.dtype
dtype('int64')
#In addition to np.array, there are a
number of other functions for
creating new arrays.
# As examples, zeros and ones create
arrays of 0’s or 1’s, respectively,
with a given length or shape. empty
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((36))
array([[0., 0., 0., 0., 0., 0.],
np.empty((232))
array([[[4.669867e-310, 0.000000e+000],
#arange is an array-valued version of the built-in
Python range function:
np.arange(15)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
array Convert input data (list, tuple, array, or other sequence type)
to an ndarray either by inferring a dtype or explicitly specifying a dtype.
Copies the input data by default. asarray Convert input to ndarray,
but do not copy if the input is already an ndarray arange
Like the built-in range but returns an ndarray instead of a list.
ones, ones_like Produce an array of all 1’s with the given
shape and dtype. ones_like takes another array and produces
a ones array of the same shape and dtype. zeros, zeros_like
Like ones and ones_like but producing arrays of 0’s instead
arr1 = np.array(, dtype=np.float64)
arr1
array([1., 2., 3.])
arr2 = np.array(, dtype=np.int32)
arr2
array([1, 2, 3], dtype=int32)
arr1.dtype
dtype('float64')
arr2.dtype
dtype('int32')
arr = np.array()
arr.dtype
dtype('int64')
float_arr = arr.astype(np.float64)
float_arr
array([1., 2., 3., 4., 5.])
```float_arr.dtypedtype('float64')#Operations between Arrays and ScalarsArrays are important because they enable you to express batch operations on data without writing any for loops. This is usually called vectorization. Any arithmetic operations between equal-size arrays applies the operation elementwise:arr = np.array([[1., 2., 3.], [4., 5., 6.]])arr*arrarray([[ 1.,  4.,  9.],
arr - arrarray([[0., 0., 0.],
1 / arrarray([[1.        , 0.5       , 0.33333333],
arr ** 0.5array([[1.        , 1.41421356, 1.73205081],
Basic Indexing and Slicing *NumPy array indexing is a rich topic, as there are many ways you may want to select a subset of your data or individual elements. One-dimensional arrays are simple; on the surface they act similarly to Python lists: *arr = np.arange(10)arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])arr5arr[5:8]array([5, 6, 7])arr[5:8] = 12arrarray([ 0,  1,  2,  3,  4, 12, 12, 12,  8,  9])arr_slice = arr[5:8]arr_slice = 12345arrarray([    0,     1,     2,     3,     4,    12, 12345,    12,     8,
arr_slice[:] = 64arrarray([ 0,  1,  2,  3,  4, 64, 64, 64,  8,  9])For more updates of numpy exercisePython```