Home  >  Article  >  Web Front-end  >  list to numpy: easy conversion tips

list to numpy: easy conversion tips

王林
王林Original
2024-01-26 10:15:19910browse

list to numpy: easy conversion tips

From list to numpy: simple conversion method, specific code examples are required

Introduction:
In the fields of scientific computing and data analysis, Numpy is the most important in Python One of the third-party libraries. Numpy provides efficient data structures and functions, making it very easy to handle large-scale array and matrix operations. In actual work and projects, we often need to convert raw data from Python lists to Numpy arrays. This article will introduce some simple methods to help readers achieve this conversion.

Method 1: Use the numpy.array() function
The most common method is to use the numpy.array() function, which can convert a Python list into a Numpy array. The usage of this function is very simple, just pass in the list as a parameter. The following is a sample code:

import numpy as np

# 原始数据
my_list = [1, 2, 3, 4, 5]

# 将list转换为numpy数组
my_array = np.array(my_list)

print(my_array)

Output result:

[1 2 3 4 5]

Method 2: Use the numpy.asarray() function
You can also use the numpy.asarray() function to convert the list to Numpy array. Unlike the numpy.array() function, the numpy.asarray() function does not create a new array copy when passing in a Numpy array, but directly returns the input parameter itself. Similarly, the following is a sample code:

import numpy as np

# 原始数据
my_list = [1, 2, 3, 4, 5]

# 将list转换为numpy数组
my_array = np.asarray(my_list)

print(my_array)

Output result:

[1 2 3 4 5]

Method 3: Use the numpy.reshape() function
Numpy provides the numpy.reshape() function, which can be used to change the shape of the array. If the original data is a multidimensional list, it can be converted into a Numpy array of corresponding shape by using the numpy.reshape() function. The following is a sample code:

import numpy as np

# 原始数据
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# 将多维list转换为numpy数组
my_array = np.reshape(my_list, (3, 3))

print(my_array)

Output result:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Method 4: Use numpy.fromiter() function
Sometimes, we need to convert an iterator to a Numpy array. The numpy.fromiter() function can achieve this function. The following is a sample code:

import numpy as np

# 原始数据
my_iter = range(10)

# 将迭代器转换为numpy数组
my_array = np.fromiter(my_iter, dtype=np.int)

print(my_array)

Output result:

[0 1 2 3 4 5 6 7 8 9]

Method 5: Use numpy.loadtxt() function
Finally, if our original data is stored in a file, This can be read and converted to a Numpy array using the numpy.loadtxt() function. The following is a sample code:

import numpy as np

# 从文件中读取数据并转换为numpy数组
my_array = np.loadtxt('data.txt')

print(my_array)

Output result:

[[1.  2.  3.  4.  5. ]
 [6.  7.  8.  9.  10.]
 [11. 12. 13. 14. 15.]]

Conclusion:
This article introduces several simple methods to quickly convert list objects in Python into Numpy arrays. These methods are very simple and easy to understand, and can help us use Numpy more conveniently for scientific calculations and data analysis in actual work and projects. Readers can choose the appropriate method for conversion according to their actual needs, and learn more about the usage of Numpy.

The above is the detailed content of list to numpy: easy conversion tips. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn