Zscore in Python and Matlab have different result

here is the code in python

here is the code in python

x = np.array( [ [1, 10, 4], [3, 2, 1], [5, 1, 0] ] , dtype = np.float128 );
x = zscore(x, axis = 0);
print x;

the output is

[[-1.2247449  1.40693  1.3728129]
[ 0.0 -0.57932412 -0.39223227]
[ 1.2247449 -0.82760589 -0.98058068]]

In matlab,

xxx = [1 10 4 ; 3 2 1; 5 1 0]
zscore(xxx)

output is

-1.0000    1.1488    1.1209
0   -0.4730   -0.3203
1.0000   -0.6757   -0.8006

why sciPy.stats.zscore and matlab zscore function are different?

Python: Issue reading in str from MATLAB .mat file using h5py and NumPy

I am having difficulty loading in 'str' variables 'Et' (Endtime) and 'St' (Starttime) from a MATLAB .mat file into Python.

I want identical output as in MATLAB. Instead I have had issues trying to solve this. See below for Python code and output.

# Import numpy and h5py to load in .mat files
import numpy as np
import h5py 
Load in Matlab ('-v7.3') data

fname = 'directory/file.mat'
f = h5py.File(fname,'r')

create dictionary for data

data= {"average":np.array(f.get('average')),"median":np.array(f.get('median')),
"stdev":np.array(f.get('stdev')),"P10":np.array(f.get('p10')),
"P90":np.array(f.get('p90')),"St":np.str(f.get('stime')),
"Et":np.str(f.get('etime'))}

All other variables are arrays

print(data["Et"])

output:

<HDF5 dataset "etime": shape (1, 6), type "<u4">

I want to have a string in python equal to the string in MATLAB. In other words, I want print(data["Et"]) = '01011212000000' which is the date and time.

How can I solve this?

MATLAB Numpy element wise multiplication issue

Please see the MATLAB code and equivalent Numpy code below. Question: How can I get the D variable same in Numpy as MATLAB's?

Please see the MATLAB code and equivalent Numpy code below. Question: How can I get the D variable same in Numpy as MATLAB's?

MATLAB Code

A = [1 2 3; 4 5 6; 7 8 9]

C = [100 1; 10 0.1; 1, 0.01]

C = reshape(C, 1,3,2)

D = bsxfun(@times, A, C)

D(:,:,1) =

   100    20     3
   400    50     6
   700    80     9

D(:,:,2) =

1.0000    0.2000    0.0300
4.0000    0.5000    0.0600
7.0000    0.8000    0.0900

Numpy Code

A = np.array([[1,2,3],[4,5,6],[7,8,9]])

C = np.array([[[100, 1], [10, 0.1], [1, 0.01]]]) # C.shape is (1, 3, 2)

D = A * C.T

D

array([[[100.  , 200.  , 300.  ],
        [ 40.  ,  50.  ,  60.  ],
        [  7.  ,   8.  ,   9.  ]],

       [[  1.  ,   2.  ,   3.  ],
        [  0.4 ,   0.5 ,   0.6 ],
        [  0.07,   0.08,   0.09]]])


Tutorial How to write MatLab functions in Python

Tutorial How to write MatLab functions in Python

A tutorial on writing MatLab-like functions using the Python language and the NumPy library.

Overview

Recently in my work, I was re-writing algorithms developed in MatLab to Python, some functions are not so simple to adapt, especially the array functions that are called Cell Arrays.

MatLab has an API where you can call MatLab functions via Python. The idea, however, was not to use MatLab, but the same algorithm works the same way using only Python and NumPy, and the GNU Octave also has an API similar to that of MatLab.

To maintain compatibility, I have created functions with the same name that are used in MatLab that is encapsulated in a class called Precision.

1. Testing

Make the repository clone and follow the instructions in the README file:

Below I will show some examples, these are contained in the unit tests.

1.1 Start Stopwatch Time

Measuring the time spent in processing.

from precision import Precision

p = Precision()
p.tic()
for i in range(0, 1000): print(i)
p.toc()

The output will look something like this:

: > Elapsed time is 0:0:2 secounds.

1.2 Percentiles of a Data Set

This is used to get a percentile. In the example below, we are creating a range of ordinal dates by cutting 5% from the left and 5% from the right.

from datetime import datetime
from precision import Precision

p = Precision()
d = [i for i in p.dtrange(datetime(2018, 6, 12), 
                          datetime(2059, 12, 12), 
                          {'days':1, 'hours':2})]
x = [p.datenum(i.date()) for i in d]

x1 = p.prctile(x, 5)
x2 = p.prctile(x, 95)
r = (x2 - x1)

The output will look something like this:

5% lower: 737980.1
5% higher: 751621.9
delta: 13641.800000000047

1.3 Cell Array (cell2mat)

This converts a cell array to an ordinary array of the underlying data type.

from precision import Precision

p = Precision()
p.cell2mat([[1, 2], [3, 4]])
p.cell2mat('1 2; 3 4')

The output will look something like this:

matrix([[1, 2],
        [3, 4]])

1.4 Cell Array (num2cell)

Convert array to cell array with consistently sized cells.

import numpy
from precision import Precision

p = Precision()
x = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], numpy.int64)
p.num2cell(x)

The output will look something like this:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

1.5 Concatenate Strings (strcat)

This concatenates strings horizontally using strcat.

import pandas
from precision import Precision

p = Precision()
df = pandas.DataFrame(data={'A': [1, 2], 'B': [3, 4]}, dtype=numpy.int8)
p.strcat(df, 'B')

The output will look something like this:

['3', '4']

1.6 Histogram (histc)

This counts the number of values in x that are within each specified bin range. The input, binranges, determines the endpoints for each bin. The output, bincounts, contains the number of elements from x in each bin.

import numpy 
from precision import Precision

p = Precision()
v = numpy.array([[1.5, 2.0, 3], [4, 5.9, 6]], numpy.int64)
p.histc(v, numpy.amax(v) + 1)

The output will look something like this:

(array([1, 1, 1, 0, 1, 1, 1]), array([1., 1.71428571, 2.42857143, 
       3.14285714, 3.85714286, 4.57142857, 5.28571429, 6.]))

1.7 Unique

Looking for unique values in an array and returning the indexes, inverse, and counts.

import numpy 
from precision import Precision

p = Precision()
x = [0, 1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 7]
p.unique(numpy.array([x]))

The output will look something like this:

array([[array([0, 1, 2, 3, 4, 5, 6, 7]),
        array([[ 0,  1,  3,  4,  5,  7,  9, 10]]),
        array([0, 1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 7]),
        array([1, 2, 1, 1, 2, 2, 1, 3])]], dtype=object)

1.8 Overlaps

Looking for the overlays between two arrays returning the index.

import numpy 
from precision import Precision

p = Precision()
x, y = p.overlap2d(numpy.array(['A','B','B','C']), 
                   numpy.array(['C','A','B','C','D']))

The output will look something like this:

(array([0, 1, 2, 3]), array([1, 2, 0, 3]))

Considerations

There are functions that are not exactly MatLab but will serve as support, I hope it can help someone. There is an interesting article in NumPy for users who are migrating from MatLab to Python.

Further Reading

MATLAB vs Python: Why and How to Make the Switch

Creating a Plot Charts in Python with Matplotlib

Python Tutorial - Python GUI Programming - Python GUI Examples (Tkinter Tutorial)

Essential Python 3 code for lists

*Originally published by Ederson Corbari   at *dzone.com

=============================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter