python/2D_array_indexing.py

30 lines
1.2 KiB
Python

import numpy as np
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Accessing the first element (1D array) with positive index
print(array_2d[0])
# Accessing the first element (1D array) with negative index
print(array_2d[-2])
# Accessing the second element of the first 1D array with negative index
print(array_2d[0, -3])
# Accessing the second element of the first 1D array with positive index
print(array_2d[0, 1])
# Accessing the last element of the last 1D array with negative index
print(array_2d[-1, -1])
# Creating a 5x5 matrix representing stock prices
stock_prices = np.array([
[120, 130, 140, 150, 160],
[210, 220, 230, 240, 250],
[310, 320, 330, 340, 350],
[410, 420, 430, 440, 450],
[510, 520, 530, 540, 550]
])
# Retrieve all the stock prices of the first company over five days with a positive index
first_company_prices = stock_prices[0]
# Retrieve the stock price of the third company on the second day with positive indices
third_company_second_day_price = stock_prices[2, 1]
# Retrieve the stock price of the last company on the last day with negative indices
last_company_last_day_price = stock_prices[-1, -1]
print(first_company_prices)
print(third_company_second_day_price)
print(last_company_last_day_price)