Merge branch 'main' into nirmalya8

This commit is contained in:
Nirmalya Misra 2021-10-06 01:44:13 +05:30 коммит произвёл GitHub
Родитель e30b7208ab 15c387f4ac
Коммит 77cfe8c707
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 389 добавлений и 2 удалений

Просмотреть файл

@ -33,7 +33,7 @@ This definition highlights the following important aspects of data science:
> Another important aspect of Data Science is that it studies how data can be gathered, stored and operated upon using computers. While statistics gives us mathematical foundations, data science applies mathematical concepts to actually draw insights from data.
One of the ways (attributed to [Jim Gray](https://en.wikipedia.org/wiki/Jim_Gray_(computer_scientist))) to look at the data science is to consider it to be a separate paradigm of science:
* **Empyrical**, in which we rely mostly on observations and results of experiments
* **Empirical**, in which we rely mostly on observations and results of experiments
* **Theoretical**, where new concepts emerge from existing scientific knowledge
* **Computational**, where we discover new principles based on some computational experiments
* **Data-Driven**, based on discovering relationships and patterns in the data

Просмотреть файл

@ -28,10 +28,294 @@ Depending on its source, raw data may contain some inconsistencies that will cau
- **Missing Data**: Missing data can cause inaccuracies as well as weak or biased results. Sometimes these can be resolved by a "reload" of the data, filling in the missing values with computation and code like Python, or simply just removing the value and corresponding data. There are numerous reasons for why data may be missing and the actions that are taken to resolve these missing values can be dependent on how and why they went missing in the first place.
## Exploring DataFrame information
> **Learning goal:** By the end of this subsection, you should be comfortable finding general information about the data stored in pandas DataFrames.
Once you have loaded your data into pandas, it will more likely than not be in a DataFrame(refer to the previous [lesson](https://github.com/microsoft/Data-Science-For-Beginners/tree/main/2-Working-With-Data/07-python#dataframe) for detailed overview). However, if the data set in your DataFrame has 60,000 rows and 400 columns, how do you even begin to get a sense of what you're working with? Fortunately, [pandas](https://pandas.pydata.org/) provides some convenient tools to quickly look at overall information about a DataFrame in addition to the first few and last few rows.
In order to explore this functionality, we will import the Python scikit-learn library and use an iconic dataset: the **Iris data set**.
```python
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
iris_df = pd.DataFrame(data=iris['data'], columns=iris['feature_names'])
```
| |sepal length (cm)|sepal width (cm)|petal length (cm)|petal width (cm)|
|----------------------------------------|-----------------|----------------|-----------------|----------------|
|0 |5.1 |3.5 |1.4 |0.2 |
|1 |4.9 |3.0 |1.4 |0.2 |
|2 |4.7 |3.2 |1.3 |0.2 |
|3 |4.6 |3.1 |1.5 |0.2 |
|4 |5.0 |3.6 |1.4 |0.2 |
- **DataFrame.info**: To start off, the `info()` method is used to print a summary of the content present in a `DataFrame`. Let's take a look at this dataset to see what we have:
```python
iris_df.info()
```
```
RangeIndex: 150 entries, 0 to 149
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 sepal length (cm) 150 non-null float64
1 sepal width (cm) 150 non-null float64
2 petal length (cm) 150 non-null float64
3 petal width (cm) 150 non-null float64
dtypes: float64(4)
memory usage: 4.8 KB
```
From this, we know that the *Iris* dataset has 150 entries in four columns with no null entries. All of the data is stored as 64-bit floating-point numbers.
- **DataFrame.head()**: Next, to check the actual content of the `DataFrame`, we use the `head()` method. Let's see what the first few rows of our `iris_df` look like:
```python
iris_df.head()
```
```
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
```
- **DataFrame.tail()**: Conversely, to check the last few rows of the `DataFrame`, we use the `tail()` method:
```python
iris_df.tail()
```
```
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
145 6.7 3.0 5.2 2.3
146 6.3 2.5 5.0 1.9
147 6.5 3.0 5.2 2.0
148 6.2 3.4 5.4 2.3
149 5.9 3.0 5.1 1.8
```
> **Takeaway:** Even just by looking at the metadata about the information in a DataFrame or the first and last few values in one, you can get an immediate idea about the size, shape, and content of the data you are dealing with.
## Dealing with Missing Data
> **Learning goal:** By the end of this subsection, you should know how to replace or remove null values from DataFrames.
Most of the time the datasets you want to use (of have to use) have missing values in them. How missing data is handled carries with it subtle tradeoffs that can affect your final analysis and real-world outcomes.
Pandas handles missing values in two ways. The first you've seen before in previous sections: `NaN`, or Not a Number. This is a actually a special value that is part of the IEEE floating-point specification and it is only used to indicate missing floating-point values.
For missing values apart from floats, pandas uses the Python `None` object. While it might seem confusing that you will encounter two different kinds of values that say essentially the same thing, there are sound programmatic reasons for this design choice and, in practice, going this route enables pandas to deliver a good compromise for the vast majority of cases. Notwithstanding this, both `None` and `NaN` carry restrictions that you need to be mindful of with regards to how they can be used.
Check out more about `NaN` and `None` from the [notebook](https://github.com/microsoft/Data-Science-For-Beginners/blob/main/4-Data-Science-Lifecycle/15-analyzing/notebook.ipynb)!
- **Detecting null values**: In `pandas`, the `isnull()` and `notnull()` methods are your primary methods for detecting null data. Both return Boolean masks over your data. We will be using `numpy` for `NaN` values:
```python
import numpy as np
example1 = pd.Series([0, np.nan, '', None])
example1.isnull()
```
```
0 False
1 True
2 False
3 True
dtype: bool
```
Look closely at the output. Does any of it surprise you? While `0` is an arithmetic null, it's nevertheless a perfectly good integer and pandas treats it as such. `''` is a little more subtle. While we used it in Section 1 to represent an empty string value, it is nevertheless a string object and not a representation of null as far as pandas is concerned.
Now, let's turn this around and use these methods in a manner more like you will use them in practice. You can use Boolean masks directly as a ``Series`` or ``DataFrame`` index, which can be useful when trying to work with isolated missing (or present) values.
> **Takeaway**: Both the `isnull()` and `notnull()` methods produce similar results when you use them in `DataFrame`s: they show the results and the index of those results, which will help you enormously as you wrestle with your data.
- **Dropping null values**: Beyond identifying missing values, pandas provides a convenient means to remove null values from `Series` and `DataFrame`s. (Particularly on large data sets, it is often more advisable to simply remove missing [NA] values from your analysis than deal with them in other ways.) To see this in action, let's return to `example1`:
```python
example1 = example1.dropna()
example1
```
```
0 0
2
dtype: object
```
Note that this should look like your output from `example3[example3.notnull()]`. The difference here is that, rather than just indexing on the masked values, `dropna` has removed those missing values from the `Series` `example1`.
Because `DataFrame`s have two dimensions, they afford more options for dropping data.
```python
example2 = pd.DataFrame([[1, np.nan, 7],
[2, 5, 8],
[np.nan, 6, 9]])
example2
```
| | 0 | 1 | 2 |
|------|---|---|---|
|0 |1.0|NaN|7 |
|1 |2.0|5.0|8 |
|2 |NaN|6.0|9 |
(Did you notice that pandas upcast two of the columns to floats to accommodate the `NaN`s?)
You cannot drop a single value from a `DataFrame`, so you have to drop full rows or columns. Depending on what you are doing, you might want to do one or the other, and so pandas gives you options for both. Because in data science, columns generally represent variables and rows represent observations, you are more likely to drop rows of data; the default setting for `dropna()` is to drop all rows that contain any null values:
```python
example2.dropna()
```
```
0 1 2
1 2.0 5.0 8
```
If necessary, you can drop NA values from columns. Use `axis=1` to do so:
```python
example2.dropna(axis='columns')
```
```
2
0 7
1 8
2 9
```
Notice that this can drop a lot of data that you might want to keep, particularly in smaller datasets. What if you just want to drop rows or columns that contain several or even just all null values? You specify those setting in `dropna` with the `how` and `thresh` parameters.
By default, `how='any'` (if you would like to check for yourself or see what other parameters the method has, run `example4.dropna?` in a code cell). You could alternatively specify `how='all'` so as to drop only rows or columns that contain all null values. Let's expand our example `DataFrame` to see this in action.
```python
example2[3] = np.nan
example2
```
| |0 |1 |2 |3 |
|------|---|---|---|---|
|0 |1.0|NaN|7 |NaN|
|1 |2.0|5.0|8 |NaN|
|2 |NaN|6.0|9 |NaN|
The `thresh` parameter gives you finer-grained control: you set the number of *non-null* values that a row or column needs to have in order to be kept:
```python
example2.dropna(axis='rows', thresh=3)
```
```
0 1 2 3
1 2.0 5.0 8 NaN
```
Here, the first and last row have been dropped, because they contain only two non-null values.
- **Filling null values**: Depending on your dataset, it can sometimes make more sense to fill null values with valid ones rather than drop them. You could use `isnull` to do this in place, but that can be laborious, particularly if you have a lot of values to fill. Because this is such a common task in data science, pandas provides `fillna`, which returns a copy of the `Series` or `DataFrame` with the missing values replaced with one of your choosing. Let's create another example `Series` to see how this works in practice.
```python
example3 = pd.Series([1, np.nan, 2, None, 3], index=list('abcde'))
example3
```
```
a 1.0
b NaN
c 2.0
d NaN
e 3.0
dtype: float64
```
You can fill all of the null entries with a single value, such as `0`:
```python
example3.fillna(0)
```
```
a 1.0
b 0.0
c 2.0
d 0.0
e 3.0
dtype: float64
```
You can **forward-fill** null values, which is to use the last valid value to fill a null:
```python
example3.fillna(method='ffill')
```
```
a 1.0
b 1.0
c 2.0
d 2.0
e 3.0
dtype: float64
```
You can also **back-fill** to propagate the next valid value backward to fill a null:
```python
example3.fillna(method='bfill')
```
```
a 1.0
b 2.0
c 2.0
d 3.0
e 3.0
dtype: float64
```
As you might guess, this works the same with `DataFrame`s, but you can also specify an `axis` along which to fill null values. taking the previously used `example2` again:
```python
example2.fillna(method='ffill', axis=1)
```
```
0 1 2 3
0 1.0 1.0 7.0 7.0
1 2.0 5.0 8.0 8.0
2 NaN 6.0 9.0 9.0
```
Notice that when a previous value is not available for forward-filling, the null value remains.
> **Takeaway:** There are multiple ways to deal with missing values in your datasets. The specific strategy you use (removing them, replacing them, or even how you replace them) should be dictated by the particulars of that data. You will develop a better sense of how to deal with missing values the more you handle and interact with datasets.
## Removing duplicate data
> **Learning goal:** By the end of this subsection, you should be comfortable identifying and removing duplicate values from DataFrames.
In addition to missing data, you will often encounter duplicated data in real-world datasets. Fortunately, `pandas` provides an easy means of detecting and removing duplicate entries.
- **Identifying duplicates: `duplicated`**: You can easily spot duplicate values using the `duplicated` method in pandas, which returns a Boolean mask indicating whether an entry in a `DataFrame` is a duplicate of an ealier one. Let's create another example `DataFrame` to see this in action.
```python
example4 = pd.DataFrame({'letters': ['A','B'] * 2 + ['B'],
'numbers': [1, 2, 1, 3, 3]})
example4
```
| |letters|numbers|
|------|-------|-------|
|0 |A |1 |
|1 |B |2 |
|2 |A |1 |
|3 |B |3 |
|4 |B |3 |
```python
example4.duplicated()
```
```
0 False
1 False
2 True
3 False
4 True
dtype: bool
```
- **Dropping duplicates: `drop_duplicates`: `drop_duplicates` simply returns a copy of the data for which all of the `duplicated` values are `False`:
```python
example4.drop_duplicates()
```
```
letters numbers
0 A 1
1 B 2
3 B 3
```
Both `duplicated` and `drop_duplicates` default to consider all columnsm but you can specify that they examine only a subset of columns in your `DataFrame`:
```python
example6.drop_duplicates(['letters'])
```
```
letters numbers
0 A 1
1 B 2
```
> **Takeaway:** Removing duplicate data is an essential part of almost every data-science project. Duplicate data can change the results of your analyses and give you inaccurate results!
## 🚀 Challenge
Give the exercises in the [notebook](https://github.com/microsoft/Data-Science-For-Beginners/blob/main/4-Data-Science-Lifecycle/15-analyzing/notebook.ipynb) a try!
All of the discussed materials are provided as a [Jupyter Notebook](https://github.com/microsoft/Data-Science-For-Beginners/blob/main/4-Data-Science-Lifecycle/15-analyzing/notebook.ipynb). Additionally, there are exercises present after each section, give them a try!
## [Post-Lecture Quiz](https://red-water-0103e7a0f.azurestaticapps.net/quiz/15)

Просмотреть файл

@ -40,6 +40,7 @@
"\n",
"> **Learning goal:** By the end of this subsection, you should be comfortable finding general information about the data stored in pandas DataFrames.\n",
"\n",
"Once you have loaded your data into pandas, it will more likely than not be in a `DataFrame`. However, if the data set in your `DataFrame` has 60,000 rows and 400 columns, how do you even begin to get a sense of what you're working with? Fortunately, pandas provides some convenient tools to quickly look at overall information about a `DataFrame` in addition to the first few and last few rows.\n",
"\n",
"In order to explore this functionality, we will import the Python scikit-learn library and use an iconic dataset that every data scientist has seen hundreds of times: British biologist Ronald Fisher's *Iris* data set used in his 1936 paper \"The use of multiple measurements in taxonomic problems\":"
@ -52,6 +53,7 @@
"trusted": false,
"id": "hB1RofhdgRrp"
},
"source": [
"import pandas as pd\n",
"from sklearn.datasets import load_iris\n",
@ -168,6 +170,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "dHHRyG0_gRrt",
@ -180,6 +183,7 @@
"iris_df.info()"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
@ -198,6 +202,7 @@
"memory usage: 4.8 KB\n"
]
}
]
},
{
@ -366,6 +371,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "DZMJZh0OgRrw",
@ -458,6 +464,7 @@
]
},
"metadata": {},
"execution_count": 6
}
]
@ -484,6 +491,7 @@
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"trusted": false,
@ -507,6 +515,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "heanjfGWgRr2",
@ -599,6 +608,7 @@
]
},
"metadata": {},
"execution_count": 8
}
]
@ -651,6 +661,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "QIoNdY4ngRr7",
@ -665,6 +676,7 @@
"example1 = np.array([2, None, 6, 8])\n",
"example1"
],
"execution_count": 9,
"outputs": [
{
@ -675,9 +687,18 @@
]
},
"metadata": {},
"execution_count": 9
}
]
"execution_count": 6
}
],
"metadata": {
"trusted": false
}
},
{
"cell_type": "markdown",
@ -692,6 +713,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "gWbx-KB9gRr8",
@ -709,6 +731,7 @@
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
@ -742,6 +765,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "rcFYfMG9gRr9",
@ -763,6 +787,7 @@
]
},
"metadata": {},
"execution_count": 11
}
]
@ -790,9 +815,18 @@
]
},
"metadata": {},
"execution_count": 12
}
]
"execution_count": 9
}
],
"metadata": {
"trusted": false
}
},
{
"cell_type": "markdown",
@ -805,6 +839,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "LCInVgSSgRr_",
@ -813,10 +848,14 @@
},
"outputId": "fa06495a-0930-4867-87c5-6023031ea8b5"
},
"execution_count": 10,
"source": [
"example2 = np.array([2, np.nan, 6, 8]) \n",
"example2.sum(), example2.min(), example2.max()"
],
"execution_count": 13,
"outputs": [
{
@ -827,9 +866,17 @@
]
},
"metadata": {},
"execution_count": 13
}
]
"execution_count": 10
}
],
"metadata": {
"trusted": false
}
},
{
"cell_type": "markdown",
@ -842,6 +889,13 @@
},
{
"cell_type": "code",
"execution_count": 11,
"source": [
"# What happens if you add np.nan and None together?\n"
],
"outputs": [],
"metadata": {
"collapsed": true,
"trusted": false,
@ -875,6 +929,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "Nji-KGdNgRsA",
@ -887,7 +942,9 @@
"int_series = pd.Series([1, 2, 3], dtype=int)\n",
"int_series"
],
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
@ -900,6 +957,7 @@
]
},
"metadata": {},
"execution_count": 15
}
]
@ -958,6 +1016,7 @@
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"trusted": false,
@ -971,6 +1030,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "1XdaJJ7PgRsC",
@ -996,6 +1056,7 @@
]
},
"metadata": {},
"execution_count": 18
}
]
@ -1050,11 +1111,13 @@
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"trusted": false,
"id": "ggDVf5uygRsD"
},
"source": [
"# Try running example3[example3.notnull()].\n",
"# Before you do so, what do you expect to see?\n"
@ -1112,6 +1175,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "7uIvS097gRsD",
@ -1136,6 +1200,7 @@
]
},
"metadata": {},
"execution_count": 21
}
]
@ -1153,6 +1218,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "an-l74sPgRsE",
@ -1162,12 +1228,14 @@
},
"outputId": "340876a0-63ad-40f6-bd54-6240cdae50ab"
},
"source": [
"example4 = pd.DataFrame([[1, np.nan, 7], \n",
" [2, 5, 8], \n",
" [np.nan, 6, 9]])\n",
"example4"
],
"execution_count": 22,
"outputs": [
{
@ -1228,6 +1296,7 @@
]
},
"metadata": {},
"execution_count": 22
}
]
@ -1245,6 +1314,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "jAVU24RXgRsE",
@ -1303,6 +1373,7 @@
]
},
"metadata": {},
"execution_count": 23
}
]
@ -1318,6 +1389,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "GrBhxu9GgRsE",
@ -1382,6 +1454,7 @@
]
},
"metadata": {},
"execution_count": 24
}
]
@ -1399,6 +1472,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "Bcf_JWTsgRsF",
@ -1412,6 +1486,7 @@
"example4[3] = np.nan\n",
"example4"
],
"execution_count": 25,
"outputs": [
{
@ -1476,6 +1551,7 @@
]
},
"metadata": {},
"execution_count": 25
}
]
@ -1504,6 +1580,12 @@
},
{
"cell_type": "code",
"execution_count": 22,
"source": [
"# How might you go about dropping just column 3?\n",
"# Hint: remember that you will need to supply both the axis parameter and the how parameter.\n"
],
"outputs": [],
"metadata": {
"collapsed": true,
"trusted": false,
@ -1527,6 +1609,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "M9dCNMaagRsG",
@ -1587,6 +1670,7 @@
]
},
"metadata": {},
"execution_count": 27
}
]
@ -1630,6 +1714,7 @@
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
@ -2438,6 +2523,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "KXMIPsQdgRsH",
@ -2464,12 +2550,14 @@
]
},
"metadata": {},
"execution_count": 39
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RRlI5f_hkfKe"
},
@ -2492,6 +2580,7 @@
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"trusted": false,
@ -2514,6 +2603,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "vO3BuNrggRsI",
@ -2540,6 +2630,7 @@
]
},
"metadata": {},
"execution_count": 41
}
]
@ -2555,6 +2646,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "4M5onHcEgRsI",
@ -2581,6 +2673,7 @@
]
},
"metadata": {},
"execution_count": 42
}
]
@ -2597,6 +2690,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "aRpIvo4ZgRsI",
@ -2673,6 +2767,7 @@
]
},
"metadata": {},
"execution_count": 43
}
]
@ -2803,6 +2898,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "OtYVErEygRsJ",
@ -2879,6 +2975,7 @@
]
},
"metadata": {},
"execution_count": 46
}
]
@ -3426,6 +3523,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "ZLu6FEnZgRsJ",
@ -3508,6 +3606,7 @@
]
},
"metadata": {},
"execution_count": 52
}
]
@ -3556,6 +3655,7 @@
},
{
"cell_type": "code",
"metadata": {
"trusted": false,
"id": "w_YPpqIqgRsK",
@ -3624,6 +3724,7 @@
]
},
"metadata": {},
"execution_count": 54
}
]
@ -3701,6 +3802,7 @@
]
},
"metadata": {},
"execution_count": 55
}
]
@ -3714,5 +3816,6 @@
"> **Takeaway:** Removing duplicate data is an essential part of almost every data-science project. Duplicate data can change the results of your analyses and give you inaccurate results!"
]
}
]
}