课堂代码及注释
from scipy import stats
import numpy as np
import scipy. stats as stats
import matplotlib. pyplot as plt
from mpl_toolkits. mplot3d import Axes3D
import pandas as pd
rv= stats. norm( 2 , 3 )
dir ( rv)
rv. cdf( 2 )
rv. pdf( 2 )
rv. ppf( 0.9 )
x= np. linspace( - 7 , 11 , num= 100 )
rv. pdf( x)
plt. plot( x, rv. pdf( x) )
x1= x2= np. linspace( 0 , 4 , 21 )
def fx ( x1, x2) :
return np. exp( - ( x1+ x2) )
fx( 1 , 2 )
np. meshgrid( x1, x2)
X , Y = np. meshgrid( x1, x2)
a, b= np. meshgrid( [ 1 , 2 ] , [ 4 , 5 , 6 ] )
np. ravel( a)
np. ravel( a) ; np. ravel( b)
fx( np. ravel( a) , np. ravel( b) )
fx( np. ravel( X) , np. ravel( Y) )
c= np. array( fx( np. ravel( a) , np. ravel( b) ) )
C= c. reshape( a. shape)
fig= plt. figure( )
ax= fig. gca( projection= "3d" )
ax. plot_surface( a, b, C)
plt. show( )
z= np. array( fx( np. ravel( X) , np. ravel( Y) ) )
Z= z. reshape( X. shape)
fig= plt. figure( )
ax= fig. gca( projection= "3d" )
ax. plot_surface( X, Y, Z)
plt. show( )
def f1x ( x1) :
return np. exp( - x1)
pass
x1= np. linspace( 0 , 10 , num= 101 )
y= f1x( x1)
plt. plot( x1, y)
dat= np. random. randn( 50 , 4 ) ; dat
dat= pd. DataFrame( dat) ; dat
dat. mean( )
dat. cov( )
dat. corr( )
trees= pd. read_csv( "F:\\基础数学课\\应用多元统计分析\\trees.csv" )
type ( trees)
trees. mean( )
trees. cov( )
trees. corr( )
trees. var( )
trees. std( )
trees. to_numpy( )
trees. to_numpy( ) . mean( )
trees. iloc[ : , 0 ]
trees. mean( ) [ 0 ]
( trees. iloc[ : , 0 ] - trees. mean( ) [ 0 ] ) / trees. std( ) [ 0 ]
D= np. diag( trees. std( ) ) ; D
trees_change= np. ones( ( 31 , 1 ) ) . dot( trees. mean( ) . to_numpy( ) . reshape( ( 1 , 3 ) ) )
( trees- trees_change) . dot( np. linalg. inv( D) )
D. dot( trees. corr( ) ) . dot( D)
其他笔记
import numpy as np
import scipy. stats as stats
import matplotlib. pyplot as plt
from mpl_toolkits. mplot3d import Axes3D
rv = stats. norm( 2 , 3 )
dir ( rv)
rv. cdf( 2 )
rv. pdf( 2 )
rv. ppf( 0.975 )
x= np. linspace( - 7 , 11 , 100 )
y= rv. pdf( x)
plt. plot( x, y)
x= y= np. linspace( 0 , 4 , 21 )
def fx ( x1, x2) :
return np. exp( - ( x1+ x2) )
X= np. meshgrid( x, y)
X, Y= np. meshgrid( x, y)
Z = fx( X, Y)
x= ( [ 1 , 2 ] )
y= ( [ 3 , 4 , 5 ] )
X, Y = np. meshgrid( x, y)
Z= fx( X, Y)
fig= plt. figure( )
ax= fig. gca( projection= "3d" )
ax. plot_surface( X, Y, Z)
plt. show( )
def f1x ( x1) :
return np. exp( - x1)
x1= np. linspace( 0 , 10 , 101 )
y= f1x( x1)
plt. plot( x1, y)
import pandas as pd
import numpy as np
dat= np. random. randn( 50 , 4 )
dat= pd. DataFrame( dat)
dat. mean( )
dat. cov( )
dat. corr( )
第三次作业
import numpy as np
import matplotlib. pyplot as plt
from mpl_toolkits. mplot3d import Axes3D
def fx ( x1, x2) :
return ( 6 / 5 * x1** 2 ) * ( 4 * x1* x2+ 1 )
x1= x2= np. linspace( 0 , 1 , 41 )
np. meshgrid( x1, x2)
X , Y = np. meshgrid( x1, x2)
z= np. array( fx( np. ravel( X) , np. ravel( Y) ) )
Z= z. reshape( X. shape)
fig= plt. figure( )
ax= fig. gca( projection= "3d" )
ax. plot_surface( X, Y, Z)
plt. show( )
def f1x ( x1) :
return 12 / 5 * x1** 3 + 6 / 5 * x1** 2
x1= np. linspace( 0 , 1 , num= 101 )
y1= f1x( x1)
plt. plot( x1, y1)
def f2x ( x2) :
return 6 / 5 * x2** 2 + 2 / 5
x1= np. linspace( 0 , 1 , num= 101 )
y2= f2x( x2)
plt. plot( x2, y2)
def fx1_x2 ( x1, x2) :
return 3 * x1** 2 * ( 4 * x1* x2+ 1 ) / ( 3 * x2+ 1 )
x1= np. linspace( 0 , 1 , num= 101 )
y3= fx1_x2( x1, 0.5 )
plt. plot( x1, y3)
def fx2_x1 ( x1, x2) :
return ( 4 * x1* x2+ 1 ) / ( 2 * x1+ 1 )
x2= np. linspace( 0 , 1 , num= 101 )
y4= fx2_x1( 0.5 , x2)
plt. plot( x2, y4)
x_cov1= np. array( [ [ 9 , 1 , - 2 ] , [ 1 , 20 , 3 ] , [ - 2 , 3 , 12 ] ] )
type ( x_cov1)
A= np. array( [ [ 2 , 3 , 1 ] , [ 1 , - 2 , 5 ] , [ 0 , 1 , - 1 ] ] )
y_cov= A. dot( x_cov1) . dot( A. T) ; y_cov
x_cov2= np. array( [ [ 16 , - 4 , 3 ] , [ - 4 , 4 , - 2 ] , [ 3 , - 2 , 9 ] ] )
D= np. diag( [ 1 / 4 , 1 / 2 , 1 / 3 ] )
R_x = D. dot( x_cov2) . dot( D) ; R_x
import pandas as pd
import numpy as np
import matplotlib. pyplot as plt
from mpl_toolkits. mplot3d import Axes3D
trees = pd. read_csv( "F:\\基础数学课\\应用多元统计分析\\trees.csv" )
trees. mean( )
trees. cov( )
trees. corr( )
trees. var( )
trees. std( )