0% found this document useful (0 votes)
9 views5 pages

What Is NumPy

This is notes about numpy this is a thing that we read in python language

Uploaded by

ayushkumar32388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

What Is NumPy

This is notes about numpy this is a thing that we read in python language

Uploaded by

ayushkumar32388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What is NumPy?

NumPy is a Python library used for working with arrays. It also has functions for working in
domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by
Travis Oliphant. It is an open source project and you can use it freely. NumPy stands for
Numerical Python.
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy
aims to provide an array object that is up to 50x faster than traditional Python lists. The array
object in NumPy is called ndarray, it provides a lot of supporting functions that make
working with ndarray very easy. Arrays are very frequently used in data science, where speed
and resources are very important.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can
access and manipulate them very efficiently. This behaviour is called locality of reference in
computer science. This is the main reason why NumPy is faster than lists. Also, it is
optimized to work with latest CPU architectures.
Which Language is NumPy written in?
NumPy is a Python library and is written partially in Python, but most of the parts that require
fast computation are written in C or C++.
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very
easy.
Install it using this command:
C:\Users\Your Name>pip install numpy
If this command fails, then use a python distribution that already has NumPy installed like,
Anaconda, Spyder etc.
Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy
Now NumPy is imported and ready to use.
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)

NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Checking NumPy Version
The version string is stored under __version__ attribute.
Example
import numpy as np
print(np.__version__)
Key Features:
Array Creation:
np.array(): Creates an array from a Python list or tuple.
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
# Output: [1 2 3]

np.zeros(): Creates an array filled with zeros.


zeros_arr = np.zeros(5)
print(zeros_arr)
# Output: [0. 0. 0. 0. 0.]

np.ones(): Creates an array filled with ones.


ones_arr = np.ones((2, 3))
print(ones_arr)
# Output: [[1. 1. 1.]
[1. 1. 1.]]

np.arange(): Creates an array with evenly spaced values within a given interval.
arange_arr = np.arange(10, 20, 2)
print(arange_arr)
# Output: [10 12 14 16 18]

np.linspace(): Creates an array with evenly spaced values over a specified interval.
linspace_arr = np.linspace(0, 10, 5)
print(linspace_arr)
# Output: [ 0. 2.5 5. 7.5 10. ]

np.random.rand(): Creates an array of random numbers.


random_arr = np.random.rand(3, 2)
print(random_arr)

# Output: [[0.67442927 0.61435318]


[0.55012783 0.41980714]
[0.15097639 0.9079732 ]]

Array Operations:
o Arithmetic operations: +, -, *, /, //, %, **.
o Comparison operations: ==, !=, <, >, <=, >=.
o Logical operations: &, |, ~.
o Universal functions: np.sin(), np.cos(), np.exp(), np.log(), etc.

Indexing and Slicing:


o Accessing elements: array[index].
o Slicing: array[start:end:step].
o Fancy indexing: array[[indices]].
o Boolean indexing: array[boolean_mask].

Example
pip install numpy
import numpy as np

# Step 1: Array Creation


print("### Array Creation ###")
# 1D array
array_1d = np.array([1, 2, 3, 4, 5])
print("1D Array:", array_1d)

# 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("2D Array:\n", array_2d)

# Array filled with zeros


zeros_array = np.zeros((3, 3))
print("Zeros Array:\n", zeros_array)

# Array with a range of numbers


range_array = np.arange(10, 20, 2)
print("Range Array:", range_array)

# Array with evenly spaced numbers


linspace_array = np.linspace(0, 1, 5)
print("Linspace Array:", linspace_array)

# Step 2: Array Manipulation


print("\n### Array Manipulation ###")
# Reshaping an array
reshaped_array = np.reshape(array_2d, (3, 2))
print("Reshaped Array:\n", reshaped_array)

# Indexing and slicing


print("Element at index 1 in 1D array:", array_1d[1])
print("First row of 2D array:", array_2d[0, :])
print("Element at [1, 2] in 2D array:", array_2d[1, 2])

# Transpose of a matrix
transposed = np.transpose(array_2d)
print("Transposed Array:\n", transposed)

# Step 3: Basic Operations


print("\n### Basic Operations ###")
array_a = np.array([10, 20, 30])
array_b = np.array([1, 2, 3])

# Element-wise addition
add_result = array_a + array_b
print("Addition:", add_result)

# Element-wise multiplication
mul_result = array_a * array_b
print("Multiplication:", mul_result)

# Dot product
dot_product = np.dot(array_a, array_b)
print("Dot Product:", dot_product)

# Statistical operations
random_array = np.random.randint(1, 100, size=10)
print("\nRandom Array:", random_array)
print("Mean:", np.mean(random_array))
print("Median:", np.median(random_array))
print("Standard Deviation:", np.std(random_array))

# Step 4: Broadcasting
print("\n### Broadcasting ###")
broadcast_result = array_1d + 5
print("Broadcasting Result (Adding 5):", broadcast_result)

Sample Output
### Array Creation ###
1D Array: [1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
Zeros Array:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Range Array: [10 12 14 16 18]
Linspace Array: [0. 0.25 0.5 0.75 1. ]

### Array Manipulation ###


Reshaped Array:
[[1 2]
[3 4]
[5 6]]
Element at index 1 in 1D array: 2
First row of 2D array: [1 2 3]
Element at [1, 2] in 2D array: 6
Transposed Array:
[[1 4]
[2 5]
[3 6]]

### Basic Operations ###


Addition: [11 22 33]
Multiplication: [10 40 90]
Dot Product: 140

Random Array: [45 12 38 79 24 16 6 35 64 22]


Mean: 34.5
Median: 31.5
Standard Deviation: 22.755

### Broadcasting ###


Broadcasting Result (Adding 5): [ 6 7 8 9 10]

You might also like