Simple trick to read mixed format data with numpy’s genfromtxt

Suppose your data is like the following


DD,MM,YY,AMT,WHERE
30,5,2015,2,TRAVEL
30,5,2015,50,TRAVEL
30,5,2015,5,PHONE
31,5,2015,6.62,TESCO
31,5,2015,5,POUNDSHOP
31,5,2015,4.51,SAINSBURY
31,5,2015,1,PHONE

Let’s load it with numpy using genfromtxt


expdata=np.genfromtxt(fname, delimiter=',',skip_header=1,usecols=[0,1,2,3,4])

print expdata[0]
print expdata[-1]

This prints the following


[  3.00000000e+01   5.00000000e+00   2.01500000e+03   2.00000000e+00             NaN]
[   30.     5.  2015.    50.    NaN]

Numeric data is read but not quite as what we wanted. Notice the NaN.

You can supply genfromtxt the datatypes using the keyword format like

dtype=([(‘f0’, ‘<i4’), (‘f1’, ‘<i4’), (‘f2’, ‘<i4’), (‘f3’, ‘<f8’), (‘f4’, ‘|S14’)])


expdata=np.genfromtxt(fname, delimiter=',',skip_header=1,usecols=[0,1,2,3,4],dtype=([('f0', '&amp;amp;lt;i4'), ('f1', '&amp;amp;lt;i4'), ('f2', '&amp;amp;lt;i4'), ('f3', '&amp;amp;lt;f8'), ('f4', '|S14')]))

(30, 5, 2015, 2.0, 'TRAVEL')
(30, 5, 2015, 50.0, 'TRAVEL')

But that looks too much work but there’s a simple smart way to do this. Use dtype=None

 


expdata=np.genfromtxt(fname, delimiter=',',skip_header=1,usecols=[0,1,2,3,4],dtype=None)

(30, 5, 2015, 2.0, 'TRAVEL')
(30, 5, 2015, 50.0, 'TRAVEL')

Using dtype=None is a good trick if you don’t know what your columns should be and you need some help in getting the format. This might be slower but it does work and once you have the data you can replace the dtype none with the appropriate arguments.

Advertisements

Exploring Matplotlib Styles

Last week got some free time. Used it to upgrade my python installation on Mac, a long awaited task.

Looking at the upgrade log, was most excited to finally see the new version of matplotlib.

So launched it and went straight to the new style package.

Matplotlib is great at graphs but the default style before 1.4.3 left many things wanting.

The style package adds support for easy-to-switch plotting “styles” with the same parameters as a matplotlibrc file.

import matplotlib.pyplot as plt

What are different styles available in matplotlib?

print(plt.style.available)

[u'dark_background', u'bmh', u'grayscale', u'ggplot', u'fivethirtyeight']

Here’s how to use this.

But first let’s generate some data

import numpy as np
data = np.sin(np.linspace(0, 2*np.pi))

The default plot

plt.plot(data, 'r-o')

default_matplotlib_1.4.3_styles

Let’s use ggplot

plt.style.use('ggplot') 
plt.plot(data, 'r-o')

ggplot_matplotlib_style

Dark Background like excel 2007

plt.style.use('dark_background')
plt.plot(data, 'r-o')

dark_background_matplotlib

BMH style

plt.style.use(‘bmh’)
plt.plot(data, 'r-o')

bmh_matplotlib_style

Graystyle

plt.style.use(‘grayscale’)
plt.plot(data, 'r-o')

grayscale_matplotlib_style

fivethirtyeight Style

plt.style.use(‘fivethirtyeight’)
plt.plot(data, 'r-o')

fivethirtyeight_matplotlib

We can even add our own custom .mplstyle files to ~/.matplotlib/stylelib or call use with a URL pointing to a file with matplotlibrc settings. Follow the following link to define your own style.

A Simple but useful Python tip.

Want to convert a numpy arrays containing coords from

x1,y1,z1
x2,y2,z2
.
.
.
xn,yn,zn

to

x1
y1
z1
x2
y2
z2
….
xn
yn
zn

Example


print coords
[[ 0.    0.    0.  ]
 [ 0.25  0.    0.  ]
 [ 0.5   0.    0.  ]
 [ 0.75  0.    0.  ]
 [ 1.    0.    0.  ]
 [ 0.    0.25  0.  ]
 [ 0.25  0.25  0.  ]
 [ 0.5   0.25  0.  ]
 [ 0.75  0.25  0.  ]
 [ 1.    0.25  0.  ]
 [ 0.    0.5   0.  ]
 [ 0.25  0.5   0.  ]
 [ 0.5   0.5   0.  ]
 [ 0.75  0.5   0.  ]
 [ 1.    0.5   0.  ]
 [ 0.    0.75  0.  ]
 [ 0.25  0.75  0.  ]
 [ 0.5   0.75  0.  ]
 [ 0.75  0.75  0.  ]
 [ 1.    0.75  0.  ]
 [ 0.    1.    0.  ]
 [ 0.25  1.    0.  ]
 [ 0.5   1.    0.  ]
 [ 0.75  1.    0.  ]
 [ 1.    1.    0.  ]]


change = coords.resh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