Python numpy matrix assign value -


i wondering why different result in 2 prints? shouldn't same?

    import numpy np      x = np.array([[1.5, 2], [2.4, 6]])      k = np.copy(x)     in range(len(x)):        j in range(len(x[i])):             k[i][j] = 1 / (1 + np.exp(-x[i][j]))             print("k[i][j]:"+str(k[i][j]))             print("value:"+str(1 / (1 + np.exp(-x[i][j])))) 

i've run code python3 , python2 , results absolutely same. besides, don't have looping when using numpy arrays allows express many kinds of data processing tasks concise array expressions might otherwise require writing loops. practice of replacing explicit loops array expressions commonly referred vectorization. in general, vectorized array operations 1 or 2 (or more) orders of magnitude faster pure python equivalents, biggest impact in kind of numerical computations.

so, keeping in mind may rewrite code follows:

import numpy np  x = np.array([[1.5, 2], [2.4, 6]], dtype=np.float) k = 1 / (1 + np.exp(-x)) 

Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -