• Python for Data Science


    Chapter 5 - Basic Math and Statistics

    Segment 7 - Transforming dataset distributions

    import numpy as np
    import pandas as pd
    import scipy
    
    import matplotlib.pyplot as plt
    from matplotlib import rcParams
    import seaborn as sb
    
    import sklearn
    from sklearn import preprocessing
    from sklearn.preprocessing import scale
    
    %matplotlib inline
    rcParams['figure.figsize'] = 5, 4
    sb.set_style('whitegrid')
    

    Normalizing and transforming features with MinMaxScalar() and fit_transform()

    address = '~/Data/mtcars.csv'
    
    cars = pd.read_csv(address)
    cars.columns = ['car_names','mpg','cyl','disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', 'carb']
    
    mpg = cars.mpg
    plt.plot(mpg)
    
    [<matplotlib.lines.Line2D at 0x7f8460556b70>]
    

    png

    cars[['mpg']].describe()
    
    mpg
    count 32.000000
    mean 20.090625
    std 6.026948
    min 10.400000
    25% 15.425000
    50% 19.200000
    75% 22.800000
    max 33.900000
    mpg_matrix = mpg.values.reshape(-1,1)
    
    scaled = preprocessing.MinMaxScaler()
    
    scaled_mpg = scaled.fit_transform(mpg_matrix)
    plt.plot(scaled_mpg)
    
    [<matplotlib.lines.Line2D at 0x7f845fe54828>]
    

    png

    scaled = preprocessing.MinMaxScaler(feature_range=(0,10))
    
    scaled_mpg = scaled.fit_transform(mpg_matrix)
    plt.plot(scaled_mpg)
    
    [<matplotlib.lines.Line2D at 0x7f845fdb8550>]
    

    png

    Using scale() to scale your features

    standardized_mpg = scale(mpg, axis=0, with_mean=False, with_std=False)
    plt.plot(standardized_mpg)
    
    [<matplotlib.lines.Line2D at 0x7f845fd91be0>]
    

    png

    standardized_mpg = scale(mpg)
    plt.plot(standardized_mpg)
    
    [<matplotlib.lines.Line2D at 0x7f845fcf6470>]
    

    png

  • 相关阅读:
    jeesite导入导出
    jeesite下载
    百度echart
    js获取日期
    清除svn
    父子窗口
    JS高级程序设计之高级技巧
    JS中离线应用与客户端存储
    JS最佳实践
    JSON之JS高级程序设计笔记
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14285856.html
Copyright © 2020-2023  润新知