以numpy的基本數據例子來學習numpy基本數據處理方法javascript
主要內容有:css
import numpy as np
data = np.arange(15)
data
data = data.reshape(3,5)
data
d = data[:, np.newaxis]
d
d.shape
data.shape
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
iris_2d[:10]
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
sepallength = iris_2d[:, 0].astype('float')
petallength = iris_2d[:, 2].astype('float')
sepallength
petallength
numpy數據運算語法與python基本一致,可是是數據中的每一個數據進行運算jquery
volume = (np.pi * petallength * (sepallength**2))/3
volume
volume原來是一維的,增長維度後,變爲二維linux
volume = volume[:, np.newaxis]
volume
data
# 取第四列數據,每一個數據+10
d = data[:,4] +10
d
d = d[:, np.newaxis]
d
np.hstack([data,d])
np.partition快速查找第n大的值android
l=[3,4,5,2,1]
np.partition(l,kth=-4)
b = np.random.rand(10)
b
b = np.random.uniform(0,10,5)
b
np.random.randn(5)
np.random.randint(0,10,5)
np.random.randint(0,10,5)
np.random.seed(5)
np.random.randint(0,10,5)
np.random.seed(6)
np.random.randint(0,10,5)
np.random.uniform(10,100,10)
np.random.standard_normal(10)
np.linspace(1,20, 10)
np.arange(0,5)
a = np.arange(10).reshape(10)
a
a+a
a-a
a*a
a/a
a.dot(a)
a
b = np.arange(12).reshape(3,4)
b
b[0]
b[:,0]
b[0,:]
b[-1]
b.size
b.shape
b.ravel()
[x for x in b.flat]
b.flat[3]
b.reshape(3,4)
c = b.resize(3,4)
c
b
np.mean(d, axis=0),計算平均值,axis來指定按那個軸運算
np.mean(b, axis=0)
a = np.array([1,0.2,3])
a
a.dtype
np.arange(10)
np.ones((5,5))
b
np.ones_like(b)
np.identity(4)
np.eye(4)
a
b
b[:,2]
# 布爾值取數據
b[b>3]
b
b[b==3]
b
c = b[b!=3]
c
取特定的行,並按照指定順序排序
b
b[[2,0,1]]
b = np.arange(32).reshape(8,4)
b
b[[1,5,7,2],[0,3,1,2]]
b[[1,5,7,2]][:,[0,3,1,2]]
b
b.swapaxes(0,1)
b.swapaxes(1,0)
b
np.add(b,b)
np.sort(b[[2,0,1]])
b[[2,0,1]]
np.linalg.eig(b[0:4])
np.linalg.det
b
b[0:4]
np.random.randn(10)
np.random.uniform(0,1,10)
np.random.random(5)
w = 5000
n = 1000
draws = np.random.randint(0,2,size=(w,n))
draws
steps = np.where(draws > 0, 1, -1)
steps
# 累計求和
walks = steps.cumsum(1)
walks