mean([1,2,3,44,8,90])
x=[1,2,3,4,5,4,3,3,3,90]
mean(x)
Import statistics
[Link](x)
[Link](x)
[Link](x)
[Link](x)
[Link](x)
[Link](x)
Python Correlation and Higher:
from scipy import stats
x=[2,3,4,5,6]
y=[18,20,22,24,26]
[Link](x,y)
[Link](x,y)
[Link](x, y)
or
import numpy as np
>>> x = [Link](10, 20)
>>> y = [Link]([2, 1, 4, 5, 8, 12, 18, 25, 96, 48])
>>> r = [Link](x, y)
For correlation more than 2 variables:
xyz = [Link]([[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
... [2, 1, 4, 5, 8, 12, 18, 25, 96, 48],
... [5, 3, 2, 1, 0, -2, -8, -11, -15, -16]])
>>> [Link](xyz)
Regression:
>>> import [Link]
>>> x = [Link](10, 20)
>>> y = [Link]([2, 1, 4, 5, 8, 12, 18, 25, 96, 48])
result = [Link](x, y)
>>> [Link]
7.4363636363636365
>>> [Link]
-85.92727272727274
>>> [Link]
0.7586402890911869
>>> [Link]
0.010964341301680825
>>> [Link]
import seaborn as sns
T=sns.load_dataset("titanic")
I=sns.load_dataset(“iris”)
[Link]()
[Link](I,hue="species")
[Link](column=['sepallength'],by='class')
x=[2,3,4,5,6,7,6,5,4,3,20]
[Link](x)
[Link](x)
[Link](x)
Solving equation:
4x+y+5z=27
2x+3y+z=19
6x+3y+3z=33
a = [Link]([[4,1,5],[2,3,1],[6,3,3]])
b=[Link]([27,19,33])
ans=[Link](a,b)
5: How to handle missing Data:
from statistics import median
>>> from math import isnan
>>> from itertools import filterfalse
data = [20.7, float('NaN'),19.2, 18.3, float('NaN'), 14.4]
>>> sorted(data) # This has surprising behavior
[20.7, nan, 14.4, 18.3, 19.2, nan]
>>> median(data) # This result is unexpected
16.35
>>> sum(map(isnan, data)) # Number of missing values
>>> clean = list(filterfalse(isnan, data)) # Strip NaN values
>>> clean
[20.7, 19.2, 18.3, 14.4]
>>> sorted(clean) # Sorting now works as expected
[14.4, 18.3, 19.2, 20.7]
>>> median(clean) # This result is now well defined
18.75
sum(map(isnan, data))
clean = list(filterfalse(isnan, data)) # Strip NaN values
>>> clean
[20.7, 19.2, 18.3, 14.4]
>>> sorted(clean) # Sorting now works as expected
[14.4, 18.3, 19.2, 20.7]
>>> median(clean) # This result is now well defined
Averages and measures of central location
6: Datframe and correlation through Pandas:
>>> import pandas as pd
>>> x = [Link](range(10, 20))
>>> x
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: int64
>>> y = [Link]([2, 1, 4, 5, 8, 12, 18, 25, 96, 48])
>>> y
0 2
1 1
2 4
3 5
4 8
5 12
6 18
7 25
8 96
9 48
dtype: int64
>>> z = [Link]([5, 3, 2, 1, 0, -2, -8, -11, -15, -16])
>>> z
0 5
1 3
2 2
3 1
4 0
5 -2
6 -8
7 -11
8 -15
9 -16
dtype: int64
>>> xy = [Link]({'x-values': x, 'y-values': y})
>>> xy
x-values y-values
0 10 2
1 11 1
2 12 4
3 13 5
4 14 8
5 15 12
6 16 18
7 17 25
8 18 96
9 19 48
>>> xyz = [Link]({'x-values': x, 'y-values': y, 'z-values': z})
>>> xyz
x-values y-values z-values
0 10 2 5
1 11 1 3
2 12 4 2
3 13 5 1
4 14 8 0
5 15 12 -2
6 16 18 -8
7 17 25 -11
8 18 96 -15
9 19 48 -16
You now have three Series objects called x, y, and z. You also have
two DataFrame objects, xy and xyz.
Note: When you work with DataFrame instances, you should be aware that the rows are
observations and the columns are features. This is consistent with the usual practice in machine
learning.
You’ve already learned how to use .corr() with Series objects to get the Pearson correlation
coefficient:
Python
>>> [Link](y)
0.7586402890911867
Here, you call .corr() on one object and pass the other as the first argument.
If you provide a nan value, then .corr() will still work, but it will exclude observations that
contain nan values:
Python
>>> u, u_with_nan = [Link]([1, 2, 3]), [Link]([1, 2, [Link], 3])
>>> v, w = [Link]([1, 4, 8]), [Link]([1, 4, 154, 8])
>>> [Link](v)
0.9966158955401239
>>> u_with_nan.corr(w)
0.9966158955401239
You get the same value of the correlation coefficient in these two examples. That’s
because .corr() ignores the pair of values ([Link], 154) that has a missing value.
You can also use .corr() with DataFrame objects. You can use it to get the correlation matrix for their
columns:
Python
>>> corr_matrix = [Link]()
>>> corr_matrix
x-values y-values
x-values 1.00000 0.75864
y-values 0.75864 1.00000
The resulting correlation matrix is a new instance of DataFrame and holds the correlation coefficients
for the columns xy['x-values'] and xy['y-values']. Such labeled results are usually very convenient to
work with because you can access them with either their labels or their integer position indices:
Python
>>> corr_matrix.at['x-values', 'y-values']
0.7586402890911869
>>> corr_matrix.iat[0, 1]
0.7586402890911869
This example shows two ways of accessing values:
1. Use .at[] to access a single value by row and column labels.
2. Use .iat[] to access a value by the positions of its row and column.
You can apply .corr() the same way with DataFrame objects that contain three or more columns:
Python
>>> [Link]()
x-values y-values z-values
x-values 1.000000 0.758640 -0.968072
y-values 0.758640 1.000000 -0.834079
z-values -0.968072 -0.834079 1.000000
You’ll get a correlation matrix with the following correlation coefficients:
0.758640 for x-values and y-values
-0.968072 for x-values and z-values
-0.834079 for y-values and z-values
Another useful method is .corrwith(), which allows you to calculate the correlation coefficients
between the rows or columns of one DataFrame object and another Series or DataFrame object
passed as the first argument: