0% found this document useful (0 votes)
5 views4 pages

NumPy Basics: Arrays and Operations

The document provides an overview of using NumPy, a numerical computing library in Python, covering the creation of 1D and 2D arrays, data types, and various array operations such as addition, subtraction, multiplication, and division. It also discusses array attributes like shape, size, and dimensions, as well as statistical functions like mean, median, standard deviation, and variance. Additionally, it includes examples of indexing, slicing, and reshaping arrays.
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)
5 views4 pages

NumPy Basics: Arrays and Operations

The document provides an overview of using NumPy, a numerical computing library in Python, covering the creation of 1D and 2D arrays, data types, and various array operations such as addition, subtraction, multiplication, and division. It also discusses array attributes like shape, size, and dimensions, as well as statistical functions like mean, median, standard deviation, and variance. Additionally, it includes examples of indexing, slicing, and reshaping arrays.
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

# Numerical Python

# pip install numpy


import numpy as np
import random

#1D
a = [1, 2, 3, 4]
arr = [Link](a)
print("List in python : ", a)
print("Numpy Array in python :", arr)
#Checking the data type of ‘a’ and ‘arr’
print(type(a))
print(type(arr))

#2D
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
sample_array = [Link]([list_1,
list_2,
list_3 ])
print("NumPy multi-dimensional array in python\n",
sample_array)
print(type(sample_array))

# Attributes/Parameters of a NumPy Array

# Shape => for 2D array


print("shape of array ", sample_array.shape)

# Datatype
arr1 = [Link]([0.1, 0.6])
print("Data type of array 1 ", [Link])
print("Data type of array 2 ", [Link])

# Size
print("Total no. of element in the array ", [Link])
print("Total no. of element in the array ", sample_array.size)

# Dimension
print("No. of dimensions(axes) of the array ", [Link])
print("No. of dimensions(axes) of the array ",
sample_array.ndim)

# NumPy Array Indexing:


# Single element access
print("Single element access:", arr[2])
# Negative indexing
print("Negative indexing:", arr[-1])

# Multidimensional array access


print("Multidimensional array access:", sample_array[1, 0])
print("Multidimensional array access:", sample_array[1][0])

# Array Operations:

#Element-wise Operations:
x = [Link]([1, 2, 3])
y = [Link]([4, 5, 6])
a = [Link]([[1,2], [3,4]])
b = [Link]([[5,6], [7, 8]])

# Addition
add = x + y
print("Addition:",add)

add1 = [Link](a, b)
add2 = a + b
print("Addition ", add1)
print("Addition ", add2)

# Subtraction
subtract = x - y
print("substration:",subtract)

subtract1 = a - b
subtract2 = [Link](a, b)
print("substration:",subtract1)
print("substration:",subtract2)

# Multiplication
multiply = x * y
print("multiplication:",multiply)

multiply1 = a * b
multiply2 = [Link](a, b)
print("multiplication:",multiply1)
print("multiplication:",multiply2)
multiply3 = [Link](a, b)
print("Dot Product ", multiply3)

# Division
divide = x / y
print("division:", divide)

divide1 = a / b
divide2 = [Link](a, b)
print("division: ", divide1)
print("division:", divide2)

#Square root
matrix = [Link]([[9, 26], [81, 49]])
matrix = [Link](matrix)
print("Sqrt() ", matrix)
print(matrix[1][0])

#sum(x, axis) -> this function is used to add all the element
in
# matrix. Optional "axis" argument computes the column sum if
axis
# is 0 and row sum if axis is 1

matrix = [Link]([[1,2], [3,4]])


print("Column wise sum ",[Link](matrix, 0))
print("Row wise sum ",[Link](matrix, 1))

# "T" -> This argument is used to transpose the specified


matrix
print("The transpose of maxtrix ", matrix.T)

arr = [Link]([1,2,4,5,6])
matrix = [Link]([[1, 2, 3], [4, 5, 6]])

#[Link]()
print("Mean on 1D array ", [Link](arr))
print("Mean on 2D array ", [Link](matrix))
print("Apply mean on axix 0 ", [Link](matrix, 0))
print("Apply mean on axix 1 ", [Link](matrix, 1))

#[Link]()
print("Median on 1D array ", [Link](arr))
print("Median on 2D array ", [Link](matrix))
print("Apply Median on axix 0 ", [Link](matrix, 0))
print("Apply Median on axix 1 ", [Link](matrix, 1))

#[Link]() => Standard deviation


print([Link](arr))

#[Link]() => Variance


print([Link](arr))

#[Link]()
print("Min element ", [Link](arr))

#[Link]()
print("Max element ", [Link](arr))

#[Link]()
print("Sum of elements ", [Link](arr))

#[Link]()
print("Max element Index ", [Link](arr))

#[Link]()
print("Min element Index", [Link](arr))

# Slicing
print(arr[1:4])

# Slicing in 2D array
matrix = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)

# Accessing first and last row of matrix


res = matrix[[0, 2]]
print(res)

# Accessing middle row of matrix


res = matrix[1]
print(res)

# Reshape

You might also like