0% found this document useful (0 votes)
2 views12 pages

Numpy Arrays

This document provides a comprehensive guide on creating and manipulating NumPy arrays, including functions for creating arrays, performing mathematical operations, and array manipulation techniques. It covers essential methods for statistical analysis, array joining and splitting, searching and sorting, and random number generation. Additionally, it addresses matrix operations, copying arrays, and unique value extraction, along with examples for clarity.

Uploaded by

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

Numpy Arrays

This document provides a comprehensive guide on creating and manipulating NumPy arrays, including functions for creating arrays, performing mathematical operations, and array manipulation techniques. It covers essential methods for statistical analysis, array joining and splitting, searching and sorting, and random number generation. Additionally, it addresses matrix operations, copying arrays, and unique value extraction, along with examples for clarity.

Uploaded by

nath veerendra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Creating NumPy Arrays


[Link]()
Creates a NumPy array from a Python list or tuple.
Syntax
[Link](object)
Example
import numpy as np

a = [Link]([10, 20, 30, 40])

print(a)
Output
[10 20 30 40]

[Link]()
Creates an array filled with zeros.
Syntax
[Link](shape)
Example
a = [Link](5)

print(a)
Output
[0. 0. 0. 0. 0.]
2D array:
a = [Link]((2, 3))

print(a)
[[0. 0. 0.]
[0. 0. 0.]]

[Link]()
Creates an array filled with ones.
Syntax
[Link](shape)
Example
a = [Link]((2, 3))
print(a)
Output
[[1. 1. 1.]
[1. 1. 1.]]

[Link]()
Creates an array filled with a specified value.
Syntax
[Link](shape, value)
Example
a = [Link]((2, 3), 7)

print(a)
Output
[[7 7 7]
[7 7 7]]

[Link]()
Creates evenly spaced values within a specified range.
Syntax
[Link](start, stop, step)
Example
a = [Link](1, 10, 2)

print(a)
Output
[1 3 5 7 9]

[Link]()
Creates a specified number of evenly spaced values.
Syntax
[Link](start, stop, number)
Example
a = [Link](0, 10, 5)

print(a)
Output
[ 0. 2.5 5. 7.5 10. ]
2. Array Properties / Information
Suppose:
a = [Link]([[10, 20, 30],
[40, 50, 60]])
ndim
Returns the number of dimensions.
print([Link])
Output:
2

shape
Returns the size of each dimension.
print([Link])
Output:
(2, 3)
Meaning:
2 rows × 3 columns

size
Returns the total number of elements.
print([Link])
Output:
6

dtype
Returns the data type.
print([Link])
Output:
int64

3. Mathematical Operations
NumPy allows mathematical operations directly on arrays.
a = [Link]([10, 20, 30])
b = [Link]([2, 4, 5])
Addition
print(a + b)
Output:
[12 24 35]
Subtraction
print(a - b)
Output:
[ 8 16 25]
Multiplication
print(a * b)
Output:
[ 20 80 150]
Division
print(a / b)
Output:
[5. 5. 6.]
Power
print(a ** 2)
Output:
[100 400 900]

4. Important Mathematical Methods


[Link]()
Calculates the sum.
Syntax
[Link](array)
Example
a = [Link]([10, 20, 30, 40])

print([Link](a))
Output:
100

[Link]()
Calculates the average.
print([Link](a))
Output:
25.0

[Link]()
Calculates the median.
a = [Link]([10, 20, 30, 40, 50])

print([Link](a))
Output:
30.0

[Link]()
Returns the smallest value.
print([Link](a))
Output:
10

[Link]()
Returns the largest value.
print([Link](a))
Output:
50

[Link]()
Calculates standard deviation.
print([Link](a))

[Link]()
Calculates variance.
print([Link](a))

[Link]()
Calculates the product of all elements.
a = [Link]([2, 3, 4])

print([Link](a))
Output:
24

5. Array Manipulation
reshape()
Changes the shape of an array without changing its data.
Syntax
[Link](rows, columns)
Example
a = [Link]([1, 2, 3, 4, 5, 6])

b = [Link](2, 3)

print(b)
Output:
[[1 2 3]
[4 5 6]]

flatten()
Converts a multidimensional array into a one-dimensional array.
a = [Link]([[1, 2], [3, 4]])

print([Link]())
Output:
[1 2 3 4]

ravel()
Also converts an array into one dimension.
print([Link]())
Output:
[1 2 3 4]

transpose()
Converts rows into columns and columns into rows.
a = [Link]([[1, 2, 3],
[4, 5, 6]])

print([Link]())
Output:
[[1 4]
[2 5]
[3 6]]
Shortcut:
print(a.T)
6. Joining Arrays
[Link]()
Joins arrays.
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

c = [Link]((a, b))

print(c)
Output:
[1 2 3 4 5 6]

[Link]()
Joins arrays vertically.
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print([Link]((a, b)))
Output:
[[1 2 3]
[4 5 6]]

[Link]()
Joins arrays horizontally.
print([Link]((a, b)))
Output:
[1 2 3 4 5 6]

7. Splitting Arrays
[Link]()
Splits an array into multiple arrays.
a = [Link]([1, 2, 3, 4, 5, 6])

result = [Link](a, 3)

print(result)
Output:
[array([1, 2]), array([3, 4]), array([5, 6])]
8. Searching and Sorting
[Link]()
Finds the positions where a condition is true.
a = [Link]([10, 20, 30, 40, 50])

print([Link](a > 25))


Output:
(array([2, 3, 4]),)
The indexes are:
2, 3, 4

[Link]()
Sorts an array.
a = [Link]([40, 10, 30, 20])

print([Link](a))
Output:
[10 20 30 40]

[Link]()
Returns the index of the maximum value.
a = [Link]([10, 50, 30, 20])

print([Link](a))
Output:
1

[Link]()
Returns the index of the minimum value.
print([Link](a))
Output:
0

9. Mathematical Functions
[Link]()
Calculates square root.
a = [Link]([4, 9, 16, 25])
print([Link](a))
Output:
[2. 3. 4. 5.]
[Link]()
print([Link](a, 2))
[Link]()
Returns absolute values.
a = [Link]([-10, -5, 5, 10])

print([Link](a))
Output:
[10 5 5 10]
[Link]()
Rounds decimal values.
a = [Link]([2.345, 4.678, 8.123])

print([Link](a, 2))
Output:
[2.35 4.68 8.12]

10. Trigonometric Functions


a = [Link]([0, [Link]/2, [Link]])
[Link]()
print([Link](a))
[Link]()
print([Link](a))
[Link]()
print([Link](a))

11. Random Number Generation


NumPy provides the random module.
[Link]()
Generates random values between 0 and 1.
print([Link](5))
Example output:
[0.21 0.73 0.45 0.89 0.12]
[Link]()
Generates random integers.
Syntax
[Link](start, stop, size)
Example
print([Link](1, 10, 5))
Possible output:
[3 8 1 6 4]

[Link]()
Selects random elements.
a = [Link]([10, 20, 30, 40, 50])

print([Link](a))

[Link]()
Makes random results reproducible.
[Link](10)

print([Link](1, 100, 5))

12. Matrix Operations


[Link]()
Calculates the dot product.
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print([Link](a, b))
Calculation:
1×4 + 2×5 + 3×6
= 4 + 10 + 18
= 32
Output:
32

[Link]()
Performs matrix multiplication.
A = [Link]([[1, 2],
[3, 4]])

B = [Link]([[5, 6],
[7, 8]])

print([Link](A, B))
Output:
[[19 22]
[43 50]]

[Link]()
Finds the inverse of a matrix.
A = [Link]([[1, 2],
[3, 4]])

print([Link](A))

[Link]()
Finds the determinant.
print([Link](A))
Output:
-2.0

13. Copying Arrays


[Link]()
Creates a copy of an array.
a = [Link]([10, 20, 30])

b = [Link](a)

print(b)
Output:
[10 20 30]

14. Unique Values


[Link]()
Returns unique values.
a = [Link]([10, 20, 10, 30, 20, 40])
print([Link](a))
Output:
[10 20 30 40]

15. Counting Elements


np.count_nonzero()
Counts non-zero elements.
a = [Link]([0, 10, 20, 0, 30])

print(np.count_nonzero(a))
Output:
3

16. Comparison Operations


NumPy supports element-by-element comparisons.
a = [Link]([10, 20, 30, 40])
print(a > 20)
Output:
[False False True True]
You can use this for filtering:
print(a[a > 20])
Output:
[30 40]

You might also like