- Hands-On Artificial Intelligence for IoT
- Amita Kapoor
- 247字
- 2021-07-02 14:01:59
Working with CSV files with the NumPy module
The NumPy module provides two functions for reading values from CSV files: np.loadtxt() and np.genfromtxt().
An example of np.loadtxt is as follows:
arr = np.loadtxt('temp.csv', skiprows=1, usecols=(2,3), delimiter=',')
arr
The preceding code reads columns 3 and 4 from the file that we created earlier, and saves them in a 9 × 2 array as follows:
The np.loadtxt() function cannot handle CSV files with missing data. For instances where data is missing, np.genfromtxt() can be used. Both of these functions offer many more arguments; details can be found in the NumPy documentation. The preceding code can be written using np.genfromtxt() as follows:
arr = np.genfromtxt('temp.csv', skip_header=1, usecols=(2,3), delimiter=',')
NumPy arrays produced as a result of applying AI to IoT data can be saved with np.savetxt(). For example, the array we loaded previously can be saved as follows:
np.savetxt('temp.csv', arr, delimiter=',')
The np.savetxt() function also accepts various other useful arguments, such as the format for saved fields and headers. Check the NumPy documentation for more details on this function.
CSV is the most popular data format on IoT platforms and devices. In this section, we learned how to read CSV data using three different packages in Python. Let's learn about XLSX, another popular format, in the next section.
- Practical Ansible 2
- 21天學(xué)通PHP
- 傳感器技術(shù)實驗教程
- 計算機網(wǎng)絡(luò)應(yīng)用基礎(chǔ)
- 傳感器與物聯(lián)網(wǎng)技術(shù)
- 內(nèi)模控制及其應(yīng)用
- TensorFlow Reinforcement Learning Quick Start Guide
- 云計算和大數(shù)據(jù)的應(yīng)用
- Linux Shell編程從初學(xué)到精通
- 會聲會影X4中文版從入門到精通
- Mastering Exploratory Analysis with pandas
- 工業(yè)機器人入門實用教程
- Hands-On DevOps
- Learning Cassandra for Administrators
- Flash CS3動畫制作