{"id":5558,"date":"2023-10-27T09:30:52","date_gmt":"2023-10-27T04:00:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=5558"},"modified":"2023-11-10T12:02:18","modified_gmt":"2023-11-10T06:32:18","slug":"python-create-boolean-numpy-array-with-all-true-or-all-false-or-random-boolean-values","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-create-boolean-numpy-array-with-all-true-or-all-false-or-random-boolean-values\/","title":{"rendered":"Python : Create boolean Numpy array with all True or all False or random boolean values"},"content":{"rendered":"
This article is about creating boolean Numpy array with all True or all False or random boolean values. Here we will discuss various ways of creating boolean Numpy array. So let’s start the topic.<\/p>\n
Python’s Numpy module provide where,<\/p>\n To create a 1-D Numpy array with random true and false values, we will initially take a bool array and pass the array to So, let’s see how it actually works.<\/p>\n For implementing this we can pass the size of 2D array as tuple in So, let’s see how it actually works.<\/p>\n We can use So, let’s see how it actually works.<\/p>\n We can use So, let’s see how it actually works.<\/p>\n Here also So, let’s see how it actually works.<\/p>\n Here also So, let’s see how it actually works.<\/p>\n Here we will pass So, let’s see how it actually works.<\/p>\n In python, list can contain elements with different data types i.e. heterogeneous in nature. But Numpy arrays are homogeneous in nature i.e. elements with same data type, so it will convert all 0s to false <\/em>and any other values which can be of any data type to true.<\/em><\/p>\n So, let’s see how it actually works.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" How to create boolean Numpy array with all True or all False or random boolean values in Python ? This article is about creating boolean Numpy array with all True or all False or random boolean values. Here we will discuss various ways of creating boolean Numpy array. So let’s start the topic. Approach-1 : …<\/p>\nrandom.choice( )<\/code> function which will create a boolean Numpy array with some random values.<\/p>\n
Syntax : numpy.random.choice(a, size=None, replace=True, p=None)<\/pre>\n
\n
numpy.random.choice()<\/code> with some size which will generate a array with random true and false values.<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# bool Array for random sampling with only 2 values i.e. True anf False\r\nsample_Barr = [True, False]\r\n\r\n# To create a numpy array with random true and false with size 5\r\nbool_Narr = sc.random.choice(sample_Barr, size=5)\r\n\r\nprint('Numpy Array with random values: ')\r\nprint(bool_Narr)<\/pre>\n
Output :\r\nNumpy Array with random values:\r\n[False False False\u00a0 True False]<\/pre>\n
Approach-2 : Creating 2-D boolean Numpy array with random values :<\/h3>\n
random.choice( )<\/code> function.<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# bool Array for random sampling with only 2 values i.e. True anf False\r\nsample_Barr = [True, False]\r\n\r\n# To create a 2D numpy array with random true and false values\r\nbool_Narr = sc.random.choice(sample_Barr, size=(3,3))\r\n\r\nprint('2D Numpy Array with random values: ')\r\nprint(bool_Narr)<\/pre>\n
Output :\r\n2D Numpy Array with random values:\r\n[[False False False]\r\n[False\u00a0 True\u00a0 True]\r\n[ True False\u00a0 True]]<\/pre>\n
Approach-3 : Create a 1-D Bool array with all True values :<\/h3>\n
numpy.ones( )<\/code> function to form boolean Numpy array with all True<\/em> values.
nump.ones( )<\/code> creates a numpy array with initializing value with 1, later
dtype<\/code> argument is passed as bool which converts all 1 to True.<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# To create a 1D numpy array with all true values\r\nbool_Narr = sc.ones(5, dtype=bool)\r\n\r\nprint('1D Numpy Array with all true values: ')\r\nprint(bool_Narr)\r\n\r\n<\/pre>\n
Output :\r\n1D Numpy Array with all true values:\r\n[ True\u00a0 True\u00a0 True\u00a0 True\u00a0 True]<\/pre>\n
Approach-4 : Create a 1-D Bool array with all False values :<\/strong><\/h3>\n
numpy.zeros( )<\/code> function to form boolean Numpy array with all True values.
nump.zeros( )<\/code> creates a numpy array with initializing value with 0, later
dtype<\/code> argument is passed as bool which converts all 0 to False.<\/p>\n
# Program :\r\n\r\nimport numpy as sc\r\n\r\n# To create a 1D numpy array with all false values\r\nbool_Narr = sc.zeros(5, dtype=bool)\r\n\r\nprint('1D Numpy Array with all false values: ')\r\nprint(bool_Narr)<\/pre>\n
Output :<\/u>\r\n1D Numpy Array with all false values:\r\n[False False False False False]<\/pre>\n
Approach-5 :\u00a0 Creating 2-D Numpy array with all True values :<\/h3>\n
numpy.ones( )<\/code> including passing the
dtype<\/code> argument as bool can be used to generate all values as true in a 2D\u00a0 numpy array.<\/p>\n
# Program :\r\n\r\nimport numpy as sc\r\n\r\n# To create a 2D numpy array with all true values\r\nbool_Narr = sc.ones((3,4), dtype=bool)\r\n\r\nprint('2D Numpy Array with all true values: ')\r\nprint(bool_Narr)<\/pre>\n
Output :\r\n2D Numpy Array with all true values:\r\n[[ True\u00a0 True\u00a0 True\u00a0 True]\r\n[ True\u00a0 True\u00a0 True\u00a0 True]\r\n[ True\u00a0 True\u00a0 True\u00a0 True]]<\/pre>\n
Approach-6 :\u00a0 Creating 2D Numpy array with all False values :<\/strong><\/h3>\n
numpy.zeros( )<\/code> including passing the
dtype<\/code> argument as bool can be used to generate all values as false in a 2D\u00a0 numpy array.<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# To create a 2D numpy array with all true values\r\nbool_Narr = sc.zeros((3,4), dtype=bool)\r\n\r\nprint('2D Numpy Array with all true values: ')\r\nprint(bool_Narr)<\/pre>\n
Output :\r\n2D Numpy Array with all false values:\r\n[[False False False False]\r\n[False False False False]\r\n[False False False False]]<\/pre>\n
Converting a List to bool Numpy array :<\/h3>\n
Approach-7 : Convert a list of integers to boolean numpy array :<\/h3>\n
dtype<\/code> arguement as bool in
numpy.array( )<\/code> function where each and every elements in the list will be converted as true\/ false values, i.e. all 0s will be converted to false <\/em>and any integer other than 0 to true.<\/em><\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# Integers list\r\nlist_ele = [8,55,0,24,100,0,0,-1]\r\n\r\n# To convert a list of integers to boolean array\r\nbool_Narr = sc.array(list_ele, dtype=bool)\r\n\r\nprint('Boolean Numpy Array: ')\r\nprint(bool_Narr)<\/pre>\n
Output :\r\nNumpy Array:\r\n[ True\u00a0 True False\u00a0 True\u00a0 True False False\u00a0 True]<\/pre>\n
Approach-8 : Convert a heterogeneous list to boolean numpy array :<\/h3>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# Integers list\r\nlist_ele = [8,55,0,24.56,100,0,-85.6,\"Satya\"]\r\n\r\n# To convert a list of integers to boolean array\r\nbool_Narr = sc.array(list_ele, dtype=bool)\r\n\r\nprint('Boolean Numpy Array: ')\r\nprint(bool_Narr)<\/pre>\n
Output :\r\nBoolean Numpy Array:\r\n[ True\u00a0 True False\u00a0 True\u00a0 True False\u00a0 True\u00a0 True]<\/pre>\n