python-scipyHow do I create a Python numpy ndarray?
Creating a Python numpy ndarray is a simple process. To create one, you must first import the numpy library.
import numpy as np
Then, you can create an array using the numpy.array() method. This method takes in a sequence of numbers or a list and creates a ndarray from it.
# Create a numpy array from a sequence of numbers
my_array = np.array([1, 2, 3, 4])
print(my_array)
# Output: [1 2 3 4]
# Create a numpy array from a list
my_list = [5, 6, 7, 8]
my_array2 = np.array(my_list)
print(my_array2)
# Output: [5 6 7 8]
You can also create an array of a certain shape and type filled with zeros or ones by using the numpy.zeros() and numpy.ones() methods respectively.
# Create an array of shape (2, 3) filled with zeros
my_array3 = np.zeros((2, 3))
print(my_array3)
# Output:
# [[0. 0. 0.]
# [0. 0. 0.]]
# Create an array of shape (2, 3) filled with ones
my_array4 = np.ones((2, 3))
print(my_array4)
# Output:
# [[1. 1. 1.]
# [1. 1. 1.]]
Finally, you can also create an array filled with random numbers using the numpy.random.rand() method.
# Create an array of shape (2, 3) filled with random numbers
my_array5 = np.random.rand(2, 3)
print(my_array5)
# Output:
# [[0.8018761 0.50878095 0.37440122]
# [0.74143782 0.92293199 0.17585087]]
In summary, creating a numpy ndarray can be done using the numpy.array() method, numpy.zeros() and numpy.ones() methods, and the numpy.random.rand() method.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use the NumPy transpose function in Python?
- How do I use Python Scipy to perform a Z test?
- How can I use Python Scipy to solve a Poisson equation?
- How to use Python, XML-RPC, and NumPy together?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I use Python Scipy to zoom in on an image?
- How can I use Python and Numpy to parse XML data?
- How can I use Python and SciPy to implement an ARIMA model?
See more codes...