NumPy Common Properties, creating arrays

Source: Internet
Author: User

1. Properties of several common numpy

    • Ndim: Dimensions
    • Shape: Number of rows and columns
    • Size: Number of elements
1>>>ImportNumPy as NP#Import the NumPy module, NP is to use the convenient shorthand2>>> array = Np.array ([[1,2,3],[2,3,4]])#list into a matrix3>>>Print(Array)4[[1 2 3]5[2 3 4]]6>>>7>>>Print('Number of Dim:', Array.ndim)#Dimension of8Number of Dim:29>>>Print('Shape:', Array.shape)#Number of rows and columnsTenShape: (2, 3) One>>>Print('Size:', array.size)#Number of elements ASize:6

2. NumPy Create array

2.1 Key Words

    • Array: Creating arrays
    • Dtype: Developing data types
    • Zeros: Create data is all 0
    • Ones: Create data is all 1
    • Empty: Create data close to 0
    • Arrange: Create data by specified range
    • Linspace: Creating a segment 2.2 creating an array
1 #Create an array2>>> a = Np.array ([2,23,4])#List 1d3>>>Print(a)4[2 23 4]5 6 #Specify Type7>>> a = Np.array ([2,23,4],dtype=np.int)8>>>Print(A.dtype)9 Int32Ten  One>>> a = Np.array ([2,23,4],dtype=Np.int32) A>>>Print(A.dtype) - Int32 -  the>>> a = Np.array ([2,23,4],dtype=np.float) ->>>Print(A.dtype) - float64 -  +>>> a = Np.array ([2,23,4],dtype=Np.float32) ->>>Print(A.dtype) + float32 A  at #Create specific data ->>> a = Np.array ([[2,23,4],[2,32,4]])#2d Matrix 2 rows 3 columns ->>>Print(a) -[[2 23 4] -[2 32 4]] -  in #Create a full zero group ->>> a = Np.zeros ((3,4))#data is all 0, 3 rows 4 columns to>>>Print(a) + [0.0. 0.0.] - [0.0. 0.0.] the [0.0. 0.0.] *  $ #Create a full 1 arrayPanax Notoginseng>>> a = Np.ones ((3,4), Dtype = Np.int)#data is 1, 3 Rows 4 columns ->>>Print(a) the[1 1 1 1] +[1 1 1 1] A[1 1 1 1]] the  + #create an all-empty array, in fact each value is close to zero number: ->>> a = Np.empty ((3,4))#data is empty,3 row 4 column $>>>Print(a) $ [0.0. 0.0.] - [0.0. 0.0.] - [0.0. 0.0.] the  - #To create a contiguous array with Arange:Wuyi>>> a = Np.arange (10,20,2)#10-19 of data, 2 steps the>>>Print(a) -[10 12 14 16 18] Wu  - #use reshape to change the shape of the data About>>> a = Np.arange (a). Reshape ((3,4))#3 Rows, 4 columns, 0 to one $>>>Print(a) -[0 1 2 3] -[4 5 6 7] -[8 9 10 11]] A  + #To create a segment type data with Linspace: the>>> a = Np.linspace (1,10,20)#start end 1, end 10, and split into 20 data to generate a segment ->>>Print(a) $[1.1.47368421 1.94736842 2.42105263 2.89473684 3.36842105 the3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632 the6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158 the9.52631579 10. ] the  -  in #the same can be done reshape work: the>>> a = Np.linspace (1,10,20). Reshape ((5,4))#Change Shape the>>>Print(a) About[1.1.47368421 1.94736842 2.42105263] the[2.89473684 3.36842105 3.84210526 4.31578947] the[4.78947368 5.26315789 5.73684211 6.21052632] the[6.68421053 7.15789474 7.63157895 8.10526316] +[8.57894737 9.05263158 9.52631579 10. ]]

NumPy Common Properties, creating arrays

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.