《利用Python进行数据分析》笔记---第4章NumPy基础:数组和矢量计算

时间:2023-01-06 14:48:32

写在前面的话:

实例中的所有数据都是在GitHub上下载的,打包下载即可。

地址是:http://github.com/pydata/pydata-book

还有一定要说明的:

我使用的是Python2.7,书中的代码有一些有错误,我使用自己的2.7版本调通。

# coding: utf-8
import numpy as np

data1 = [6.,7.5,8.,0.,1.]
arr1 = np.array(data1)
arr1

data2 = [[1,2,3,4],[5,6,7,8]]
arr2 = np.array(data2)
arr2

arr2.ndim

arr2.shape

arr1.dtype
arr2.dtype

np.zeros(10)
np.zeros_like(10)

np.ones((3,6))

np.empty((2,3,2))

np.arange(15)

arr1 = np.array([1,2,3],dtype=np.float64)
arr2 = np.array([1,2,3],dtype=np.int32)
arr1.dtype
arr2.dtype

arr = np.array([1,2,3,4,5])
arr.dtype
float_arr = arr.astype(np.float64)
float_arr.dtype

arr = np.array([3.7,-1.2,-2.6,0.5,12.9,10.1])
arr
arr.astype(np.int32)

numeric_strings = np.array(['1.25','56.36','36.235'],dtype=np.string_)
numeric_strings.astype(float)

int_array = np.array(10)
calibers = np.array([.22,.270,.357,.380,.44,.50],dtype=np.float64)
int_array.astype(calibers.dtype)

empty_uint32 = np.empty(8,dtype='u4')
empty_uint32

arr = np.array([[1.,2.,3.],[4.,5.,6.]])
arr
arr * arr
arr - arr
1/arr
arr ** 0.5

arr = np.arange(10)
arr
arr[5]
arr[5:8]
arr

arr_slice = arr[5:8]
arr_slice[1] = 12345
arr
arr_slice[:] = 64
arr

arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2d[2]
arr2d[0][2]
arr2d[0,2]

arr3d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
arr3d
arr3d[0]

old_values = arr3d[0].copy()
arr3d[0] = 42
arr3d
arr3d[0] = old_values
arr3d
arr3d[1,0,1]

arr[1:6]

arr2d
arr2d[:2]
arr2d[:2,1:]
arr2d[1,:2]
arr2d[2,:1]
arr2d[:,:1]
arr2d[:2,1:] = 0
arr2d

names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
names
import numpy.random as npr
data = npr.randn(7,4)
data
names == 'Bob'
data[names == 'Bob']
data[names == 'Bob', 2:]
data[names == 'Bob', 3]
names != 'Bob'
data[-(names == 'Bob')]
mask = (names == 'Bob') | (names == 'Will')
mask
data[mask]
data[data < 0] = 0
data

data[names != 'Joe'] = 7

arr = np.empty((8,4))
for i in range(8):
    arr[i] = i
arr
arr[[4,3,0,6]]
arr[[-3,-5,-7]]

arr = np.arange(32).reshape((8,4))
arr
arr[[1,5,7,2],[0,3,1,2]]
arr[[1,5,7,2]][:,[0,3,1,2]]
arr[np.ix_([1,5,7,2],[0,3,1,2])]

arr = np.arange(15).reshape((3,5))
arr
arr.T

arr = np.random.randn(6,3)
np.dot(arr.T,arr)

arr = np.arange(16).reshape((2,2,4))
arr
arr.transpose((1,0,2))
arr.swapaxes(1,2)

arr = np.arange(10)
np.sqrt(arr)
np.exp(arr)

x = npr.randn(8)
y = npr.randn(8)
x
y
np.maximum(x,y)

arr = npr.randn(7) * 5
np.modf(arr)

points = np.arange(-5,5,0.01)
xs,ys = np.meshgrid(points,points)
ys

import matplotlib.pyplot as plt
z = np.sqrt(xs ** 2 + ys ** 2)
z
plt.imshow(z,cmap=plt.cm.gray);plt.colorbar()
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")

xarr = np.array([1.1,1.2,1.3,1.4,1.5])
yarr = np.array([2.1,2.2,2.3,2.4,2.5])
cond = np.array([True,False,True,True,False])
result = [(x if c else y) for x,y,c in zip(xarr,yarr,cond)]
result
result = np.where(cond,xarr,yarr)
result

arr = npr.randn(4,4)
arr

np.where(arr>0,2,-2)
np.where(arr>0,2,arr)

arr = np.random.randn(5,4)
arr.mean()
np.mean(arr)
arr.sum()
arr.mean(axis=1)
arr.sum(0)
arr = np.array([[0,1,2],[3,4,5],[6,7,8]])
arr.cumsum(0)
arr.cumprod(1)

arr =npr.randn(100)
(arr > 0).sum()

bools =np.array([False,False,True,False])
bools.any()
bools.all()

arr = npr.randn(8)
arr
arr.sort()
arr

arr = npr.randn(5,3)
arr
arr.sort(1)
arr

large_arr = npr.randn(1000)
large_arr.sort()
large_arr[int(0.05 * len(large_arr))]

np.unique(names)
ints = np.array([3,3,3,2,2,1,1,55,55,6,6,])
np.unique(ints)
sorted(set(names))

values = np.array([6,0,0,3,2,5,6])
np.in1d(values,[2,3,6])

arr = np.arange(10)
np.save('some_array',arr)
np.load('some_array.npy')

x = np.array([[1,2,3],[4,5,6]])
y = np.array([[6,23],[-1,7],[8,9]])
x.dot(y)
np.dot(x,np.ones(3))

from numpy.linalg import  inv,qr

X = npr.randn(5,5)
mat = X.T.dot(X)
inv(mat)
mat.dot(inv(mat))
q,r = qr(mat)
r

samples = np.random.normal(size=(4,4))
samples

from random import normalvariate
N = 1000000
samples = [normalvariate(0,1) for _ in xrange(N)]

import  random
position = 0
walk = [position]
steps = 1000;
for i in xrange(steps):
    step = 1 if random.randint(0,1) else -1
    position += step
    walk.append(position)

nwalks = 5000
nsteps = 1000
draws = np.random.randint(0,2,size=(nwalks,nsteps))
steps = np.where(draws>0,1,-1)
walk = steps.cumsum(1)
walk.min()
walk.max()
(np.abs(walk) >= 10).argmax()
nwalks