Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Numpy fromrecords() method
Next article icon

NumPyis a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions.

Vectorare built from components, which are ordinary numbers. We can think of a vector as a list of numbers, and vector algebra as operations performed on the numbers in the list. In other words vector is the numpy 1-D array.

In order to create a vector, we use np.array method. 

Syntax : np.array(list)
Argument : It take 1-D list it can be 1 row and n columns or n rows and 1 column
Return : It returns vector which is numpy.ndarray
 

Note:We can create vector with other method as well which return 1-D numpy array for example np.arange(10), np.zeros((4, 1)) gives 1-D array, but most appropriate way is using np.array with the 1-D list.

Creating a Vector 
In this example we will create a horizontal vector and a vertical vector 

Python3
# importing numpyimportnumpyasnp# creating a 1-D list (Horizontal)list1=[1,2,3]# creating a 1-D list (Vertical)list2=[[10],[20],[30]]# creating a vector1# vector as rowvector1=np.array(list1)# creating a vector 2# vector as columnvector2=np.array(list2)# showing horizontal vectorprint("Horizontal Vector")print(vector1)print("----------------")# showing vertical vectorprint("Vertical Vector")print(vector2)

Output : 

Horizontal Vector[1 2 3]----------------Vertical Vector[[10] [20] [30]]

Basic Arithmetic operation: 
In this example we will see do arithmetic operations which are element-wise between two vectors of equal length to result in a new vector with the same length 

Python3
# importing numpyimportnumpyasnp# creating a 1-D list (Horizontal)list1=[5,6,9]# creating a 1-D list (Horizontal)list2=[1,2,3]# creating first vectorvector1=np.array(list1)# printing vector1print("First Vector          : "+str(vector1))# creating second vectorvector2=np.array(list2)# printing vector2print("Second Vector         : "+str(vector2))# adding both the vector# a + b = (a1 + b1, a2 + b2, a3 + b3)addition=vector1+vector2# printing addition vectorprint("Vector Addition       : "+str(addition))# subtracting both the vector# a - b = (a1 - b1, a2 - b2, a3 - b3)subtraction=vector1-vector2# printing addition vectorprint("Vector Subtraction   : "+str(subtraction))# multiplying  both the vector# a * b = (a1 * b1, a2 * b2, a3 * b3)multiplication=vector1*vector2# printing multiplication vectorprint("Vector Multiplication : "+str(multiplication))# dividing  both the vector# a / b = (a1 / b1, a2 / b2, a3 / b3)division=vector1/vector2# printing division vectorprint("Vector Division       : "+str(division))

Output : 

First Vector: [5 6 9]Second Vector: [1 2 3]Vector Addition: [ 6  8 12]Vector Subtraction: [4 4 6]Vector Multiplication: [ 5 12 27]Vector Division: [5 3 3]

Vector Dot Product 
In mathematics, the dot product or scalar product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number. 
For this we will use dot method.

Python3
# importing numpyimportnumpyasnp# creating a 1-D list (Horizontal)list1=[5,6,9]# creating a 1-D list (Horizontal)list2=[1,2,3]# creating first vectorvector1=np.array(list1)# printing vector1print("First Vector  : "+str(vector1))# creating second vectorvector2=np.array(list2)# printing vector2print("Second Vector : "+str(vector2))# getting dot product of both the vectors# a . b = (a1 * b1 + a2 * b2 + a3 * b3)# a . b = (a1b1 + a2b2 + a3b3)dot_product=vector1.dot(vector2)# printing dot productprint("Dot Product   : "+str(dot_product))

Output:

First Vector  : [5 6 9]Second Vector : [1 2 3]Dot Product   : 44

Vector-Scalar Multiplication 
Multiplying a vector by a scalar is called scalar multiplication. To perform scalar multiplication, we need to multiply the scalar by each component of the vector.

Python3
# importing numpyimportnumpyasnp# creating a 1-D list (Horizontal)list1=[1,2,3]# creating first vectorvector=np.array(list1)# printing vector1print("Vector  : "+str(vector))# scalar valuescalar=2# printing scalar valueprint("Scalar  : "+str(scalar))# getting scalar multiplication value# s * v = (s * v1, s * v2, s * v3)scalar_mul=vector*scalar# printing dot productprint("Scalar Multiplication : "+str(scalar_mul))

Output 

Vector  : [1 2 3]Scalar  : 2Scalar Multiplication : [2 4 6]


 


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp