sthoooon
2/11/2017 - 7:41 AM

## Creating Arrays from Python Lists

Creating Arrays from Python Lists

``````import numpy as np

# integer array:
np.array([1, 4, 2, 5, 3])

# set data type
np.array([1, 2, 3, 4], dtype='float32')

# nested lists result in multi-dimensional arrays
np.array([range(i, i + 3) for i in [2, 4, 6]])

# Create a length-10 integer array filled with zeros
np.zeros(3, dtype=int) #array([ 0, 0, 0])
np.zeros( (2,3) ) # array([[ 0, 0, 0], [ 0, 0, 0]])

# Create a 3x5 floating-point array filled with ones
np.ones((3, 5), dtype=float)

# Create a 3x5 array filled with 3.14
np.full((3, 5), 3.14)

# Create an array filled with a linear sequence
# Starting at 0, ending at 20, stepping by 2
# (this is similar to the built-in range() function)
np.arange(start, end, step)
np.arange(0, 20, 2)

# Create an array of five values evenly spaced between 0 and 1
np.linspace(start, end, length)
np.linspace(0, 1, 5)

# Create a 3x3 array of uniformly distributed
# random values between 0 and 1
np.random.random((3, 3))

# Create a 3x3 array of normally distributed random values
# with mean 0 and standard deviation 1
np.random.normal(0, 1, (3, 3))

# Create a 3x3 array of random integers in the interval [0, 10)
np.random.randint(0, 10, (3, 3))

# Create a 3x3 identity matrix
np.eye(3)

# Create an uninitialized array of three integers
# The values will be whatever happens to already exist at that memory location
np.empty(3)

# Generates a numpy array from given numerical data.
np.array([[1,2], [3,4]]) # array([ [1, 2], [3, 4] ])

``````