Introduction to NumPy part 1
Alka kumari
Aspiring Data Analyst ?? | Skilled in Data Visualization ?? | Proficient in Python, SQL, & Excel ?? | Enthusiastic about Uncovering Business Insights ?? | Learning from Google Data Analytics Program ??
import numpy as np
Numpy is a powerful Python library that provides support for large, multi-dimensional arrays and matrices, along with a wide range of mathematical functions to operate on these arrays. By importing the NumPy module as np, we gain access to all of its functionality and can easily manipulate arrays in our code.
One of the key features of NumPy is its ability to perform vectorized operations, which allows for faster and more efficient computations. Instead of looping over individual elements of an array, we can perform operations on the entire array at once. This not only simplifies our code but also improves its performance.
NumPy also provides numerous functions for creating arrays of different shapes and sizes. For example, we can use the np.array() function to create a new array from a list or a tuple. We can specify the data type of the array elements using the dtype parameter.
In addition to creating arrays, NumPy offers a wide range of functions for performing various mathematical operations. We can easily perform basic arithmetic operations, such as addition, subtraction, multiplication, and division, on NumPy arrays. NumPy also provides functions for calculating statistics, finding maximum and minimum values, and performing linear algebra operations.
Overall, NumPy is an essential library for any data scientist or programmer working with numerical data in Python. Its efficient array operations and wide range of mathematical functions make it a powerful tool for scientific computing and data analysis.
Creating (one-dimensional) arrays
In NumPy, creating one-dimensional arrays is a straightforward process. We can use the np.array() function to create an array from a list or a tuple. For example, to create an array of integers, we can do:
import numpy as np
?my_array = np.array([1, 2, 3, 4, 5])
In this case, my_array will be a one-dimensional numpy array with the values [1, 2, 3, 4, 5].
We can also create arrays of different data types by specifying the dtype parameter. For instance, if we want to create an array of floating-point numbers, we can do:
my_float_array = np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=float)
In this example, my_float_array will be a one-dimensional NumPy array with the values [1.1, 2.2, 3.3, 4.4, 5.5] and the data type set to float.
Another way to create one-dimensional arrays is by using NumPy’s built-in functions. For instance, we can use the np.arange() function to generate a range of numbers. Here’s an example:
1
my_range_array = np.arange(1, 10, 2)
In this case, my_range_array will be a one-dimensional NumPy array with the values [1, 3, 5, 7, 9]. The np.arange() function takes three arguments: the start value, the stop value (exclusive), and the step value.
Apart from np.arange(), NumPy also provides functions like np.zeros(), np.ones(), and np.linspace() for creating arrays with specific values or spacing.
With these methods, you can easily create one-dimensional arrays in NumPy and start performing various mathematical operations on them. NumPy’s efficient array operations make it a powerful tool for handling and manipulating large amounts of numerical data.
# argument is the size of the array
# result is an array with 5 zeros
np.zeros(5)
# Output:
# array([0., 0., 0., 0., 0.])
np.ones(10)
# Output:
# array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
# first argument is the size of the array
# second argument is the element you want to fill the array with
np.full(10, 2.5)
# Output:
# array([2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5])
# create an array with the range from 0 to <argument>
np.arange(10)
# Output:
# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(3, 10)
# Output:
# array([3, 4, 5, 6, 7, 8, 9])
# linspace creates an array filled with numbers between first argument and second argument
np.linspace(0, 1, 11)
# Output:
# array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
Converting a python list
To convert a Python list into a NumPy array, we can use the np.array() function. This function takes the list as an argument and returns a NumPy array with the same elements.
Here’s an example:
import numpy as np
my_list = [1, 2, 3, 5, 7, 12]
my_array = np.array(my_list)
# Output:
# array([ 1, 2, 3, 5, 7, 12])
In this case, my_array will be a one-dimensional NumPy array with the values [1, 2, 3, 5, 7, 12], which is equivalent to the original Python list.
Once we have converted a list into a NumPy array, we can perform various mathematical operations on it. NumPy provides convenient functions for element-wise operations, such as addition, subtraction, multiplication, and division. These operations are performed on each element of the array individually.
Accessing array elements
In NumPy, we can access individual elements of an array by using indexing. NumPy arrays are zero-indexed, which means the first element of the array has an index of 0, the second element has an index of 1, and so on.
To access a specific element of an array, we can use the indexing syntax array_name[index]. For example, let’s say we have the following array:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
If we want to access the second element of my_array, we can do:
second_element = my_array[1]
In this case, second_element will have the value 2, since the second element of the array has an index of 1.
We can also use negative indexing to access elements from the end of the array. For example, to access the last element, we can use index -1:
last_element = my_array[-1]
In this case, last_element will have the value 5, since -1 refers to the last element of the array.
In addition to accessing individual elements, we can also access a range of elements using slicing. Slicing allows us to extract a subset of elements from an array. The syntax for slicing is.
array_name[start_index:end_index].
For example, if we want to extract the elements from index 1 to index 3 (inclusive) from my_array, we can do
subset_array = my_array[1:4]
In this case, subset_array will be a new array with the values [2, 3, 4].
If we omit the start or end index in the slicing syntax, it will default to the beginning or end of the array, respectively. For example, if we want to extract all elements from index 2 to the end of the array, we can do:
subset_array = my_array[2:]
In this case, subset_array will be a new array with the values [3, 4, 5].
Overall, accessing array elements in NumPy is a powerful feature that allows us to extract specific values or subsets of data from arrays. This functionality is very useful when working with large datasets or performing calculations on specific elements of an array.
Associate Software Engineer Intern at cleverPe | AIML Enthusiast | Passionate About C++, Python & DSA | Java Developer ?? | Innovating the Future with AI
1 年That's gud hearing from you !... Still we're unable to connect ! Might be cuz of premium feature...
Associate Software Engineer Intern at cleverPe | AIML Enthusiast | Passionate About C++, Python & DSA | Java Developer ?? | Innovating the Future with AI
1 年Is there a way in which we can connect if it help each other in certain way !...
Associate Software Engineer Intern at cleverPe | AIML Enthusiast | Passionate About C++, Python & DSA | Java Developer ?? | Innovating the Future with AI
1 年Amazing !... You're just sharing content like I do...