1. 程式人生 > >numpy 基本操作

numpy 基本操作

numpy基本操作

NumPy系統是Python的一種開源的數值計算擴充套件。這種工具可用來儲存和處理大型矩陣。 更多操作參考http://www.runoob.com/numpy/numpy-tutorial.html

numpy reshape

  reshape()
  revel()

numpy 加法 乘法 (數字)

  加法 乘法 (陣列)      
  點乘      (陣列np.dot(a,b))      
  廣播      (匹配維度,否則報錯)
  < = >       (bool判斷)

numpy 內建函式

  cos()三角函式    
  exp()指數
  sqrt()平方根

numpy 統計功能

  sum()
  mean()
  min() max()
  argmin()
  argmax()  返回索引
  std()    標準差

numpy 多維度 行列

  axis = 0
  axis = 1

numpy 陣列排序功能

  np.sort()
  np.argsout()

numpy 求解多項式 多項式擬合。。。

numpy 檔案操作。。。

 

 

numpy reshape

import numpy as np

 

a = np.arange(12)
print(a)

b = a.reshape(3,4)
print(b)

c = b.ravel()
print(c)
 

numpy 加法 乘法 (數字)

import numpy as np

a = np.arange(6)
print(a+5)

b = a.reshape(2,3)
print(b)
c = np.ones((3,2))

d = np.dot(b,c)
print(d)

e = np.arange(3)
f = np.arange(2)

print(b + e)
print(b + f.reshape(2,1))

g = [[2,3]]
print(f > g)

numpy 內建函式

 

a = np.arange(6)

print(np.cos(a))
print(np.exp(a))
print(np.sqrt(a))
 

numpy 統計功能

a = np.random.randint(1,8,20)
print(a)

print(a.sum())
print(a.mean())
print(a.min())
print(a.argmax())

numpy 多維度 行列

b = np.random.randint(1,8,(5,6))
print(b)

print(b.sum(axis=1))
print(b.std(axis=1))

numpy 陣列排序功能

c = np.sort(b,axis=1)
d = np.argsort(b,axis=0)

print(c)
print(d)

 

numpy 求解多項式 多項式擬合

p = np.poly1d([1,-4,3])   #二項式的係數

print(p(1))

print(p.roots)      #多項式的根
print(p.order)      #多項式階數
print(p.coeffs)     #多項式係數
%matplotlib inline import matplotlib.pyplot as plt

n_dots = 20 n_order =3

x = np.linspace(0,1,20) #0,1之間的20個點 y = np.sqrt(x) + 0.2*np.random.rand(n_dots)

p = np.poly1d(np.polyfit(x,y,n_order)) print(p.coeffs)

t = np.linspace(0,1,200) plt.plot(x,y,'ro',t,p(t),'-')

計算pi

n_dots = 1000000
x = np.random.random(n_dots)
y = np.random.random(n_dots)

distance = np.sqrt(x**2 + y**2)

in_circle = distance[distance<1]
print(in_circle.shape)

pi = 4*float(len(in_circle)) / n_dots
print(pi)

numpy 檔案操作