Subscribe Us

Python NumPy

 What is NumPy?


NumPy is one of the most popular packages in Python.NumPy is used for scientific computing.NumPy is used for working with arrays.


Python NumPy


NumPy provides arrays, which are great alternatives to traditional lists.NumPy arrays are much faster than Python lists.


How to install NumPy?


To install NumPy, simply type the following in the cmd (command-line):


pip install numpy


If that does not work, type the following:


python -m pip install numpy


That's it, now you can use numpy in your Python scripts like so:


import numpy


Don't Have a PC? No Problem


With this app, you don't need to have a PC to learn NumPy. Our online compiler supports NumPy, so if you want to learn NumPy you can do so right now without installing anything.


Python NumPy Arrays


NumPy arrays are like the better version of Python lists. Arrays are much faster than lists, and is easier to work with.


Creating Array


To create an array, use the array() method of NumPy.


import numpy as np

arr = np.array([1, 2, 3, 4]) 

print(arr)


The ndarray Object


The object that gets created when we use the array() method is called ndarray. This can be shown by checking the type of the object using the type() function.


import numpy as np

arr = np.array([1, 2, 3, 4]) 

x = type(arr)

print(x)



Elements in an Array


The objects inside an array are called elements. A standard numpy array is required to have elements of the same data type.


import numpy as np

# all elements are numbers 

x = np.array([1, 2, 3])

# all elements are strings

y = np.array(["dog", "cat", "rat"])


The objects inside an array are called elements.

A standard numpy array is required to have elements of the same data type.


import numpy as np

# all elements are numbers x = np.array([1, 2, 3])

# all elements are strings

y = np.array(["dog", "cat", "rat

print(x) 

print(y)


Python Array Indexing


  • Indexing is the process of accessing individual elements of an array.
  • Indexing uses integers to access elements.
  • 0 represents the first element, 1 represents the second element and so on.


Example


In this example, we will access the elements of an array using indexing.


import numpy as np

pets = np.array(["dog", "cat"])

print(pets[0])

print(pets[1])

print(pets[2])


Note! Always remember that 0 represents the first element.


import numpy as np

pets = np.array(["dog", "cat",])

 print(pets[0])


Negative Indexing


  • Negative indexing is used to access elements of an array from its end.
  • -1 represents the last element, -2 represents the second to last element and so on.


import numpy as np

pets = np.array(["dog", "cat","camel"])

print(pets[-1])

print(pets[-2])

print(pets[-3])


Python Array Slicing


Slicing is used to access elements of an array using a range of two indexes. The first index is the start of the range while the second index is the end of the range.

The indexes are separated by a colon like this:


[start_index:end_index]


Note! The end index will not be included.


import numpy as np

arr = ["a", "b", "c", "d", "e"]

print(arr[1:3])


Here is another example:


import numpy as np

arr = ["a", "b", "c", "d", "e"] 

print(arr[0:4])



If we don't specify the end index, elements from the given start index until the end of the array will be included.


import numpy as np

arr = ["a", "b", "c", "d", "e"]

 print(arr[2:])


If we don't specify a start index, elements from the start of the array until the given end index will be included.


import numpy as np

arr = ["a", "b", "c", "d", "e"] 

print(arr[:2])


Negative Slicing


Negative slicing uses negative indexes to access elements.

Note that when using negative indexes, -1 represents the last element, -2 represents the second to last element and so on.


import numpy as np

arr = ["a", "b", "c", "d", "e"]

print(arr[-4:-1])



Python NumPy Data Types


  • NumPy supports greater data types than Python does.
  • As you already know, Python supports these basic data types:


• string

• int, float

• bool

• list, tuple

• set


NumPy, on the other hand, supports these basic data types:


The dtype Property


The dtype property is used to return the data type of a NumPy array.


import numpy as np

x = np.array([1, 2, 3]) 

print(x.dtype)


import numpy as np

x = np.array([1.1, 2.1, 3.1]) 

print(x.dtype)


import numpy as np

x = np.array([True, False]) 

print(x.dtype)



 Iterate Through a NumPy Array


Iterating through an array means accessing its elements one-by-one.


To iterate through an array, use the for loop.


import numpy as np


pets = np.array(["dog", "cat"])


for pet in pets: 

print("I love my + pet)


We can also use the nditer() method with the for loop.


import numpy as np


pets = np.array(["dog", "cat"])


for pet in pets: 

print("I love my + pet)



We can also use the nditer() method with the for loop.


import numpy as np

pets = np.array(["dog", "cat"])

for pet in np.nditer (pets):

print(pet)


Join NumPy Arrays


With NumPy, we can join or add arrays together.


To do that, use the concatenate() method of NumPy.


import numpy as np


x = np.array(["banana", "apple", y = np.array(["grape", "cherry"])


fruits = np.concatenate((x, y)) print(fruits)


Here's another example, this time we'll join three arrays together.


import numpy as np

x = np.array([1, 2, 3])

= np.array([4, 5, 6])

z = np.array([7, 8, 9])

nums = np.concatenate((x, y, z))

print(nums)



Python NumPy Search


With NumPy, we can search for a specific value in an array using the where() method.


The where() method returns where in an array the given condition is met.


In this example, we'll search for every "dog" in the pets array.


import numpy as np

pets = np.array(["dog", "cat"]) np.where(pets == "dog")

print(dogs)


As you may notice, the where() method returned a tuple containing an array of the indexes where the value "dog" is found.


Find Even and Odd Numbers


The where() method can be used to find even or odd numbers in an array.


This can be done by using the remainder operator (%).


Simply use this operator with number 2 being the right operand, if it returns 0 then the number is even, if it returns 1 then the number is odd.



import numpy as np


nums = np.array ([10, 8, 5, 4, 2,


# returns the position of even i


even = np.where(nums % 2 == 0) # returns the position of odd n odd = np.where (nums % 2 == 1)


print("Even:")

for x in even:

print(nums[x])

print("0dd:")

for y in odd:


Python NumPy Sort


NumPy allows us to sort arrays using the sort() method.


Sorting From Lowest to Highest


In this example, we'll sort an array of numbers from lowest to highest.


import numpy as np

nums = np.array ([5, 3, 4, 1, 2]) sorted = np.sort(nums)

print(sorted)


nums = np.array([5, 5, 4, 1, 2]J


sorted = np.sort(nums)


print(sorted)


Sorting Alphabetically


In this example, we'll sort an array of strings alphabetically.


import numpy as np

persons = np.array(["carl", "ale"]) sorted = np.sort(persons)

print(sorted)

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.