Skip to content

Creating

Using NumPy for my data analysis and scientific computing tasks. Let's understand the basics of NumPy arrays and how to create them.

Creating NumPy Arrays

There are several ways to create a NumPy array, including:

  • Create a NumPy array from a Python list
  • Create a NumPy array with a specified data type
  • Create a NumPy array with a specified shape

Creating a NumPy array from a Python list

import numpy as np

# Create a Python list
python_list = [1, 2, 3, 4, 5]

# Create a NumPy array from the Python list
numpy_array = np.array(python_list)

print("Python List: ", python_list)
print("NumPy Array: ", numpy_array)

# Output:
# Python List:  [1, 2, 3, 4, 5]
# NumPy Array:  [1 2 3 4 5]

Create a NumPy array with a specified data type

import numpy as np

# Create a NumPy array with a specified data type as np.float64
array = np.array([1, 2, 3, 4, 5], dtype=np.float64)

print("Array: ", array)
print("Data Type: ", array.dtype)

# Output:
# Array:  [1. 2. 3. 4. 5.]
# Data Type:  float64

A real-world example:

Suppose we have a list of stock prices for a State Bank of India(SBIN), and we want to calculate the average price and the standard deviation of the prices. We can use NumPy to create an array from the list of prices and then use NumPy's built-in functions to calculate the average and standard deviation.

import numpy as np

# Create a list of stock prices
stock_prices = [744.15, 745.90, 753.45, 759.05, 779.25, 764.10, 766.30,
                753.70, 748.15, 729.50, 743.25, 760.45, 771.15, 778.75,
                776.40, 793.40, 801.20, 793.20, 794.95, 788.30, 799.65]

# Create a NumPy array from the list of stock prices
prices_array = np.array(stock_prices)

# Calculate the average price
average_price = np.mean(prices_array)

# Calculate the standard deviation of the prices
std_dev = np.std(prices_array)

print("Stock Prices: ", stock_prices)
print("Average Price: ", average_price)
print("Standard Deviation: ", std_dev)

Creating a NumPy array with a specified shape:

In this example, we create a NumPy array array with the numbers 1 through 6, and we use the reshape() method to specify the shape of the array as (2, 3). This means that the array will have 2 rows and 3 columns.

import numpy as np

# Create a NumPy array with a specified shape
array = np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3)

print("Array: \n", array)
print("Shape: ", array.shape)

# Output:
# Array: 
#  [[1 2 3]
#  [4 5 6]]
# Shape:  (2, 3)

Here's a revised example:

Stock Market Data Analysis

Suppose we are working with a dataset of daily stock prices for a portfolio of 5 stocks, and we want to store the prices in a NumPy array. We can create a NumPy array with a specified shape to store the prices, where each row represents a stock and each column represents a trading day.a subject.

import numpy as np

# Create a list of daily stock prices for 5 stocks and 3 trading days
stock_prices = [
    [85.50, 90.25, 78.75],
    [92.10, 88.50, 76.25],
    [95.75, 89.90, 91.50],
    [82.25, 86.10, 93.75],
    [89.50, 85.75, 90.25]
]

# Create a NumPy array with the stock prices and specify the shape as (5, 3)
price_array = np.array(stock_prices).reshape(5, 3)

print("Price Array: \n", price_array)
print("Shape: ", price_array.shape)

# Output:
# Price Array: 
#  [[85.5  90.25 78.75]
#  [92.1  88.5  76.25]
#  [95.75 89.9  91.5 ]
#  [82.25 86.1  93.75]
#  [89.5  85.75 90.25]]
# Shape:  (5, 3)

In this example, we create a list of daily stock prices stock_prices for 5 stocks and 3 trading days, and we use the np.array() function to create a NumPy array price_array with the prices. We specify the shape of the array as (5, 3) using the reshape() method, which means that the array will have 5 rows (one for each stock) and 3 columns (one for each trading day).

We can then use the price_array to perform calculations, such as calculating the average price for each stock or the average price for each trading day.

# Calculate the average price for each stock
average_prices = np.mean(price_array, axis=1)

# Calculate the average price for each trading day
average_daily_prices = np.mean(price_array, axis=0)

print("Average Prices: ", average_prices)
print("Average Daily Prices: ", average_daily_prices)

# Output:
# Average Prices:  [84.51666667 85.61666667 92.41666667 87.36666667 88.51666667]
# Average Daily Prices:  [88.93 88.1  86.55]