谷歌董事长施密特曾说过: 虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关注, 但是这家公司真正的未来在于机器学习, 一种让计算机更聪明更个性化的技术
也许我们生活在人类历史上最关键的时期: 从使用大型计算机, 到个人电脑, 再到现在的云计算关键的不是过去发生了什么, 而是将来会有什么发生
工具和技术的民主化, 让像我这样的人对这个时期兴奋不已计算的蓬勃发展也是一样如今, 作为一名数据科学家, 用复杂的算法建立数据处理机器一小时能赚到好几美金但能做到这个程度可并不简单! 我也曾有过无数黑暗的日日夜夜
谁能从这篇指南里受益最多?
我今天所给出的, 也许是我这辈子写下的最有价值的指南
这篇指南的目的, 是为那些有追求的数据科学家和机器学习狂热者们, 简化学习旅途这篇指南会让你动手解决机器学习的问题, 并从实践中获得真知我提供的是几个机器学习算法的高水平理解, 以及运行这些算法的 R 和 Python 代码这些应该足以让你亲自试一试了
我特地跳过了这些技术背后的数据, 因为一开始你并不需要理解这些如果你想从数据层面上理解这些算法, 你应该去别处找找但如果你想要在开始一个机器学习项目之前做些准备, 你会喜欢这篇文章的
广义来说, 有三种机器学习算法
1 监督式学习
工作机制: 这个算法由一个目标变量或结果变量 (或因变量) 组成这些变量由已知的一系列预示变量 (自变量) 预测而来利用这一系列变量, 我们生成一个将输入值映射到期望输出值的函数这个训练过程会一直持续, 直到模型在训练数据上获得期望的精确度监督式学习的例子有: 回归决策树随机森林 K 近邻算法逻辑回归等
2 非监督式学习
工作机制: 在这个算法中, 没有任何目标变量或结果变量要预测或估计这个算法用在不同的组内聚类分析这种分析方式被广泛地用来细分客户, 根据干预的方式分为不同的用户组非监督式学习的例子有: 关联算法和 K 均值算法
3 强化学习
工作机制: 这个算法训练机器进行决策它是这样工作的: 机器被放在一个能让它通过反复试错来训练自己的环境中机器从过去的经验中进行学习, 并且尝试利用了解最透彻的知识作出精确的商业判断 强化学习的例子有马尔可夫决策过程
常见机器学习算法名单
这里是一个常用的机器学习算法名单这些算法几乎可以用在所有的数据问题上:
线性回归
逻辑回归
决策树
SVM
朴素贝叶斯
K 最近邻算法
K 均值算法
随机森林算法
降维算法
Gradient Boost 和 Adaboost 算法
1 线性回归
线性回归通常用于根据连续变量估计实际数值 (房价呼叫次数总销售额等) 我们通过拟合最佳直线来建立自变量和因变量的关系这条最佳直线叫做回归线, 并且用 Y= a *X + b 这条线性等式来表示
理解线性回归的最好办法是回顾一下童年假设在不问对方体重的情况下, 让一个五年级的孩子按体重从轻到重的顺序对班上的同学排序, 你觉得这个孩子会怎么做? 他 (她) 很可能会目测人们的身高和体型, 综合这些可见的参数来排列他们这是现实生活中使用线性回归的例子实际上, 这个孩子发现了身高和体型与体重有一定的关系, 这个关系看起来很像上面的等式
在这个等式中:
Y: 因变量
a: 斜率
x: 自变量
b : 截距
系数 a 和 b 可以通过最小二乘法获得
参见下例我们找出最佳拟合直线 y=0.2811x+13.9 已知人的身高, 我们可以通过这条等式求出体重
线性回归的两种主要类型是一元线性回归和多元线性回归一元线性回归的特点是只有一个自变量多元线性回归的特点正如其名, 存在多个自变量找最佳拟合直线的时候, 你可以拟合到多项或者曲线回归这些就被叫做多项或曲线回归
Python 代码
- #Import Library
- #Import other necessary libraries like pandas, numpy...
- from sklearn import linear_model
- #Load Train and Test datasets
- #Identify feature and response variable(s) and values must be numeric and numpy arrays
- x_train=input_variables_values_training_datasets
- y_train=target_variables_values_training_datasets
- x_test=input_variables_values_test_datasets
- # Create linear regression object
- linear = linear_model.LinearRegression()
- # Train the model using the training sets and check score
- linear.fit(x_train, y_train)
- linear.score(x_train, y_train)
- #Equation coefficient and Intercept
- print('Coefficient: n', linear.coef_)
- print('Intercept: n', linear.intercept_)
- #Predict Output
- predicted= linear.predict(x_test)
R 代码
- #Load Train and Test datasets
- #Identify feature and response variable(s) and values must be numeric and numpy arrays
- x_train <- input_variables_values_training_datasets
- y_train <- target_variables_values_training_datasets
- x_test <- input_variables_values_test_datasets
- x <- cbind(x_train,y_train)
- # Train the model using the training sets and check score
- linear <- lm(y_train ~ ., data = x)
- summary(linear)
- #Predict Output
- predicted= predict(linear,x_test)
2 逻辑回归
别被它的名字迷惑了! 这是一个分类算法而不是一个回归算法该算法可根据已知的一系列因变量估计离散数值 (比方说二进制数值 0 或 1 , 是或否, 真或假) 简单来说, 它通过将数据拟合进一个逻辑函数来预估一个事件出现的概率因此, 它也被叫做逻辑回归因为它预估的是概率, 所以它的输出值大小在 0 和 1 之间(正如所预计的一样)
让我们再次通过一个简单的例子来理解这个算法
假设你的朋友让你解开一个谜题这只会有两个结果: 你解开了或是你没有解开想象你要解答很多道题来找出你所擅长的主题这个研究的结果就会像是这样: 假设题目是一道十年级的三角函数题, 你有 70% 的可能会解开这道题然而, 若题目是个五年级的历史题, 你只有 30% 的可能性回答正确这就是逻辑回归能提供给你的信息
从数学上看, 在结果中, 几率的对数使用的是预测变量的线性组合模型
- odds= p/ (1-p) = probability of event occurrence / probability of not event occurrence
- ln(odds) = ln(p/(1-p))
- logit(p) = ln(p/(1-p)) = b0+b1X1+b2X2+b3X3....+bkXk
在上面的式子里, p 是我们感兴趣的特征出现的概率它选用使观察样本值的可能性最大化的值作为参数, 而不是通过计算误差平方和的最小值(就如一般的回归分析用到的一样)
现在你也许要问了, 为什么我们要求出对数呢? 简而言之, 这种方法是复制一个阶梯函数的最佳方法之一我本可以更详细地讲述, 但那就违背本篇指南的主旨了
Python 代码
- #Import Library
- from sklearn.linear_model import LogisticRegression
- #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create logistic regression object
- model = LogisticRegression()
- # Train the model using the training sets and check score
- model.fit(X, y)
- model.score(X, y)
- #Equation coefficient and Intercept
- print('Coefficient: n', model.coef_)
- print('Intercept: n', model.intercept_)
- #Predict Output
- predicted= model.predict(x_test)
R 代码
- x <- cbind(x_train,y_train)
- # Train the model using the training sets and check score
- logistic <- glm(y_train ~ ., data = x,family='binomial')
- summary(logistic)
- #Predict Output
- predicted= predict(logistic,x_test)
更进一步:
你可以尝试更多的方法来改进这个模型:
加入交互项
精简模型特性
使用正则化方法
使用非线性模型
3 决策树
这是我最喜爱也是最频繁使用的算法之一这个监督式学习算法通常被用于分类问题令人惊奇的是, 它同时适用于分类变量和连续因变量在这个算法中, 我们将总体分成两个或更多的同类群这是根据最重要的属性或者自变量来分成尽可能不同的组别想要知道更多, 可以阅读: 简化决策树
- #Import Library
- #Import other necessary libraries like pandas, numpy...
- from sklearn import tree
- #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create tree object
- model = tree.DecisionTreeClassifier(criterion='gini') # for classification, here you can change the algorithm as gini or entropy (information gain) by default it is gini
- # model = tree.DecisionTreeRegressor() for regression
- # Train the model using the training sets and check score
- model.fit(X, y)
- model.score(X, y)
- #Predict Output
- predicted= model.predict(x_test)
- library(rpart)
- x <- cbind(x_train,y_train)
- # grow tree
- fit <- rpart(y_train ~ ., data = x,method="class")
- summary(fit)
- #Predict Output
- predicted= predict(fit,x_test)
- #Import Library
- from sklearn import svm
- #Assumed you have, X (predic
- tor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create SVM classification object
- model = svm.svc() # there is various option associated with it, this is simple for classification. You can refer link, for mo# re detail.
- # Train the model using the training sets and check score
- model.fit(X, y)
- model.score(X, y)
- #Predict Output
- predicted= model.predict(x_test)
- library(e1071)
- x <- cbind(x_train,y_train)
- # Fitting model
- fit <-svm(y_train ~ ., data = x)
- summary(fit)
- #Predict Output
- predicted= predict(fit,x_test)
- #Import Library
- from sklearn.naive_bayes import GaussianNB
- #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create SVM classification object model = GaussianNB() # there is other distribution for multinomial classes like Bernoulli Naive Bayes, Refer link
- # Train the model using the training sets and check score
- model.fit(X, y)
- #Predict Output
- predicted= model.predict(x_test)
- library(e1071)
- x <- cbind(x_train,y_train)
- # Fitting model
- fit <-naiveBayes(y_train ~ ., data = x)
- summary(fit)
- #Predict Output
- predicted= predict(fit,x_test)
- #Import Library
- from sklearn.neighbors import KNeighborsClassifier
- #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create KNeighbors classifier object model
- KNeighborsClassifier(n_neighbors=6) # default value for n_neighbors is 5
- # Train the model using the training sets and check score
- model.fit(X, y)
- #Predict Output
- predicted= model.predict(x_test)
- library(knn)
- x <- cbind(x_train,y_train)
- # Fitting model
- fit <-knn(y_train ~ ., data = x,k=5)
- summary(fit)
- #Predict Output
- predicted= predict(fit,x_test)
- #Import Library
- from sklearn.cluster import KMeans
- #Assumed you have, X (attributes) for training data set and x_test(attributes) of test_dataset
- # Create KNeighbors classifier object model
- k_means = KMeans(n_clusters=3, random_state=0)
- # Train the model using the training sets and check score
- model.fit(X)
- #Predict Output
- predicted= model.predict(x_test)
- library(cluster)
- fit <- kmeans(X, 3) # 5 cluster solution
- Python
- #Import Library
- from sklearn.ensemble import RandomForestClassifier
- #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create Random Forest object
- model= RandomForestClassifier()
- # Train the model using the training sets and check score
- model.fit(X, y)
- #Predict Output
- predicted= model.predict(x_test)
- library(randomForest)
- x <- cbind(x_train,y_train)
- # Fitting model
- fit <- randomForest(Species ~ ., x,ntree=500)
- summary(fit)
- #Predict Output
- predicted= predict(fit,x_test)
- #Import Library
- from sklearn import decomposition
- #Assumed you have training and test data set as train and test
- # Create PCA obeject pca= decomposition.PCA(n_components=k) #default value of k =min(n_sample, n_features)
- # For Factor analysis
- #fa= decomposition.FactorAnalysis()
- # Reduced the dimension of training dataset using PCA
- train_reduced = pca.fit_transform(train)
- #Reduced the dimension of test dataset
- test_reduced = pca.transform(test)
- #For more detail on this, please refer this link.
- R Code
- library(stats)
- pca <- princomp(train, cor = TRUE)
- train_reduced <- predict(pca,train)
- test_reduced <- predict(pca,test)
- #Import Library
- from sklearn.ensemble import GradientBoostingClassifier
- #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
- # Create Gradient Boosting Classifier object
- model= GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
- # Train the model using the training sets and check score
- model.fit(X, y)
- #Predict Output
- predicted= model.predict(x_test)
- library(caret)
- x <- cbind(x_train,y_train)
- # Fitting model
- fitControl <- trainControl( method = "repeatedcv", number = 4, repeats = 4)
- fit <- train(y ~ ., data = x, method = "gbm", trControl = fitControl,verbose = FALSE)
- predicted= predict(fit,x_test,type= "prob")[,2]
来源: http://www.tuicool.com/articles/JbYjQru