Python Learning-numpy Data processing

Source: Internet
Author: User
Tags shuffle python list

Citation

Standard Python uses list to save values, which can be used when using arrays. But because the elements of the list are arbitrary objects, the list holds pointers to the objects. For numeric operations, this structure obviously wastes memory and CPU compute time.

In addition, Python provides an array module, but because it does not support multidimensional arrays, it is also not suitable for numerical calculations.

So,numpy just made up for these shortcomings, NumPy provided two basic objects: Ndarray and Ufunc. Ndarray is a multidimensional array that stores a single data type, whereas Ufunc is a function that can be processed on an array.

Import method

import numpy as np

list to array: using an array conversion method with NumPy

>>>list=[1,2]>>>_array = np.array(lists)

Array to list: nunpy array to list is also very convenient, directly using the ToList () method can be

>>>_list = _array.tolist()

Use the Random.shuffle method of NumPy to convert a given set of ordered sequences into a random sequence

>>>arr = np.arange(1000#生成一个序列,from 0 to 999>>>np.random.shuffle(arr)

Save NumPy array Format data in text format

>>>np.savetxt("a.txt",a,fmt="%d",delimiter=‘\t‘)

To convert the Python list to numpy format:

>>>pyList = [5, 11, 122]>>>mat(pyList)

An example:

>>> fromNumPyImport*>>>A = Arange ( the). Reshape (3,5)>>>Aarray ([[0,1,2,3,4],       [5,6,7,8,9],       [Ten, One, A, -, -]])>>>A.shape (3,5)>>>A.ndim2>>>A.dtype.name' Int32 '>>>A.itemsize4>>>A.size the>>>Type (a) Numpy.ndarray>>>b = Array ([6,7,8])>>>Barray ([6,7,8])>>>Type (b) numpy.ndarray

Array creation

>>> fromimport *>>> a = array( [2,3,4] )>>> aarray([234])>>> a.dtypedtype(‘int32‘)>>> b = array([1.23.55.1])>>> b.dtypedtype(‘float64‘)

Compare two ways of creating, the first of which is the wrong

array(1,2,3,4)    # WRONGarray([1,2,3,4])  # RIGHT

Two-dimensional array creation

>>> b = array( [ (1.5,2,3), (4,5,6) ] )>>> barray([[ 1.5,  2. ,  3. ],       [ 4. ,  5. ,  6. ]])

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python Learning-numpy Data processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.