I/O Operation With NumPy

image.png

Image Source

Hello everyone. In this NumPy tutorial we will be talking about input output functions in NumPy. I/O operations is also typically referred to as transferring data to and from a computer or other peripheral devices. Example includes: reading a data, adding new data to an existing file and so on. For the tutorial purpose, I am going to use a simple data that you can download from Kaggle. The link to the data can be found here. Well the data description doesn't state anything about the data. It's just a sample data for someone who want to practice. It contains two columns: one column that has years of experience of an employee and the second column is the salary amount based on the experience. You need to signup in order to download the data. After downloading data, you need to unzip(extract) the files and move that file to the same directory where your code is. Alternatively, if you are using a Jupyter Notebook you have an option to upload the file like below:

image.png

Go on just upload the file called "Salary_Data.csv" file into the same directory. Or you don't need to do it but in the code you need to specify the directory URL to the same file.

import numpy as np

x = np.loadtxt('Salary_Data.csv', delimiter=",", usecols=(1), skiprows=1)

x



If you run the above code you will get output as below:

image.png

We use a loadtxt() function that loads data from a text and also takes many arguments. delimiter="," means that we want to separate the values by using a comma, usecols=(1) means that we want to work with only Salary column which is index number 1 and Years of experience is index 0. skiprows=1 means to skip the header which doesn't contain the actual value(just the column name). If you want to see total number of non zero values we can use np.count_nonzero(x) that will give output as 30 as the data don't have any zero values. We can also use min(), max(), mean() and many functions to perform operations on these values. The code is also pretty simple and straightforward

np.count_nonzero(x)

x.min()

x.max()

x.mean()


image.png

Lets suppose we want to save these values to other file. If you got a file already in that directory or somewhere, you can use that filename. Otherwise NumPy will create a file with the name you specify in the code if you don't have any file. This code will save all the values in variable x by creating a new file values.txt

np.savetxt("values.txt", x)

You can see in your working directory that values.txt has been created and you can open the file to see the data yourself or use the loadtxt() function to read the data.

image.png

Link to previous NumPy Post:

  1. Intro To NumPy Library
  2. Working With Arrays in NumPy
  3. Indexing and Slicing an Array in NumPy
  4. Array Manipulation Using NumPy
  5. Arithmetic and Mathematical Operations in NumPy


0
0
0.000
2 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000