NumPy 基础

NumPy 提供一个N维数组对象类ndarray。它是SciPy和Scikit-learn等的数据结构,它支持快速线性代数计算。

创建数组

从list到一维数组

import numpy as np
list_of_ints = [1, 2, 3]
array_1 = np.array(list_of_ints)
array_1
Out[22]: 
array([1, 2, 3])
type(array_1)
Out[23]: 
numpy.ndarray
array_1.dtype
Out[24]: 
dtype('int64')
指定数据类型,控制内存大小

array_1.nbytes
Out[30]: 
24
array_1 = np.array(list_of_ints, dtype='int8')
array_1.nbytes
Out[32]: 
3
array_1b = array_1.astype('float32')
array_1b
Out[34]: 
array([ 1.,  2.,  3.], dtype=float32)
array_1b.nbytes
Out[35]: 
12

异构列表(数据类型不同)

取精度更高的类型

import numpy as np
complex_list = [1,2,3] + [1.1,2.2,3.3] + ['a','b','c']
array_2 = np.array(complex_list[:3])
print(array_2.dtype)
int64
array_2 = np.array(complex_list[:6])
print(array_2.dtype)
float64
array_2 = np.array(complex_list)
print(array_2.dtype)
|S32

二维数组

import numpy as np
a_list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
array_2d = np.array(a_list_of_lists)
array_2d
Out[43]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
改变数组的大小reshape()、resize()和shape

array_2d_1 = array_2d.reshape(1,9).copy()
array_2d_1
Out[46]: 
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
用NumPy函数生产数组

import numpy as np
ordinal_values = np.arange(9).reshape(3,3)
ordinal_values
Out[49]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
np.arange(9)[::-1]
Out[51]: 
array([8, 7, 6, 5, 4, 3, 2, 1, 0])
np.arange(9)[:-1]
Out[52]: 
array([0, 1, 2, 3, 4, 5, 6, 7])
np.zeros((3,3))
Out[53]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
np.ones((3,3))
Out[54]: 
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
np.eye(3)
Out[55]: 
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
np.linspace(start=0, stop=1, num=10)
Out[56]: 
array([ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
        0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ])
growth = np.logspace(start=0, stop=1, num=10, base=10.0)
growth
Out[58]: 
array([  1.        ,   1.29154967,   1.66810054,   2.15443469,
         2.7825594 ,   3.59381366,   4.64158883,   5.9948425 ,
         7.74263683,  10.        ])
np.random.normal(size=(3,3))
Out[59]: 
array([[ 1.35884452,  0.40577809,  1.63872935],
       [-2.36304665,  0.35944907,  0.58849736],
       [ 1.1812921 ,  1.12403039,  0.07716541]])

np.random.normal(loc=1.0, scale=3.0, size=(3,3))
Out[61]: 
array([[ 4.72521417,  4.08956132,  0.53066751],
       [ 1.23055934, -1.30784141, -2.92347621],
       [ 2.43437376,  0.54167036,  3.13887534]])
np.random.uniform(low=0.0, high=1.0, size=(3,3))
Out[62]: 
array([[ 0.99686131,  0.44151759,  0.19651618],
       [ 0.56318673,  0.1481807 ,  0.35430769],
       [ 0.76847528,  0.76751349,  0.09021368]])
从文件装载数据生成数组

np.loadtxt('',delimiter=',',dtype=float)

从pandas获取数据

df.values

Numpy计算

import numpy as np
a = np.arange(5).reshape(1,5)
a += 1
a*a
Out[66]: 
array([[ 1,  4,  9, 16, 25]])

array([[ 1,  4,  9, 16, 25]])
import numpy as np
a = np.arange(5).reshape(1,5) + 1
b = np.arange(5).reshape(5,1) + 1
a*b
Out[67]: 
array([[ 1,  2,  3,  4,  5],
       [ 2,  4,  6,  8, 10],
       [ 3,  6,  9, 12, 15],
       [ 4,  8, 12, 16, 20],
       [ 5, 10, 15, 20, 25]])
import math
%timeit -n 1 -r 3 [math .sqrt(i) for i in range(10**6)]
1 loop, best of 3: 146 ms per loop
%timeit -n 1 -r 3 np.sqrt(np.arange(10**6))
1 loop, best of 3: 5.73 ms per loop
矩阵运算
import numpy as np
M = np.arange(5*5, dtype=float).reshape(5,5)
coefs = np.array([1., 0.5, 0.5, 0.5, 0.5])
coefs_matrix = np.column_stack((coefs, coefs[::-1]))
print("M --- ")
print(M)
print("coefs")
print(coefs)
print("dot")
np.dot(M,coefs)
M --- 
[[  0.   1.   2.   3.   4.]
 [  5.   6.   7.   8.   9.]
 [ 10.  11.  12.  13.  14.]
 [ 15.  16.  17.  18.  19.]
 [ 20.  21.  22.  23.  24.]]
coefs
[ 1.   0.5  0.5  0.5  0.5]
dot
Out[80]: 
array([  5.,  20.,  35.,  50.,  65.])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值