python科学计算——numpy - webdancer's Blog

python科学计算——numpy

webdancer posted @ 2014年1月16日 03:22 in 未分类 with tags python , 2963 阅读

numpy是python进行科学计算的一个基础包,它的核心功能是提供ndarray对象,一个实现了n维数组的对象,与python自带的list相比,数组大小初始化时候确定,而且是单一类型(当然也可以使用 对象,这样也能包含多种对象),速度更快,大多数科学计算的程序可能都是基于numpy的。

1.基本的思想有两个:vectorization和broadcasting.

  • vectorization:说的是可以不适用for,而像线性代数里面提供的操作一样进行计算;
  • broadcasting:指的是对于element-by-element的计算,可以自动进行;

2.数据类型:支持丰富的数值类型,如下:

Data type Description
bool_ Boolean (True or False) stored as a byte
int_ Default integer type (same as C long; normally either int64 or int32)
intc Identical to C int (normally int32 or int64)
intp Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64.
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_ Shorthand for complex128.
complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
complex128 Complex number, represented by two 64-bit floats (real and imaginary components)

3.创建数组:有5中基本的方式:

  1. Conversion from other Python structures (e.g., lists, tuples)
  2. Intrinsic numpy array array creation objects (e.g., arange, ones, zeros, etc.)
  3. Reading arrays from disk, either from standard or custom formats: genfromtxt
  4. Creating arrays from raw bytes through the use of strings or buffers
  5. Use of special library functions (e.g., random)

4.索引:

import numpy as np

x = np.arange(10)

x[0]  ##从零开始索引,返回0

x.shape = (2,5)

x[1,2] ##这是与python的list,tuple不同的地方,支持多维的索引,返回7

x[0]等价于x[0,:]

x.shape=(10)

x[2:5] #与python的list一样,支持切片操作;

5.broadcasting:描述的是在进行算术操作时,Array的大小不一致的情况,numpy如何处理;将短的进行扩展,然后进行处理。

>>> a = np.array([1.0, 2.0, 3.0])
>>> b = 2.0
>>> a * b
array([ 2.,  4.,  6.])

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when

  1. they are equal, or
  2. one of them is 1

 

memen 说:
2017年9月20日 03:29

thanks, <a href="https://indotv.id/" rel="dofollow">trans tv streaming</a> very useful information i will try it directly. Once again I thank you :) <a href="https://indotv.id/transtv" rel="dofollow">streaming trans tv</a>

memen 说:
2017年9月20日 03:30

thanks, trans tv streaming very useful information i will try it directly. Once again I thank you :) streaming trans tv

Incredimail Customer 说:
2020年8月03日 18:57

One of the finest email services is now offers best resolution for their email clients. “Yes” you heard this right Incredimail is now offering quick assistance. Get easy customer care solutions with email professional's team and surely get hassle free troubleshooting email assistance within sort time to feel an extra ordinary email service.

Comcast Email Suppor 说:
2020年8月03日 18:59

First email solution offers for millions of clients, Get professional’s advice to solve numerous email troubles without extra wait cause Toll-free facility is really easy to reach and comes up with hassle free connectivity and solve clients problems with no wait. If you want to know more about the services please visit our webpage to explore more: – Comcast Email Support

10thmodelpaper.in 说:
2023年6月30日 18:49

which provides in-depth news coverage of current events across the nation Our team is made up of professional writers and citizen journalists with a wide range of journalism interests who are committed about 10thmodelpaper.in delivering education updates in the public interest while maintaining transparency.Our reporting team plans to release the Education & Recruitment Update for all age groups and provide inside coverage to show the real picture of current occurrences.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee