Docsity

Deep learning information, Exercises of Artificial Intelligence

Toggle sidebar
bg1
T. Y. B. Tech (CSE) - II Subject: Open Source Lab-II
Department of Computer Science & Engineering,
Textile and Engineering Institute, Ichalkaranji. Page 1
Experiment No.: 1
Title: Write a program to import and export, clean and prepare the data.
Objectives:
1. To learn how to clean data.
2. To learn how to deal with missing values.
Theory:
Once we have our data in Python, then we can perform all the subsequent data analysis
procedures we need.
Data acquisition is a process of loading and reading data into notebook from various sources.
To read any data using Python‟s pandas package, there are two important factors to consider:
format and file path.
Format is the way data is encoded. We can usually tell different encoding schemes by looking
at the ending of the file name. Some common encodings are csv, json, xlsx, hdf and so forth.
The (file) path tells us where the data is stored. Usually it is stored either on the computer we
are using, or online on the internet.
A large number of properties are associated with each data point. Because the properties are
separated from each other by commas, we can guess the data format is CAV, which stands
for Comma Separated Values. At this point, these are just numbers and don‟t mean much to
humans, but once we read in this data, we can try to make more sense out of it. In pandas, the
“read_csv()” method can read in files with columns separated by commas into a pandas
DataFrame. Reading data in pandas can be done quickly in three lines.
1. First, import pandas.
2. Then define a variable with the file path.
3. And then use the read_csv method to import the data.
However, “read_csv” assumes that the data contains a header. If data set has no column
headers, so we need to specify “read_csv” to not assign headers by setting header to “none”.
After reading the dataset, it is a good idea to look at the dataframe to get a better intuition and
to ensure that everything occurred the way you expected. Since printing the entire dataset
may take up too much time and resources, to save time, we can just use dataframe.head() to
show the first n rows of the data frame. Similarly, dataframe.tail(n) shows the bottom n rows
of data frame. Here, we printed out the first 5 rows of data. It seems that the dataset was read
successfully!
We can see that pandas automatically set the column header as a list of integers, because we
set header=None when we read the data. It is difficult to work with the DataFrame without
having meaningful column names, however, we can assign column names in pandas. Column
names can be in a separate file. We first put the Column names in a list called headers. Then,
set df.columns equals headers to replace the default integer headers by the list. If we use the
pf3
pf4

Partial preview of the text

Download Deep learning information and more Exercises Artificial Intelligence in PDF only on Docsity!

Department of Computer Science & Engineering,

Experiment No.: 1

Title: Write a program to import and export, clean and prepare the data.

Objectives:

  1. To learn how to clean data.
  2. To learn how to deal with missing values.

Theory:

Once we have our data in Python, then we can perform all the subsequent data analysis procedures we need. Data acquisition is a process of loading and reading data into notebook from various sources. To read any data using Python‟s pandas package, there are two important factors to consider: format and file path. Format is the way data is encoded. We can usually tell different encoding schemes by looking at the ending of the file name. Some common encodings are csv, json, xlsx, hdf and so forth. The (file) path tells us where the data is stored. Usually it is stored either on the computer we are using, or online on the internet. A large number of properties are associated with each data point. Because the properties are separated from each other by commas, we can guess the data format is CAV, which stands for Comma Separated Values. At this point, these are just numbers and don‟t mean much to humans, but once we read in this data, we can try to make more sense out of it. In pandas, the “read_csv()” method can read in files with columns separated by commas into a pandas DataFrame. Reading data in pandas can be done quickly in three lines.

  1. First, import pandas.
  2. Then define a variable with the file path.
  3. And then use the read_csv method to import the data. However, “read_csv” assumes that the data contains a header. If data set has no column headers, so we need to specify “read_csv” to not assign headers by setting header to “none”. After reading the dataset, it is a good idea to look at the dataframe to get a better intuition and to ensure that everything occurred the way you expected. Since printing the entire dataset may take up too much time and resources, to save time, we can just use dataframe.head() to show the first n rows of the data frame. Similarly, dataframe.tail(n) shows the bottom n rows of data frame. Here, we printed out the first 5 rows of data. It seems that the dataset was read successfully! We can see that pandas automatically set the column header as a list of integers, because we set header=None when we read the data. It is difficult to work with the DataFrame without having meaningful column names, however, we can assign column names in pandas. Column names can be in a separate file. We first put the Column names in a list called headers. Then, set df.columns equals headers to replace the default integer headers by the list. If we use the

Department of Computer Science & Engineering,

head() method introduced to check the dataset, we see the correct headers inserted at the top of each column. At some point in time after you‟ve done operations on your dataframe, you may want to export your pandas dataframe to a new CSV file. You can do this using the method, ”to_csv()". To do this, specify the file path (which includes the filename) that you want to write to. For example, if you would like to save the dataframe "df" as "automobile.csv" to your own computer, you can use the syntax: df.to_csv ("automobile.csv"). This shows how to read and save csv files. However, pandas also supports importing and exporting of most data filetypes with different dataset formats. The code syntax for reading and saving other data formats is very similar to read or save csv file. Data pre-processing is a necessary step in data analysis. It is the process of converting or mapping data from one “raw” form into another format to make it ready for further analysis. Data pre-processing is also often called “data cleaning” or “data wrangling”, and there are likely other terms. Here are the topics that we‟ll be covering in this experiment: First, we‟ll show you how to identify and handle missing values. A “missing value” condition occurs whenever a data entry is left empty. Then, we‟ll cover data formats. Data from different sources may be in various formats, in different units or in various conventions. We will introduce some methods in Python pandas that can standardize the values into the same format, or unit, or convention. After that, we‟ll cover data normalization. Different columns of numerical data may have very different ranges, and direct comparison is often not meaningful. Normalization is a way to bring all data into a similar range, for more useful comparison. Specifically, we‟ll focus on the techniques of centering and scaling. And then, we‟ll introduce data binning. Binning creates bigger categories from a set of numerical values. It is particularly useful for comparison between groups of data. And lastly, we‟ll talk about categorical variables and show you how to convert categorical values into numeric variables to make statistical modeling easier. In Python, we usually perform operations along columns; each row of the column represents a sample, i.e., a different used car in the database. You access a column by specifying the name of the column. For example, you can access "symbolling” and “body-style”; each of these columns is a pandas series. When nodata value is stored for a feature for a particular observation, we say this feature has a “missing value”. Usually “missing value: in dataset” appears as “?”, “N/A”, 0 or just a blank cell. In the example here, the “normalized-losses” feature has a “missing value”, which is represented with NaN. But how can you deal with missing data? There are many ways to deal with missing values, and this is regardless of Python, R, or whatever tool you use. Of course, each situation is different and should be judged differently. However, these are the typical options you can consider:

  • The first is to check if the person or group that collected the data can go back and find what the actual value should be. Another possibility is just to remove the data where that missing value is found.
  • When you drop data, you can either drop the whole variable or just the single data

Department of Computer Science & Engineering,

containing missing values. And then we learned how to replace missing values with other values. But don‟t forget the other ways to deal with missing data: you can always check for a higher quality dataset or source. Or, in some cases, you may want to leave the missing data as missing data.

Key concepts: Data Cleaning, Data Pre-processing

Algorithm:

 Read data set in Pandas Dataframe  Change the default header in DataFrame.  Replace missing values with constant or values specified  Replace missing values with mean  Replace missing values with mode  Replace the missing values with imputed values based on the other characteristics of the record  Export pre-processed dataset in CSV or other formats