{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5577c946",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"import datetime\n",
"import numpy as np\n",
"\n",
"from signals import * #потом удалить"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "371f1ed0",
"metadata": {},
"outputs": [],
"source": [
"class voter_v2():\n",
" \n",
" def __init__(self,name):\n",
" self.name=name\n",
" pass\n",
" \n",
" def createPredictMatrixBySignals(self,signalsName):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "686462c0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7cd1daf0",
"metadata": {},
"outputs": [],
"source": [
"reqSig={\n",
" 'BB1':{\n",
" 'className':signal_BB,\n",
" 'indParams':{'MeanType':'SMA','window':15,'valueType':'close','kDev':2.5},\n",
" 'signalParams':{'source':'close','target':'close'},\n",
" 'batchSize':15\n",
" },\n",
" 'BB2':{\n",
" 'className':signal_BB,\n",
" 'indParams':{'MeanType':'SMA','window':15,'valueType':'close','kDev':2.5},\n",
" 'signalParams':{'source':'close','target':'close'},\n",
" 'batchSize':20\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f8307ee5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['BB1', 'BB2'])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reqCreate=reqSig.keys()\n",
"reqCreate"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c4498529",
"metadata": {},
"outputs": [],
"source": [
"class Voter():\n",
" \n",
" def __init__ (self, name=''):\n",
" \n",
" self.name=name\n",
" self.mop={\n",
" 'up': pd.DataFrame(),\n",
" 'down':pd.DataFrame(),\n",
" 'none':pd.DataFrame()\n",
" \n",
" }\n",
" self.value={}\n",
" self.decision=''\n",
" self.real_decision=''\n",
" self.keys=[]\n",
" self.slice_dict={}\n",
" \n",
" def addValue(self, dic_value):\n",
" self.value=dic_value\n",
" self.checkForNew()\n",
" self.setSlice()\n",
" self.getDecision()\n",
" \n",
"\n",
" \n",
" def checkForNew(self):\n",
" \n",
" if not (list(self.value.keys()) == self.keys):\n",
" self.createNewMop(list(self.value.keys()))\n",
"\n",
" \n",
" def createNewMop(self,missing_indicators):\n",
" print('reassembly mop')\n",
" new_columns= (missing_indicators)\n",
" \n",
" \n",
" #new_columns=new_columns.append(['value','p'])\n",
" \n",
" \n",
" \n",
" n=len(new_columns)\n",
" start_value=-1\n",
" variator=3\n",
" new_lst=[]\n",
" buf_lst=[]\n",
" for i in range(n):\n",
" buf_lst.append(start_value)\n",
"\n",
"\n",
" for i in range(pow(variator,n)):\n",
" new_lst.append(buf_lst.copy())\n",
"\n",
" for j in range(n):\n",
" for i in range(len(new_lst)):\n",
" dob_iterator=(i // pow(variator,j)) % variator\n",
" new_lst[i][j]=new_lst[i][j] + dob_iterator\n",
" \n",
" \n",
" #print (new_columns)\n",
" self.keys=new_columns\n",
" new_columns = new_columns+['amount']+['percentage']\n",
" \n",
" for i in new_lst:\n",
" i = i.extend([0,0])\n",
" #i = i.append(0)\n",
" \n",
" #print(new_lst)\n",
" #print(new_columns)\n",
" new_df=pd.DataFrame(new_lst,columns=new_columns)\n",
" \n",
" self.mop['up']=pd.DataFrame.from_dict(new_df.to_dict())\n",
" self.mop['down']=pd.DataFrame.from_dict(new_df.to_dict())\n",
" self.mop['none']=pd.DataFrame.from_dict(new_df.to_dict())\n",
" \n",
" \n",
" def setSlice(self):\n",
" \n",
" row_flg=True\n",
" self.slice_dict={}\n",
" for j in self.mop.keys():\n",
" for index, row in self.mop[j].iterrows():\n",
" for key, value in self.value.items():\n",
" if value != row[key]:\n",
" #print('fasle ',key,value,row[key])\n",
" row_flg=False\n",
" break\n",
" if row_flg:\n",
" self.slice_dict[j]=dict(row)\n",
" #print(j,dict(row))\n",
" row_flg=True\n",
"\n",
" def getDecision (self):\n",
" \n",
" max_value=0\n",
" for key, value in self.slice_dict.items():\n",
" if value['amount'] >= max_value:\n",
" max_value = value['amount']\n",
" self.decision = key\n",
" return self.decision\n",
" \n",
" def setDecision (self,real_decision):\n",
" self.real_decision=real_decision\n",
" self.updMop()\n",
" self.slice_dict[real_decision]['amount']+=1\n",
"\n",
"\n",
" def updMop(self):\n",
" \n",
" row_flg=True\n",
" for index, row in self.mop[self.real_decision].iterrows():\n",
" for key, value in self.value.items():\n",
" if value != row[key]:\n",
" row_flg=False\n",
" break\n",
" if row_flg:\n",
" #self.slice_dict[j]=dict(row)\n",
" row['amount']=row['amount']+1\n",
" row_flg=True "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0acec1f4",
"metadata": {},
"outputs": [],
"source": [
"test_dic_value_1={'lupa':1 }\n",
"test_dic_value_2={'lupa':1 , 'pupa':1}\n",
"test_dic_value_3={'lupa':1 , 'pupa':1 , 'zalupa':1 , 'zapupa':1 }\n",
"test_dic_value_4={'lupa':1 , 'pupa':1 , 'zalupa':1 , 'zapupa':-1 }"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c527bb7b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"reassembly mop\n"
]
},
{
"data": {
"text/plain": [
"'none'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test=Voter('huita')\n",
"test.addValue(test_dic_value_2)\n",
"test.decision\n",
"test.getDecision()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "070f2c3d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'down'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test.setDecision('down')\n",
"test.getDecision()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e604c217",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'up': {'lupa': 1, 'pupa': 1, 'amount': 0, 'percentage': 0},\n",
" 'down': {'lupa': 1, 'pupa': 1, 'amount': 1, 'percentage': 0},\n",
" 'none': {'lupa': 1, 'pupa': 1, 'amount': 0, 'percentage': 0}}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test.slice_dict"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77e7d47c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 10,
"id": "bae1a649",
"metadata": {},
"outputs": [],
"source": [
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9fcd15a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 1, 'b': 2}\n"
]
}
],
"source": [
"dictionary_data = {\"a\": 1, \"b\": 2}\n",
"\n",
"\n",
"a_file = open(\"data.pkl\", \"wb\")\n",
"\n",
"pickle.dump(dictionary_data, a_file)\n",
"\n",
"a_file.close()\n",
"\n",
"\n",
"a_file = open(\"data.pkl\", \"rb\")\n",
"\n",
"output = pickle.load(a_file)\n",
"\n",
"print(output)\n",
"\n",
"\n",
"\n",
"a_file.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "479a0a6c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6dd64c9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 12,
"id": "9a7f4443",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('bar', 'one'),\n",
" ('bar', 'two'),\n",
" ('baz', 'one'),\n",
" ('baz', 'two'),\n",
" ('foo', 'one'),\n",
" ('foo', 'two'),\n",
" ('qux', 'one'),\n",
" ('qux', 'two')]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arrays = [\n",
"\n",
" [\"bar\", \"bar\", \"baz\", \"baz\", \"foo\", \"foo\", \"qux\", \"qux\"],\n",
"\n",
" [\"one\", \"two\", \"one\", \"two\", \"one\", \"two\", \"one\", \"two\"],\n",
"\n",
"]\n",
"tuples = list(zip(*arrays))\n",
"tuples"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7c25608e",
"metadata": {},
"outputs": [],
"source": [
"index = pd.MultiIndex.from_tuples(tuples, names=[\"first\", \"second\"])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "7a336297",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" 0 | \n",
"
\n",
" \n",
" | first | \n",
" second | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | bar | \n",
" one | \n",
" -0.323133 | \n",
"
\n",
" \n",
" | two | \n",
" -0.668283 | \n",
"
\n",
" \n",
" | baz | \n",
" one | \n",
" 0.731206 | \n",
"
\n",
" \n",
" | two | \n",
" 0.540344 | \n",
"
\n",
" \n",
" | foo | \n",
" one | \n",
" -0.468415 | \n",
"
\n",
" \n",
" | two | \n",
" 0.608409 | \n",
"
\n",
" \n",
" | qux | \n",
" one | \n",
" 1.287331 | \n",
"
\n",
" \n",
" | two | \n",
" 0.971639 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0\n",
"first second \n",
"bar one -0.323133\n",
" two -0.668283\n",
"baz one 0.731206\n",
" two 0.540344\n",
"foo one -0.468415\n",
" two 0.608409\n",
"qux one 1.287331\n",
" two 0.971639"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.DataFrame(np.random.randn(8), index=index)\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "2ba869ba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: {('bar', 'one'): -0.32313296864285923,\n",
" ('bar', 'two'): -0.6682834081303028,\n",
" ('baz', 'one'): 0.7312057226105624,\n",
" ('baz', 'two'): 0.540343770516433,\n",
" ('foo', 'one'): -0.4684147754956174,\n",
" ('foo', 'two'): 0.6084089009514035,\n",
" ('qux', 'one'): 1.2873307544293144,\n",
" ('qux', 'two'): 0.9716394379849151}}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.to_dict()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "255ef65b",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "ValueError",
"evalue": "No axis named ('bar', 'one') for object type DataFrame",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(cls, axis)\u001b[0m\n\u001b[1;32m 552\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_AXIS_TO_AXIS_NUMBER\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 553\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 554\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"No axis named {axis} for object type {cls.__name__}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: ('bar', 'one')",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_30327/2389569466.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'bar'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'one'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, axis)\u001b[0m\n\u001b[1;32m 718\u001b[0m \u001b[0;31m# we need to return a copy of ourselves\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 719\u001b[0m \u001b[0mnew_self\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 720\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 721\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0maxis\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 722\u001b[0;31m \u001b[0maxis_int_none\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_axis_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 723\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 724\u001b[0m \u001b[0maxis_int_none\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mnew_self\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maxis\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0maxis_int_none\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(cls, axis)\u001b[0m\n\u001b[1;32m 550\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_get_axis_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAxis\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mAxisInt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 551\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 552\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_AXIS_TO_AXIS_NUMBER\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 553\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 554\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"No axis named {axis} for object type {cls.__name__}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: No axis named ('bar', 'one') for object type DataFrame"
]
}
],
"source": [
"s.loc(('bar', 'one'))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "b040e5c3",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" col1 | \n",
" col2 | \n",
"
\n",
" \n",
" | first | \n",
" second | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | up | \n",
" up | \n",
" -0.830328 | \n",
" 0.809854 | \n",
"
\n",
" \n",
" | down | \n",
" 0.266291 | \n",
" -1.134302 | \n",
"
\n",
" \n",
" | none | \n",
" -0.006435 | \n",
" -0.469855 | \n",
"
\n",
" \n",
" | down | \n",
" up | \n",
" -0.749732 | \n",
" 0.062220 | \n",
"
\n",
" \n",
" | down | \n",
" 0.410828 | \n",
" 0.764155 | \n",
"
\n",
" \n",
" | none | \n",
" -1.100494 | \n",
" -0.326324 | \n",
"
\n",
" \n",
" | none | \n",
" up | \n",
" -0.153249 | \n",
" 2.090658 | \n",
"
\n",
" \n",
" | down | \n",
" -2.504126 | \n",
" 0.974883 | \n",
"
\n",
" \n",
" | none | \n",
" -0.324719 | \n",
" 0.857353 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" col1 col2\n",
"first second \n",
"up up -0.830328 0.809854\n",
" down 0.266291 -1.134302\n",
" none -0.006435 -0.469855\n",
"down up -0.749732 0.062220\n",
" down 0.410828 0.764155\n",
" none -1.100494 -0.326324\n",
"none up -0.153249 2.090658\n",
" down -2.504126 0.974883\n",
" none -0.324719 0.857353"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iterables = [[\"up\", \"down\", \"none\"], [\"up\", \"down\", \"none\"]]\n",
"df = pd.DataFrame({'col1': np.random.randn(9),'col2': np.random.randn(9)}, index=pd.MultiIndex.from_product(iterables, names=[\"first\", \"second\"]))\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "c4ee0bcf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'_is_copy': None,\n",
" '_mgr': BlockManager\n",
" Items: Index(['col1', 'col2'], dtype='object')\n",
" Axis 1: MultiIndex([( 'up', 'up'),\n",
" ( 'up', 'down'),\n",
" ( 'up', 'none'),\n",
" ('down', 'up'),\n",
" ('down', 'down'),\n",
" ('down', 'none'),\n",
" ('none', 'up'),\n",
" ('none', 'down'),\n",
" ('none', 'none')],\n",
" names=['first', 'second'])\n",
" NumpyBlock: slice(0, 2, 1), 2 x 9, dtype: float64,\n",
" '_item_cache': {},\n",
" '_attrs': {},\n",
" '_flags': }"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20c753b5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b53c4870",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d4c91cd9",
"metadata": {},
"outputs": [],
"source": [
"def createDF(namesIndex, namesColoms):\n",
" trandeValuesList = ['up','none','down']\n",
" colomsName_lvl = ['trande','amaunt','probability'] \n",
" #micolumns = pd.MultiIndex.from_tuples(\n",
" #[('amaunt', 'up'), ('amaunt', 'none'), ('amaunt', 'down'), ('trande',),('probability',)], names=[\"lvl0\", \"lvl1\"]\n",
" #) \n",
" df = pd.DataFrame({\n",
" 'trande': [None]*pow(3,len(namesIndex)),\n",
" 'amaunt': [None]*pow(3,len(namesIndex)),\n",
" 'probability': [None]*pow(3,len(namesIndex))\n",
" },\n",
" index=pd.MultiIndex.from_product([trandeValuesList]*len(namesIndex), names=namesIndex)\n",
" ,columns=namesColoms\n",
" )\n",
" return(df)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "d560869c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" | \n",
" trande | \n",
" amaunt | \n",
" probability | \n",
"
\n",
" \n",
" | 1 | \n",
" 2 | \n",
" 3 | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | up | \n",
" up | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" up | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" up | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" up | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | none | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" | down | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" trande amaunt probability\n",
"1 2 3 \n",
"up up up None None None\n",
" none None None None\n",
" down None None None\n",
" none up None None None\n",
" none None None None\n",
" down None None None\n",
" down up None None None\n",
" none None None None\n",
" down None None None\n",
"none up up None None None\n",
" none None None None\n",
" down None None None\n",
" none up None None None\n",
" none None None None\n",
" down None None None\n",
" down up None None None\n",
" none None None None\n",
" down None None None\n",
"down up up None None None\n",
" none None None None\n",
" down None None None\n",
" none up None None None\n",
" none None None None\n",
" down None None None\n",
" down up None None None\n",
" none None None None\n",
" down None None None"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd=createDF( ['1','2','3'],['trande','amaunt','probability'] )\n",
"dd"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "fb8619e4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" col1 | \n",
" col2 | \n",
"
\n",
" \n",
" | first | \n",
" second | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | up | \n",
" down | \n",
" 0.266291 | \n",
" -1.134302 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" col1 col2\n",
"first second \n",
"up down 0.266291 -1.134302"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs(('up','down'), level=['first','second'])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "71a26580",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 2 3 \n",
"up up up None\n",
" none None\n",
" down None\n",
" none up None\n",
" none None\n",
" down None\n",
" down up None\n",
" none None\n",
" down None\n",
"none up up None\n",
" none None\n",
" down None\n",
" none up None\n",
" none None\n",
" down None\n",
" down up None\n",
" none None\n",
" down None\n",
"down up up None\n",
" none None\n",
" down None\n",
" none up None\n",
" none None\n",
" down None\n",
" down up None\n",
" none None\n",
" down None\n",
"Name: trande, dtype: object"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd['trande']"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "e0c593d4",
"metadata": {},
"outputs": [],
"source": [
"tvl = ['up','none','down']\n",
"colomsName_lvl = ['trande','amaunt','probability'] "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "5630f999",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('amaunt', 'up'), ('amaunt', 'none'), ('amaunt', 'down')]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuplesCol = list(zip(['amaunt']*3,tvl))\n",
"tuplesCol"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "6c1a5251",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"col1 0.266291\n",
"col2 -1.134302\n",
"Name: (up, down), dtype: float64"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['up','down']"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "154cf214",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"col1 0.266291\n",
"col2 -1.134302\n",
"Name: (up, down), dtype: float64"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs(('up','down'), level=['first','second']).iloc[0]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "93b3c13c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'index': [('up', 'up'),\n",
" ('up', 'down'),\n",
" ('up', 'none'),\n",
" ('down', 'up'),\n",
" ('down', 'down'),\n",
" ('down', 'none'),\n",
" ('none', 'up'),\n",
" ('none', 'down'),\n",
" ('none', 'none')],\n",
" 'columns': ['col1', 'col2'],\n",
" 'data': [[-0.830327734063029, 0.8098539186544432],\n",
" [0.2662913915879257, -1.1343017479770143],\n",
" [-0.006434801735169448, -0.46985488578256207],\n",
" [-0.7497323836837398, 0.0622198023411267],\n",
" [0.4108275780419986, 0.7641549838488797],\n",
" [-1.100493867912701, -0.3263235032404246],\n",
" [-0.15324871520432234, 2.090658352096281],\n",
" [-2.504125539787604, 0.9748834191274302],\n",
" [-0.32471865443845527, 0.8573529089017704]],\n",
" 'index_names': ['first', 'second'],\n",
" 'column_names': [None]}"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_d=df.to_dict('tight')\n",
"df_d"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "8dab7e8a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['first', 'second']"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_d['index_names']"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "3927495f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" col1 | \n",
" col2 | \n",
"
\n",
" \n",
" | first | \n",
" second | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | up | \n",
" up | \n",
" -0.830328 | \n",
" 0.809854 | \n",
"
\n",
" \n",
" | down | \n",
" 0.266291 | \n",
" -1.134302 | \n",
"
\n",
" \n",
" | none | \n",
" -0.006435 | \n",
" -0.469855 | \n",
"
\n",
" \n",
" | down | \n",
" up | \n",
" -0.749732 | \n",
" 0.062220 | \n",
"
\n",
" \n",
" | down | \n",
" 0.410828 | \n",
" 0.764155 | \n",
"
\n",
" \n",
" | none | \n",
" -1.100494 | \n",
" -0.326324 | \n",
"
\n",
" \n",
" | none | \n",
" up | \n",
" -0.153249 | \n",
" 2.090658 | \n",
"
\n",
" \n",
" | down | \n",
" -2.504126 | \n",
" 0.974883 | \n",
"
\n",
" \n",
" | none | \n",
" -0.324719 | \n",
" 0.857353 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" col1 col2\n",
"first second \n",
"up up -0.830328 0.809854\n",
" down 0.266291 -1.134302\n",
" none -0.006435 -0.469855\n",
"down up -0.749732 0.062220\n",
" down 0.410828 0.764155\n",
" none -1.100494 -0.326324\n",
"none up -0.153249 2.090658\n",
" down -2.504126 0.974883\n",
" none -0.324719 0.857353"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf = pd.DataFrame.from_dict(df_d, orient='tight')\n",
"ddf"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "a8a0dc96",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "369b0f8f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"col1 0.266291\n",
"col2 -1.134302\n",
"Name: (up, down), dtype: float64"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf.xs(('up','down'), level=['first','second']).iloc[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85e35de4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}