Multi-dimensional array objects ndarray and numpyndarray

Source: Internet
Author: User

Multi-dimensional array objects ndarray and numpyndarray

PS: content comes from "Data Analysis Using Python"

 

1. Create an ndarray

1. array: converts a sequence (nested sequence) into an array (multi-dimensional array)

In[2]: import numpy as npIn[3]: arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])In[4]: arrOut[4]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])In[5]: arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])In[6]: arrOut[6]: array([[1, 2, 3],       [4, 5, 6],       [7, 8, 9]])
View Code

2. arange: similar to the built-in range, an ndarray object is returned. Specify array dimensions through reshape

In[8]: arr = np.arange(1, 10).reshape(3, 3)In[9]: arrOut[9]: array([[1, 2, 3],       [4, 5, 6],       [7, 8, 9]])
View Code

3. ones, ones_like, zeros, and zeros_like: Create an array of all 1 or all 0 based on the specified shape and dtype. The ones_like or zeros_like array is a parameter, create an array of all 1 or all 0 based on the shape and dtype of the parameter Array

arr = np.ones((2, 4), dtype = np.int32)arrOut[11]: array([[1, 1, 1, 1],       [1, 1, 1, 1]])arr2 = np.zeros_like(arr)arr2Out[13]: array([[0, 0, 0, 0],       [0, 0, 0, 0]])
View Code

4. empty and empy_like: similar to ones and ones_like, but the allocated memory space is not filled with any value, that is, the elements of the array are not initialized.

arr = np.empty((3, 3), dtype = np.float64)arrOut[15]: array([[ 0.,  0.,  0.],       [ 0.,  0.,  0.],       [ 0.,  0.,  0.]])arr1 = np.empty_like(arr)arr1Out[17]: array([[ 0.,  0.,  0.],       [ 0.,  0.,  0.],       [ 0.,  0.,  0.]])
View Code

5. eye and identity: Create an N x N matrix (the diagonal line is 1, and the rest is 0)

In[18]: arr = np.eye(4, dtype = np.float32)In[19]: arrOut[19]: array([[ 1., 0., 0., 0.],[ 0., 1., 0., 0.],[ 0., 0., 1., 0.],[ 0., 0., 0., 1.]], dtype=float32)In[20]: arr1 = np.identity(4, dtype = np.int32)In[21]: arr1Out[21]: array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
View Code

 

2. shape and dtype of ndarray

Ndarray is a universal multi-dimensional container of homogeneous data. All elements must be of the same data type.

Ndarray. shape gets a tuple that represents the size of each dimension of the array. Ndarray. dtype: obtain the data type of the array element.

Numpy data types include: int8 int16 int32 int64 uint8 uint16 uint16 uint64 float16 float32 float64 float128 complex64 complex128 complex256 bool object (O) string _ (S) unicode _ (U)

In[2]: import numpy as npIn[3]: arr = np.arange(16).reshape((2, 2, 4))In[4]: arr.shapeOut[4]: (2, 2, 4)In[5]: arr.dtypeOut[5]: dtype('int32')
View Code

You can use the astype method of ndarray to display and convert dtype. If the conversion process fails (the type conversion cannot be implemented), a TypeError is thrown. Calling astype creates a new array, even if the new and old dtpye are the same.

In[16]: arr = np.array(10)In[17]: arr.dtypeOut[17]: dtype('int32')In[18]: float_arr = arr.astype(np.float32)In[19]: float_arr.dtypeOut[19]: dtype('float32')In[20]: str = np.array(['1.3', '-4.5', '33'], dtype = np.string_)In[21]: str.dtypeOut[21]: dtype('S4')In[22]: num = str.astype(np.float32)In[23]: num.dtypeOut[23]: dtype('float32')
View Code

 

Iii. vector operation between arrays and scalar

Any arithmetic operation between arrays of the same size applies the operation to the element level.

In[26]: arr = np.arange(1, 10).reshape((3, 3))In[27]: arrOut[27]: array([[1, 2, 3],       [4, 5, 6],       [7, 8, 9]])In[28]: arr ** 2Out[28]: array([[ 1,  4,  9],       [16, 25, 36],       [49, 64, 81]])In[29]: arr - arrOut[29]: array([[0, 0, 0],       [0, 0, 0],       [0, 0, 0]])In[30]: arr * arr.TOut[30]: array([[ 1,  8, 21],       [ 8, 25, 48],       [21, 48, 81]])
View Code

 

 

 

  

  

 

 

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.