{"id":2148,"date":"2024-04-21T14:53:51","date_gmt":"2024-04-21T21:53:51","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2148"},"modified":"2024-04-21T14:53:51","modified_gmt":"2024-04-21T21:53:51","slug":"419-random-permutations-using-numpy","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/04\/419-random-permutations-using-numpy\/","title":{"rendered":"#419 Random permutations using numpy"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\noriginal_array = np.array(&#x5B;&#x5B;1,2,3],&#x5B;4,5,6],&#x5B;7,8,9]])\r\npermuted_rows = np.random.permutation(original_array)\r\npermuted_columns = np.random.permutation(original_array.T).T\r\nprint(&quot;original array:&quot;)\r\nprint(original_array)\r\nprint(&quot;permuted rows:&quot;)\r\nprint(permuted_rows)\r\nprint(&quot;permuted columns:&quot;)\r\nprint(permuted_columns)\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\noriginal array:\r\n&#x5B;&#x5B;1 2 3]\r\n &#x5B;4 5 6]\r\n &#x5B;7 8 9]]\r\npermuted rows:\r\n&#x5B;&#x5B;4 5 6]\r\n &#x5B;1 2 3]\r\n &#x5B;7 8 9]]\r\npermuted columns:\r\n&#x5B;&#x5B;2 1 3]\r\n &#x5B;5 4 6]\r\n &#x5B;8 7 9]]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np original_array = np.array(&#x5B;&#x5B;1,2,3],&#x5B;4,5,6],&#x5B;7,8,9]]) permuted_rows = np.random.permutation(original_array) permuted_columns = np.random.permutation(original_array.T).T print(&quot;original array:&quot;) print(original_array) print(&quot;permuted rows:&quot;) print(permuted_rows) print(&quot;permuted columns:&quot;) print(permuted_columns) Output: original array: &#x5B;&#x5B;1 2 3] &#x5B;4 5 6] &#x5B;7 8 9]] permuted rows: &#x5B;&#x5B;4 5 6] &#x5B;1 2 3] &#x5B;7 8 9]] permuted columns: &#x5B;&#x5B;2 1 3] &#x5B;5 4 6] &#x5B;8 7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[99,2],"tags":[],"class_list":["post-2148","post","type-post","status-publish","format-standard","hentry","category-numpy","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-yE","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":379,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/find-nearest-value-in-numpy-array\/","url_meta":{"origin":2148,"position":0},"title":"#54 Find nearest value in numpy array","author":"gantovnik","date":"2019-02-07","format":false,"excerpt":"[code language=\"python\"] import numpy as np def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).idxmin() return array[idx] array = np.random.random(10) print(array) # [ 0.21069679 0.61290182 0.63425412 0.84635244 0.91599191 0.00213826 # 0.17104965 0.56874386 0.57319379 0.28719469] value = 0.5 print(find_nearest(array, value)) # 0.568743859261 [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1716,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/324-matrix-operations-using-numpy\/","url_meta":{"origin":2148,"position":1},"title":"#324 Matrix operations using numpy","author":"gantovnik","date":"2022-12-13","format":false,"excerpt":"The numpy.matrix method is syntactically the simplest. However, numpy.array is the most practical. [code language=\"python\"] import numpy as np A = np.matrix([[3,6,-5], [1,-3,2], [5,-1,4]]) b = np.matrix([[12], [-2], [10]]) x = A**(-1) * b print(x) #output: #[[1.75] # [1.75] # [0.75]] A = np.array([[3,6,-5], [1,-3,2], [5,-1,4]]) b = np.array([[12], [-2],\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1114,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/195-use-numpy-linalg-solve-to-solve-system-of-linear-equations\/","url_meta":{"origin":2148,"position":2},"title":"#195 Use numpy.linalg.solve to solve system of linear equations","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] import numpy as np A = np.array([[4, 3, -5],[-2, -4, 5],[8, 8, 0]]) y = np.array([2, 5, -3]) x = np.linalg.solve(A, y) print(x) [\/code] Result: [code language=\"python\"] [ 2.20833333 -2.58333333 -0.18333333] [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":104,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/unconstrained-multivariate-optimization\/","url_meta":{"origin":2148,"position":3},"title":"Unconstrained multivariate optimization","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy import sympy os.chdir('\/home\/vg\/Downloads\/projects\/ex18') os.getcwd() x1,x2=sympy.symbols(\"x_1,x_2\") f_sym=(x1-1)**4 + 5*(x2-1)**2 - 2*x1*x2 fprime_sym=[f_sym.diff(x_) for x_ in (x1,x2)] sympy.Matrix(fprime_sym) fhess_sym=[[f_sym.diff(x1_,x2_) for x1_ in (x1,x2)] for x2_ in (x1,x2)] sympy.Matrix(fhess_sym) f_lmbda=sympy.lambdify((x1,x2),f_sym,'numpy') fprime_lmbda=sympy.lambdify((x1,x2),fprime_sym,'numpy') fhess_lmbda=sympy.lambdify((x1,x2),fhess_sym,'numpy') def func_XY_to_xy(f): return lambda X: np.array(f(X[0],X[1])) f=func_XY_to_xy(f_lmbda) fprime=func_XY_to_xy(fprime_lmbda) fhess=func_XY_to_xy(fhess_lmbda)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example18","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1872,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/352-optimization-using-genetic-algorithm-in-python\/","url_meta":{"origin":2148,"position":4},"title":"#352 Optimization using Genetic Algorithm in python","author":"gantovnik","date":"2023-06-28","format":false,"excerpt":"[code language=\"python\"] #pip install geneticalgorithm import numpy as np from geneticalgorithm import geneticalgorithm as ga # Define charateristics of variables: varbound=np.array([[0,1],[0,1]]) vartype=np.array([['real'],['real']]) # Define settings of the algorithm: algorithm_param = {'max_num_iteration': 100,\\ 'population_size':60,\\ 'mutation_probability':0.1,\\ 'elit_ratio': 0.01,\\ 'crossover_probability': 0.5,\\ 'parents_portion': 0.3,\\ 'crossover_type':'uniform',\\ 'max_iteration_without_improv':None} # Define your optimization model: def MyOptProb(X): y\u2026","rel":"","context":"In &quot;genetic algorithm&quot;","block_context":{"text":"genetic algorithm","link":"https:\/\/gantovnik.com\/bio-tips\/category\/genetic-algorithm\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ga_history.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":845,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/01\/149-optimization-in-python\/","url_meta":{"origin":2148,"position":5},"title":"#149 Optimization in python","author":"gantovnik","date":"2021-01-24","format":false,"excerpt":"#149 Optimization in python [code language=\"python\"] import os import numpy as np from scipy.optimize import minimize os.chdir(r'D:\\projects\\wordpress\\ex149') os.getcwd() def obj(x): return (1-x[0])**2 + 100*(x[1]-x[0]**2)**2 def con(x): g=np.zeros(2) g[0]=1-x[0]**2-x[1]**2 g[1]=5-x[0]-3*x[1] return g x0=[5.0,5.0] constraints = {'type': 'ineq','fun': con} options={'disp':True} res = minimize(obj,x0,constraints=constraints,options=options) print('x =',res.x) print('f =',res.fun) print(res.success) [\/code] Output: [code language=\"python\"]\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=2148"}],"version-history":[{"count":1,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2148\/revisions"}],"predecessor-version":[{"id":2149,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2148\/revisions\/2149"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}