Numpy

 No file chosenNo file chosenNo file chosen


Chat with ChatGPT

Ask anything



ChatGPT is AI and can make mistakes.



Log in

You said:

Numpy python example with results



ChatGPT said:

Here are a few common NumPy examples with their outputs.


1. Create a NumPy array

import numpy as np


arr = np.array([10, 20, 30, 40, 50])


print(arr)


Output:


[10 20 30 40 50]


2. Basic arithmetic

import numpy as np


arr = np.array([1, 2, 3, 4])


print("Original:", arr)

print("Add 5:", arr + 5)

print("Multiply by 2:", arr * 2)


Output:


Original: [1 2 3 4]

Add 5: [6 7 8 9]

Multiply by 2: [2 4 6 8]


3. Array statistics

import numpy as np


arr = np.array([10, 20, 30, 40, 50])


print("Sum:", np.sum(arr))

print("Mean:", np.mean(arr))

print("Maximum:", np.max(arr))

print("Minimum:", np.min(arr))


Output:


Sum: 150

Mean: 30.0

Maximum: 50

Minimum: 10


4. 2D array (matrix)

import numpy as np


matrix = np.array([[1, 2, 3],

                   [4, 5, 6]])


print(matrix)

print("Shape:", matrix.shape)


Output:


[[1 2 3]

 [4 5 6]]

Shape: (2, 3)


5. Reshape an array

import numpy as np


arr = np.arange(1, 7)


print("Original:", arr)


reshaped = arr.reshape(2, 3)

print("Reshaped:")

print(reshaped)


Output:


Original: [1 2 3 4 5 6]

Reshaped:

[[1 2 3]

 [4 5 6]]


6. Element-wise addition

import numpy as np


a = np.array([1, 2, 3])

b = np.array([4, 5, 6])


print(a + b)


Output:


[5 7 9]


7. Indexing and slicing

import numpy as np


arr = np.array([10, 20, 30, 40, 50])


print("First element:", arr[0])

print("Last element:", arr[-1])

print("Slice:", arr[1:4])


Output:


First element: 10

Last element: 50

Slice: [20 30 40]


These examples demonstrate some of the most commonly used NumPy operations: creating arrays, performing vectorized arithmetic, computing statistics, working with matrices, reshaping arrays, and indexing.

Comments

Popular posts from this blog

Python Introduction

STORE PROCEDURE BASIC PROGRAMMING - PART 1

STORE PROCEDURE ADVANCE CONCEPT - PART 2