From the course: Python Data Analysis

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating NumPy arrays

Creating NumPy arrays - Python Tutorial

From the course: Python Data Analysis

Start my 1-month free trial

Creating NumPy arrays

- [Instructor] The easiest way to get a NumPy array is to load it from a file. NumPy recognizes several file formats, including, of course, simple text files. I have prepared one for you that describes a very well-known painting. The file is called monalisa.txt and it's included in your exercise files in the same directory as this notebook. Let's have a look at the content. We open the file with open in reading mode, and we use the readlines method to get all the lines of the file. So we see that we have 200 lines and that each line is a sequence of integers. NumPy loads the file without any trouble using loadtxt. The result is a two-dimensional array. If we display it, NumPy omits some rows and columns so it fits on the screen. We can query the object, the array, for the number of dimensions, which are two, the shape, which is 200 by 134, 200 rows by 134 columns, the total number of elements, and the type of…

Contents