類似の操作を検索する方法の選択

組織化された証券市場で取引を行う際に同じ投資行動をとる顧客のグループを特定するという課題に直面しました。





問題を効果的に解決するには、まず、正しい定式化を決定する必要があります。





, . «» . – , . , , , .  , !





, :





  • ,





  • ( )





  • (/).





2 , , , , . – !





:





from matplotlib import pyplot as plt
from mpl_toolkits import mplot3d

fig = plt.figure()
ax = plt.axes(projection = '3d')
ax.scatter3D(X[:,0],X[:,1],X[:,2])
plt.show()
      
      



2 SKlearn ML – KMeans, DBSCAN () BallTree.





DBSCAN

DBSCAN (Density-based spatial clustering of applications with noise), ), , . —  ϵ- . , , , DBSCAN ? .





:





, , , . , , :





X = df[['ORDERDATETIME','SECURCODE','OPERATION']].values
model = DBSCAN(min_samples=2, eps = 0.5).fit(X)
df['LabelsDBS'] = model.labels_
df['ORDERDATETIME']=pd.to_datetime(df['ORDERDATETIME'])
gr=df.groupby(['LabelsDBS','CLIENTCODE']).count()

l=[]
ll=[]
for i in range(gr.shape[0]):
    l.append(gr.index[i][0])
    ll.append(gr.index[i][1])
l=pd.DataFrame(l)
l.rename(columns={0:'Ind'}, inplace=True)
l['Code']=ll
l=l.query('Ind > 0')
a = l.groupby('Ind').count()
a=a.query('Code>1 & Code<4').index.values
a=list(a)
l=l.query('Ind == @a')
l = pd.DataFrame(l.groupby('Ind')['Code'].apply(list))
      
      



, DBSCAN , .





KMeans

 k- .  k  – , . , .





  – ,  X. , .





  – , , . , – « k-».





  , , k- n- 1. . .. , , . .





, DBSCAN ( ) :





X = df[['ORDERDATETIME','SECURCODE','OPERATION']].values
model = KMeans(n_clusters=9900).fit(X)
      
      



, , – .





BallTree

ML – . . , , , .





, :





tree = BallTree(X, leaf_size=2)
dist, ind = tree.query(X, k=2)
l=[]
ll=[]
lll=[]
dist, ind = tree.query(X, k=2)
for i, (ind, d) in enumerate(zip(ind, dist)):
    print(f'Y index {i}, closest index X is {ind[1]}, dist {d[1]}')
    l.append(ind[0])
    ll.append(ind[1])
    lll.append(d[1])
for i in range(len(l)):
    l[i]=df.iloc[l[i]].CLIENTCODE
    ll[i]=df.iloc[ll[i]].CLIENTCODE
l=pd.DataFrame(l)
l.rename(columns={0:'Ind1'}, inplace=True)
l['Ind2']=ll
l['DIST']=lll
l = l.query('DIST>0 & Ind1 != Ind2')
l
      
      



, , , – , , , .





実行された作業に基づいて、類似した顧客操作を見つける方法が異なり、異なる数学的方法を使用しているという事実にもかかわらず、結論はほぼ類似していると結論付けることができます。これは、任意の方法を使用して、同様の操作の検索を絞り込むことができることを意味します。特定のタスクのためにより便利なツールを選択するという問題が残っています-利便性のために速度を犠牲にするか、自分で見つけた距離のアプリケーションを探すためです。あなたが決める!








All Articles