Introduction to NumPy part 1

Introduction to NumPy part 1


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.

Shailesh Thorat

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...

回复
Shailesh Thorat

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 !...

Shailesh Thorat

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...

要查看或添加评论,请登录

Alka kumari的更多文章

  • DATA ANALYTICS

    DATA ANALYTICS

    Data analytics is the art and science of drawing actionable insights from data. Data Analytics + Business Knowledge =…

  • Why Statistics is Important

    Why Statistics is Important

    Statistics is the discipline that concerns the collection, organization, analysis, interpretation, and presentation of…

  • Data Analytics Techniques

    Data Analytics Techniques

    Part 1: What is Data Analysis and What Does a Data Analyst Do? What is Data Analysis? 1. There is no one-size-fits-all…

    1 条评论
  • The Epic 2024 Guide to Data Mastery

    The Epic 2024 Guide to Data Mastery

    In today’s data-driven world, the role of a Data Scientist is not just lucrative but also transformative. These highly…

  • The data analytics project life cycle

    The data analytics project life cycle

    Here are the Important stages: Identifying the right problem statements for your Business Problem Designing the right…

  • ?? Introduction to Descriptive Statistics ??

    ?? Introduction to Descriptive Statistics ??

    Descriptive statistics are a set of techniques and methods used in data analysis to provide a clear and concise summary…

    1 条评论
  • Special matrix types

    Special matrix types

    Identity matrix An identity matrix is a special type of square matrix in linear algebra. It is denoted as I and has…

  • Vector vector multiplication (dot product) 2

    Vector vector multiplication (dot product) 2

    The dot product, also known as the scalar product, is a key operation in linear algebra. It involves multiplying the…

  • Introduction to NumPy part 3/3

    Introduction to NumPy part 3/3

    Randomly generated arrays In addition to creating and manipulating multi-dimensional arrays, NumPy also provides the…

  • Introduction to NumPy part 2/3

    Introduction to NumPy part 2/3

    Multi-dimensional arrays One of the key advantages of NumPy is its ability to efficiently work with multi-dimensional…

社区洞察

其他会员也浏览了