创建通用函数
from __future__ import print_function
import numpy as np
def double ( a) :
return 2 * a
ufunc = np. frompyfunc( double, 1 , 1 )
print ( "Result" , ufunc( np. arange( 4 ) ) )
勾股数
from __future__ import print_function
import numpy as np
m = np. arange( 33 )
n = np. arange( 33 )
a = np. subtract. outer( m ** 2 , n ** 2 )
b = 2 * np. multiply. outer( m, n)
c = np. add. outer( m ** 2 , n ** 2 )
idx = np. where( ( a + b + c) == 1000 )
np. testing. assert_equal( a[ idx] ** 2 + b[ idx] ** 2 , c[ idx] ** 2 )
print ( a[ idx] , b[ idx] , c[ idx] )
CharArray 字符串操作
import urllib2
import numpy as np
import re
response = urllib2. urlopen( 'http://python.org/' )
html = response. read( )
html = re. sub( r'<.*?>' , '' , html)
carray = np. array( html) . view( np. chararray)
carray = carray. expandtabs( 1 )
carray = carray. splitlines( )
print ( carray)
创建屏蔽数组
from __future__ import print_function
import numpy as np from scipy. misc
import lena
import matplotlib. pyplot as plt
lena = lena( )
random_mask = np. random. randint( 0 , 2 , size= lena. shape)
plt. subplot( 221 )
plt. title( "Original" )
plt. imshow( lena)
plt. axis( 'off' )
masked_array = np. ma. array( lena, mask= random_mask)
print ( masked_array)
plt. subplot( 222 )
plt. title( "Masked" )
plt. imshow( masked_array)
plt. axis( 'off' )
忽略负数以及极值
from __future__ import print_function
import numpy as np
from matplotlib. finance
import quotes_historical_yahoo
from datetime import date
import matplotlib. pyplot as plt
def get_close ( ticker) :
today = date. today( )
start = ( today. year - 1 , today. month, today. day)
quotes = quotes_historical_yahoo( ticker, start, today)
return np. array( [ q[ 4 ] for q in quotes] )
close = get_close( 'AAPL' )
triples = np. arange( 0 , len ( close) , 3 )
print ( "Triples" , triples[ : 10 ] , "..." )
signs = np. ones( len ( close) )
print ( "Signs" , signs[ : 10 ] , "..." )
signs[ triples] = - 1
print ( "Signs" , signs[ : 10 ] , "..." )
ma_log = np. ma. log( close * signs)
print ( "Masked logs" , ma_log[ : 10 ] , "..." )
dev = close. std( )
avg = close. mean( )
inside = np. ma. masked_outside( close, avg - dev, avg + dev)
print ( "Inside" , inside[ : 10 ] , "..." )
plt. subplot( 311 )
plt. title( "Original" )
plt. plot( close)
plt. subplot( 312 )
plt. title( "Log Masked" )
plt. plot( np. exp( ma_log) )
plt. subplot( 313 )
plt. title( "Not Extreme" )
plt. plot( inside)
plt. tight_layout( )
plt. show( )
记录数组
from __future__ import print_function
import numpy as np from matplotlib. finance
import quotes_historical_yahoo
from datetime import date
tickers = [ 'MRK' , 'T' , 'VZ' ]
def get_close ( ticker) :
today = date. today( )
start = ( today. year - 1 , today. month, today. day)
quotes = quotes_historical_yahoo( ticker, start, today)
return np. array( [ q[ 4 ] for q in quotes] )
weights = np. recarray( ( len ( tickers) , ) , dtype= [ ( 'symbol' , np. str_, 16 ) ,
( 'stdscore' , float ) , ( 'mean' , float ) , ( 'score' , float ) ] )
for i, ticker in enumerate ( tickers) :
close = get_close( ticker)
logrets = np. diff( np. log( close) )
weights[ i] [ 'symbol' ] = ticker
weights[ i] [ 'mean' ] = logrets. mean( )
weights[ i] [ 'stdscore' ] = 1 / logrets. std( )
weights[ i] [ 'score' ] = 0
for key in [ 'mean' , 'stdscore' ] :
wsum = weights[ key] . sum ( )
weights[ key] = weights[ key] / wsum
weights[ 'score' ] = ( weights[ 'stdscore' ] + weights[ 'mean' ] ) / 2 weights[ 'score' ] . sort( )
for record in weights:
print ( "%s,mean=%.4f,stdscore=%.4f,score=%.4f" % ( record[ 'symbol' ] , record[ 'mean' ] , record[ 'stdscore' ] , record[ 'score' ] ) )
'''
MRK,mean=0.8185,stdscore=0.2938,score=0.2177
T,mean=0.0927,stdscore=0.3427,score=0.2262
VZ,mean=0.0888,stdscore=0.3636,score=0.5561
'''