python 自分定義の関数モジュールを、同じディレクトリ内で使う

同じディレクトリ下にあるなら、話は簡単で、
my_func.pyのファイルに、関数を定義する。

import numpy as np
import pandas as pd
from sklearn.neighbors import KernelDensity
from scipy.optimize import minimize
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import LeaveOneOut

def mpPDF(var,q,pts):
    #Marchenko-Pastur probability density variable
    # q=T/N
    eMin,eMax=var*(1-(1./q)**.5)**2,var*(1+(1./q)**.5)**2
    eVal=np.linspace(eMin,eMax,pts)
    pdf=q/(2*np.pi*var*eVal)*((eMax-eVal)*(eVal-eMin))**.5
    if eVal.ndim == 2:
        eVal=eVal.reshape(-1)
    if pdf.ndim==2:
        pdf=pdf.reshape(-1)
    pdf=pd.Series(pdf,index=eVal)

    return pdf

def getPCA(matrix):
    #eVal and eVec from Hermite Matrix
    eVal,eVec=np.linalg.eigh(matrix)
    indices=eVal.argsort()[::-1]
    eVal,eVec=eVal[indices],eVec[:,indices]
    eVal=np.diagflat(eVal)
    return eVal, eVec

def fitKDE(obs,bWidth=.25,kernel='gaussian',x=None):
    #fit Kernel to observe value array and get PDF

これらの関数を、同じディレクトリの別コードで使うには、

import my_func as MP
eVal0,eVec0=MP.getPCA(return_corr)

のように、使う。

この記事が気に入ったらサポートをしてみませんか?