首页 > > 详细

EGM271程序辅导、辅导Python程序语言、辅导Data Analytics辅导Python编程|辅导留学生 Statistics统计、回归、迭代

School of Engineering
Laboratory Session 2
Course : Diploma in Engineering with Business
Module : EGM271 Statistics & Data Analytics
Title : The NumPy Library (Part 1)
What is NumPy?
NumPy is a basic package for scientific computing with Python and especially for data
analysis. In fact, this library is the basis of a large amount of mathematical and scientific
Python packages, and among them, as you will see later in the book, the pandas library.
This library, specialized for data analysis, is fully developed using the concepts introduced
by NumPy. In fact, the built-in tools provided by the standard Python library could be too
simple or inadequate for most of the calculations in data analysis. Having knowledge of
the NumPy library is important to being able to use all scientific Python packages, and
particularly, to use and understand the pandas library.
Note that throughout all the lab sheets in this module, we are using Spyder:
1. Lines with “In:” are the codes you type in the console. You can run them
simply by pressing “Enter”.
2. Lines with “Out:” are the output shown in the console.
3. Lines without “In” or “Out” are the codes you type in the editor. You need to
click the Run button to run it.
To start using Numpy, type this in your Spyder editor and run the program:
import numpy as np
Ndarray: The Heart of the Library
The NumPy library is based on one main object: ndarray (which stands for N-dimensional
array). This object is a multidimensional homogeneous array with a predetermined
number of items:
• Homogeneous because virtually all the items in it are of the same type and the
same size. In fact, the data type is specified by another NumPy object called dtype
(data-type); each ndarray is associated with only one type of dtype.
• The number of the dimensions and items in an array is defined by its shape, a
tuple of N-positive integers that specifies the size for each dimension. The
dimensions are defined as axes and the number of axes as rank.
• Another peculiarity of NumPy arrays is that their size is fixed, that is, once you
define their size at the time of creation, it remains unchanged. This behaviour is
different from Python lists, which can grow or shrink in size.
The easiest way to define a new ndarray is to use the array() function, passing a Python
list containing the elements to be included in it as an argument.
In: a = np.array([1, 2, 3])
In: a
Out: array([1, 2, 3])
You can easily check that a newly created object is an ndarray by passing the new
variable to the type() function.
In: type(a)
Out:
You can also refer to the Variable Explorer for information about your array:
The name is a, data type is int32. The Size (3, ) indicates that it is of rank 1 (1 row), and
size 3 (3 columns).
What you have just seen is the simplest case of a one-dimensional array. But the use of
arrays can be easily extended to several dimensions. For example, if you define a twodimensional
array 2x2:
In: b = np.array([[1.3, 2.4],[0.3, 4.1]])
This array has rank 2, since it has two rows, each of length 2.
Types of Data
So far you have seen only simple integer and float numeric values, but NumPy arrays are
designed to contain a wide variety of data types. For example, you can use the data type
string:
In: g = np.array([['a', 'b'],['c', 'd']])
In: g
Out: array([['a', 'b'],['c', 'd']], dtype='

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!