Data Science

数据分析 2023-01-05 1449
预计阅读时间:68 分钟
import numpy as np
import pandas as pd
data = {'ages': [14, 18, 24, 12],
        'heights': [165, 180, 176, 184]}
df = pd.DataFrame(data)
df
ages heights
0 14 165
1 18 180
2 24 176
3 12 184
df = pd.DataFrame(data, index=['James', 'Bob', 'Amy', 'Dave'])
df
ages heights
James 14 165
Bob 18 180
Amy 24 176
Dave 12 184
# 选择行  select row
df.loc['Bob']
ages        18
heights    180
Name: Bob, dtype: int64
# 选择多行 select multiple rows
df.loc[['Bob', 'James']]
ages heights
Bob 18 180
James 14 165
# 选择列 select columns
df["ages"]
James    14
Bob      18
Amy      24
Dave     12
Name: ages, dtype: int64
# 选择多列 select multiple columns
df[['ages', 'heights']]
ages heights
James 14 165
Bob 18 180
Amy 24 176
Dave 12 184
# 根据索引选择行 按照列表切片方法 》》》!!!索引从0开始 第二个参数代表某列索引从0开始
# "iloc" follows the same rules as slicing does with Python lists.
df.iloc[1]
ages        18
heights    180
Name: Bob, dtype: int64
df.iloc[:3]
ages heights
James 14 165
Bob 18 180
Amy 24 176
df.iloc[0:4]
ages heights
James 14 165
Bob 18 180
Amy 24 176
Dave 12 184
df.iloc[:, 1]
James    165
Bob      180
Amy      176
Dave     184
Name: heights, dtype: int64
# 逻辑操作符 &且 |或
df[(df['ages'] > 18) & (df['heights'] > 180)]
ages heights
df[(df['ages'] > 18) | (df['heights'] > 180)]
ages heights
Amy 24 176
Dave 12 184
# 读取文件
df = pd.read_csv('https://www.sololearn.com/uploads/ca-covid.csv')
df
date state cases deaths
0 25.01.20 California 1 0
1 26.01.20 California 1 0
2 27.01.20 California 0 0
3 28.01.20 California 0 0
4 29.01.20 California 0 0
... ... ... ... ...
337 27.12.20 California 37555 62
338 28.12.20 California 41720 246
339 29.12.20 California 34166 425
340 30.12.20 California 32386 437
341 31.12.20 California 32264 574

342 rows × 4 columns

# 默认读取前5行  也可加入参数读取多行 example:df.head(100) 读取前100行 df.head(-1)除去最后1行 如果是负数以次内推
df.head()
date state cases deaths
0 25.01.20 California 1 0
1 26.01.20 California 1 0
2 27.01.20 California 0 0
3 28.01.20 California 0 0
4 29.01.20 California 0 0
# 默认读取后5行  也可加入参数读取多行 example:df.tail(100) 读取后100行 df.head(-2)除去第1行 如果是负数以次内推
df.tail()
date state cases deaths
337 27.12.20 California 37555 62
338 28.12.20 California 41720 246
339 29.12.20 California 34166 425
340 30.12.20 California 32386 437
341 31.12.20 California 32264 574
# 获取dataframe的信息  df.info()
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 342 entries, 0 to 341
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   date    342 non-null    object
 1   state   342 non-null    object
 2   cases   342 non-null    int64 
 3   deaths  342 non-null    int64 
dtypes: int64(2), object(2)
memory usage: 10.8+ KB
# 设置列中的日期为索引,inplace=True 含义将改变应用到原表,不需要我们新建变量接受。
df.set_index("date", inplace=True)
df
state cases deaths
date
25.01.20 California 1 0
26.01.20 California 1 0
27.01.20 California 0 0
28.01.20 California 0 0
29.01.20 California 0 0
... ... ... ...
27.12.20 California 37555 62
28.12.20 California 41720 246
29.12.20 California 34166 425
30.12.20 California 32386 437
31.12.20 California 32264 574

342 rows × 3 columns

# 删除某一列  axis有两个参数1代表列,0代表行
df.drop('state', axis=1, inplace=True)
df
cases deaths
date
25.01.20 1 0
26.01.20 1 0
27.01.20 0 0
28.01.20 0 0
29.01.20 0 0
... ... ...
27.12.20 37555 62
28.12.20 41720 246
29.12.20 34166 425
30.12.20 32386 437
31.12.20 32264 574

342 rows × 2 columns

# 找出20年12月31日的病例及死亡数
df.loc['31.12.20']
cases     32264
deaths      574
Name: 31.12.20, dtype: int64
# 新增一列月份
month = pd.to_datetime(pd.read_csv('https://www.sololearn.com/uploads/ca-covid.csv')['date'], format="%d.%m.%y").dt.month_name()
df['month'] = list(month)
df
cases deaths month
date
25.01.20 1 0 January
26.01.20 1 0 January
27.01.20 0 0 January
28.01.20 0 0 January
29.01.20 0 0 January
... ... ... ...
27.12.20 37555 62 December
28.12.20 41720 246 December
29.12.20 34166 425 December
30.12.20 32386 437 December
31.12.20 32264 574 December

342 rows × 3 columns

# 新增weekday
df['weekday'] = list(pd.to_datetime(pd.read_csv('https://www.sololearn.com/uploads/ca-covid.csv')['date'], format="%d.%m.%y").dt.strftime("%A"))
df
cases deaths month weekday
date
25.01.20 1 0 January Saturday
26.01.20 1 0 January Sunday
27.01.20 0 0 January Monday
28.01.20 0 0 January Tuesday
29.01.20 0 0 January Wednesday
... ... ... ... ...
27.12.20 37555 62 December Sunday
28.12.20 41720 246 December Monday
29.12.20 34166 425 December Tuesday
30.12.20 32386 437 December Wednesday
31.12.20 32264 574 December Thursday

342 rows × 4 columns

# 描述统计 统计数字列  包含总数,均值,标准差,最小值,25%、50%、75%数及最大值
df.describe()
cases deaths
count 342.000000 342.000000
mean 6747.862573 75.921053
std 10023.201267 76.639861
min 0.000000 -5.000000
25% 1352.250000 22.000000
50% 3462.500000 62.500000
75% 7637.250000 104.000000
max 64987.000000 574.000000
# 描述统计单列
df['cases'].describe()
count      342.000000
mean      6747.862573
std      10023.201267
min          0.000000
25%       1352.250000
50%       3462.500000
75%       7637.250000
max      64987.000000
Name: cases, dtype: float64
# value_counts() 频率统计
df['month'].value_counts()
March        31
May          31
July         31
August       31
October      31
December     31
April        30
June         30
September    30
November     30
February     29
January       7
Name: month, dtype: int64
# 按月份分组统计感染数
df.groupby('month')['cases'].sum()
month
April          41887
August        210268
December     1070577
February          25
January            3
July          270120
June          119039
March           8555
May            62644
November      301944
October       114123
September     108584
Name: cases, dtype: int64
# total cases
df['cases'].sum()
2307769
# 图形部分
import matplotlib.pyplot as plt
data = df.groupby('month')['cases'].sum()
data.plot(kind='bar')
plt.show()

# 折线图 lines
(df[df['month'] == 'July'])[['cases', 'deaths']].plot()
<AxesSubplot:xlabel='date'>

# 柱状图
(df.groupby('month')['cases'].sum()).plot(kind='bar')
<AxesSubplot:xlabel='month'>

# 柱状图 bar stacked参数 表示两个图形是否堆叠,默认不堆叠 即stacked = False
(df.groupby('month')[['cases', 'deaths']].sum()).plot(kind='bar', stacked=False)
<AxesSubplot:xlabel='month'>

(df.groupby('month')[['cases', 'deaths']].sum()).plot(kind='bar', stacked=True)
<AxesSubplot:xlabel='month'>

(df.groupby('month')[['cases', 'deaths']].sum()).plot(kind='barh')
<AxesSubplot:ylabel='month'>

# 箱线图 box
df[df['month'] == 'June']['cases'].plot(kind='box')
<AxesSubplot:>

# 无间断柱状图 histograms
df[df['month'] == 'July']['cases'].plot(kind='hist', bins=10)
<AxesSubplot:ylabel='Frequency'>

# 面积图 stacked为是否堆叠  默认为True
df[df['month'] == 'June'][['cases', 'deaths']].plot(kind='area', stacked=False)
<AxesSubplot:xlabel='date'>

df[df['month'] == 'June'][['cases', 'deaths']].plot(kind='area', stacked=True)
<AxesSubplot:xlabel='date'>

# 散点图 Scatter Plot
df[df['month'] == 'June'][['cases', 'deaths']].plot(kind='scatter', x='cases', y='deaths')
<AxesSubplot:xlabel='cases', ylabel='deaths'>

# 饼图
(df.groupby('month')['cases'].sum()).plot(kind='pie')
<AxesSubplot:ylabel='cases'>

(df.groupby('month')['deaths'].sum()).plot(kind='pie')
<AxesSubplot:ylabel='deaths'>

# 格式化绘图
df[['cases', 'deaths']].plot(kind='line', legend=True, color=['green', 'gray'])
plt.xlabel('Day')  # X轴标题
plt.ylabel('Number')  # Y轴标题
plt.suptitle('Covid-19 in 2022')
Text(0.5, 0.98, 'Covid-19 in 2022')

# Data Science
heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183,
           177, 185, 188, 188, 182, 185, 191]
heights_arr = np.array(heights)
bool_data = heights_arr > 188
count = bool_data.sum()
count
5
heights_arr.size
45
heights_arr.shape
(45,)
heights_arr.ndim
1
ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70]
heights_and_ages = heights + ages
heights_and_ages
[189,
 170,
 189,
 163,
 183,
 171,
 185,
 168,
 173,
 183,
 173,
 173,
 175,
 178,
 183,
 193,
 178,
 173,
 174,
 183,
 183,
 180,
 168,
 180,
 170,
 178,
 182,
 180,
 183,
 178,
 182,
 188,
 175,
 179,
 183,
 193,
 182,
 183,
 177,
 185,
 188,
 188,
 182,
 185,
 191,
 57,
 61,
 57,
 57,
 58,
 57,
 61,
 54,
 68,
 51,
 49,
 64,
 50,
 48,
 65,
 52,
 56,
 46,
 54,
 49,
 51,
 47,
 55,
 55,
 54,
 42,
 51,
 56,
 55,
 51,
 54,
 51,
 60,
 62,
 43,
 55,
 56,
 61,
 52,
 69,
 64,
 46,
 54,
 47,
 70]
# 转化为数组
heights_and_ages_arr = np.array(heights_and_ages)
heights_and_ages_arr
array([189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175,
       178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
       182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
       185, 188, 188, 182, 185, 191,  57,  61,  57,  57,  58,  57,  61,
        54,  68,  51,  49,  64,  50,  48,  65,  52,  56,  46,  54,  49,
        51,  47,  55,  55,  54,  42,  51,  56,  55,  51,  54,  51,  60,
        62,  43,  55,  56,  61,  52,  69,  64,  46,  54,  47,  70])
a = heights_and_ages_arr.reshape((2, 45))
a
array([[189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175,
        178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
        182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
        185, 188, 188, 182, 185, 191],
       [ 57,  61,  57,  57,  58,  57,  61,  54,  68,  51,  49,  64,  50,
         48,  65,  52,  56,  46,  54,  49,  51,  47,  55,  55,  54,  42,
         51,  56,  55,  51,  54,  51,  60,  62,  43,  55,  56,  61,  52,
         69,  64,  46,  54,  47,  70]])
a.dtype
dtype('int32')
heights_float = [189.0, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193,
                 182, 183, 177, 185, 188, 188, 182, 185, 191]
heights_float = np.array(heights_float)
heights_float.dtype
dtype('float64')
heights_float[3]
163.0
a[1, 1]
61
a[0, 0:3]
array([189, 170, 189])
a[:, 0]
array([189,  57])
a[0, 3] = 165
a
array([[189, 170, 189, 165, 183, 171, 185, 168, 173, 183, 173, 173, 175,
        178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
        182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
        185, 188, 188, 182, 185, 191],
       [ 57,  61,  57,  57,  58,  57,  61,  54,  68,  51,  49,  64,  50,
         48,  65,  52,  56,  46,  54,  49,  51,  47,  55,  55,  54,  42,
         51,  56,  55,  51,  54,  51,  60,  62,  43,  55,  56,  61,  52,
         69,  64,  46,  54,  47,  70]])
a[0, 3] = 210
a
array([[189, 170, 189, 210, 183, 171, 185, 168, 173, 183, 173, 173, 175,
        178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
        182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
        185, 188, 188, 182, 185, 191],
       [ 57,  61,  57,  57,  58,  57,  61,  54,  68,  51,  49,  64,  50,
         48,  65,  52,  56,  46,  54,  49,  51,  47,  55,  55,  54,  42,
         51,  56,  55,  51,  54,  51,  60,  62,  43,  55,  56,  61,  52,
         69,  64,  46,  54,  47,  70]])
# 多数据更新
new_data = [[180, 183, 190], [53, 48, 69]]
a[:, 42:] = new_data
a
array([[189, 170, 189, 210, 183, 171, 185, 168, 173, 183, 173, 173, 175,
        178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
        182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
        185, 188, 188, 180, 183, 190],
       [ 57,  61,  57,  57,  58,  57,  61,  54,  68,  51,  49,  64,  50,
         48,  65,  52,  56,  46,  54,  49,  51,  47,  55,  55,  54,  42,
         51,  56,  55,  51,  54,  51,  60,  62,  43,  55,  56,  61,  52,
         69,  64,  46,  53,  48,  69]])
# 水平组合 np.hstack 连接相同的行数
heights = [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183,
           177, 185, 188, 188, 182, 185, 191]
ages = [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70]
heights_arr = np.array(heights).reshape((45, 1))
ages_arr = np.array(ages).reshape((45, 1))
heights_ages_array = np.hstack((heights_arr, ages_arr))
heights_ages_array
array([[189,  57],
       [170,  61],
       [189,  57],
       [163,  57],
       [183,  58],
       [171,  57],
       [185,  61],
       [168,  54],
       [173,  68],
       [183,  51],
       [173,  49],
       [173,  64],
       [175,  50],
       [178,  48],
       [183,  65],
       [193,  52],
       [178,  56],
       [173,  46],
       [174,  54],
       [183,  49],
       [183,  51],
       [180,  47],
       [168,  55],
       [180,  55],
       [170,  54],
       [178,  42],
       [182,  51],
       [180,  56],
       [183,  55],
       [178,  51],
       [182,  54],
       [188,  51],
       [175,  60],
       [179,  62],
       [183,  43],
       [193,  55],
       [182,  56],
       [183,  61],
       [177,  52],
       [185,  69],
       [188,  64],
       [188,  46],
       [182,  54],
       [185,  47],
       [191,  70]])
# 水平组合 np.concatenate
heights_ages_array1 = np.concatenate((heights_arr, ages_arr), axis=1)
heights_ages_array1
array([[189,  57],
       [170,  61],
       [189,  57],
       [163,  57],
       [183,  58],
       [171,  57],
       [185,  61],
       [168,  54],
       [173,  68],
       [183,  51],
       [173,  49],
       [173,  64],
       [175,  50],
       [178,  48],
       [183,  65],
       [193,  52],
       [178,  56],
       [173,  46],
       [174,  54],
       [183,  49],
       [183,  51],
       [180,  47],
       [168,  55],
       [180,  55],
       [170,  54],
       [178,  42],
       [182,  51],
       [180,  56],
       [183,  55],
       [178,  51],
       [182,  54],
       [188,  51],
       [175,  60],
       [179,  62],
       [183,  43],
       [193,  55],
       [182,  56],
       [183,  61],
       [177,  52],
       [185,  69],
       [188,  64],
       [188,  46],
       [182,  54],
       [185,  47],
       [191,  70]])
# 垂直组合  np.vstack 连接相同的列数
heights_arr = heights_arr.reshape((1, 45))
ages_arr = ages_arr.reshape((1, 45))
heights_ages_arr = np.vstack((heights_arr, ages_arr))
heights_ages_arr
array([[189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175,
        178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
        182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
        185, 188, 188, 182, 185, 191],
       [ 57,  61,  57,  57,  58,  57,  61,  54,  68,  51,  49,  64,  50,
         48,  65,  52,  56,  46,  54,  49,  51,  47,  55,  55,  54,  42,
         51,  56,  55,  51,  54,  51,  60,  62,  43,  55,  56,  61,  52,
         69,  64,  46,  54,  47,  70]])
# 垂直组合 np.concatenate
heights_ages_arr1 = np.concatenate((heights_arr, ages_arr), axis=0)
heights_ages_arr1
array([[189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175,
        178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178,
        182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
        185, 188, 188, 182, 185, 191],
       [ 57,  61,  57,  57,  58,  57,  61,  54,  68,  51,  49,  64,  50,
         48,  65,  52,  56,  46,  54,  49,  51,  47,  55,  55,  54,  42,
         51,  56,  55,  51,  54,  51,  60,  62,  43,  55,  56,  61,  52,
         69,  64,  46,  54,  47,  70]])
# such as .min(), .max(), .mean(), work in a similar way to .sum()
heights_ages_arr1.sum()
10575
heights_ages_arr1.sum(axis=1)
array([8100, 2475])
heights_ages_arr1.sum(axis=0)
array([246, 231, 246, 220, 241, 228, 246, 222, 241, 234, 222, 237, 225,
       226, 248, 245, 234, 219, 228, 232, 234, 227, 223, 235, 224, 220,
       233, 236, 238, 229, 236, 239, 235, 241, 226, 248, 238, 244, 229,
       254, 252, 234, 236, 232, 261])
# "<", ">", ">=", "<=", and "==
heights_arr = np.array(
    [189, 170, 189, 163, 183, 171, 185, 168, 173, 183, 173, 173, 175, 178, 183, 193, 178, 173, 174, 183, 183, 180, 168, 180, 170, 178, 182, 180, 183, 178, 182, 188, 175, 179, 183, 193, 182, 183, 177,
     185, 188, 188, 182, 185, 191])
ages_arr = np.array(
    [57, 61, 57, 57, 58, 57, 61, 54, 68, 51, 49, 64, 50, 48, 65, 52, 56, 46, 54, 49, 51, 47, 55, 55, 54, 42, 51, 56, 55, 51, 54, 51, 60, 62, 43, 55, 56, 61, 52, 69, 64, 46, 54, 47, 70]).reshape(
    (45, 1))

heights_arr = heights_arr.reshape((45, 1))
height_age_arr = np.hstack((heights_arr, ages_arr))
height_age_arr[:, 1] < 55
array([False, False, False, False, False, False, False,  True, False,
        True,  True, False,  True,  True, False,  True, False,  True,
        True,  True,  True,  True, False, False,  True,  True,  True,
       False, False,  True,  True,  True, False, False,  True, False,
       False, False,  True, False, False,  True,  True,  True, False])
height_age_arr[:, 1] == 51
array([False, False, False, False, False, False, False, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False,  True, False, False, False, False, False,  True,
       False, False,  True, False,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False])
(height_age_arr[:, 1] == 51).sum()
5
# Pandas
import pandas as pd

a = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
a
a    1
b    2
c    3
dtype: int64
import numpy as np

a = pd.Series(np.array([1, 2, 3]), index=['a', 'b', 'c'])
a
a    1
b    2
c    3
dtype: int32
a = pd.Series({'a': 1, 'b': 2, 'c': 3, 'd': 4})
a
a    1
b    2
c    3
d    4
dtype: int64
a['b']
2
# pandas dataframe 需要数据均匹配
wine_dict = {
    'red_wine': [3, 6, 5, 10],
    'white_wine': [5, 0, 10, 9]
}
index = ['adam', 'bob', 'charles', 'runker']

a = pd.DataFrame(wine_dict, index=index)
a
red_wine white_wine
adam 3 5
bob 6 0
charles 5 10
runker 10 9
# 读取数据
import pandas as pd
import numpy as np

presidents_df = pd.read_csv('https://sololearn.com/uploads/files/president_heights_party.csv', index_col='name')
presidents_df.shape
(45, 4)
presidents_df.shape[0]
45
presidents_df.size
180
presidents_df.head()
order age height party
name
George Washington 1 57 189 none
John Adams 2 61 170 federalist
Thomas Jefferson 3 57 189 democratic-republican
James Madison 4 57 163 democratic-republican
James Monroe 5 58 183 democratic-republican
presidents_df.head(3)
order age height party
name
George Washington 1 57 189 none
John Adams 2 61 170 federalist
Thomas Jefferson 3 57 189 democratic-republican
presidents_df.head(-1)
order age height party
name
George Washington 1 57 189 none
John Adams 2 61 170 federalist
Thomas Jefferson 3 57 189 democratic-republican
James Madison 4 57 163 democratic-republican
James Monroe 5 58 183 democratic-republican
John Quincy Adams 6 57 171 democratic-republican
Andrew Jackson 7 61 185 democratic
Martin Van Buren 8 54 168 democratic
William Henry Harrison 9 68 173 whig
John Tyler 10 51 183 whig
James K. Polk 11 49 173 democratic
Zachary Taylor 12 64 173 whig
Millard Fillmore 13 50 175 whig
Franklin Pierce 14 48 178 democratic
James Buchanan 15 65 183 democratic
Abraham Lincoln 16 52 193 republican
Andrew Johnson 17 56 178 national union
Ulysses S. Grant 18 46 173 republican
Rutherford B. Hayes 19 54 174 republican
James A. Garfield 20 49 183 republican
Chester A. Arthur 21 51 183 republican
Grover Cleveland 22 47 180 democratic
Benjamin Harrison 23 55 168 republican
Grover Cleveland 25 55 180 democratic
William McKinley 25 54 170 republican
Theodore Roosevelt 26 42 178 republican
William Howard Taft 27 51 182 republican
Woodrow Wilson 28 56 180 democratic
Warren G. Harding 29 55 183 republican
Calvin Coolidge 30 51 178 republican
Herbert Hoover 31 54 182 republican
Franklin D. Roosevelt 32 51 188 democratic
Harry S. Truman 33 60 175 democratic
Dwight D. Eisenhower 34 62 179 republican
John F. Kennedy 35 43 183 democratic
Lyndon B. Johnson 36 55 193 democratic
Richard Nixon 37 56 182 republican
Gerald Ford 38 61 183 republican
Jimmy Carter 39 52 177 democratic
Ronald Reagan 40 69 185 republican
George H. W. Bush 41 64 188 republican
Bill Clinton 42 46 188 democratic
George W. Bush 43 54 182 republican
Barack Obama 44 47 185 democratic
presidents_df.tail()
order age height party
name
George H. W. Bush 41 64 188 republican
Bill Clinton 42 46 188 democratic
George W. Bush 43 54 182 republican
Barack Obama 44 47 185 democratic
Donald J. Trump 45 70 191 republican
presidents_df.tail(3)
order age height party
name
George W. Bush 43 54 182 republican
Barack Obama 44 47 185 democratic
Donald J. Trump 45 70 191 republican
presidents_df.tail(-1)
order age height party
name
John Adams 2 61 170 federalist
Thomas Jefferson 3 57 189 democratic-republican
James Madison 4 57 163 democratic-republican
James Monroe 5 58 183 democratic-republican
John Quincy Adams 6 57 171 democratic-republican
Andrew Jackson 7 61 185 democratic
Martin Van Buren 8 54 168 democratic
William Henry Harrison 9 68 173 whig
John Tyler 10 51 183 whig
James K. Polk 11 49 173 democratic
Zachary Taylor 12 64 173 whig
Millard Fillmore 13 50 175 whig
Franklin Pierce 14 48 178 democratic
James Buchanan 15 65 183 democratic
Abraham Lincoln 16 52 193 republican
Andrew Johnson 17 56 178 national union
Ulysses S. Grant 18 46 173 republican
Rutherford B. Hayes 19 54 174 republican
James A. Garfield 20 49 183 republican
Chester A. Arthur 21 51 183 republican
Grover Cleveland 22 47 180 democratic
Benjamin Harrison 23 55 168 republican
Grover Cleveland 25 55 180 democratic
William McKinley 25 54 170 republican
Theodore Roosevelt 26 42 178 republican
William Howard Taft 27 51 182 republican
Woodrow Wilson 28 56 180 democratic
Warren G. Harding 29 55 183 republican
Calvin Coolidge 30 51 178 republican
Herbert Hoover 31 54 182 republican
Franklin D. Roosevelt 32 51 188 democratic
Harry S. Truman 33 60 175 democratic
Dwight D. Eisenhower 34 62 179 republican
John F. Kennedy 35 43 183 democratic
Lyndon B. Johnson 36 55 193 democratic
Richard Nixon 37 56 182 republican
Gerald Ford 38 61 183 republican
Jimmy Carter 39 52 177 democratic
Ronald Reagan 40 69 185 republican
George H. W. Bush 41 64 188 republican
Bill Clinton 42 46 188 democratic
George W. Bush 43 54 182 republican
Barack Obama 44 47 185 democratic
Donald J. Trump 45 70 191 republican
presidents_df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 45 entries, George Washington to Donald J. Trump
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   order   45 non-null     int64 
 1   age     45 non-null     int64 
 2   height  45 non-null     int64 
 3   party   45 non-null     object
dtypes: int64(3), object(1)
memory usage: 1.8+ KB
presidents_df.describe()
order age height
count 45.000000 45.000000 45.000000
mean 23.022222 55.000000 180.000000
std 13.136502 6.595453 6.977236
min 1.000000 42.000000 163.000000
25% 12.000000 51.000000 175.000000
50% 23.000000 55.000000 182.000000
75% 34.000000 58.000000 183.000000
max 45.000000 70.000000 193.000000
# 索引
presidents_df.loc['Abraham Lincoln']
order             16
age               52
height           193
party     republican
Name: Abraham Lincoln, dtype: object
type(presidents_df.loc['Abraham Lincoln'])
pandas.core.series.Series
(presidents_df.loc['Abraham Lincoln']).shape
(4,)
# 索引切片 datafrme.loc[x,y]  x:行,y:列 遵循python列表规则
presidents_df.loc['Abraham Lincoln':'Ulysses S. Grant']
order age height party
name
Abraham Lincoln 16 52 193 republican
Andrew Johnson 17 56 178 national union
Ulysses S. Grant 18 46 173 republican
presidents_df.loc[:, 'age':'party'].head(3)
age height party
name
George Washington 57 189 none
John Adams 61 170 federalist
Thomas Jefferson 57 189 democratic-republican
presidents_df.iloc[15]
order             16
age               52
height           193
party     republican
Name: Abraham Lincoln, dtype: object
presidents_df.iloc[15:18]
order age height party
name
Abraham Lincoln 16 52 193 republican
Andrew Johnson 17 56 178 national union
Ulysses S. Grant 18 46 173 republican
#dataframe.iloc[x:y:z] x:起始位置,y:结束位置,z:步长
presidents_df.iloc[15:20:2]
order age height party
name
Abraham Lincoln 16 52 193 republican
Ulysses S. Grant 18 46 173 republican
James A. Garfield 20 49 183 republican
# 按列取值
presidents_df.columns
Index(['order', 'age', 'height', 'party'], dtype='object')
presidents_df['age']
name
George Washington         57
John Adams                61
Thomas Jefferson          57
James Madison             57
James Monroe              58
John Quincy Adams         57
Andrew Jackson            61
Martin Van Buren          54
William Henry Harrison    68
John Tyler                51
James K. Polk             49
Zachary Taylor            64
Millard Fillmore          50
Franklin Pierce           48
James Buchanan            65
Abraham Lincoln           52
Andrew Johnson            56
Ulysses S. Grant          46
Rutherford B. Hayes       54
James A. Garfield         49
Chester A. Arthur         51
Grover Cleveland          47
Benjamin Harrison         55
Grover Cleveland          55
William McKinley          54
Theodore Roosevelt        42
William Howard Taft       51
Woodrow Wilson            56
Warren G. Harding         55
Calvin Coolidge           51
Herbert Hoover            54
Franklin D. Roosevelt     51
Harry S. Truman           60
Dwight D. Eisenhower      62
John F. Kennedy           43
Lyndon B. Johnson         55
Richard Nixon             56
Gerald Ford               61
Jimmy Carter              52
Ronald Reagan             69
George H. W. Bush         64
Bill Clinton              46
George W. Bush            54
Barack Obama              47
Donald J. Trump           70
Name: age, dtype: int64
(presidents_df['age']).shape
(45,)
presidents_df[['order', 'party']].head(3)
order party
name
George Washington 1 none
John Adams 2 federalist
Thomas Jefferson 3 democratic-republican
presidents_df[['order', 'party']].describe()
order
count 45.000000
mean 23.022222
std 13.136502
min 1.000000
25% 12.000000
50% 23.000000
75% 34.000000
max 45.000000
# min max mean std variance
presidents_df.min()
order              1
age               42
height           163
party     democratic
dtype: object
presidents_df.max()
order       45
age         70
height     193
party     whig
dtype: object
presidents_df[presidents_df['party'] == 'whig']
order age height party
name
William Henry Harrison 9 68 173 whig
John Tyler 10 51 183 whig
Zachary Taylor 12 64 173 whig
Millard Fillmore 13 50 175 whig
presidents_df.mean()
C:\Users\Runker\AppData\Local\Temp\ipykernel_17476\337715475.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
  presidents_df.mean()





order      23.022222
age        55.000000
height    180.000000
dtype: float64
presidents_df.std()
C:\Users\Runker\AppData\Local\Temp\ipykernel_17476\1534286438.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
  presidents_df.std()





order     13.136502
age        6.595453
height     6.977236
dtype: float64
presidents_df.var()
C:\Users\Runker\AppData\Local\Temp\ipykernel_17476\2544867294.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
  presidents_df.var()





order     172.567677
age        43.500000
height     48.681818
dtype: float64
presidents_df['age'].var()
43.5
presidents_df['age'].std()
6.59545297913646
presidents_df['age'].mean()
55.0
presidents_df['age'].max()
70
presidents_df['age'].min()
42
presidents_df['age'].median()
55.0
# 分位数 参数默认0.5
presidents_df['age'].quantile()
55.0
presidents_df['age'].quantile([0.25, 0.5, 0.75, 1])
0.25    51.0
0.50    55.0
0.75    58.0
1.00    70.0
Name: age, dtype: float64
(pd.Series([1, 2, 4, 2, 4, 1])).median()
2.0
# describe 计算所有的非空值
a = pd.Series([1, None, 2, 3, 4])
a.describe()
count    4.000000
mean     2.500000
std      1.290994
min      1.000000
25%      1.750000
50%      2.500000
75%      3.250000
max      4.000000
dtype: float64
presidents_df['age'].describe()
count    45.000000
mean     55.000000
std       6.595453
min      42.000000
25%      51.000000
50%      55.000000
75%      58.000000
max      70.000000
Name: age, dtype: float64
# Categorical Variable
presidents_df['party'].value_counts()
republican               19
democratic               15
democratic-republican     4
whig                      4
none                      1
federalist                1
national union            1
Name: party, dtype: int64
presidents_df['party'].describe()
count             45
unique             7
top       republican
freq              19
Name: party, dtype: object
# Groupby
presidents_df.groupby('party').mean()
order age height
party
democratic 26.066667 52.600000 181.066667
democratic-republican 4.500000 57.250000 176.500000
federalist 2.000000 61.000000 170.000000
national union 17.000000 56.000000 178.000000
none 1.000000 57.000000 189.000000
republican 29.631579 55.263158 180.894737
whig 11.000000 58.250000 176.000000
presidents_df.groupby('party').describe()
order age height
count mean std min 25% 50% 75% max count mean ... 75% max count mean std min 25% 50% 75% max
party
democratic 15.0 26.066667 12.566774 7.0 14.50 28.0 35.50 44.0 15.0 52.600000 ... 55.50 65.0 15.0 181.066667 6.430582 168.0 177.5 180.0 185.0 193.0
democratic-republican 4.0 4.500000 1.290994 3.0 3.75 4.5 5.25 6.0 4.0 57.250000 ... 57.25 58.0 4.0 176.500000 11.704700 163.0 169.0 177.0 184.5 189.0
federalist 1.0 2.000000 NaN 2.0 2.00 2.0 2.00 2.0 1.0 61.000000 ... 61.00 61.0 1.0 170.000000 NaN 170.0 170.0 170.0 170.0 170.0
national union 1.0 17.000000 NaN 17.0 17.00 17.0 17.00 17.0 1.0 56.000000 ... 56.00 56.0 1.0 178.000000 NaN 178.0 178.0 178.0 178.0 178.0
none 1.0 1.000000 NaN 1.0 1.00 1.0 1.00 1.0 1.0 57.000000 ... 57.00 57.0 1.0 189.000000 NaN 189.0 189.0 189.0 189.0 189.0
republican 19.0 29.631579 9.081180 16.0 22.00 29.0 37.50 45.0 19.0 55.263158 ... 58.50 70.0 19.0 180.894737 6.471259 168.0 178.0 182.0 183.0 193.0
whig 4.0 11.000000 1.825742 9.0 9.75 11.0 12.25 13.0 4.0 58.250000 ... 65.00 68.0 4.0 176.000000 4.760952 173.0 173.0 174.0 177.0 183.0

7 rows × 24 columns

#  agg
presidents_df.groupby('party')['height'].agg(['min', np.median, max])
min median max
party
democratic 168 180.0 193
democratic-republican 163 177.0 189
federalist 170 170.0 170
national union 178 178.0 178
none 189 189.0 189
republican 168 182.0 193
whig 173 174.0 183
presidents_df.groupby('party').agg({'height': [np.median, np.min, np.mean, np.max], 'age': [np.median, np.min, np.mean, np.max]})
height age
median amin mean amax median amin mean amax
party
democratic 180.0 168 181.066667 193 52.0 43 52.600000 65
democratic-republican 177.0 163 176.500000 189 57.0 57 57.250000 58
federalist 170.0 170 170.000000 170 61.0 61 61.000000 61
national union 178.0 178 178.000000 178 56.0 56 56.000000 56
none 189.0 189 189.000000 189 57.0 57 57.000000 57
republican 182.0 168 180.894737 193 54.0 42 55.263158 70
whig 174.0 173 176.000000 183 57.5 50 58.250000 68
# matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes()
plt.show()

x = np.linspace(0, 10, 1000)
x
array([ 0.        ,  0.01001001,  0.02002002,  0.03003003,  0.04004004,
        0.05005005,  0.06006006,  0.07007007,  0.08008008,  0.09009009,
        0.1001001 ,  0.11011011,  0.12012012,  0.13013013,  0.14014014,
        0.15015015,  0.16016016,  0.17017017,  0.18018018,  0.19019019,
        0.2002002 ,  0.21021021,  0.22022022,  0.23023023,  0.24024024,
        0.25025025,  0.26026026,  0.27027027,  0.28028028,  0.29029029,
        0.3003003 ,  0.31031031,  0.32032032,  0.33033033,  0.34034034,
        0.35035035,  0.36036036,  0.37037037,  0.38038038,  0.39039039,
        0.4004004 ,  0.41041041,  0.42042042,  0.43043043,  0.44044044,
        0.45045045,  0.46046046,  0.47047047,  0.48048048,  0.49049049,
        0.5005005 ,  0.51051051,  0.52052052,  0.53053053,  0.54054054,
        0.55055055,  0.56056056,  0.57057057,  0.58058058,  0.59059059,
        0.6006006 ,  0.61061061,  0.62062062,  0.63063063,  0.64064064,
        0.65065065,  0.66066066,  0.67067067,  0.68068068,  0.69069069,
        0.7007007 ,  0.71071071,  0.72072072,  0.73073073,  0.74074074,
        0.75075075,  0.76076076,  0.77077077,  0.78078078,  0.79079079,
        0.8008008 ,  0.81081081,  0.82082082,  0.83083083,  0.84084084,
        0.85085085,  0.86086086,  0.87087087,  0.88088088,  0.89089089,
        0.9009009 ,  0.91091091,  0.92092092,  0.93093093,  0.94094094,
        0.95095095,  0.96096096,  0.97097097,  0.98098098,  0.99099099,
        1.001001  ,  1.01101101,  1.02102102,  1.03103103,  1.04104104,
        1.05105105,  1.06106106,  1.07107107,  1.08108108,  1.09109109,
        1.1011011 ,  1.11111111,  1.12112112,  1.13113113,  1.14114114,
        1.15115115,  1.16116116,  1.17117117,  1.18118118,  1.19119119,
        1.2012012 ,  1.21121121,  1.22122122,  1.23123123,  1.24124124,
        1.25125125,  1.26126126,  1.27127127,  1.28128128,  1.29129129,
        1.3013013 ,  1.31131131,  1.32132132,  1.33133133,  1.34134134,
        1.35135135,  1.36136136,  1.37137137,  1.38138138,  1.39139139,
        1.4014014 ,  1.41141141,  1.42142142,  1.43143143,  1.44144144,
        1.45145145,  1.46146146,  1.47147147,  1.48148148,  1.49149149,
        1.5015015 ,  1.51151151,  1.52152152,  1.53153153,  1.54154154,
        1.55155155,  1.56156156,  1.57157157,  1.58158158,  1.59159159,
        1.6016016 ,  1.61161161,  1.62162162,  1.63163163,  1.64164164,
        1.65165165,  1.66166166,  1.67167167,  1.68168168,  1.69169169,
        1.7017017 ,  1.71171171,  1.72172172,  1.73173173,  1.74174174,
        1.75175175,  1.76176176,  1.77177177,  1.78178178,  1.79179179,
        1.8018018 ,  1.81181181,  1.82182182,  1.83183183,  1.84184184,
        1.85185185,  1.86186186,  1.87187187,  1.88188188,  1.89189189,
        1.9019019 ,  1.91191191,  1.92192192,  1.93193193,  1.94194194,
        1.95195195,  1.96196196,  1.97197197,  1.98198198,  1.99199199,
        2.002002  ,  2.01201201,  2.02202202,  2.03203203,  2.04204204,
        2.05205205,  2.06206206,  2.07207207,  2.08208208,  2.09209209,
        2.1021021 ,  2.11211211,  2.12212212,  2.13213213,  2.14214214,
        2.15215215,  2.16216216,  2.17217217,  2.18218218,  2.19219219,
        2.2022022 ,  2.21221221,  2.22222222,  2.23223223,  2.24224224,
        2.25225225,  2.26226226,  2.27227227,  2.28228228,  2.29229229,
        2.3023023 ,  2.31231231,  2.32232232,  2.33233233,  2.34234234,
        2.35235235,  2.36236236,  2.37237237,  2.38238238,  2.39239239,
        2.4024024 ,  2.41241241,  2.42242242,  2.43243243,  2.44244244,
        2.45245245,  2.46246246,  2.47247247,  2.48248248,  2.49249249,
        2.5025025 ,  2.51251251,  2.52252252,  2.53253253,  2.54254254,
        2.55255255,  2.56256256,  2.57257257,  2.58258258,  2.59259259,
        2.6026026 ,  2.61261261,  2.62262262,  2.63263263,  2.64264264,
        2.65265265,  2.66266266,  2.67267267,  2.68268268,  2.69269269,
        2.7027027 ,  2.71271271,  2.72272272,  2.73273273,  2.74274274,
        2.75275275,  2.76276276,  2.77277277,  2.78278278,  2.79279279,
        2.8028028 ,  2.81281281,  2.82282282,  2.83283283,  2.84284284,
        2.85285285,  2.86286286,  2.87287287,  2.88288288,  2.89289289,
        2.9029029 ,  2.91291291,  2.92292292,  2.93293293,  2.94294294,
        2.95295295,  2.96296296,  2.97297297,  2.98298298,  2.99299299,
        3.003003  ,  3.01301301,  3.02302302,  3.03303303,  3.04304304,
        3.05305305,  3.06306306,  3.07307307,  3.08308308,  3.09309309,
        3.1031031 ,  3.11311311,  3.12312312,  3.13313313,  3.14314314,
        3.15315315,  3.16316316,  3.17317317,  3.18318318,  3.19319319,
        3.2032032 ,  3.21321321,  3.22322322,  3.23323323,  3.24324324,
        3.25325325,  3.26326326,  3.27327327,  3.28328328,  3.29329329,
        3.3033033 ,  3.31331331,  3.32332332,  3.33333333,  3.34334334,
        3.35335335,  3.36336336,  3.37337337,  3.38338338,  3.39339339,
        3.4034034 ,  3.41341341,  3.42342342,  3.43343343,  3.44344344,
        3.45345345,  3.46346346,  3.47347347,  3.48348348,  3.49349349,
        3.5035035 ,  3.51351351,  3.52352352,  3.53353353,  3.54354354,
        3.55355355,  3.56356356,  3.57357357,  3.58358358,  3.59359359,
        3.6036036 ,  3.61361361,  3.62362362,  3.63363363,  3.64364364,
        3.65365365,  3.66366366,  3.67367367,  3.68368368,  3.69369369,
        3.7037037 ,  3.71371371,  3.72372372,  3.73373373,  3.74374374,
        3.75375375,  3.76376376,  3.77377377,  3.78378378,  3.79379379,
        3.8038038 ,  3.81381381,  3.82382382,  3.83383383,  3.84384384,
        3.85385385,  3.86386386,  3.87387387,  3.88388388,  3.89389389,
        3.9039039 ,  3.91391391,  3.92392392,  3.93393393,  3.94394394,
        3.95395395,  3.96396396,  3.97397397,  3.98398398,  3.99399399,
        4.004004  ,  4.01401401,  4.02402402,  4.03403403,  4.04404404,
        4.05405405,  4.06406406,  4.07407407,  4.08408408,  4.09409409,
        4.1041041 ,  4.11411411,  4.12412412,  4.13413413,  4.14414414,
        4.15415415,  4.16416416,  4.17417417,  4.18418418,  4.19419419,
        4.2042042 ,  4.21421421,  4.22422422,  4.23423423,  4.24424424,
        4.25425425,  4.26426426,  4.27427427,  4.28428428,  4.29429429,
        4.3043043 ,  4.31431431,  4.32432432,  4.33433433,  4.34434434,
        4.35435435,  4.36436436,  4.37437437,  4.38438438,  4.39439439,
        4.4044044 ,  4.41441441,  4.42442442,  4.43443443,  4.44444444,
        4.45445445,  4.46446446,  4.47447447,  4.48448448,  4.49449449,
        4.5045045 ,  4.51451451,  4.52452452,  4.53453453,  4.54454454,
        4.55455455,  4.56456456,  4.57457457,  4.58458458,  4.59459459,
        4.6046046 ,  4.61461461,  4.62462462,  4.63463463,  4.64464464,
        4.65465465,  4.66466466,  4.67467467,  4.68468468,  4.69469469,
        4.7047047 ,  4.71471471,  4.72472472,  4.73473473,  4.74474474,
        4.75475475,  4.76476476,  4.77477477,  4.78478478,  4.79479479,
        4.8048048 ,  4.81481481,  4.82482482,  4.83483483,  4.84484484,
        4.85485485,  4.86486486,  4.87487487,  4.88488488,  4.89489489,
        4.9049049 ,  4.91491491,  4.92492492,  4.93493493,  4.94494494,
        4.95495495,  4.96496496,  4.97497497,  4.98498498,  4.99499499,
        5.00500501,  5.01501502,  5.02502503,  5.03503504,  5.04504505,
        5.05505506,  5.06506507,  5.07507508,  5.08508509,  5.0950951 ,
        5.10510511,  5.11511512,  5.12512513,  5.13513514,  5.14514515,
        5.15515516,  5.16516517,  5.17517518,  5.18518519,  5.1951952 ,
        5.20520521,  5.21521522,  5.22522523,  5.23523524,  5.24524525,
        5.25525526,  5.26526527,  5.27527528,  5.28528529,  5.2952953 ,
        5.30530531,  5.31531532,  5.32532533,  5.33533534,  5.34534535,
        5.35535536,  5.36536537,  5.37537538,  5.38538539,  5.3953954 ,
        5.40540541,  5.41541542,  5.42542543,  5.43543544,  5.44544545,
        5.45545546,  5.46546547,  5.47547548,  5.48548549,  5.4954955 ,
        5.50550551,  5.51551552,  5.52552553,  5.53553554,  5.54554555,
        5.55555556,  5.56556557,  5.57557558,  5.58558559,  5.5955956 ,
        5.60560561,  5.61561562,  5.62562563,  5.63563564,  5.64564565,
        5.65565566,  5.66566567,  5.67567568,  5.68568569,  5.6956957 ,
        5.70570571,  5.71571572,  5.72572573,  5.73573574,  5.74574575,
        5.75575576,  5.76576577,  5.77577578,  5.78578579,  5.7957958 ,
        5.80580581,  5.81581582,  5.82582583,  5.83583584,  5.84584585,
        5.85585586,  5.86586587,  5.87587588,  5.88588589,  5.8958959 ,
        5.90590591,  5.91591592,  5.92592593,  5.93593594,  5.94594595,
        5.95595596,  5.96596597,  5.97597598,  5.98598599,  5.995996  ,
        6.00600601,  6.01601602,  6.02602603,  6.03603604,  6.04604605,
        6.05605606,  6.06606607,  6.07607608,  6.08608609,  6.0960961 ,
        6.10610611,  6.11611612,  6.12612613,  6.13613614,  6.14614615,
        6.15615616,  6.16616617,  6.17617618,  6.18618619,  6.1961962 ,
        6.20620621,  6.21621622,  6.22622623,  6.23623624,  6.24624625,
        6.25625626,  6.26626627,  6.27627628,  6.28628629,  6.2962963 ,
        6.30630631,  6.31631632,  6.32632633,  6.33633634,  6.34634635,
        6.35635636,  6.36636637,  6.37637638,  6.38638639,  6.3963964 ,
        6.40640641,  6.41641642,  6.42642643,  6.43643644,  6.44644645,
        6.45645646,  6.46646647,  6.47647648,  6.48648649,  6.4964965 ,
        6.50650651,  6.51651652,  6.52652653,  6.53653654,  6.54654655,
        6.55655656,  6.56656657,  6.57657658,  6.58658659,  6.5965966 ,
        6.60660661,  6.61661662,  6.62662663,  6.63663664,  6.64664665,
        6.65665666,  6.66666667,  6.67667668,  6.68668669,  6.6966967 ,
        6.70670671,  6.71671672,  6.72672673,  6.73673674,  6.74674675,
        6.75675676,  6.76676677,  6.77677678,  6.78678679,  6.7967968 ,
        6.80680681,  6.81681682,  6.82682683,  6.83683684,  6.84684685,
        6.85685686,  6.86686687,  6.87687688,  6.88688689,  6.8968969 ,
        6.90690691,  6.91691692,  6.92692693,  6.93693694,  6.94694695,
        6.95695696,  6.96696697,  6.97697698,  6.98698699,  6.996997  ,
        7.00700701,  7.01701702,  7.02702703,  7.03703704,  7.04704705,
        7.05705706,  7.06706707,  7.07707708,  7.08708709,  7.0970971 ,
        7.10710711,  7.11711712,  7.12712713,  7.13713714,  7.14714715,
        7.15715716,  7.16716717,  7.17717718,  7.18718719,  7.1971972 ,
        7.20720721,  7.21721722,  7.22722723,  7.23723724,  7.24724725,
        7.25725726,  7.26726727,  7.27727728,  7.28728729,  7.2972973 ,
        7.30730731,  7.31731732,  7.32732733,  7.33733734,  7.34734735,
        7.35735736,  7.36736737,  7.37737738,  7.38738739,  7.3973974 ,
        7.40740741,  7.41741742,  7.42742743,  7.43743744,  7.44744745,
        7.45745746,  7.46746747,  7.47747748,  7.48748749,  7.4974975 ,
        7.50750751,  7.51751752,  7.52752753,  7.53753754,  7.54754755,
        7.55755756,  7.56756757,  7.57757758,  7.58758759,  7.5975976 ,
        7.60760761,  7.61761762,  7.62762763,  7.63763764,  7.64764765,
        7.65765766,  7.66766767,  7.67767768,  7.68768769,  7.6976977 ,
        7.70770771,  7.71771772,  7.72772773,  7.73773774,  7.74774775,
        7.75775776,  7.76776777,  7.77777778,  7.78778779,  7.7977978 ,
        7.80780781,  7.81781782,  7.82782783,  7.83783784,  7.84784785,
        7.85785786,  7.86786787,  7.87787788,  7.88788789,  7.8978979 ,
        7.90790791,  7.91791792,  7.92792793,  7.93793794,  7.94794795,
        7.95795796,  7.96796797,  7.97797798,  7.98798799,  7.997998  ,
        8.00800801,  8.01801802,  8.02802803,  8.03803804,  8.04804805,
        8.05805806,  8.06806807,  8.07807808,  8.08808809,  8.0980981 ,
        8.10810811,  8.11811812,  8.12812813,  8.13813814,  8.14814815,
        8.15815816,  8.16816817,  8.17817818,  8.18818819,  8.1981982 ,
        8.20820821,  8.21821822,  8.22822823,  8.23823824,  8.24824825,
        8.25825826,  8.26826827,  8.27827828,  8.28828829,  8.2982983 ,
        8.30830831,  8.31831832,  8.32832833,  8.33833834,  8.34834835,
        8.35835836,  8.36836837,  8.37837838,  8.38838839,  8.3983984 ,
        8.40840841,  8.41841842,  8.42842843,  8.43843844,  8.44844845,
        8.45845846,  8.46846847,  8.47847848,  8.48848849,  8.4984985 ,
        8.50850851,  8.51851852,  8.52852853,  8.53853854,  8.54854855,
        8.55855856,  8.56856857,  8.57857858,  8.58858859,  8.5985986 ,
        8.60860861,  8.61861862,  8.62862863,  8.63863864,  8.64864865,
        8.65865866,  8.66866867,  8.67867868,  8.68868869,  8.6986987 ,
        8.70870871,  8.71871872,  8.72872873,  8.73873874,  8.74874875,
        8.75875876,  8.76876877,  8.77877878,  8.78878879,  8.7987988 ,
        8.80880881,  8.81881882,  8.82882883,  8.83883884,  8.84884885,
        8.85885886,  8.86886887,  8.87887888,  8.88888889,  8.8988989 ,
        8.90890891,  8.91891892,  8.92892893,  8.93893894,  8.94894895,
        8.95895896,  8.96896897,  8.97897898,  8.98898899,  8.998999  ,
        9.00900901,  9.01901902,  9.02902903,  9.03903904,  9.04904905,
        9.05905906,  9.06906907,  9.07907908,  9.08908909,  9.0990991 ,
        9.10910911,  9.11911912,  9.12912913,  9.13913914,  9.14914915,
        9.15915916,  9.16916917,  9.17917918,  9.18918919,  9.1991992 ,
        9.20920921,  9.21921922,  9.22922923,  9.23923924,  9.24924925,
        9.25925926,  9.26926927,  9.27927928,  9.28928929,  9.2992993 ,
        9.30930931,  9.31931932,  9.32932933,  9.33933934,  9.34934935,
        9.35935936,  9.36936937,  9.37937938,  9.38938939,  9.3993994 ,
        9.40940941,  9.41941942,  9.42942943,  9.43943944,  9.44944945,
        9.45945946,  9.46946947,  9.47947948,  9.48948949,  9.4994995 ,
        9.50950951,  9.51951952,  9.52952953,  9.53953954,  9.54954955,
        9.55955956,  9.56956957,  9.57957958,  9.58958959,  9.5995996 ,
        9.60960961,  9.61961962,  9.62962963,  9.63963964,  9.64964965,
        9.65965966,  9.66966967,  9.67967968,  9.68968969,  9.6996997 ,
        9.70970971,  9.71971972,  9.72972973,  9.73973974,  9.74974975,
        9.75975976,  9.76976977,  9.77977978,  9.78978979,  9.7997998 ,
        9.80980981,  9.81981982,  9.82982983,  9.83983984,  9.84984985,
        9.85985986,  9.86986987,  9.87987988,  9.88988989,  9.8998999 ,
        9.90990991,  9.91991992,  9.92992993,  9.93993994,  9.94994995,
        9.95995996,  9.96996997,  9.97997998,  9.98998999, 10.        ])
y = np.sin(x)
y
array([ 0.        ,  0.01000984,  0.02001868,  0.03002552,  0.04002934,
        0.05002916,  0.06002396,  0.07001275,  0.07999452,  0.08996827,
        0.09993302,  0.10988774,  0.11983146,  0.12976317,  0.13968188,
        0.14958659,  0.15947632,  0.16935006,  0.17920684,  0.18904566,
        0.19886554,  0.20866549,  0.21844453,  0.22820168,  0.23793597,
        0.24764642,  0.25733206,  0.26699191,  0.276625  ,  0.28623038,
        0.29580708,  0.30535414,  0.3148706 ,  0.32435552,  0.33380793,
        0.3432269 ,  0.35261147,  0.36196071,  0.37127369,  0.38054946,
        0.3897871 ,  0.39898569,  0.4081443 ,  0.41726201,  0.42633791,
        0.4353711 ,  0.44436066,  0.45330569,  0.46220531,  0.47105861,
        0.47986471,  0.48862273,  0.49733179,  0.50599102,  0.51459954,
        0.52315651,  0.53166105,  0.54011232,  0.54850948,  0.55685167,
        0.56513807,  0.57336784,  0.58154016,  0.58965421,  0.59770917,
        0.60570425,  0.61363863,  0.62151153,  0.62932216,  0.63706972,
        0.64475345,  0.65237258,  0.65992634,  0.66741398,  0.67483474,
        0.68218788,  0.68947267,  0.69668838,  0.70383427,  0.71090964,
        0.71791378,  0.72484599,  0.73170556,  0.73849182,  0.74520409,
        0.75184168,  0.75840394,  0.76489021,  0.77129983,  0.77763218,
        0.7838866 ,  0.79006248,  0.7961592 ,  0.80217614,  0.8081127 ,
        0.81396829,  0.81974232,  0.82543421,  0.8310434 ,  0.83656931,
        0.84201141,  0.84736913,  0.85264195,  0.85782933,  0.86293076,
        0.86794572,  0.87287371,  0.87771425,  0.88246684,  0.887131  ,
        0.89170627,  0.8961922 ,  0.90058833,  0.90489422,  0.90910944,
        0.91323357,  0.91726619,  0.9212069 ,  0.92505531,  0.92881103,
        0.93247368,  0.9360429 ,  0.93951832,  0.94289961,  0.94618642,
        0.94937842,  0.9524753 ,  0.95547674,  0.95838244,  0.96119211,
        0.96390547,  0.96652224,  0.96904217,  0.97146501,  0.9737905 ,
        0.97601842,  0.97814855,  0.98018066,  0.98211456,  0.98395005,
        0.98568695,  0.98732509,  0.9888643 ,  0.99030442,  0.99164532,
        0.99288685,  0.9940289 ,  0.99507134,  0.99601408,  0.99685702,
        0.99760007,  0.99824317,  0.99878624,  0.99922924,  0.99957211,
        0.99981483,  0.99995736,  0.9999997 ,  0.99994184,  0.99978379,
        0.99952556,  0.99916717,  0.99870867,  0.9981501 ,  0.99749152,
        0.99673299,  0.99587459,  0.9949164 ,  0.99385852,  0.99270105,
        0.99144412,  0.99008785,  0.98863237,  0.98707782,  0.98542438,
        0.98367219,  0.98182145,  0.97987232,  0.97782501,  0.97567973,
        0.97343668,  0.97109609,  0.9686582 ,  0.96612325,  0.9634915 ,
        0.9607632 ,  0.95793864,  0.95501809,  0.95200185,  0.94889022,
        0.94568351,  0.94238205,  0.93898615,  0.93549618,  0.93191246,
        0.92823537,  0.92446527,  0.92060254,  0.91664757,  0.91260074,
        0.90846248,  0.90423319,  0.89991329,  0.89550322,  0.89100343,
        0.88641436,  0.88173646,  0.87697022,  0.87211611,  0.86717461,
        0.86214622,  0.85703145,  0.8518308 ,  0.84654479,  0.84117397,
        0.83571886,  0.83018001,  0.82455797,  0.81885332,  0.81306662,
        0.80719845,  0.80124939,  0.79522006,  0.78911104,  0.78292295,
        0.77665642,  0.77031206,  0.76389052,  0.75739244,  0.75081847,
        0.74416927,  0.7374455 ,  0.73064784,  0.72377697,  0.71683358,
        0.70981836,  0.70273201,  0.69557526,  0.68834881,  0.68105338,
        0.67368972,  0.66625855,  0.65876062,  0.65119668,  0.6435675 ,
        0.63587383,  0.62811645,  0.62029613,  0.61241365,  0.60446981,
        0.59646541,  0.58840124,  0.58027811,  0.57209684,  0.56385824,
        0.55556315,  0.54721239,  0.5388068 ,  0.53034722,  0.5218345 ,
        0.51326949,  0.50465305,  0.49598605,  0.48726935,  0.47850382,
        0.46969035,  0.46082982,  0.45192311,  0.44297112,  0.43397474,
        0.42493488,  0.41585245,  0.40672834,  0.39756348,  0.38835878,
        0.37911517,  0.36983358,  0.36051492,  0.35116015,  0.34177018,
        0.33234597,  0.32288847,  0.3133986 ,  0.30387734,  0.29432562,
        0.28474442,  0.27513468,  0.26549738,  0.25583347,  0.24614393,
        0.23642973,  0.22669183,  0.21693122,  0.20714888,  0.19734578,
        0.1875229 ,  0.17768124,  0.16782177,  0.15794548,  0.14805337,
        0.13814643,  0.12822564,  0.118292  ,  0.10834651,  0.09839017,
        0.08842397,  0.0784489 ,  0.06846598,  0.05847619,  0.04848055,
        0.03848005,  0.02847569,  0.01846848,  0.00845942, -0.00155049,
       -0.01156024, -0.02156884, -0.03157527, -0.04157854, -0.05157764,
       -0.06157158, -0.07155935, -0.08153994, -0.09151237, -0.10147562,
       -0.11142871, -0.12137064, -0.1313004 , -0.141217  , -0.15111946,
       -0.16100677, -0.17087795, -0.18073201, -0.19056796, -0.20038482,
       -0.21018159, -0.21995731, -0.22971099, -0.23944165, -0.24914832,
       -0.25883002, -0.26848579, -0.27811466, -0.28771566, -0.29728783,
       -0.30683021, -0.31634185, -0.32582179, -0.33526908, -0.34468278,
       -0.35406195, -0.36340563, -0.37271291, -0.38198284, -0.39121449,
       -0.40040694, -0.40955928, -0.41867057, -0.42773992, -0.4367664 ,
       -0.44574912, -0.45468718, -0.46357968, -0.47242573, -0.48122444,
       -0.48997494, -0.49867633, -0.50732776, -0.51592836, -0.52447726,
       -0.53297361, -0.54141656, -0.54980525, -0.55813886, -0.56641654,
       -0.57463746, -0.58280081, -0.59090576, -0.5989515 , -0.60693723,
       -0.61486214, -0.62272545, -0.63052635, -0.63826408, -0.64593786,
       -0.65354691, -0.66109048, -0.66856781, -0.67597814, -0.68332075,
       -0.69059488, -0.69779982, -0.70493484, -0.71199922, -0.71899227,
       -0.72591327, -0.73276153, -0.73953637, -0.74623711, -0.75286308,
       -0.75941361, -0.76588805, -0.77228575, -0.77860606, -0.78484836,
       -0.79101202, -0.79709642, -0.80310095, -0.80902501, -0.814868  ,
       -0.82062935, -0.82630847, -0.83190479, -0.83741776, -0.84284682,
       -0.84819143, -0.85345104, -0.85862515, -0.86371322, -0.86871474,
       -0.87362922, -0.87845616, -0.88319509, -0.88784551, -0.89240698,
       -0.89687902, -0.9012612 , -0.90555308, -0.90975421, -0.91386419,
       -0.91788261, -0.92180905, -0.92564312, -0.92938445, -0.93303265,
       -0.93658737, -0.94004823, -0.94341491, -0.94668706, -0.94986435,
       -0.95294646, -0.95593309, -0.95882393, -0.9616187 , -0.96431712,
       -0.96691891, -0.96942382, -0.97183159, -0.97414198, -0.97635477,
       -0.97846973, -0.98048664, -0.98240531, -0.98422555, -0.98594716,
       -0.98756998, -0.98909385, -0.99051862, -0.99184413, -0.99307026,
       -0.99419689, -0.99522389, -0.99615118, -0.99697865, -0.99770623,
       -0.99833384, -0.99886141, -0.9992889 , -0.99961626, -0.99984346,
       -0.99997048, -0.9999973 , -0.99992392, -0.99975034, -0.9994766 ,
       -0.9991027 , -0.9986287 , -0.99805464, -0.99738057, -0.99660656,
       -0.9957327 , -0.99475906, -0.99368575, -0.99251287, -0.99124054,
       -0.98986889, -0.98839806, -0.98682819, -0.98515943, -0.98339197,
       -0.98152597, -0.97956163, -0.97749913, -0.97533868, -0.97308051,
       -0.97072484, -0.9682719 , -0.96572194, -0.96307521, -0.96033199,
       -0.95749254, -0.95455715, -0.95152611, -0.94839974, -0.94517833,
       -0.94186221, -0.93845173, -0.93494721, -0.93134901, -0.92765748,
       -0.92387301, -0.91999597, -0.91602674, -0.91196573, -0.90781333,
       -0.90356998, -0.89923609, -0.8948121 , -0.89029844, -0.88569558,
       -0.88100397, -0.87622409, -0.87135641, -0.86640142, -0.86135962,
       -0.85623151, -0.8510176 , -0.84571842, -0.84033451, -0.83486639,
       -0.82931462, -0.82367975, -0.81796235, -0.81216299, -0.80628225,
       -0.80032073, -0.79427901, -0.7881577 , -0.78195742, -0.77567879,
       -0.76932244, -0.762889  , -0.75637913, -0.74979346, -0.74313266,
       -0.7363974 , -0.72958836, -0.72270621, -0.71575164, -0.70872536,
       -0.70162807, -0.69446047, -0.68722329, -0.67991724, -0.67254307,
       -0.66510151, -0.65759331, -0.65001922, -0.64238   , -0.63467641,
       -0.62690923, -0.61907923, -0.6111872 , -0.60323392, -0.59522021,
       -0.58714685, -0.57901466, -0.57082446, -0.56257706, -0.55427329,
       -0.54591398, -0.53749997, -0.5290321 , -0.52051123, -0.5119382 ,
       -0.50331387, -0.49463911, -0.48591479, -0.47714179, -0.46832097,
       -0.45945322, -0.45053944, -0.44158052, -0.43257735, -0.42353083,
       -0.41444188, -0.4053114 , -0.39614031, -0.38692953, -0.37767997,
       -0.36839258, -0.35906827, -0.34970798, -0.34031265, -0.33088322,
       -0.32142064, -0.31192585, -0.3023998 , -0.29284346, -0.28325777,
       -0.2736437 , -0.26400222, -0.25433428, -0.24464085, -0.23492291,
       -0.22518144, -0.2154174 , -0.20563177, -0.19582554, -0.18599969,
       -0.1761552 , -0.16629307, -0.15641427, -0.14651979, -0.13661064,
       -0.12668779, -0.11675226, -0.10680502, -0.09684708, -0.08687944,
       -0.0769031 , -0.06691904, -0.05692829, -0.04693183, -0.03693066,
       -0.0269258 , -0.01691823, -0.00690898,  0.00310097,  0.01311061,
        0.02311894,  0.03312495,  0.04312764,  0.05312601,  0.06311905,
        0.07310577,  0.08308517,  0.09305624,  0.10301799,  0.11296941,
        0.12290952,  0.13283731,  0.14275178,  0.15265196,  0.16253684,
        0.17240543,  0.18225675,  0.19208981,  0.20190362,  0.2116972 ,
        0.22146956,  0.23121974,  0.24094675,  0.25064961,  0.26032736,
        0.26997903,  0.27960364,  0.28920024,  0.29876786,  0.30830554,
        0.31781233,  0.32728728,  0.33672943,  0.34613784,  0.35551157,
        0.36484968,  0.37415123,  0.38341529,  0.39264093,  0.40182723,
        0.41097327,  0.42007813,  0.42914089,  0.43816066,  0.44713652,
        0.45606758,  0.46495294,  0.47379172,  0.48258302,  0.49132596,
        0.50001968,  0.50866329,  0.51725594,  0.52579676,  0.53428489,
        0.54271949,  0.5510997 ,  0.5594247 ,  0.56769364,  0.5759057 ,
        0.58406006,  0.59215589,  0.60019239,  0.60816875,  0.61608417,
        0.62393786,  0.63172904,  0.63945691,  0.64712071,  0.65471967,
        0.66225303,  0.66972002,  0.67711992,  0.68445196,  0.69171543,
        0.69890958,  0.70603371,  0.71308709,  0.72006902,  0.7269788 ,
        0.73381574,  0.74057914,  0.74726835,  0.75388267,  0.76042146,
        0.76688406,  0.77326981,  0.77957808,  0.78580824,  0.79195966,
        0.79803173,  0.80402383,  0.80993537,  0.81576576,  0.82151441,
        0.82718074,  0.83276419,  0.8382642 ,  0.84368021,  0.84901169,
        0.85425809,  0.8594189 ,  0.8644936 ,  0.86948167,  0.87438263,
        0.87919597,  0.88392121,  0.88855789,  0.89310553,  0.89756369,
        0.90193191,  0.90620976,  0.9103968 ,  0.91449263,  0.91849682,
        0.92240898,  0.92622871,  0.92995564,  0.93358938,  0.93712958,
        0.94057589,  0.94392794,  0.94718542,  0.95034798,  0.95341533,
        0.95638714,  0.95926312,  0.96204298,  0.96472645,  0.96731325,
        0.96980313,  0.97219584,  0.97449113,  0.97668877,  0.97878856,
        0.98079027,  0.9826937 ,  0.98449867,  0.986205  ,  0.9878125 ,
        0.98932103,  0.99073043,  0.99204056,  0.99325128,  0.99436249,
        0.99537405,  0.99628589,  0.99709789,  0.99780999,  0.9984221 ,
        0.99893418,  0.99934616,  0.99965801,  0.99986969,  0.99998119,
        0.99999249,  0.99990359,  0.9997145 ,  0.99942524,  0.99903584,
        0.99854633,  0.99795677,  0.99726722,  0.99647774,  0.99558841,
        0.99459933,  0.99351059,  0.9923223 ,  0.99103458,  0.98964756,
        0.98816137,  0.98657617,  0.98489212,  0.98310938,  0.98122814,
        0.97924858,  0.97717089,  0.9749953 ,  0.97272201,  0.97035125,
        0.96788327,  0.9653183 ,  0.96265661,  0.95989847,  0.95704414,
        0.95409391,  0.95104809,  0.94790697,  0.94467087,  0.94134012,
        0.93791504,  0.93439599,  0.93078331,  0.92707737,  0.92327853,
        0.91938718,  0.91540371,  0.91132852,  0.90716201,  0.9029046 ,
        0.89855673,  0.89411881,  0.88959131,  0.88497468,  0.88026936,
        0.87547585,  0.87059461,  0.86562614,  0.86057094,  0.85542951,
        0.85020236,  0.84489002,  0.83949303,  0.83401192,  0.82844724,
        0.82279955,  0.81706941,  0.81125741,  0.80536412,  0.79939013,
        0.79333605,  0.78720247,  0.78099002,  0.77469931,  0.76833097,
        0.76188565,  0.75536399,  0.74876664,  0.74209427,  0.73534754,
        0.72852712,  0.72163371,  0.71466799,  0.70763067,  0.70052243,
        0.69334401,  0.68609611,  0.67877947,  0.67139481,  0.66394288,
        0.65642443,  0.6488402 ,  0.64119095,  0.63347746,  0.6257005 ,
        0.61786084,  0.60995927,  0.60199658,  0.59397358,  0.58589106,
        0.57774983,  0.56955071,  0.56129452,  0.5529821 ,  0.54461426,
        0.53619185,  0.52771572,  0.51918671,  0.51060568,  0.50197348,
        0.49329099,  0.48455907,  0.4757786 ,  0.46695046,  0.45807552,
        0.44915469,  0.44018886,  0.43117891,  0.42212577,  0.41303032,
        0.40389349,  0.39471619,  0.38549934,  0.37624387,  0.36695069,
        0.35762075,  0.34825497,  0.3388543 ,  0.32941967,  0.31995204,
        0.31045234,  0.30092154,  0.29136059,  0.28177045,  0.27215207,
        0.26250642,  0.25283447,  0.24313718,  0.23341553,  0.2236705 ,
        0.21390305,  0.20411417,  0.19430484,  0.18447604,  0.17462875,
        0.16476397,  0.15488267,  0.14498586,  0.13507452,  0.12514965,
        0.11521223,  0.10526327,  0.09530377,  0.08533471,  0.07535711,
        0.06537195,  0.05538024,  0.04538299,  0.03538119,  0.02537584,
        0.01536795,  0.00535852, -0.00465145, -0.01466095, -0.02466899,
       -0.03467455, -0.04467663, -0.05467424, -0.06466637, -0.07465203,
       -0.0846302 , -0.09459989, -0.1045601 , -0.11450984, -0.1244481 ,
       -0.13437389, -0.14428622, -0.15418409, -0.16406652, -0.1739325 ,
       -0.18378105, -0.19361119, -0.20342193, -0.21321229, -0.22298128,
       -0.23272793, -0.24245127, -0.2521503 , -0.26182408, -0.27147162,
       -0.28109195, -0.29068412, -0.30024717, -0.30978013, -0.31928205,
       -0.32875198, -0.33818897, -0.34759207, -0.35696034, -0.36629285,
       -0.37558865, -0.38484682, -0.39406643, -0.40324656, -0.41238627,
       -0.42148467, -0.43054084, -0.43955386, -0.44852284, -0.45744688,
       -0.46632509, -0.47515657, -0.48394043, -0.49267581, -0.50136182,
       -0.5099976 , -0.51858227, -0.52711499, -0.53559488, -0.54402111])
fig = plt.figure()
ax = plt.axes()
ax.plot(x, y)
plt.show()

plt.plot(x, y)
plt.show()

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.suptitle('function sin(x)')
plt.title("this is title")
plt.show()

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()

plt.plot(x, np.sin(x), color='blue', linestyle='--')
plt.plot(x, np.cos(x), color='red', linestyle=':')
plt.show()

plt.plot(x, np.sin(x), 'b--', label='sin(x)')
plt.plot(x, np.cos(x), 'k:', label='cos(x)')
plt.legend()
plt.show()

plt.scatter(presidents_df['height'], presidents_df['age'])
plt.show()

# customize
plt.scatter(presidents_df['height'], presidents_df['age'], marker='x', edgecolors='red')
plt.xlabel('height')
plt.ylabel('age')
plt.title('U.S. presidents')
plt.show()
C:\Users\Runker\AppData\Local\Temp\ipykernel_25348\795236275.py:2: UserWarning: You passed a edgecolor/edgecolors ('red') for an unfilled marker ('x').  Matplotlib is ignoring the edgecolor in favor of the facecolor.  This behavior may change in the future.
  plt.scatter(presidents_df['height'],presidents_df['age'],marker='x',edgecolors='red')

# plotting with pandas
presidents_df.plot(kind='scatter', x='height', y='age',
                   marker='<', color='gray', title='U.S. presidents',
                   xlabel='height', ylabel='age')
plt.suptitle("Sup title")
plt.show()

presidents_df['height'].plot(kind='hist', title='height', bins=5)
plt.show()

plt.hist(presidents_df['height'], bins=10)
plt.show()

presidents_df['height'].describe()
count     45.000000
mean     180.000000
std        6.977236
min      163.000000
25%      175.000000
50%      182.000000
75%      183.000000
max      193.000000
Name: height, dtype: float64
plt.style.use('classic')
presidents_df.boxplot(column='height')
<AxesSubplot:>

presidents_df.plot(kind='box', column='height')
<AxesSubplot:>

presidents_df['height'].plot(kind='box')
plt.style.use('ggplot')

party_cnt = presidents_df['party'].value_counts()
plt.style.use('ggplot')
party_cnt.plot(kind='bar')
plt.show()

(presidents_df.groupby('party').max()).plot(kind='bar')
<AxesSubplot:xlabel='party'>

(presidents_df.groupby('party').mean()).plot(kind='bar')
<AxesSubplot:xlabel='party'>

import pandas as pd
import numpy as np
from sklearn.datasets import load_boston

boston_dataset = load_boston()
boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
boston['MEDV'] = boston_dataset.target
boston
D:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\deprecation.py:87: FutureWarning: Function load_boston is deprecated; `load_boston` is deprecated in 1.0 and will be removed in 1.2.

    The Boston housing prices dataset has an ethical problem. You can refer to
    the documentation of this function for further details.

    The scikit-learn maintainers therefore strongly discourage the use of this
    dataset unless the purpose of the code is to study and educate about
    ethical issues in data science and machine learning.

    In this special case, you can fetch the dataset from the original
    source::

        import pandas as pd
        import numpy as np


        data_url = "http://lib.stat.cmu.edu/datasets/boston"
        raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
        data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
        target = raw_df.values[1::2, 2]

    Alternative datasets include the California housing dataset (i.e.
    :func:`~sklearn.datasets.fetch_california_housing`) and the Ames housing
    dataset. You can load the datasets as follows::

        from sklearn.datasets import fetch_california_housing
        housing = fetch_california_housing()

    for the California housing dataset and::

        from sklearn.datasets import fetch_openml
        housing = fetch_openml(name="house_prices", as_frame=True)

    for the Ames housing dataset.

  warnings.warn(msg, category=FutureWarning)
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0 296.0 15.3 396.90 4.98 24.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0 242.0 17.8 396.90 9.14 21.6
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0 242.0 17.8 392.83 4.03 34.7
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0 222.0 18.7 394.63 2.94 33.4
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0 222.0 18.7 396.90 5.33 36.2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
501 0.06263 0.0 11.93 0.0 0.573 6.593 69.1 2.4786 1.0 273.0 21.0 391.99 9.67 22.4
502 0.04527 0.0 11.93 0.0 0.573 6.120 76.7 2.2875 1.0 273.0 21.0 396.90 9.08 20.6
503 0.06076 0.0 11.93 0.0 0.573 6.976 91.0 2.1675 1.0 273.0 21.0 396.90 5.64 23.9
504 0.10959 0.0 11.93 0.0 0.573 6.794 89.3 2.3889 1.0 273.0 21.0 393.45 6.48 22.0
505 0.04741 0.0 11.93 0.0 0.573 6.030 80.8 2.5050 1.0 273.0 21.0 396.90 7.88 11.9

506 rows × 14 columns

boston.head()
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0 296.0 15.3 396.90 4.98 24.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0 242.0 17.8 396.90 9.14 21.6
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0 242.0 17.8 392.83 4.03 34.7
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0 222.0 18.7 394.63 2.94 33.4
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0 222.0 18.7 396.90 5.33 36.2
boston.describe()
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
count 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000 506.000000
mean 3.613524 11.363636 11.136779 0.069170 0.554695 6.284634 68.574901 3.795043 9.549407 408.237154 18.455534 356.674032 12.653063 22.532806
std 8.601545 23.322453 6.860353 0.253994 0.115878 0.702617 28.148861 2.105710 8.707259 168.537116 2.164946 91.294864 7.141062 9.197104
min 0.006320 0.000000 0.460000 0.000000 0.385000 3.561000 2.900000 1.129600 1.000000 187.000000 12.600000 0.320000 1.730000 5.000000
25% 0.082045 0.000000 5.190000 0.000000 0.449000 5.885500 45.025000 2.100175 4.000000 279.000000 17.400000 375.377500 6.950000 17.025000
50% 0.256510 0.000000 9.690000 0.000000 0.538000 6.208500 77.500000 3.207450 5.000000 330.000000 19.050000 391.440000 11.360000 21.200000
75% 3.677083 12.500000 18.100000 0.000000 0.624000 6.623500 94.075000 5.188425 24.000000 666.000000 20.200000 396.225000 16.955000 25.000000
max 88.976200 100.000000 27.740000 1.000000 0.871000 8.780000 100.000000 12.126500 24.000000 711.000000 22.000000 396.900000 37.970000 50.000000
boston.describe().round(2)
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
count 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00 506.00
mean 3.61 11.36 11.14 0.07 0.55 6.28 68.57 3.80 9.55 408.24 18.46 356.67 12.65 22.53
std 8.60 23.32 6.86 0.25 0.12 0.70 28.15 2.11 8.71 168.54 2.16 91.29 7.14 9.20
min 0.01 0.00 0.46 0.00 0.38 3.56 2.90 1.13 1.00 187.00 12.60 0.32 1.73 5.00
25% 0.08 0.00 5.19 0.00 0.45 5.89 45.02 2.10 4.00 279.00 17.40 375.38 6.95 17.02
50% 0.26 0.00 9.69 0.00 0.54 6.21 77.50 3.21 5.00 330.00 19.05 391.44 11.36 21.20
75% 3.68 12.50 18.10 0.00 0.62 6.62 94.07 5.19 24.00 666.00 20.20 396.22 16.96 25.00
max 88.98 100.00 27.74 1.00 0.87 8.78 100.00 12.13 24.00 711.00 22.00 396.90 37.97 50.00
boston['AGE'].describe().round(2)
count    506.00
mean      68.57
std       28.15
min        2.90
25%       45.02
50%       77.50
75%       94.07
max      100.00
Name: AGE, dtype: float64
import matplotlib.pyplot as plt

boston.hist(column='CHAS')
plt.show()

boston.hist(column='RM', bins=20)
plt.show()

boston.hist(column='LSTAT', bins=20)
plt.show()

boston
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0 296.0 15.3 396.90 4.98 24.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0 242.0 17.8 396.90 9.14 21.6
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0 242.0 17.8 392.83 4.03 34.7
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0 222.0 18.7 394.63 2.94 33.4
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0 222.0 18.7 396.90 5.33 36.2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
501 0.06263 0.0 11.93 0.0 0.573 6.593 69.1 2.4786 1.0 273.0 21.0 391.99 9.67 22.4
502 0.04527 0.0 11.93 0.0 0.573 6.120 76.7 2.2875 1.0 273.0 21.0 396.90 9.08 20.6
503 0.06076 0.0 11.93 0.0 0.573 6.976 91.0 2.1675 1.0 273.0 21.0 396.90 5.64 23.9
504 0.10959 0.0 11.93 0.0 0.573 6.794 89.3 2.3889 1.0 273.0 21.0 393.45 6.48 22.0
505 0.04741 0.0 11.93 0.0 0.573 6.030 80.8 2.5050 1.0 273.0 21.0 396.90 7.88 11.9

506 rows × 14 columns

corr_matrix = boston.corr().round(2)
corr_matrix
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT MEDV
CRIM 1.00 -0.20 0.41 -0.06 0.42 -0.22 0.35 -0.38 0.63 0.58 0.29 -0.39 0.46 -0.39
ZN -0.20 1.00 -0.53 -0.04 -0.52 0.31 -0.57 0.66 -0.31 -0.31 -0.39 0.18 -0.41 0.36
INDUS 0.41 -0.53 1.00 0.06 0.76 -0.39 0.64 -0.71 0.60 0.72 0.38 -0.36 0.60 -0.48
CHAS -0.06 -0.04 0.06 1.00 0.09 0.09 0.09 -0.10 -0.01 -0.04 -0.12 0.05 -0.05 0.18
NOX 0.42 -0.52 0.76 0.09 1.00 -0.30 0.73 -0.77 0.61 0.67 0.19 -0.38 0.59 -0.43
RM -0.22 0.31 -0.39 0.09 -0.30 1.00 -0.24 0.21 -0.21 -0.29 -0.36 0.13 -0.61 0.70
AGE 0.35 -0.57 0.64 0.09 0.73 -0.24 1.00 -0.75 0.46 0.51 0.26 -0.27 0.60 -0.38
DIS -0.38 0.66 -0.71 -0.10 -0.77 0.21 -0.75 1.00 -0.49 -0.53 -0.23 0.29 -0.50 0.25
RAD 0.63 -0.31 0.60 -0.01 0.61 -0.21 0.46 -0.49 1.00 0.91 0.46 -0.44 0.49 -0.38
TAX 0.58 -0.31 0.72 -0.04 0.67 -0.29 0.51 -0.53 0.91 1.00 0.46 -0.44 0.54 -0.47
PTRATIO 0.29 -0.39 0.38 -0.12 0.19 -0.36 0.26 -0.23 0.46 0.46 1.00 -0.18 0.37 -0.51
B -0.39 0.18 -0.36 0.05 -0.38 0.13 -0.27 0.29 -0.44 -0.44 -0.18 1.00 -0.37 0.33
LSTAT 0.46 -0.41 0.60 -0.05 0.59 -0.61 0.60 -0.50 0.49 0.54 0.37 -0.37 1.00 -0.74
MEDV -0.39 0.36 -0.48 0.18 -0.43 0.70 -0.38 0.25 -0.38 -0.47 -0.51 0.33 -0.74 1.00
plt.scatter(x=boston['RM'], y=boston['MEDV'])
plt.show()

boston.plot(kind='scatter', x='RM', y='MEDV', figsize=(8, 6))
<AxesSubplot:xlabel='RM', ylabel='MEDV'>

boston.plot(kind='scatter', x='LSTAT', y='MEDV', figsize=(8, 6))
<AxesSubplot:xlabel='LSTAT', ylabel='MEDV'>

# 导入回归模型
from sklearn.linear_model import LinearRegression

model = LinearRegression()
from sklearn.model_selection import train_test_split
X = boston[['RM']]
Y = boston[['MEDV']]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=1)
print(X_train.shape)
print(Y_train.shape)
print(X_test.shape)
print(Y_test.shape)
(354, 1)
(354, 1)
(152, 1)
(152, 1)
model.fit(X_train,Y_train)
LinearRegression()
model.intercept_.round(2)
array([-30.57])
model.coef_.round(2)
array([[8.46]])
new_RM = np.array([6,5]).reshape(-1,1)
new_RM
array([[6],
       [5]])
model.predict(new_RM)
D:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py:450: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names
  warnings.warn(





array([[20.19551741],
       [11.73442577]])
y_test_predicted = model.predict(X_test)
y_test_predicted.shape
(152, 1)
X_test.shape
(152, 1)
y_test_predicted
array([27.37898421, 26.08443719, 20.44935016, 19.16326423, 14.79734095,
       23.3430435 , 25.52600514, 20.80471601, 19.78092392, 23.72379262,
       26.54133614, 27.5905115 , 17.91948376, 18.39330489, 20.39012252,
       16.87876949, 25.68676588, 43.25199212, 21.56621426, 11.10830499,
       17.69949538, 17.04799132, 24.99295637, 23.70687044, 22.64923399,
       19.31556388, 21.00778221, 20.31397269, 33.0563767 , 15.03425151,
       25.09448947, 16.85338622, 37.4053778 , 22.31925141, 21.41391461,
       19.56939663, 23.08074966, 29.90038952,  4.44096478, 21.16008186,
       23.01306093, 22.81845582, 25.42447204, 21.56621426, 21.76081936,
       27.16745692, 35.44240454, 19.23941406, 21.05008767, 23.62225952,
       28.48738722, 23.22458822, 23.83378682, 22.89460564, 12.5213073 ,
       24.0537752 , 18.94327585, 21.22777059, 20.1109065 , 39.20759032,
       23.67302607, 25.52600514, 18.05486123, 23.1399773 , 15.22039553,
       28.48738722, 39.96908857, 24.7645069 , 20.27166724, 18.96019803,
       22.835378  ,  8.78996588, 11.42136538, 25.89829318, 21.27007605,
       18.1817776 , 28.79198652, 24.02839192, 24.51067415, 29.90885061,
       23.84224791, 20.37320034, 30.221911  , 25.51754405, 24.34145231,
       23.11459403, 23.72379262, 23.84224791, 18.45253253, 23.02998311,
       28.45354285, 43.0743092 , 20.05167885, 20.29705051, 21.90465792,
       20.27166724, 25.21294475, 23.93531992, 39.36835106, 21.84543028,
       18.81635947, 24.29914686, 21.6254419 , 21.4223757 , 14.07814816,
       22.15849067, 16.51494255, 22.31079032, 21.49006443, 26.80362998,
       32.63332212, 23.71533153, 21.8285081 , 32.21026754, 31.47415256,
       30.16268336, 20.06860104, 15.59268356, 28.85967525, 39.6306449 ,
       19.07865332, 26.35519212, 26.71055797, 22.31925141, 24.97603419,
       20.35627815, 21.27007605, 21.21084841, 16.57417019, 26.34673103,
       18.3848438 , 23.42765442, 21.98080775, 12.03902507, 25.43293314,
       30.65342668, 15.1442457 , 19.99245121, 35.12934415, 28.38585412,
       19.65400755, 21.47314225, 18.80789838, 24.26530249, 27.48051731,
       21.10931531, 23.79994245, 20.22090069, 25.48369968, 19.86553484,
       30.37421065, 18.61329328])
X_test.head()
RM
307 6.849
343 6.696
47 6.030
67 5.878
362 5.362
Y_test.head()
type(X_test)
pandas.core.frame.DataFrame
plt.scatter(X_test, Y_test,label='testing data')
plt.plot(X_test['RM'], y_test_predicted,label='prediction')
plt.xlabel('RM'); plt.ylabel('MEDV')
plt.legend(loc='upper left')
plt.savefig("plot.png")
plt.show()

residuals = Y_test - y_test_predicted
residuals.head()
307    0.821016
343   -2.184437
47    -3.849350
67     2.836736
362    6.002659
Name: MEDV, dtype: float64
plt.scatter(X_test,residuals)
<matplotlib.collections.PathCollection at 0x1ef90fb0820>

plt.scatter(X_test,residuals)
plt.hlines(y =0,xmin=X_test.min(),xmax=X_test.max(),linestyles='--')
plt.xlim((4,10))
plt.xlabel('RM')
plt.ylabel('residuals')
plt.show()

residuals[:5]
307    0.821016
343   -2.184437
47    -3.849350
67     2.836736
362    6.002659
Name: MEDV, dtype: float64
residuals.mean()
-0.23645087766388564
# 均方误差
(residuals**2).mean()
36.5172147308386
# 均方误差
from sklearn.metrics import mean_squared_error
mean_squared_error(Y_test,y_test_predicted)
36.517214730838624
((Y_test-Y_test.mean())**2).sum()
13931.482039473683
(residuals**2).sum()
5550.6166390874705
1-((residuals**2).sum())/((Y_test-Y_test.mean())**2).sum()
0.6015774471545623
model.score(X_test,Y_test)
0.6015774471545623
# 多元回归
x2 = boston[['RM','LSTAT']]
y = boston['MEDV']
x2_train,x2_test,y_train,y_test = train_test_split(x2,y,test_size=0.3,random_state=1)
model2 = LinearRegression()
model2.fit(x2_train,y_train)
LinearRegression()
model2.intercept_
5.316818471096038
model2.coef_
array([ 4.12674118, -0.67762654])
y_test_predicted2 = model2.predict(x2_test)
mean_squared_error(y_test,y_test_predicted2).round(2)
28.93


本文由 runker 原创,转载请注明出处。

相关推荐

评论

0
暂无评论,来发表第一条评论吧

发表评论

登录 后发表评论