- Python:Data Analytics and Visualization
- Phuong Vo.T.H Martin Czygan Ashish Kumar Kirthi Raman
- 329字
- 2021-07-09 18:51:37
Data processing using arrays
With the NumPy package, we can easily solve many kinds of data processing tasks without writing complex loops. It is very helpful for us to control our code as well as the performance of the program. In this part, we want to introduce some mathematical and statistical functions.
See the following table for a listing of mathematical and statistical functions:

Loading and saving data
We can also save and load data to and from a disk, either in text or binary format, by using different supported functions in NumPy package.
Saving an array
Arrays are saved by default in an uncompressed raw binary format, with the file extension .npy
by the np.save
function:
>>> a = np.array([[0, 1, 2], [3, 4, 5]]) >>> np.save('test1.npy', a)
Note
The library automatically assigns the .npy
extension, if we omit it.
If we want to store several arrays into a single file in an uncompressed .npz
format, we can use the np.savez
function, as shown in the following example:
>>> a = np.arange(4) >>> b = np.arange(7) >>> np.savez('test2.npz', arr0=a, arr1=b)
The .npz
file is a zipped archive of files named after the variables they contain. When we load an .npz
file, we get back a dictionary-like object that can be queried for its lists of arrays:
>>> dic = np.load('test2.npz') >>> dic['arr0'] array([0, 1, 2, 3])
Another way to save array data into a file is using the np.savetxt
function that allows us to set format properties in the output file:
>>> x = np.arange(4) >>> # e.g., set comma as separator between elements >>> np.savetxt('test3.out', x, delimiter=',')
Loading an array
We have two common functions such as np.load
and np.loadtxt
, which correspond to the saving functions, for loading an array:
>>> np.load('test1.npy') array([[0, 1, 2], [3, 4, 5]]) >>> np.loadtxt('test3.out', delimiter=',') array([0., 1., 2., 3.])
Similar to the np.savetxt
function, the np.loadtxt
function also has a lot of options for loading an array from a text file.
- 21天學通JavaScript
- 計算機應用復習與練習
- 樂高創(chuàng)意機器人教程(中級 下冊 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實踐指導叢書)
- 最后一個人類
- OpenStack Cloud Computing Cookbook
- 基于神經(jīng)網(wǎng)絡的監(jiān)督和半監(jiān)督學習方法與遙感圖像智能解譯
- Word 2007,Excel 2007辦公應用融會貫通
- Spatial Analytics with ArcGIS
- 基于敏捷開發(fā)的數(shù)據(jù)結構研究
- Web編程基礎
- 計算機應用基礎實訓(職業(yè)模塊)
- Cloudera Hadoop大數(shù)據(jù)平臺實戰(zhàn)指南
- 超好玩的Python少兒編程
- 網(wǎng)絡設備規(guī)劃、配置與管理大全(Cisco版)
- CAD應用程序開發(fā)詳解