Python Tutorial : Looking at the datatypes

Опубликовано: 22 Март 2020
на канале: DataCamp
225
4

Want to learn more? Take the full course at https://learn.datacamp.com/courses/ca... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---

We've seen we have some numeric values and some text values in our dataset. It's common to have data where each value is from a known set of categories. For example, a column season_of_year may have the values winter, spring, summer, and fall. These kinds of data are not simply strings. Let's look at our sample dataset again. If we look at the label column, we can see that it takes either the value a or the value b. We can treat these kinds of variables in a way that solves two problems for us.

The first problem is that our machine learning algorithms work on numbers. We need a numeric representation of these strings before we can do any sort of model-fitting. The second problem is that strings can be slow. We never know ahead of time how long a string is, so our computers have to take more time processing strings than numbers, which have a precise number of bits. In pandas, there is a special datatype called category that encodes our categorical data numerically, and--because of this numerical encoding--it can speed up our code. In pandas, we can call the astype function with the string category to change a column's type from object to category.

Here are two rows of the 'label' column from the sample dataframe. When the data is loaded, pandas assumes that these variables are strings, so the dtype is object. By calling astype('category'), we are returned a categorical variable. As we can see, pandas is already smarter about the values that appear in the column--in this case, the two values a and b.### Slide 5: Dummy variable encoding. We have actually converted these strings into a numeric representation of the categories.

To see this numeric representation, we can use the get_dummies function in pandas. This is called get_dummies because this process is called creating "dummy variables". Our dummy variables dataframe has two columns: the first, if the value is "label_a," the second if the value is "label_b." Each row contains a 1 if that row is of that category, and a 0 if not. This is also called a "binary indicator" representation. Note that the prefix_sep parameter is useful to tell the get_dummies function what character should separate the original column name and the column value for our dummy variable. Before we create categorical representations of our budget data, we want to mention

lambda functions, a feature of the Python language. Instead of using the def syntax you may have seen before, lambda functions let you make simple, one-line functions. For example, we may want a function that squares a variable. We can define a lambda function that takes a parameter, the variable x. The function itself just multiplies x by x and returns the result. We can call this function just like any other Python function, and it returns whatever the one line of code evaluates to.

In the sample dataframe, we only have one relevant column called label. But, you'll remember from looking at the budget data that there are multiple columns we want to make categorical. To make multiple columns into categories, we need to apply the function to each column separately. We will use a small lambda function to convert each column to a category. We then use the apply method on a pandas dataframe to apply this function to each of the relevant columns separately by passing the axis equals 0 parameter. Now it's your turn to use astype('category') to encode categorical variables in our actual dataset to help speed up the models that you're going to use on your data.

#DataCamp #PythonTutorial #Machinelearning #SchoolBudgetingwithMachineLearninginPython


На этой странице сайта вы можете посмотреть видео онлайн Python Tutorial : Looking at the datatypes длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь DataCamp 22 Март 2020, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 225 раз и оно понравилось 4 зрителям. Приятного просмотра!