{ "cells": [ { "cell_type": "code", "execution_count": 374, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", " \n", "def select_alternating_columns(a: np.ndarray) -> np.ndarray:\n", " bb=np.delete(a,np.s_[1::2],1) \n", " return bb\n", " \"\"\" \n", " Select alternating columns starting from the 0-th\n", " index of `a`. `a` will be at least 2 dimensions.\n", " \n", " >>> a = np.array([[0, 1, 2],\n", " ... [3, 4, 5]])\n", " >>> select_alternating_columns(a)\n", " array([[0, 2],\n", " [3, 5]])\n", " \"\"\"\n", "\n", " \n", "def popcount_rows(a: np.ndarray) -> np.ndarray:\n", " BB=np.array([])\n", " for idd in range(a.shape[0]):\n", " c=''.join([str(aa) for aa in list(a[idd,:])]).count('1')\n", " BB=np.append(BB,c)\n", " return BB\n", " \"\"\"\n", " Return an array containing the popcount of every row \n", " in `a`. `a` is 2d and consists of 0s and 1s only.\n", " \n", " >>> a = np.array([[0, 0, 1],\n", " ... [0, 0, 0],\n", " ... [1, 0, 1],\n", " ... [1, 1, 1]])\n", " >>> popcount_rows(a)\n", " array([1, 0, 2, 3])\n", " \"\"\"\n", " pass\n", "\n", "def remove_all_zero_rows(a: np.ndarray) -> np.ndarray:\n", " B=np.sum(abs(a),axis=1)\n", " C=a[B!=0]\n", " return C\n", " \"\"\" \n", " Removes any rows that entirely consist of zeros from `a`.\n", " \n", " >>> a = np.array([[0, 0, 0],\n", " ... [0, 0, 1]])\n", " >>> remove_all_zero_rows(a)\n", " array([[0, 0, 1]])\n", " \"\"\"\n", " pass\n", " \n", "def swap_halves(a: np.ndarray) -> np.ndarray:\n", " b=a.copy()\n", " d=len(a)\n", " if d%2!=0:\n", " c=d//2+1\n", " b[:c]=a[-c:]\n", " b[-c+1:]=a[:c-1]\n", " else:\n", " c=d//2\n", " b[:c]=a[-c:]\n", " b[-c:]=a[:c]\n", " return b \n", " \"\"\"\n", " Swaps the front and back halves of `a`, which\n", " is at least 2 dimensions. If the array's size \n", " is odd, includes the middle element as the \n", " first element of the back half.\n", " \n", " >>> swap_halves(np.array([0, 1, 2, 3]))\n", " array([2, 3, 0, 1])\n", " >>> swap_halves(np.array([0, 1, 2]))\n", " array([1, 2, 0])\n", " >>> a = np.reshape(range(8), [4, 2])\n", " >>> a\n", " array([[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7]])\n", " >>> swap_halves(a)\n", " array([[4, 5],\n", " [6, 7],\n", " [0, 1],\n", " [2, 3]])\n", " \"\"\"\n", " pass\n", " \n", "def trim_zeros_on_edges_2d(a: np.ndarray) -> np.ndarray:\n", " B=np.sum(abs(a),axis=1)\n", " C=a[B!=0]\n", " MM=np.sum(abs(C),axis=0)\n", " return C.T[MM!=0].T\n", " \"\"\"\n", " Trims zeros around a rectangular 1-delimited section.\n", " The section delimited by 1s will always be\n", " rectangular and there is only one such section.\n", " `a` will be 2d and consist of 0s and 1s only.\n", "\n", " >>> a = np.array([[0, 0, 0, 0, 0, 0],\n", " ... [0, 1, 1, 1, 0, 0],\n", " ... [0, 1, 0, 1, 0, 0],\n", " ... [0, 1, 1, 1, 0, 0],\n", " ... [0, 0, 0, 0, 0, 0]])\n", " >>>\n", " >>> trim_zeros_on_edges_2d(a)\n", " array([[1, 1, 1],\n", " [1, 0, 1],\n", " [1, 1, 1]])\n", " \"\"\"\n", " pass\n", " \n", "def make_chessboard(size: int) -> np.ndarray:\n", " A=np.zeros([size,size],dtype=int)\n", " for i in range(size):\n", " for j in range(size):\n", " if (i+j)%2==1:\n", " A[i,j]=1\n", " return A\n", " \"\"\"\n", " Makes a 2d chessboard pattern with both dimensions \n", " equal to `size`. The top-left corner should be 0.\n", " `size` must be >= 0.\n", " \n", " >>> make_chessboard(3)\n", " array([[0, 1, 0],\n", " [1, 0, 1],\n", " [0, 1, 0]])\n", " >>> make_chessboard(4)\n", " array([[0, 1, 0, 1],\n", " [1, 0, 1, 0],\n", " [0, 1, 0, 1],\n", " [1, 0, 1, 0]])\n", " \"\"\"\n", " pass\n", " \n", "def quad(a: np.ndarray) -> np.ndarray:\n", " if len(a.shape) ==1:\n", " a=a[None,:]\n", " a1,a2=a.shape\n", " B=np.empty([a1*2,a2*2],dtype=type(a))\n", " for idd in range(a1):\n", " B[idd]=np.append(a[idd],a[idd])\n", " B[idd+1:]=B[0:idd+1]\n", " return B\n", " \"\"\"\n", " Repeat the array horizontally and vertically.\n", " \n", " >>> quad(np.array([0, 1]))\n", " array([[0, 1, 0, 1],\n", " [0, 1, 0, 1]])\n", " >>> quad(np.array([[0, 1], [2, 3]]))\n", " array([[0, 1, 0, 1],\n", " [2, 3, 2, 3],\n", " [0, 1, 0, 1],\n", " [2, 3, 2, 3]])\n", " \"\"\"\n", " pass\n", " \n", "def reflect_quad(a: np.ndarray) -> np.ndarray:\n", " if len(a.shape) ==1:\n", " a=a[None,:]\n", " a1,a2=a.shape\n", " B=np.empty([a1*2,a2*2],dtype=type(a))\n", " for idd in range(a1):\n", " B[idd]=np.append(a[idd],np.flip(a[idd]))\n", " B[idd+1:]=B[idd::-1]\n", " return B\n", " \"\"\"\n", " Repeat the array horizontally and vertically but\n", " also flip/mirror/reflect the repeated array around \n", " the middle.\n", " \n", " >>> reflect_quad(np.array([0, 1, 2]))\n", " array([[0, 1, 2, 2, 1, 0],\n", " [0, 1, 2, 2, 1, 0]])\n", " >>> a = np.array([[0, 1],\n", " ... [2, 3]])\n", " >>> reflect_quad(a)\n", " array([[0, 1, 1, 0],\n", " [2, 3, 3, 2],\n", " [2, 3, 3, 2],\n", " [0, 1, 1, 0]])\n", " \"\"\"\n", " pass\n", "\n", "def rows_where_bits_set_at_idxes(a: np.ndarray, \n", " set_id: np.ndarray) -> np.ndarray:\n", " out=np.array([],dtype=int)\n", " if len(a.shape) ==1:\n", " a=a[None,:]\n", " a1,a2=a.shape\n", " for idd in range(a1):\n", " MM=a[idd]\n", " if all(MM[set_id]):\n", " out=np.append(out,idd)\n", " return out\n", " \"\"\"\n", " Return a list of indexes of rows with bits \n", " set at indexes specified by `set_idxes`.\n", " \n", " >>> a = np.array([[1, 0, 1, 0],\n", " ... [0, 1, 1, 0],\n", " ... [0, 1, 0, 1],\n", " ... [0, 1, 1, 1]])\n", " >>> rows_where_bits_set_at_idxes(a, np.array([1, 3]))\n", " array([2, 3], dtype=int64)\n", " \"\"\"\n", " pass\n", " \n", "def frozen_precip(tt: np.ndarray,\n", " pp: np.ndarray) -> np.ndarray:\n", " F=np.zeros(tt.shape,dtype=int) \n", " F[(tt<=0) * (pp>0)]=1\n", " return F\n", " \n", " \n", " \"\"\" \n", " Return an array the same shape as `temperature` and `precip`\n", " with a 1 in each location at which the value of `temperature`\n", " is at or below 0, and the value of `precip` is greater than zero. \n", " `temperature` and `precip` will always have the same shape as each\n", " other, but they are not constrained to be 2-dimensional.\n", "\n", " >>> frozen_precip(np.array([[-2, -1, 0],\n", " [-1, 0, 1],\n", " [0, 1, 2]]),\n", " np.array([[0.0, 0.1, 0.3],\n", " [0.0, 0.4, 0.9],\n", " [2.2, 1.3, 0.0]]))\n", " array([[0, 1, 1],\n", " [0, 1, 0],\n", " [1, 0, 0]])\n", " \"\"\"\n", "# pass\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "def clean_mean(ss, cutoff):\n", " Alast=np.nan\n", " ss=np.array(ss)\n", " AM=np.nanmean(ss)\n", " while AM!=Alast:\n", " AM=np.nanmean(ss)\n", " AS=np.nanstd(ss)\n", " mma=AM+AS*cutoff\n", " mmi=AM-AS*cutoff\n", " ss=np.where((ssmmi).astype(int),ss,np.nan)\n", " Alast=np.nanmean(ss)\n", " return round(Alast,2)\n", "def withdraw(aa: int) -> list:\n", " S=int(aa/50)\n", " F=int((aa-S*50)/20)\n", " if (aa-S*50)%20==10:\n", " S=S-1\n", " F=F+3\n", " B=int(S/2)\n", " S=S-2*B\n", " return [int(B), int(S), int(F)]\n", "def high_and_low(numbers: str) -> str:\n", " A=numbers.split(' ')\n", " FF=[int(aa) for aa in A] \n", " return f\"{max(FF)} {min(FF)}\"" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "numbers=\"1 2 3 4 5\"\n", "A=numbers.split(' ')\n", "FF=[int(aa) for aa in A]\n", "max(FF)\n", "min(FF)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "def withdraw(amount: int) -> list\n", " B=int(aa/100)\n", " S=int((aa-B*100)/50)\n", " F=int((aa-B*100-S*50)/20)\n", " if (aa-B*100-S*50)%20==10:\n", " S=S-1\n", " F=F+3\n", " return [B, S, F]" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28.637229424141385" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import statistics\n", "sample = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]\n", "statistics.mean(sample)\n", "statistics.stdev(sample)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.0" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aa=260\n", "(aa-B*100-S*50)%20" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "aa=[0.2,0.1,0,1,2,4,5,6,8,15,29,77,100]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "q25,q75=np.percentile(aa,[25,75])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.2, 0.1, 0. , 1. , 2. , 4. , 5. , 6. , 8. , 15. , 29. ,\n", " nan, nan])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mma=q75+(q75-q25)*1.5\n", "mmi=q25-(q75-q25)*1.5\n", "ii=np.where((aammi).astype(int),aa,np.nan)\n", "ii" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Your function must remove any outliers and return the mean of the sample, rounded to two decimal places (round only at the end)." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True, True, True, True, True, True, True,\n", " True, True, True, True])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aa>mmi" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "ss = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]\n", "cutoff=3\n", "ss=np.array(ss)\n", "AM=ss.mean()\n", "AS=ss.std()\n", "mma=AM+AS*cutoff\n", "mmi=AM-AS*cutoff\n", "ss=np.where((ssmmi).astype(int),ss,np.nan)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., nan])" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ss" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 4)" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.reshape(np.arange(16), [4, 4])\n", "a.shape" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "a=np.array([[0,1,2],[3,4,5]])" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [], "source": [ "def select_alternating_columns(a: np.ndarray) -> np.ndarray:\n", " a=np.delete(a,np.s_[1::2],1) \n", " return a" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 2],\n", " [3, 5]])" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "select_alternating_columns_even(a)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(a.shape[1]))" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5]])" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.reshape(np.arange(6), [2, 3])\n", "a" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "a = np.array([[0, 0, 1],[0, 0, 0],[1, 0, 1],[1, 1, 1]])" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "0\n", "2\n", "3\n" ] } ], "source": [ "BB=np.array([])\n", "\n", "for idd in range(a.shape[0]):\n", " c=''.join([str(aa) for aa in list(a[idd,:])]).count('1')\n", " print(c)\n", " BB=np.append(BB,c)\n" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 7])" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "BB" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 4)" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(a.shape[0])" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('111',2)" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "a=np.array([[0, 1],[2, 3],[4, 5],[6, 7]])" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7]])" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b=a.copy()\n", "b" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [], "source": [ "c=len(a)//2+1\n", "# c=len(a)/2+1 if len(a)\n", "b[:c]=a[-c:]\n", "b[-c+1:]=a[:c-1]" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 3],\n", " [4, 5],\n", " [0, 1],\n", " [2, 3]])" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 1],\n", " [1, 0, 1],\n", " [1, 1, 1]])" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[0, 0, 0, 0, 0, 0],\n", " [0, 1, 0, 1, 0, 0],\n", " [0, 1, 0, 1, 0, 0],\n", " [0, 1, 1, 1, 0, 0],\n", " [0, 0, 0, 0, 0, 0]])\n", "B=np.sum(abs(a),axis=1)\n", "C=a[B!=0]\n", "MM=np.sum(abs(C),axis=0)\n", "C.T[MM!=0].T" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 0, 1, 0, 0],\n", " [0, 1, 0, 1, 0, 0],\n", " [0, 1, 1, 1, 0, 0]])" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.T.T" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0.]])" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros([5,5])" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [], "source": [ "size=5\n", "A=np.zeros([size,size],dtype=int)\n", "for i in range(0,size):\n", " for j in range(0,size):\n", " if (i+j)%2==1:\n", " A[i,j]=1" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "not enough values to unpack (expected 2, got 1)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\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[1;32m 2\u001b[0m \u001b[0ma1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ma2\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mempty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ma1\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ma2\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\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[1;32m 5\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0midd\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma1\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[0;31mValueError\u001b[0m: not enough values to unpack (expected 2, got 1)" ] } ], "source": [ "\n", "a1,a2=a.shape\n", "B=np.empty([a1*2,a2*2],dtype=type(a))\n", "for idd in range(a1):\n", " B[idd]=np.append(a[idd],a[idd])\n", "B[idd+1:]=B[0:idd+1]\n", "B" ] }, { "cell_type": "code", "execution_count": 330, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 1, 0],\n", " [0, 1, 1, 0]], dtype=object)" ] }, "execution_count": 330, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "B=np.empty([a1*2,a2*2],dtype=type(a))\n", "for idd in range(a1):\n", " B[idd]=np.append(a[idd],np.flip(a[idd]))\n", "B[idd+1:]=B[idd::-1]\n", "B" ] }, { "cell_type": "code", "execution_count": 359, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 1],\n", " [0, 1, 0],\n", " [1, 0, 0]])" ] }, "execution_count": 359, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tt=np.array([[-2, -1, 0],[-1, 0, 1],[0, 1, 2]])\n", "pp=np.array([[0.0, 0.1, 0.3],[0.0, 0.4, 0.9], [2.2, 1.3, 0.0]])\n", "F=np.zeros(tt.shape,dtype=int)\n", "\n", "F[(tt<=0) * (pp>0)]=1\n" ] }, { "cell_type": "code", "execution_count": 373, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([], dtype=int64)" ] }, "execution_count": 373, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=np.array([1, 0, 1, 0])\n", "\n", "def rows_where_bits_set_at_idxes(a: np.ndarray, \n", " set_id: np.ndarray) -> np.ndarray:\n", " out=np.array([],dtype=int)\n", " if len(a.shape) ==1:\n", " a=a[None,:]\n", " a1,a2=a.shape\n", " for idd in range(a1):\n", " MM=a[idd]\n", " if all(MM[set_id]):\n", " out=np.append(out,idd)\n", " return out\n", "rows_where_bits_set_at_idxes(a,[1,3])" ] }, { "cell_type": "code", "execution_count": 366, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 366, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=np.array([[1, 0, 1, 0],[0, 1, 1, 0],[0, 1, 0, 1],[0, 1, 1, 1]])\n", "set_id=np.array([1, 3])\n", "MM=a[0]\n", "all(MM[set_id])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 368, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([], dtype=int64)" ] }, "execution_count": 368, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out=np.array([],dtype=int)\n", "out" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0, 1, 1]" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Yd=[]\n", "for idd in range(a.shape[0]):\n", " for aa in a[idd,:]:\n", " bb=np.where(aa!=0)\n", " if bb[0].size==0:\n", " Yd.append(idd)\n", "Yd" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 1]])" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B=np.sum(abs(a),axis=1)\n", "C=a[B!=0]\n", "C" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "a = np.array([[0, 0, 0],\n", "... [0, 0, 1]])" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [], "source": [ "bb=np.where(a[0,:]!=0)" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bb[0].size" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 2)" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(a.shape[0])" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }