这本书类似于工具书或者字典,对于python具体代码的调用和使用场景写的很清楚,感觉虽然是工具书,但是对照着做一遍应该可以对机器学习中python常用的这些库有更深入的理解,在应用中也能更为熟练。
以下是根据书上的代码进行实操,注释基本写明了每句代码的作用(写在本句代码之前)和print的输出结果(写在print之后)。不一定严格按照书上内容进行,根据代码运行时具体情况稍作顺序调整,也加入了一些自己的理解。
如果复制到自己的环境下跑一遍输出,相信理解会更深刻更清楚。
博客中每个代码块代表一次完整的运行结果,可以直接以此为单位复制并运行。
包括:
主要是sklearn模块,对数值特征处理的一些应用。
包含归一化、标准化、处理有离群值的数据三种情况。
from sklearn import preprocessing
import numpy as np
# 创建特征
feature = np.array([[-500.5], [-100.1], [0], [100.1], [900.9]])
print(feature)
# [[-500.5]
# [-100.1]
# [ 0. ]
# [ 100.1]
# [ 900.9]]
# --创建缩放器,归一化,特征的最小值和最大值分别赋予0和1
minmax_scale = preprocessing.MinMaxScaler(feature_range = (0, 1))
# 缩放特征
scaled_feature = minmax_scale.fit_transform(feature)
print(scaled_feature)
# [[0. ]
# [0.28571429]
# [0.35714286]
# [0.42857143]
# [1. ]]
# 输出平均值,标准差
print(scaled_feature.mean())
print(scaled_feature.std())
# 0.41428571428571426
# 0.32701494692170274
# --创建缩放器,标准化,平均值为0,标准差为1
scaler = preprocessing.StandardScaler()
# 标准化特征
scaled_feature = scaler.fit_transform(feature)
print(scaled_feature)
# [[-1.26687088]
# [-0.39316683]
# [-0.17474081]
# [ 0.0436852 ]
# [ 1.79109332]]
# 输出平均值,标准差
print(scaled_feature.mean())
print(scaled_feature.std())
# 0.0
# 1.0
# --创建缩放器,缩放有离群值的数据
scaler = preprocessing.RobustScaler()
# 标准化特征
scaled_feature = scaler.fit_transform(feature)
print(scaled_feature)
# [[-2.5]
# [-0.5]
# [ 0. ]
# [ 0.5]
# [ 4.5]]
# 输出平均值,标准差
print(scaled_feature.mean())
print(scaled_feature.std())
# 0.4
# 2.2891046284519194
与特征缩放的区别在于:特征缩放以整体所有特征为单位进行计算,观察值以样本(行)为单位进行计算。
from sklearn.preprocessing import Normalizer
import numpy as np
# 创建特征矩阵
feature = np.array([[0.5, 0.5], [1.1, 3.4], [1.5, 20.2], [1.63, 34.4], [10.9, 3.3]])
print(feature)
# [[ 0.5 0.5 ]
# [ 1.1 3.4 ]
# [ 1.5 20.2 ]
# [ 1.63 34.4 ]
# [10.9 3.3 ]]
# 创建归一化器,L2范数
normalizer = Normalizer(norm = 'l2')
# 转换特征矩阵
print(normalizer.transform(feature))
# [[0.70710678 0.70710678]
# [0.30782029 0.95144452]
# [0.07405353 0.99725427]
# [0.04733062 0.99887928]
# [0.95709822 0.28976368]]
# 创建归一化器,L1范数
normalizer = Normalizer(norm = 'l1')
# 转换特征矩阵
print(normalizer.transform(feature))
# [[0.5 0.5 ]
# [0.24444444 0.75555556]
# [0.06912442 0.93087558]
# [0.04524008 0.95475992]
# [0.76760563 0.23239437]]
# 创建归一化器,最大值归一化
normalizer = Normalizer(norm = 'max')
# 转换特征矩阵
print(normalizer.transform(feature))
# [[1. 1. ]
# [0.32352941 1. ]
# [0.07425743 1. ]
# [0.04738372 1. ]
# [1. 0.30275229]]
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
# 创建特征矩阵
features = np.array([[2, 3], [2, 3], [2, 3]])
print(features)
# [[2 3]
# [2 3]
# [2 3]]
# 创建PolynomialFeatures对象
polynomial_interaction = PolynomialFeatures(degree = 2, include_bias = False)
# --创建多项式特征,解决特征与目标是非线性关系的问题,degree是最高阶数
# x1, x2, x1^2, x1*x2, x2^2
print(polynomial_interaction.fit_transform(features))
# [[2. 3. 4. 6. 9.]
# [2. 3. 4. 6. 9.]
# [2. 3. 4. 6. 9.]]
polynomial_interaction = PolynomialFeatures(degree = 3, include_bias = False)
# degree = 3,最大值为原特征最大值的三次方
print(polynomial_interaction.fit_transform(features))
# [[ 2. 3. 4. 6. 9. 8. 12. 18. 27.]
# [ 2. 3. 4. 6. 9. 8. 12. 18. 27.]
# [ 2. 3. 4. 6. 9. 8. 12. 18. 27.]]
interaction = PolynomialFeatures(degree = 2, interaction_only = True, include_bias = False)
# --创建交互特征,解决目标由多个特征决定的问题,degree是最高阶数
# # x1, x2, x1*x2
print(interaction.fit_transform(features))
# [[2. 3. 6.]
# [2. 3. 6.]
# [2. 3. 6.]]
有时需要按照自己的需求转换特征,比如求特征的对数。可以通过函数转换器FunctionTransformer()或者pandas中的apply()方法两种方式达到自定义特征转换的目的。
from sklearn.preprocessing import FunctionTransformer
import numpy as np
# 创建特征矩阵
features = np.array([[2, 3], [2, 3], [2, 3]])
print(features)
# [[2 3]
# [2 3]
# [2 3]]
# 自定义函数
def add_ten(x):
return x + 10
# 创建转换器
ten_transformer = FunctionTransformer(add_ten)
print(ten_transformer.transform(features))
# [[12 13]
# [12 13]
# [12 13]]
# 同样可以采用pandas来转换
import pandas as pd
df = pd.DataFrame(features, columns = ['feature_1', 'feature_2'])
print(df.apply(add_ten))
# feature_1 feature_2
# 0 12 13
# 1 12 13
# 2 12 13
from sklearn.covariance import EllipticEnvelope
from sklearn.datasets import make_blobs
import numpy as np
# 创建聚类的模拟数据集
features,_ = make_blobs(n_samples = 10, n_features = 2, centers = 1, random_state = 1)
print(features)
# [[-1.83198811 3.52863145]
# [-2.76017908 5.55121358]
# [-1.61734616 4.98930508]
# [-0.52579046 3.3065986 ]
# [ 0.08525186 3.64528297]
# [-0.79415228 2.10495117]
# [-1.34052081 4.15711949]
# [-1.98197711 4.02243551]
# [-2.18773166 3.33352125]
# [-0.19745197 2.34634916]]
# 替换极端值
features[0,1] = 10000
features[1,1] = 10000
print(features)
# [[-1.83198811e+00 1.00000000e+04]
# [-2.76017908e+00 1.00000000e+04]
# [-1.61734616e+00 4.98930508e+00]
# [-5.25790464e-01 3.30659860e+00]
# [ 8.52518583e-02 3.64528297e+00]
# [-7.94152277e-01 2.10495117e+00]
# [-1.34052081e+00 4.15711949e+00]
# [-1.98197711e+00 4.02243551e+00]
# [-2.18773166e+00 3.33352125e+00]
# [-1.97451969e-01 2.34634916e+00]]
# ----方法一:EllipticEnvelope()
# 创建异常值识别器,污染指数contamination是异常值的比例
outlier_detector = EllipticEnvelope(contamination = .1)
# 拟合识别器
outlier_detector.fit(features)
# 预测异常值
print(outlier_detector.predict(features))
# [-1 1 1 1 1 1 1 1 1 1]
# 修改污染指数
outlier_detector = EllipticEnvelope(contamination = .3)
# 拟合识别器
outlier_detector.fit(features)
# 预测异常值
print(outlier_detector.predict(features))
# [-1 -1 1 1 -1 1 1 1 1 1]
# ----方法二:四分位差IQR识别
# 也可以只查看某个特征的异常值,采用四分位差IQR识别
# IQR = 第一个四分位数和第三个四分位数的差值
# 异常值常常被定义为比第一个四分位数小1.5个IQR,或比第三个四分位数大1.5个IQR的值
feature = features[:,1]
print(feature)
# [1.00000000e+04 1.00000000e+04 4.98930508e+00 3.30659860e+00
# 3.64528297e+00 2.10495117e+00 4.15711949e+00 4.02243551e+00
# 3.33352125e+00 2.34634916e+00]
# 创建通过四分位差IQR识别法,返回异常值下标的函数
def indicies_of_outliers(x):
q1, q3 = np.percentile(x, [25, 75])
iqr = q3 - q1
lower_bound = q1 - (iqr * 1.5)
upper_bound = q3 + (iqr * 1.5)
return np.where((x > upper_bound) | (x < lower_bound))
# 识别异常值下标
print(indicies_of_outliers(feature))
# (array([0, 1]),)
# ----处理异常值
# -----方法一:采用RobustScaler()缩放含有离群值的特征
from sklearn import preprocessing
scaler = preprocessing.RobustScaler()
scaled_feature = scaler.fit_transform(features)
print(scaled_feature)
# [[-2.61212566e-01 6.80970487e+03]
# [-9.47948061e-01 6.80970487e+03]
# [-1.02406616e-01 7.87126291e-01]
# [ 7.05196630e-01 -3.59186642e-01]
# [ 1.15728512e+00 -1.28464128e-01]
# [ 5.06645267e-01 -1.17778692e+00]
# [ 1.02406616e-01 2.20215119e-01]
# [-3.72184092e-01 1.28464128e-01]
# [-5.24414566e-01 -3.40846083e-01]
# [ 9.48122608e-01 -1.01333897e+00]]
# -----方法二:分析特征值的成因,针对性处理
import pandas as pd
# 创建数据帧
houses = pd.DataFrame()
houses['Price'] = [534433, 392333, 293222, 4322032]
houses['Bathrooms'] = [2, 3.5, 2, 116] # 卧室数量?
houses['Square_Feet'] = [1500, 2500, 1500, 48000]
print(houses)
# Price Bathrooms Square_Feet
# 0 534433 2.0 1500
# 1 392333 3.5 2500
# 2 293222 2.0 1500
# 3 4322032 116.0 48000
# 可以通过已知条件直接筛选的方式来筛选观察值
print(houses[houses['Bathrooms'] < 20])
# Price Bathrooms Square_Feet
# 0 534433 2.0 1500
# 1 392333 3.5 2500
# 2 293222 2.0 1500
# 或者把他们标记为异常值,并作为数据集的一个特征
houses['Outlier'] = np.where(houses['Bathrooms'] < 20, 0, 1)
print(houses)
# Price Bathrooms Square_Feet Outlier
# 0 534433 2.0 1500 0
# 1 392333 3.5 2500 0
# 2 293222 2.0 1500 0
# 3 4322032 116.0 48000 1
# 对异常值进行转换,降低异常值的影响
# 对特征取对数值
houses['log_of_square_feet'] = [np.log(x) for x in houses['Square_Feet']]
print(houses)
# Price Bathrooms Square_Feet Outlier log_of_square_feet
# 0 534433 2.0 1500 0 7.313220
# 1 392333 3.5 2500 0 7.824046
# 2 293222 2.0 1500 0 7.313220
# 3 4322032 116.0 48000 1 10.778956
from sklearn.preprocessing import Binarizer
import numpy as np
age = np.array([[6], [12], [20], [36], [65]])
# -- 方法一:两个区间,二值化
# 创建二值化器
binarizer = Binarizer(18)
# 二值化特征
print(binarizer.fit_transform(age))
# [[0]
# [0]
# [1]
# [1]
# [1]]
# -- 方法二:多个区间,离散化
# 将特征离散化,bins是区间列表,落在第i(0-n)个区间,返回的值就是i
print(np.digitize(age, bins = [18]))
# [[0]
# [0]
# [1]
# [1]
# [1]]
print(np.digitize(age, bins = [20, 30, 64]))
# [[0]
# [0]
# [1]
# [2]
# [3]]
# -- 方法三:无显式关系联,聚类分组
import pandas as pd
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
# 创建模拟的矩阵特征
features, _ = make_blobs(n_samples = 50, n_features = 2, centers = 3, random_state = 1)
print(features[:5])
# [[-9.87755355 -3.33614544]
# [-7.28721033 -8.35398617]
# [-6.94306091 -7.0237442 ]
# [-7.44016713 -8.79195851]
# [-6.64138783 -8.07588804]]
# 创建数据帧
dataframe = pd.DataFrame(features, columns = ['feature_1', 'feature_2'])
print(dataframe.head(5))
# feature_1 feature_2
# 0 -9.877554 -3.336145
# 1 -7.287210 -8.353986
# 2 -6.943061 -7.023744
# 3 -7.440167 -8.791959
# 4 -6.641388 -8.075888
# 创建K-Means聚类器
clusterer = KMeans(3, random_state = 0)
# 将聚类应用在特征上
clusterer.fit(features)
# 预测聚类的值
dataframe['group'] = clusterer.predict(features)
print(dataframe.head(5))
# feature_1 feature_2 group
# 0 -9.877554 -3.336145 0
# 1 -7.287210 -8.353986 2
# 2 -6.943061 -7.023744 2
# 3 -7.440167 -8.791959 2
# 4 -6.641388 -8.075888 2
import numpy as np
# 创建特征矩阵
features = np.array([[1.1, 11.1], [2.2, 22.2], [3.3, 33.3], [4.4, 44.4], [np.nan, 55]])
print(features)
# [[ 1.1 11.1]
# [ 2.2 22.2]
# [ 3.3 33.3]
# [ 4.4 44.4]
# [ nan 55. ]]
# -- 方法一:只保留没有(~表示取反补集)缺失值的观察值
print(features[~np.isnan(features).any(axis = 1)])
# [[ 1.1 11.1]
# [ 2.2 22.2]
# [ 3.3 33.3]
# [ 4.4 44.4]]
# -- 方法二:pd.dropna()
import pandas as pd
dataframe = pd.DataFrame(features, columns = ['feature_1', 'feature_2'])
# 删除带有缺失值的观察值
print(dataframe.dropna())
# feature_1 feature_2
# 0 1.1 11.1
# 1 2.2 22.2
# 2 3.3 33.3
# 3 4.4 44.4
# -- 填充缺失值
# --- 方法一:fancyimpute模块
from fancyimpute import KNN
# 填充算法:最近邻估算,使用两行都具有观测数据的特征的均方差来对样本进行加权。然后用加权的结果进行特征值填充
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_blobs
# 创建模拟特征矩阵
features, _ = make_blobs(n_samples = 1000, n_features = 2, random_state = 1)
print(features[:5])
# [[-3.05837272 4.48825769]
# [-8.60973869 -3.72714879]
# [ 1.37129721 5.23107449]
# [-9.33917563 -2.9544469 ]
# [-8.63895561 -8.05263469]]
# 标准化特征
scaler = StandardScaler()
standardized_features = scaler.fit_transform(features)
print(standardized_features[:5])
# [[ 0.87301861 1.31426523]
# [-0.67073178 -0.22369263]
# [ 2.1048424 1.45332359]
# [-0.87357709 -0.07903966]
# [-0.67885655 -1.03344137]]
# 替换为缺失值
true_value = standardized_features[0,0]
standardized_features[0,0] = np.nan
print(standardized_features[:5])
# [[ nan 1.31426523]
# [-0.67073178 -0.22369263]
# [ 2.1048424 1.45332359]
# [-0.87357709 -0.07903966]
# [-0.67885655 -1.03344137]]
# 预测特征矩阵中的缺失值
features_knn_imputed = KNN(k = 5, verbose = 0).fit_transform(standardized_features)
# 对比真实值和填充值
print('True:', true_value)
print('Imputed:', features_knn_imputed[0,0])
# True: 0.8730186113995938
# Imputed: 1.0955332713113226
# --- 方法二:sklearn的Imputer模块
# 用特征的平均数、中位数或众数填充均值,效果一般比KNN的差
from sklearn.impute import SimpleImputer
# 创建填充器
mean_imputer = SimpleImputer(strategy = 'mean')
# 填充缺失值
features_mean_imputed = mean_imputer.fit_transform(standardized_features)
# 对比真实值和填充值
print('True:', true_value)
print('Imputed:', features_knn_imputed[0,0])
# True: 0.8730186113995938
# Imputed: 1.0955332713113226
# 如果采用填充策略,最好创建一个新的二元特征来表示该观察值是否具有填充值,有时缺失值也是一个信息
关于掉包,感谢一些前辈的踩坑经验:
fancyimpute
Imputer
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co