Table of contents
6. Matrix dot multiplication (element-wise multiplication)
1. Introduction to NumPy
NumPy is a Python library commonly used in scientific computing, especially in deep learning and machine learning.
1. Official website
2. Official Tutorial
2. Experimental content
1. Import the numpy library
- Import numpy library (you should follow the standard NumPy conventions).
Import the numpy library (should follow standard NumPy conventions).
import numpy as np
2. Print the version number
- Print the version number of NumPy.
Print the version number of NumPy.
print(np.__version__)
3. arange function
- Use the
arange
function to generate 10 elements from 0 to 9 and store them in a variable namedndarray
.
Use the arange function to generate 10 elements from 0 to 9 and store them in a variable named ndarray.
ndarray = np.arange(10)
print(ndarray)
4. array
function
- Utilize the
array
function to convert data in Python list format into an equivalentndarray
namedndarray1
.
Utilize array
a function to convert data in Python list format to an equivalent ndarray named ndarray1.
ndarray1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(ndarray1)
5. reshape function
- Reshape the
ndarray
and thendarray1
into a 2-row by 5-column array.
Transform ndarray and ndarray1 into 2 row by 5 column arrays.
ndarray = ndarray.reshape(2, 5)
ndarray1 = ndarray1.reshape(2, 5)
print(ndarray)
print(ndarray1)
6. Matrix point multiplication ( element-wise multiplication)
- Calculate the elementwise product of
ndarray
andndarray1
using the*
operator, and print the result
Computes the element-wise product of ndarray and ndarray1 using the * operator and prints the result
result = ndarray * ndarray1
print(result)
7. Matrix multiplication
- Calculate the matrix product of
ndarray
andndarray1
using the@
operator, and print the result. You need to use theT
attribute to perform a transpose operation onndarray1
.
Computes the matrix product of ndarray and ndarray1 using the @ operator and prints the result. The transpose operation needs to be performed on ndarray1 using the T attribute.
result1 = ndarray @ ndarray1.T
print(result1)