- 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.
- Learning Apache Spark 2
- Blockchain Quick Start Guide
- Hands-On Neural Networks with Keras
- Photoshop CS3特效處理融會(huì)貫通
- 完全掌握AutoCAD 2008中文版:綜合篇
- INSTANT Autodesk Revit 2013 Customization with .NET How-to
- Docker High Performance(Second Edition)
- 工業(yè)自動(dòng)化技術(shù)實(shí)訓(xùn)指導(dǎo)
- Salesforce Advanced Administrator Certification Guide
- 自動(dòng)化生產(chǎn)線安裝與調(diào)試(三菱FX系列)(第二版)
- Silverlight 2完美征程
- Artificial Intelligence By Example
- HBase Essentials
- Photoshop CS4數(shù)碼照片處理入門(mén)、進(jìn)階與提高
- 暗戰(zhàn)強(qiáng)人:黑客攻防入門(mén)全程圖解