0% found this document useful (0 votes)
8 views

numpy_basics

This document provides an overview of NumPy, including array creation, properties, reshaping, mathematical operations, statistical functions, universal functions, random number generation, boolean indexing, and matrix operations. Each section includes code examples demonstrating the functionality of various NumPy features. It serves as a comprehensive guide for users to understand and utilize NumPy effectively.

Uploaded by

sushilasaini1538
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)
8 views

numpy_basics

This document provides an overview of NumPy, including array creation, properties, reshaping, mathematical operations, statistical functions, universal functions, random number generation, boolean indexing, and matrix operations. Each section includes code examples demonstrating the functionality of various NumPy features. It serves as a comprehensive guide for users to understand and utilize NumPy effectively.

Uploaded by

sushilasaini1538
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/ 3

NumPy Basics with Examples

## 1. NumPy Array Creation

### 1.1 Simple Array


```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr) # Output: [1 2 3 4 5]
```

### 1.2 Multi-Dimensional Array


```python
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
```

### 1.3 Zeros, Ones, and Empty Arrays


```python
print(np.zeros((3, 3))) # 3x3 matrix filled with zeros
print(np.ones((2, 4))) # 2x4 matrix filled with ones
print(np.empty((2, 2))) # Uninitialized array
```

### 1.4 Identity Matrix (eye)


```python
print(np.eye(3)) # 3x3 Identity Matrix
```

### 1.5 Range and Linspace


```python
print(np.arange(1, 10, 2)) # [1 3 5 7 9]
print(np.linspace(1, 10, 5)) # 5 values between 1 and 10
```

## 2. Array Properties
```python
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # (2, 3) -> Rows, Columns
print(arr.size) # 6 -> Total elements
print(arr.ndim) # 2 -> Number of dimensions
print(arr.dtype) # int32 or int64 -> Data type
```

## 3. Reshaping and Flattening


```python
arr = np.array([[1, 2, 3], [4, 5, 6]])
reshaped = arr.reshape(3, 2) # Change shape
print(reshaped)
flattened = arr.flatten() # Convert to 1D
print(flattened)
```

## 4. Mathematical Operations
```python
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a - b) # [-3 -3 -3]
print(a * b) # [4 10 18]
print(a / b) # [0.25 0.4 0.5]
```

## 5. Statistical Functions
```python
arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr)) # Average
print(np.median(arr)) # Median
print(np.std(arr)) # Standard Deviation
print(np.var(arr)) # Variance
print(np.sum(arr)) # Sum of all elements
print(np.min(arr)) # Minimum value
print(np.max(arr)) # Maximum value
```

## 6. Universal Functions (ufuncs)


```python
arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr)) # Square root
print(np.exp(arr)) # Exponential
print(np.log(arr)) # Natural Log
print(np.sin(arr)) # Sine function
print(np.cos(arr)) # Cosine function
print(np.tan(arr)) # Tangent function
print(np.abs([-1, -2, -3])) # Absolute values
```
## 7. Random Number Generation
```python
print(np.random.rand(3, 3)) # 3x3 random numbers (0 to 1)
print(np.random.randint(1, 10, (3, 3))) # 3x3 random integers (1 to 10)
print(np.random.randn(3, 3)) # 3x3 Gaussian distribution
```

## 8. Boolean Indexing & Filtering


```python
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr > 2]) # [3 4 5] -> Filter values greater than 2
```

## 9. Matrix Operations
```python
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B) # Matrix Multiplication
print(np.linalg.inv(A)) # Inverse of matrix A
print(np.linalg.det(A)) # Determinant of A
```

This document contains the most important NumPy functions with exampl

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