Introducing NumPy 您所在的位置:网站首页 numpysize Introducing NumPy

Introducing NumPy

#Introducing NumPy| 来源: 网络整理| 查看: 265

Numeric is a package that was originally developed by Jim Hugunin. It is considered the ancestor of NumPy, a Python library and an open-source project created by Travis Oliphant which stands for Numerical Python. Travis created NumPy by incorporating features of the Numarray package into Numeric. 

The fundamental idea of NumPy is support for multidimensional arrays. So NumPy can be considered as the base for numerical computing in Python, and has been created to enable Python to be used in solving mathematical and scientific problems. The NumPy module provides us with hundreds of useful mathematical functions in addition to constants such as the base of natural logarithms (e) and pi (π).

This tutorial shows how we can use NumPy to work with multidimensional arrays, and describes the ndarray object, a fundamental object of the library.

Installing NumPy

Since Python doesn't come bundled with NumPy, the first step to use this library is to go ahead and install it. This can be simply done by running the following command in your command prompt:

pip install numpy

To make sure that NumPy was installed successfully, run the following commands in Python's IDLE:

Python-IDLE-NumpyPython-IDLE-NumpyPython-IDLE-Numpy

If the import statement at least runs successfully, then you are all set!

The ndarry Object

The ndarray is a fundamental object of NumPy. This object is an N-dimensional array, meaning that it contains a collection of elements of the same type indexed using N (dimensions of the array) integers.

The main attributes of ndarray are data type (dtype), shape, size, itemsize, data, and ndim. Let's learn what each attribute means through an example. 

In this example we are going to use NumPy to create an array. I will not give the dimensions of the array and other information, as we will see that using the above attributes.

create_array_numpycreate_array_numpycreate_array_numpy

Notice that we used the array function to create an array. The output of the above script is as follows:

array_numpy_outputarray_numpy_outputarray_numpy_output

Let's now return to our attributes.

dtype

The dtype attribute can be run as shown in the following statement:

data_type = my_array.dtype

The above statement will return int32 as the data type. This means that the elements of the array are of type int32. I'm getting 32 as I'm using a 32-bit Python. If you are using a 64-bit Python, you will get int64, but we are dealing with integers at the end.

Since NumPy is used in scientific computing, it has many data types, as shown in the documentation. Notice that the majority of the NumPy data types end with a number, which indicates the number of bits associated with that type (this was mentioned briefly in the above paragraph).

The following examples show how we can convert from one type to another:

bool(35) bool(0) bool(-1) float(True) int(67.7) float(87)

The above statements return the following:

True False True 1.0 67 87.0

Although we can convert from one type to another, it is important to note that we cannot convert a complex number into an integer or a float.

shape

The shape attribute returns a tuple of the array dimensions. So the following statement:

array_shape = my_array.shape

will return (4,4), meaning that our array is composed of 4 rows and 4 columns.

size

The size attribute returns the number of elements in the array. Thus, if we type:

array_size = my_array.size

we will get 16 as the result, meaning that we have 16 elements in our array.

itemsize

The itemsize attribute returns the size of one array element in bytes. The following statement:

array_item_size = my_array.itemsize

will return 4. This means that each array element is of size 4-bytes.

data

The data attribute is a Python buffer object that points to the start of the array's data. If we type the following:

array_location = my_array.data

we will get the following: .

ndim

The attribute ndim will return the number of the array dimensions. So typing the following statement:

array_dimension = my_array.ndim

will return 2, that is the array consists of two dimensions.

After understanding what the different ndarray attributes mean, let's take a look at some more examples of using ndarray.

Example 1

Say we want to create a new array with one row and five columns. We would do that as follows:

my_array = np.array( (1, 2, 3, 4, 5) )

The output of the above statement is: [1 2 3 4 5].

Example 2

In this example, I'm going to rewrite the first example in this tutorial, but using [ ] instead of ( ), as follows:

numpy_array_square_bracketsnumpy_array_square_bracketsnumpy_array_square_bracketsExample 3

This example shows how we use a structured data type, where we declare the field name and the corresponding data type:

import numpy as np height_type = np.dtype([('height', np.float)])

If we print(data_type), we will get the following:

[('height', '


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有