{"id":8297,"date":"2023-11-03T08:19:26","date_gmt":"2023-11-03T02:49:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=8297"},"modified":"2023-11-10T12:14:02","modified_gmt":"2023-11-10T06:44:02","slug":"python-find-unique-values-in-a-numpy-array-with-frequency-and-indices","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-find-unique-values-in-a-numpy-array-with-frequency-and-indices\/","title":{"rendered":"Python: Find Unique Values in a Numpy Array With Frequency and Indices"},"content":{"rendered":"
In this article, we will discuss how to find unique values, rows, and columns in a 1D & 2D Numpy array. Before going to the methods first we see numpy.unique() method because this method is going to be used.<\/p>\n
numpy.unique() method help us to get the unique() values from given array.<\/p>\n
Now we will see different methods to find unique value with their indices and frequencies in a numpy array.<\/p>\n As we only need unique values and not their frequencies and indices hence we simply pass our numpy array in the unique() method because the default value of other parameters is false so we don’t need to change them. Let see this with the help of an example.<\/p>\n Output<\/p>\n In this method, as we want to get unique values along with their indices hence we make the return_index parameter true and pass our array. Let see this with the help of an example.<\/p>\n Output<\/p>\n In this method, as we want to get unique values along with their frequencies hence we make the return_counts parameter true and pass our array. Let see this with the help of an example.<\/p>\n Output<\/p>\n Here we simply pass our array and all the parameter remain the same. Here we don’t make any changes because we want to work on both rows and columns. Let see this with the help of an example.<\/p>\n Output<\/p>\n As here want to want to work only on rows so here we will make axis=0 and simply pass our array. Let see this with the help of an example.<\/p>\n Output<\/p>\n As here want to want to work only on columns so here we will make axis=1 and simply pass our array. Let see this with the help of an example.<\/p>\n Output<\/p>\n so these are the methods to find unique values in a numpy array with frequency and indices.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Methods to find unique values in a numpy array with frequency and indices In this article, we will discuss how to find unique values, rows, and columns in a 1D & 2D Numpy array. Before going to the methods first we see numpy.unique() method because this method is going to be used. numpy.unique() method numpy.unique() …<\/p>\nsyntax:numpy.<\/span>unique<\/span>(<\/span>array, return_index=<\/span>False<\/span>, return_inverse=<\/span>False<\/span>, return_counts=<\/span>False<\/span>, axis=<\/span>None<\/span>)<\/span><\/code><\/p>\n
Parameters<\/h3>\n
\n
case 1-When our array is 1-D<\/h3>\n
\n
Method 1-Find unique value from the array<\/h3>\n<\/li>\n<\/ul>\n
import numpy as np\r\narr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7])\r\nunique_values=np.unique(arr)\r\nprint(\"Original array is\")\r\nprint(arr)\r\nprint(\"------------------\")\r\nprint(\"Unique values are\")\r\nprint(unique_values)<\/pre>\n
Original array is\r\n<\/span>[1 1 2 3 4 5 6 7 2 3 1 4 7]\r\n------------------\r\nUnique values are\r\n[1 2 3 4 5 6 7]<\/span><\/pre>\n
\n
Method 2-Find unique value from the array along with their indices<\/h3>\n<\/li>\n<\/ul>\n
import numpy as np\r\narr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7])\r\nunique_values,index=np.unique(arr,return_index=True)\r\nprint(\"Original array is\")\r\nprint(arr)\r\nprint(\"------------------\")\r\nprint(\"Unique values are\")\r\nprint(unique_values)\r\nprint(\"First index of unique values are:\")\r\nprint(index)<\/pre>\n
Original array is\r\n<\/span>[1 1 2 3 4 5 6 7 2 3 1 4 7]\r\n------------------\r\nUnique values are\r\n[1 2 3 4 5 6 7]\r\nFirst index of unique values are:\r\n[0 2 3 4 5 6 7]<\/span><\/pre>\n
\n
Method 3-Find unique value from the array along with their frequencies<\/h3>\n<\/li>\n<\/ul>\n
import numpy as np\r\narr = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4, 7])\r\nunique_values,count=np.unique(arr,return_counts=True)\r\nprint(\"Original array is\")\r\nprint(arr)\r\nprint(\"------------------\")\r\nprint(\"Unique values are\")\r\nprint(unique_values)\r\nprint(\"Count of unique values are:\")\r\nfor i in range(0,len(unique_values)):\r\n print(\"count of \",unique_values[i],\" is \",count[i])<\/pre>\n
Original array is\r\n<\/span>[1 1 2 3 4 5 6 7 2 3 1 4 7]\r\n------------------\r\nUnique values are\r\n[1 2 3 4 5 6 7]\r\nCount of unique values are:\r\ncount of 1 is 3\r\ncount of 2 is 2\r\ncount of 3 is 2\r\ncount of 4 is 2\r\ncount of 5 is 1\r\ncount of 6 is 1\r\ncount of 7 is 2<\/span><\/pre>\n
Case 2: When our array is 2-D<\/h3>\n
\n
Method 1-Find unique value from the array<\/h3>\n<\/li>\n<\/ul>\n
import numpy as np\r\narr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1], [1, 1, 2, 1]])\r\nunique_values=np.unique(arr)\r\nprint(\"Original array is\")\r\nprint(arr)\r\nprint(\"------------------\")\r\nprint(\"Unique values are\")\r\nprint(unique_values)\r\n<\/pre>\n
Original array is\r\n[[1 1 2 1]\r\n [3 1 2 1]\r\n [6 1 2 1]\r\n [1 1 2 1]]\r\n------------------\r\nUnique values are\r\n[1 2 3 6]<\/span><\/pre>\n
Method 2-Get unique rows<\/h3>\n
import numpy as np\r\narr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1], [1, 1, 2, 1]])\r\nunique_values=np.unique(arr,axis=0)\r\nprint(\"Original array is\")\r\nprint(arr)\r\nprint(\"------------------\")\r\nprint(\"Unique rows are\")\r\nprint(unique_values)\r\n<\/pre>\n
Original array is\r\n<\/span>[[1 1 2 1]\r\n [3 1 2 1]\r\n [6 1 2 1]\r\n [1 1 2 1]]\r\n------------------\r\nUnique rows are\r\n[[1 1 2 1]\r\n [3 1 2 1]\r\n [6 1 2 1]]<\/span><\/pre>\n
Method 3-Get unique columns<\/h3>\n
import numpy as np\r\narr = np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [ 6, 1, 2, 1], [1, 1, 2, 1]])\r\nunique_values=np.unique(arr,axis=1)\r\nprint(\"Original array is\")\r\nprint(arr)\r\nprint(\"------------------\")\r\nprint(\"Unique columns are\")\r\nprint(unique_values)\r\n<\/pre>\n
Original array is\r\n<\/span>[[1 1 2 1]\r\n [3 1 2 1]\r\n [6 1 2 1]\r\n [1 1 2 1]]\r\n------------------\r\nUnique columns are\r\n[[1 1 2]\r\n [1 3 2]\r\n [1 6 2]\r\n [1 1 2]]<\/span><\/pre>\n