程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python data analysis] common operations of numpy (II)

編輯:Python

One 、 Common mathematical and statistical methods

1、 One dimensional array

#np.random.randn Generate random arrays , It changes every time it runs 
arr=np.random.randn(9)
print(arr)

[ 0.2303069 1.00300936 -0.92357035 0.06525841 0.19994871 -1.40363692
0.01230236 -0.08950691 -1.13518494]

# The most value min max
print(arr.min())
print(arr.max())

-1.403636921406203
1.003009355191414

# mean value 、 Sum up 、 Sort 
print(arr.mean())
print(arr.sum())
arr.sort()
print(arr)

-0.22678593093886062
-2.0410733784497457
[-1.40363692 -1.13518494 -0.92357035 -0.08950691 0.01230236 0.06525841
0.19994871 0.2303069 1.00300936]

2、 Two dimensional array

# Create a 2D array 
arr1=np.random.randn(5,3)
print(arr1)

[[-0.37967759 1.35964448 -0.91838218]
[ 0.59445228 0.50795044 -1.04871713]
[ 0.95627647 0.51861237 -1.91861238]
[ 0.71488425 0.80140304 0.85057234]
[-0.83144865 1.40915456 -0.48493178]]

# Find the maximum value , mean value , Sort 
print(arr1.min())
print(arr1.mean())
arr1.sort
print(arr1)

-1.9186123788695604
0.14207870043754137
[[-0.37967759 1.35964448 -0.91838218]
[ 0.59445228 0.50795044 -1.04871713]
[ 0.95627647 0.51861237 -1.91861238]
[ 0.71488425 0.80140304 0.85057234]
[-0.83144865 1.40915456 -0.48493178]]

#linspace function : Interval bisection 
np.linspace(0,10,2)

array([ 0., 10.])

np.linspace(0,10,5)

array([ 0. , 2.5, 5. , 7.5, 10. ])

# Generate 0-10 Evenly distributed between 11 Number , Include 0,10
np.linspace(0,10,11)

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

Two 、 Generation of linear algebra

1. Matrix multiplication dot

#(1) Matrix multiplication dot
x=np.array([[1,2,3],[4,5,6]])
print(x)

[[1 2 3]
[4 5 6]]

2. Matrix inversion inv

y=np.array([[6,23],[-1,7],[8,9]])
print(y)

[[ 6 23]
[-1 7]
[ 8 9]]

x.dot(y) # Multiplication of matrices , Be careful : The number of rows to the left of the matrix = The number of columns in the right matrix 

array([[ 28, 64],
[ 67, 181]])

#(2) Matrix inversion inv
from numpy.linalg import inv
m=np.array([[4,2],[3,1]])
print(m)

[[4 2]
[3 1]]

inv(m)

array([[-0.5, 1. ],
[ 1.5, -2. ]])

3、 ... and 、 Generation of random numbers

from numpy import random
# normal Method : Generate random numbers that conform to normal distribution 
rArray=random.normal(size=(4,4))
print(rArray)

[[-0.06795971 -0.9353821 2.26863073 -0.75476106]
[-0.57931197 -0.35099822 0.84149375 1.55841483]
[ 1.3666115 0.16391788 -0.64410767 -1.5884498 ]
[-0.86659227 -0.60654638 1.34484013 -0.2681763 ]]

# randint Method : Generates randomly selected integers within a given upper and lower bounds 
random.randint(4,10)

9

i=0
while i <20:
print(random.randint(0,2)) # Random generation 0 and 1, Each run generates a random 
i+=1

0
0
0
1
1
1
0
0
0
0
1
0
1
0
0
1
1
0
1
0


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved