[Machine Learning] Python Machine Learning Library

Source: Internet
Author: User

Numpy:

NumPy provides two basic objects: Ndarray and Ufunc,ndarray are many arrays that store a single data type, and Ufunc are functions that can operate on an array.

To create an array:

A = Numpy.array ([1, 2, 3, 4= Np.array ([[1, 2, 3, 4], [4, 5, 6, 7]])

The shape of an array can be obtained by its Shape property, which is a tuple that describes the length of each axis of an array:

1 A.shape 2 # Results: (4,) 3 B.shape 4 # results: (2, 4)

To keep the number of array elements constant, you can change the size of each axis of the array by changing the shape property of the array: (the position of the array element in memory is unchanged)

1 b.shape = 4,22# result 3#Array ([[1, 2],4# [        3, 4],5#      [4, 5],6#       [6 , 7]])

When you set the number of elements in an axis to 1, the length of this axis is automatically calculated. Using the Reshape () method, you can create a new array of the specified shape, the original array remains unchanged, and the new array and the original array are shared storage spaces, and the modifications affect each other:

1 c = B.reshape (2, 4)2# result 3#Array ([[1, 2, 3, 4],  4#      [4, 5, 6, 7]])

The type of the array element can be obtained through the Dtype property:

C.dtype # Results # dtype (' Int64 ')

You can specify the element type when creating an array with the Dtype parameter, float is a double-precision floating-point type of 64bit, and complex is a double-precision complex type of 128bit:

1 numpy.array ([1,2,3,4], Dtype=np.float)

The complete type list of NumPy is stored in the Typedict dictionary and can be converted to a collection to view:

{Numpy.bool_, numpy.object_, Numpy.string_, Numpy.unicode_, Numpy.void, Numpy.int8, numpy.int16, Numpy.int32, Numpy.int64, Numpy.int64, Numpy.uint8, Numpy.uint16, Numpy.uint32, Numpy.uint64, Numpy.uint64, Numpy.float16, Numpy.float32, Numpy.float64, numpy.float128, Numpy.datetime64, Numpy.timedelta64, numpy.complex64, numpy.complex128 , numpy.complex256}

Arange () creates an array (not including a terminating value) that represents arithmetic progression by specifying a start value, a terminating value, and a step size:

1 numpy.arange (0, 1, 0.1)2# result 3#Array ([0.,  0.1,  0.2,  0.3,  0.4,  0.5   , 0.6, 0.7, 0.8, 0.9])

Linspace () creates a one-dimensional array of arithmetic progression by specifying the start value, the terminating value, and the number of elements:

1Numpy.linspace (0, 1, 10)#Step 1/92 #Results3 #Array ([0. , 0.11111111, 0.22222222, 0.33333333, #0.44444444,4 #0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.])5 6Numpy.linspace (0, 1, ten, Endpoint=false)#Step 1/107 #Results8 #Array ([0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 , 0.8, 0.9])

Logspace () is used to create the geometric series.

Zeros (), ones, empty () can create arrays of specified shapes and types, zeros_like (), Ones_like (), Empty_like () can be used to create arrays of the same shape and type, for example Zeros_like (a) and zeros (A.shape, A.dtype):

# assign type only, do not initialize # Results # Array ([[ -5764607523034234880,  6917537799820997240,           4327211011],#      [          4327214544,           4327214608,      844424930131968]])Numpy.zeros (# element is initialized to 0, Default Type Numpy.float# Result:#Array ([0.,  0.,  0.,  0.])

Frombuffer (), fromstring (), and FromFile () can create an array from a sequence of bytes or files:

1s ="ABCDEFG"2Numpy.fromstring (S, dtype=numpy.int8)3 #Results:4 #Array ([98, 101, 102, 103], dtype=int8)5 6s ="abcdef"7Numpy.fromstring (S, dtype=numpy.int16)8 #Results9 #Array ([25185, 25699, 26213], dtype=int16)Ten #each of these elements equals two bytes representing an integer, 98 * + +

You can also define a function that calculates a value from subscript, and then use FromFunction () to create an array:

def func (i):     return I% 4 + 1numpy.fromfunction (func, (ten,))# result #Array ([ 1.,  2.,  3.,  4.,  1.,  2.,  3.,  4.,  1.,  2.]

Access elements:

A = Numpy.arange (1, 2, 3, 4, 5, 6, 7, 8, 9]) a[# An element in an array can be obtained by using an integer as subscript #  Results: 5a[# get part of an array with slices as subscript, excluding a[5]# results: Array ([3, 4])a[ # use subscript to modify the value of an element # results: Array ([  0,   1, +, 101,   4,   5,   6,   7,   8,   9])

A new array obtained from a slice is an array of the original array that shares the same data space as the original array.

When accessing an array element using an integer list, the resulting new arrays do not share data with the original array, and the integer array and the Boolean array are similar:

1 x = Numpy.arange (1,-1)2#Array ([Ten,  9,  8,  7,  6,  5 ,  4,  3,  2])34# Gets the 3 elements in the array labeled 3, 1, 8, four, to form a new array

[Machine Learning] Python Machine Learning Library

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.