0% found this document useful (0 votes)
4 views2 pages

Py Expt 12

The document demonstrates the use of NumPy for creating and manipulating array objects, including 1D and 2D arrays, arrays of zeros and ones, and arrays with ranges and evenly spaced values. It covers indexing, slicing, element-wise operations, basic statistics, reshaping, and flattening arrays. Various examples are provided with print statements to illustrate each operation.

Uploaded by

shanne724
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)
4 views2 pages

Py Expt 12

The document demonstrates the use of NumPy for creating and manipulating array objects, including 1D and 2D arrays, arrays of zeros and ones, and arrays with ranges and evenly spaced values. It covers indexing, slicing, element-wise operations, basic statistics, reshaping, and flattening arrays. Various examples are provided with print statements to illustrate each operation.

Uploaded by

shanne724
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/ 2

Exp 12 : Program to demonstrate use of NumPy: Array objects.

import numpy as np

# 1. Creating NumPy Arrays


# Creating a 1D array from a list
array_1d = np.array([10, 20, 30, 40, 50])
print("1D Array:", array_1d)

# Creating a 2D array (matrix) from a list of lists


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

# Creating an array of zeros


zeros_array = np.zeros((2, 3)) # 2 rows, 3 columns
print("Array of Zeros:\n", zeros_array)

# Creating an array of ones


ones_array = np.ones((3, 2)) # 3 rows, 2 columns
print("Array of Ones:\n", ones_array)

# Creating an array with a range of values


range_array = np.arange(5) # Array from 0 to 4
print("Range Array:", range_array)

# Creating an array with evenly spaced values


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

# 2. Indexing and Slicing


# Accessing elements
print("First element of 1D Array:", array_1d[0]) # Accessing the first element
print("Second row of 2D Array:", array_2d[1]) # Accessing the second row

# Slicing
print("Slicing 1D Array (first three elements):", array_1d[:3]) # First three elements
print("Slicing 2D Array (first row):", array_2d[0, :]) # First row

# 3. Array Operations
# Element-wise operations
array_addition = array_1d + 5 # Adding 5 to each element
print("Array after addition:", array_addition)

array_multiplication = array_1d * 2 # Multiplying each element by 2


print("Array after multiplication:", array_multiplication)

# 4. Basic Statistics
print("Sum of 1D Array:", np.sum(array_1d))
print("Mean of 1D Array:", np.mean(array_1d))
print("Max of 1D Array:", np.max(array_1d))
print("Min of 1D Array:", np.min(array_1d))

# 5. Reshaping the Array


reshaped_array = array_1d.reshape((5, 1)) # Reshape to 5 rows, 1 column
print("Reshaped Array:\n", reshaped_array)

# 6. Flattening the Array


flattened_array = array_2d.flatten() # Flatten the 2D array to 1D
print("Flattened Array:", flattened_array)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy