1. BASIC METHODS IN NUMPY Creating Array

Post Reply
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

1. BASIC METHODS IN NUMPY Creating Array

Post by admin »

Hi everyone,

Here's a useful Python script for beginners learning **NumPy array operations**, as covered in Chapter 2 of the ICAI AICITSS Python Module. This script includes examples like 1D arrays, 2D matrices, zero/one arrays, and value ranges — all very helpful in financial data modeling!

You can copy and run this directly in your Python environment:

Code: Select all

import numpy as np

# 1. Creating a 1D array
arr = np.array([1, 2, 3])
print("1D Array:\n", arr)

Code: Select all

# 2. Creating a 2D array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print("\n2D Matrix:\n", matrix)

Code: Select all

# 3. Creating arrays of zeros
zeros = np.zeros((3, 3))
print("\nZeros Array (3x3):\n", zeros)

Code: Select all

# 4. Creating arrays of ones
ones = np.ones((2, 2))
print("\nOnes Array (2x2):\n", ones)

Code: Select all

# 5. Creating a range of values
rng = np.arange(0, 10, 2)  # Start at 0, stop before 10, step by 2
print("\nRange Array (0 to 10 step 2):\n", rng)
---

💡 **CA Tip**: You can adapt this logic for real-world CA tasks like:

* Representing monthly trial balances
* Creating GST payment matrices
* Simulating fixed entries like depreciation or standard costs
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Use Case: Ledger Balance Matrix for Departments Across Quarters**

Post by admin »

This Python script creates:

* A **3×4 matrix** showing the ledger balances of 3 departments (Sales, HR, IT)
* For **4 quarters** (Q1 to Q4)
* Uses `numpy.array`, `numpy.sum`, `numpy.mean`, and slicing

Code: Select all

import numpy as np

# Ledger balances (in ₹ thousands) for 3 departments across 4 quarters
# Rows = Departments: Sales, HR, IT
# Columns = Quarters: Q1, Q2, Q3, Q4

ledger_balances = np.array([
    [125, 132, 128, 140],  # Sales
    [85,  90,  88,  93],   # HR
    [200, 210, 205, 215]   # IT
])

departments = ['Sales', 'HR', 'IT']
quarters = ['Q1', 'Q2', 'Q3', 'Q4']

# Print full matrix
print("Ledger Balance Matrix (₹ in thousands):\n", ledger_balances)

Code: Select all

# Total balance per department
total_per_dept = np.sum(ledger_balances, axis=1)
print("\nTotal per Department:")
for i, total in enumerate(total_per_dept):
    print(f"{departments[i]}: ₹{total}K")

Code: Select all

# Average balance per quarter
avg_per_quarter = np.mean(ledger_balances, axis=0)
print("\nAverage Balance per Quarter:")
for i, avg in enumerate(avg_per_quarter):
    print(f"{quarters[i]}: ₹{avg:.2f}K")

Code: Select all

# Extract Q2 balances only
q2_balances = ledger_balances[:, 1]
print("\nQ2 Balances:\n", q2_balances)
---

💼 **CA Tip**: This can be extended to:

* GST reconciliation per quarter
* Budget variance analysis
* Departmental expenditure tracking

Python helps bring clarity and automation to financial analysis.
Happy coding!
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Re: 1. Basic Operations

Post by admin »

Basic Operations:
NumPy allows you to perform element-wise operations on arrays. For example:

Code: Select all

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Addition
Addition = a + b
print(Addition)  # [5, 7, 9]

# Subtraction
Subtraction = a - b
print(Subtraction)  # [-3, -3, -3]

# Multiplication
Multiplication = a * b
print(Multiplication)  # [4, 10, 18]

# Division
Division = a / b
print(Division)  # [0.25, 0.4, 0.5]
Post Reply