2d arrayと1d arrayの掛け算

a=np.arange(9).reshape([3,3]) b=np.array([10,10,10]) a Out[16]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) b Out[18]: array([10, 10, 10]) np.dot(a,b) Out[22]: array([ 30, 120, 210]) np.dot(b,a) Out[23]: array([ 90, 120, 150]) c=np.arange(6).…

IDに対応する値のベクトル

ids = np.array([0,0,0,1,1,2]) x=np.array([5,10,15]) x[ids] Out[0]: array([ 5, 5, 10, 10, 15])

csvを読むよりnumpy arrayをロードした方がだいぶ速い

大きなファイルだと、np.genfromtxtとnp.loadでスピードにかなり差が出る。 import numpy as np import time a=np.arange(10000).reshape([1000,10]) np.save('data_to_read_np',a) np.savetxt('data_to_read_csv.csv',a,delimiter=',') 1000回ずつ読み込む…

Bonnet, C{\'e}line and Dubois, Pierre (2010) Inference on vertical contracts between manufacturers and retailers allowing for nonlinear pricing and resale price maintenance., The RAND Journal of Economics, 41(1), 139-164. フランスのミネラ…

社会学と心理学の関係について触れられている論文二つ。著者は"The new institutionalism in organizational analysis"のDimaggioとPowell。 Dimaggio, Paul (1997) Culture and Cognition. Annual review of sociology, 23, 263-287. 心理学のスキーマ概念…

Fan, Ying (2013) Ownership Consolidation and Product Characteristics: A Study of the US Daily Newspaper Market. American Economic Review, 103(5), 1598-1628 countyレベルの新聞のデータを使って、競争環境が紙面に与える影響を推定した論文。Steve…

arrayの中から要素の位置をみつける

np.in1dを使うと、arrayの中から特定した要素と一致するindexを探せる。 a=np.array([0,0,0,1,1,2,2]) b=np.array([1,2]) np.in1d(a,b) Out[51]: array([False, False, False, True, True, True, True], dtype=bool)

ベクトルの繰り返しでmatrixをつくる

一次元のarrayの繰り返しでmatrixを作る機会はそれなりに多い。Matlabではonesとのクロネッカー積を使っていたが、pythonでは代わりにrepeatとtileを使っている。 a=np.arange(3) a Out[0]: array([0, 1, 2]) 列ベクトルとみなして行方向に繰り返すなら、rep…

scipy.optimize: argsの付け方

方法1:inputにアスタリスクをつけてタプルで受け取れるようにする。 def func(x,*a): return np.sum((x-a)**2)+1 scipy.optimize.minimize(func,x0=np.array([7,10,3.]),method='SLSQP',args=(2.5, )) 方法2:関数自体を返す関数を作る。 def make_func()…

scipy.optimize:制約条件の付け方

import numpy as np import scipy.optimize def func(x): return np.sum((x-2.5)**2)+1 inequality constraintは、returnが正であるような形で関数を書いて指定する。 def con1(x): return 2-x[0] cons1 = ({'type':'ineq','fun':con1}) boundaryはnp.array…