栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

About Introduction to NumPy Arrays

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

About Introduction to NumPy Arrays

Arrays are the main data structure used in machine learning. In Python, arrays from the NumPy library, called N-dimensional arrays or the ndarray, are used as the primary data structure for representing data. In this tutorial, you will discover the N-dimensional array in NumPy for representing numerical and manipulating data in Python. After completing this tutorial, you will know:

  • What the ndarray is and how to create and inspect an array in Python.
  • Key functions for creating new empty arrays and arrays with default values.
  • How to combine existing arrays to create new arrays.
1.1 Tutorial Overview

 This tutorial is divided into 3 parts; they are:

  • NumPy N-dimensional Array
  • Functions to Create Arrays
  • Combining Arrays
1.2 NumPy N-dimensional Array

NumPy is a Python library that can be used for scientific and numerical applications and is the tool to use for linear algebra operations. The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array. It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.

        The data type supported by an array can be accessed via the dtype attribute on the array. The dimensions of an array can be accessed via the shape attribute that returns a tuple describing the length of each dimension. There are a host of other attributes. A simple way to create an array from data or simple Python data structures like a list is to use the array() function. The example below creates a Python list of 3 floating point values, then creates an ndarray from the list and access the arrays’ shape and data type.

# Example of creating an array with the array() function
# create array
from numpy import array
# create array
l = [1.0, 2.0, 3.0]
a = array(l)

# display array
print(a)

# display array shape
print(a.shape)

# display array data type
print(a.dtype)
1.3 Functions to Create Arrays

There are more convenience functions for creating fixed-sized arrays that you may encounter or be required to use. Let's look at just a few.

1.3.1 Empty

The empty() function will create a new array of the specified shape.The argument to the function is an array or tuple that specifies the length of each dimension of the array to create.The values or content of the created array will be random and will need to be assigned before use.The example below creates an empty 3 x 3 two-dimension array.

# Example of Creating an array with the empty() function
# create empty array
from numpy import empty
a = empty([3,3])
print(a)

Running the example prints the content of the empty array. Your specific array contents will vary.

[[0.0e+000 0.0e+000 0.0e+000]
 [0.0e+000 0.0e+000 6.6e-321]
 [0.0e+000 0.0e+000 0.0e+000]]
 1.3.2 Zeros

The zeros() function will create a new array of the specified size with the contents filled with zero values. The argument to the function is an array or tuple that specifies the length of each dimension of the array to create. The example creates a 3 X 5 zero  two-dimensional array.

# Example of creating an array with the zeros() function
# create zero array
from numpy import zeros
a = zeros([3,5])
print(a)

Running the example prints the contents of the created zero array.

 1.3.3 Ones

The ones() function will create a new array of the specified size with the contents filled with one values. The argument to the function is an array or tuple that specifics the length of each dimension of the array to create. The example below creates a 5-element one-dimensional array.

# create one array
from numpy import ones
a = ones([5])
print(a)

Running the example prints the contents of the created ones array.

[1. 1. 1. 1. 1.]
1.4 Combing Arrays

NumPy provides many functions to create new arrays from existing arrays. Let’s look at two of the most popular functions you may need or encounter.

1.4.1 Vertical Stack

Given two or more existing arrays, you can stack them vertically using the vstack() function. For example, given two one-dimensional arrays, you can create a new two-dimensional array with two rows by vertically stacking them. This is demonstrated in the example below.

# Example of creating an array from other arrays using the vstack() function
# create array with vstack
from numpy import array
from numpy import vstack
# create first array
a1 = array([1,2,3])
print(a1)
a2 = array([4,5,6])
print(a2)
a3 = vstack((a1,a2))
print(a3)
print(a3.shape)

Running the example first prints the two separately defined one-dimensional arrays. The arrays are vertically stacked resulting in a new 2 × 3 array, the contents and shape of which are printed.

1.4.2 Horizontal Stack

Given two or more existing arrays, you can stack them horizontally using the hstack() function. For example, given two one-dimensional arrays, you can create a new one-dimensional array or one row with the columns of the first and second arrays concatenated. This is demonstrated in the example below.
 

# Example of creating an array from other arrays using the hstack() function
# create array with hstack
from numpy import array
from numpy import hstack
# create first array
a1 = array([1,2,3])
print(a1)
# create second array
a2 = array([4,5,6])
print(a2)
# create horitontal stack
a3 = hstack((a1,a2))
print(a3)
print(a3.shape)

Running the example first prints the two separately defined one-dimensional arrays. The arrays are then horizontally stacked resulting in a new one-dimensional array with 6 elements, the contents and shape of which are printed.

1.5 Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

  • Experiment with the different ways of creating arrays to your own sizes or with new data.
  • Locate and develop an example for 3 additional NumPy functions for creating arrays.
  • Locate and develop an example for 3 additional NumPy functions for combining arrays.

If you explore any of these extensions, I’d love to know.

1.6 Further Reading

This section provides more resources on the topic if you are looking to go deeper.

1.6.1 Books
  • Python for Data Analysis, 2017.
  • Elegant SciPy, 2017.
  • Guide to NumPy, 2015.
1.6.2 References
  • NumPy Reference.
  • https://docs.scipy.org/doc/numpy-1.13.0/reference/
  • The N-dimensional array.
  • The N-dimensional array (ndarray) — NumPy v1.13 Manual
  • Array creation routines.
  • Array creation routines — NumPy v1.13 Manual

4.6.3 API numpy.array() API.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.array.html

numpy.empty() API.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.empty.html

numpy.zeros() API.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.zeros.html

numpy.ones() API.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ones.html

numpy.vstack() API.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.vstack.html

numpy.hstack() API.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.hstack.html

1.7 Summary

In this tutorial, you discovered the N-dimensional array in NumPy for representing numerical and manipulating data in Python. Specifically, you learned:

  • What the ndarray is and how to create and inspect an array in Python.
  • Key functions for creating new empty arrays and arrays with default values.
  • How to combine existing arrays to create new arrays.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号