My Four Most Common Python NumPy Array Initializations

I use several different programming languages. Whenever I switch between languages, there’s always an adjustment time in my head. For some reason, whenever I switch from C# to Python with NumPy, it always takes me about an hour to start thinking fully in Python. In particular, it always takes me time to recall Python/NumPy array initializations.

One of the causes of this is that C# has basically two ways to instantiate an array:

double[] arr1 = new double[4];
double[] arr2 = new double[] { 1.0, 5.0, 2.0 };

But Python NumPy has many ways to instantiate. The ones I use most are often are np.zeros(), np.array(), np.full(), and a return from np.random.choice(). For example:

arr1 = np.zeros(shape=5, dtype=np.float32) # 5 0.0 cells
arr2 = np.array([17,2,5,0,5,12], dtype=np.int)
arr3 = np.array(range(0,5), dtype=np.int) # [0,1,2,3,4]
arr4 = np.full(shape=3, fill_value=0.01, dtype=np.float64)
arr5 = np.random.choice(7, 2) # 2 random ints between [0,6]
mat1 = np.zeros(shape=(2,3), dtype=np.float32) # 2x3 matrix

There are several implications. One is that I prefer programming languages that have sparse feature sets — I prefer to know everything about a small language. For example, the np.zeros() function is redundant in a sense because you can get the same effect using np.full(fill_value=0.0).

Another implication is the high cost of context switching when programming. It costs time and effort to switch languages (for example, working with C# on Monday, Wednesday, Friday, and with Python on Tuesday, Thursday). Or doing programming from 9:00 AM to 11:00 AM, then switching over to email tasks, then switching back to programming.

Advertisements
This entry was posted in Machine Learning. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s