<jupyter_start><jupyter_text>Creating Matrices in Python using NumPyNow we will see the ways to create the matrices in Python using the NumPy library.If you don’t have the NumPy library installed on your computer, use the Python package manager (pip) to get it installed. Type the following line in your command prompt (terminal):```nonepip install numpy```Plain textCopyCheck my article on [setting up Python for science](https://www.codeformech.com/setting-up-python-for-science-and-engineering/) to get started.To use the functions available in the NumPy library, we first need to import it using:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Here **np** is called an *alias* to *numpy* and is used to avoid typing the word *numpy* every time we use any function from the library.One thing to mention before we dive in is that the matrices can be made up of integers, floating-point numbers, complex numbers, or boolean values. These are called the *data types* in the programming world. Creating the matrices from the existing dataThe simplest way to create a matrix from the existing data is to use the *numpy.array( )* function. Let’s consider that we have the data in the form of a list or a tuple. The *numpy.array( )* function takes mainly two arguments, first is the *list* (or *tuple*) and second is the data type (*dtype*). The second argument is optional and the data type will be detected from the list provided by us if not specified. For example:<jupyter_code>A = np.array([1,2,3])
print('Row Matrix of integers:')
print(A)

B = np.array([[1.0,2.0,3.0],[4.0,5.0,6.0]])
print('Rectangular Matrix of floats:')
print(B)<jupyter_output>Row Matrix of integers:
[1 2 3]
Rectangular Matrix of floats:
[[1. 2. 3.]
 [4. 5. 6.]]<jupyter_text>Here the row matrix and the rectangular matrix are two of the [types of matrices](https://www.codeformech.com/types-of-matrices-linear-algebra-using-python/).The data type will be enforced if we specify it using the *dtype* argument to the array function.<jupyter_code>my_list = [1,2,3,4,5]

P = np.array(my_list,dtype=int)
print('Matrix P with integer elements:')
print(P)

my_tuple = ((1,2,3),(4,5,6))

Q = np.array(my_tuple ,dtype=float)
print('Matrix Q with real number elements:')
print(Q)

R = np.array(my_tuple ,dtype=complex)
print('Matrix R with complex number elements:')
print(R)<jupyter_output>Matrix P with integer elements:
[1 2 3 4 5]
Matrix Q with real number elements:
[[1. 2. 3.]
 [4. 5. 6.]]
Matrix R with complex number elements:
[[1.+0.j 2.+0.j 3.+0.j]
 [4.+0.j 5.+0.j 6.+0.j]]<jupyter_text>Another function we can use to create a [Row Matrix](https://www.codeformech.com/types-of-matrices-linear-algebra-using-python/) from an existing iterable is the *numpy.fromiter( )* function.This function takes three arguments. First is the *iterable*, second is the *dtype* and *count* is an optional argument. *count* is used to specify the number of items to be read from the *iterable* with the default value of -1, i.e., read all the elements.<jupyter_code>my_list = [1,2,3,4,5] 
iter_1 = iter(my_list)
iter_2 = (x*x*x for x in range(1,10))

A = np.fromiter(iter_1,float)
B = np.fromiter(iter_2,int,5)

print('Matrix A:')
print(A)
print('Matrix B with elements as cubes of first 5 integers:')
print(B)<jupyter_output>Matrix A:
[1. 2. 3. 4. 5.]
Matrix B with elements as cubes of first 5 integers:
[  1   8  27  64 125]<jupyter_text>We can also *reshape* an existing matrix to create a new matrix with a different shape, provided that the total number of elements remains the same. We will use the *reshape( )* method associated with the NumPy array.<jupyter_code>A = np.array(range(1,13)) # 12 elements

B = A.reshape((3,4))
C = B.reshape((2,6))

print('Row Matrix A:')
print(A)
print('Matrix B:')
print(B)
print('Matrix C:')
print(C)<jupyter_output>Row Matrix A:
[ 1  2  3  4  5  6  7  8  9 10 11 12]
Matrix B:
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
Matrix C:
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]<jupyter_text>Along with the in-built *range( )* function used above, we can make use of the sequence generation routines from the NumPy library, which include the *arange( )*, *linspace( )* and *logspace( )* functions.The *numpy.arange( )* function can be used in the same way as the *range( )* function. The only difference is that we can have the real numbers (*float* data type) as the *start*, *stop* and *step* arguments of the function.<jupyter_code>P = np.arange(2.5,4.8,0.2)
Q = P.reshape((3,4))

print('Row Matrix P:')
print(P)
print('Matrix P:')
print(Q)<jupyter_output>Row Matrix P:
[2.5 2.7 2.9 3.1 3.3 3.5 3.7 3.9 4.1 4.3 4.5 4.7]
Matrix P:
[[2.5 2.7 2.9 3.1]
 [3.3 3.5 3.7 3.9]
 [4.1 4.3 4.5 4.7]]<jupyter_text>The *numpy.linspace( )* and *numpy.logspace( )* functions require the *start*, *stop* and the number of elements (*num*) as the primary arguments. In case of the *logspace( )* function, we can also specify the *base* of the log space. The default value of *base* is 10.<jupyter_code>L = np.reshape(np.linspace(1,4,num=9),(3,3))

M = np.reshape(np.logspace(1,4,num=9),(3,3))
N = np.reshape(np.logspace(1,10,num=10,base=2),(2,5))

print('Matrix L:')
print(L)
print('------------------------------')
print('Matrix M:')
print(M)
print('------------------------------')
print('Matrix N:')
print(N)<jupyter_output>Matrix L:
[[1.    1.375 1.75 ]
 [2.125 2.5   2.875]
 [3.25  3.625 4.   ]]
------------------------------
Matrix M:
[[   10.            23.71373706    56.23413252]
 [  133.35214322   316.22776602   749.89420933]
 [ 1778.27941004  4216.96503429 10000.        ]]
------------------------------
Matrix N:
[[   2.    4.    8.   16.   32.]
 [  64.  128.  256.  512. 1024.]]<jupyter_text>Creating the matrices with zeros and onesVarious functions are available in NumPy to create matrices with different combinations of zeros and ones. Null or Zero MatrixThe *numpy.zeros( )* function is used to create a [Null or Zero Matrix](https://www.codeformech.com/types-of-matrices-linear-algebra-using-python/). This function takes the tuple of the shape of a matrix as an input and the data type is an optional argument.<jupyter_code>L = np.zeros(3)
M = np.zeros((2,4))
N = np.zeros((3,1),dtype='int')

print('Matrix L:')
print(L)
print('Matrix M:')
print(M)
print('Matrix N:')
print(N)<jupyter_output>Matrix L:
[0. 0. 0.]
Matrix M:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
Matrix N:
[[0]
 [0]
 [0]]<jupyter_text>Here the null matrix $N$ is also a [Column Matrix](https://www.codeformech.com/types-of-matrices-linear-algebra-using-python/).There is one more function to create a zero matrix, with the same number of rows and columns as any given matrix called *numpy.zero_like( )*. The same data type as the input matrix is followed.<jupyter_code>A = np.array([[1,2,3],[4,5,6]])
K = np.zeros_like(A)

print('Matrix A:')
print(A)
print('Zero Matrix K:')
print(K)<jupyter_output>Matrix A:
[[1 2 3]
 [4 5 6]]
Zero Matrix K:
[[0 0 0]
 [0 0 0]]
{'fraction_non_alphanumeric': 0.15012942191544434, 'fraction_numerical': 0.05565142364106989, 'mean_word_length': 3.897887323943662, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 5, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '272899', 'license': 'MIT', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2435, 'n_words': 903, 'path': 'notebooks/intro-to-matrices.ipynb', 'repo_name': 'vitormeriat/linear_algegra_project', 'size': '6954'}