6 ~5 `$ z3 l' s' r/ V, N% Z我们可以假想一个场景来理解线性回归.比如你让一个五年级的孩子在不问同学具体体重多少的情况下,把班上的同学按照体重从轻到重排队。这个孩子会怎么做呢?他有可能会通过观察大家的身高和体格来排队。这就是线性回归!这个孩子其实是认为身高和体格与人的体重有某种相关。而这个关系就像是前一段的Y和X的关系。' l/ j( n: N, Y
/ k) M' c' K2 X: Y2 s% m+ H1 |给大家画一个图,方便理解,下图用的线性回归方程是Y=0.28x+13.9.通过这个方程,就可以根据一个人的身高预测他的体重信息.6 C; ^5 H& K! Z! U/ U. ^2 Q" R% F
- |% ^# y' V0 n' S$ [4 A, p3 [4 W# K; K
- u: v4 l* K% i% ~% O
线性回归还分为:一元线性回归和多元线性回归.很明显一元只有一个自变量,多元有多个自变量. 5 R9 m/ x# A9 C$ h1 j* Z$ ^# d6 }& z# L
拟合多元线性回归的时候,可以利用多项式回归或曲线回归5 e, q/ w6 E r7 d5 q# U) H* J* {2 @
8 B3 X; F$ R% @: o( F% f- R9 J
Import Library$ ^3 b6 C4 s0 j
from sklearn import linear_model . V/ `9 Z6 x9 M0 J$ k4 | 5 z, t [& y' U4 k6 f4 j Wx_train=input_variables_values_training_datasets, }! K0 t6 ]7 |: ?& F9 P' @
y_train=target_variables_values_training_datasets , E7 f& k8 l3 L/ y; Gx_test=input_variables_values_test_datasets 1 A* c+ i1 z8 z( B - m k# h+ K! k( Z& c# Create linear regression object5 ]$ T" _5 @% U- ^! H$ v3 L1 P
linear = linear_model.LinearRegression() ) E8 B& o. Z* a, m% t) ]: W' a. [+ O; |% m7 ^
# Train the model using the training sets and check score % E( F: k( ^" z9 o( W4 @linear.fit(x_train, y_train) ; M' _9 |0 Q+ s! {linear.score(x_train, y_train) ! q' r; {, c5 w. _2 D5 q* K ; j# d$ ^+ P2 J# u0 B9 O#Equation coefficient and Intercept $ `1 `4 E; ^8 E! u3 ~- u8 p: }print('Coefficient: \n', linear.coef_) ' e2 R' H. n3 t) x( y) K: gprint('Intercept: \n', linear.intercept_)8 H7 ]& T, M3 M/ ]% Q
$ N. N/ D0 X+ b3 k5 y- modds= p/ (1-p) = probability of event occurrence / probability of not event occurrence/ t. @: Y/ J4 k5 `4 @
+ K% ~4 x" f% s) D/ ?* \- N
ln(odds) = ln(p/(1-p)) 9 H+ e" h3 g" a+ n. I % t- T* |5 b+ E3 elogit(p) = ln(p/(1-p)) = b0+b1X1+b2X2+b3X3....+bkXk 4 q: B' E1 C! }' T9 | O8 s3 J在这里,p是我们感兴趣的事件出现的概率.他通过筛选出特定参数值使得观察到的样本值出现的概率最大化,来估计参数,而不是像普通回归那样最小化误差的平方和.- x: K4 T! H6 i+ q# W/ S! M, I
- ]' X3 L; l- { Z9 S* X% S- U7 E至于有的人会问,为什么需要做对数呢?简单来说这是重复阶梯函数的最佳方法. 6 T. J' ]9 @( A0 {" R6 @ 2 H) [" l* W( |" E) \4 E: g5 {2 Y2 @4 \% G( Q" [* V3 e1 Z3 V( d
) q: V* x! \. \: O
from sklearn.linear_model import LogisticRegression 2 i( v/ _+ _& u7 B7 M % W' Z& z+ e+ ]( _) u/ n& c# V" ]; C( Z, x model = LogisticRegression()* \# H6 B6 o. p7 g ?2 D* |( K6 W
! {& s& x& L' }' E1 k7 w9 Z
# Train the model using the training sets and check score0 p; E6 W# D k7 u) C
model.fit(X, y) 1 U0 `6 G0 q8 V( n8 o3 V model.score(X, y) 9 r1 o. B/ u8 i( S" D) N. K$ T( J4 X# ~8 w- L
#Equation coefficient and Intercept 1 F# A2 g- a3 g1 l print('Coefficient: \n', model.coef_); o! s0 j7 T: s" u( W8 F# Z2 ]( r
print('Intercept: \n', model.intercept_). M& A7 ^$ T' w8 f# b
9 J6 J1 {% P+ F$ Q
#Predict Output 0 S# Z) {3 S6 M2 X5 X9 M predicted= model.predict(x_test) & R7 l* ~% I [& U# v6 }逻辑回归的优化: * p6 }- P( I( E6 e加入交互项 ! @* q6 W* F/ w . X; u6 s5 _7 F' q( n- @ 减少特征变量 : ~+ B! @3 x: }0 Y/ v ; K/ Q2 O r1 j& K3 o$ S) v 正则化6 C0 {+ ^* e. H/ O {+ G; A. L0 ~
, i6 ]5 o) d$ \/ D 使用非线性模型" u; T5 Y2 g6 l6 _
) B6 V0 | K" @3 |' W
3.决策树) D" T2 M( @9 F4 f/ G
这是我最喜欢也是能经常使用到的算法。它属于监督式学习,常用来解决分类问题。令人惊讶的是,它既可以运用于类别变量(categorical variables)也可以作用于连续变量。这个算法可以让我们把一个总体分为两个或多个群组。分组根据能够区分总体的最重要的特征变量/自变量进行。: @1 p% b, T4 g
7 a2 J4 p: T# v" J " I" r4 B: H$ ?+ M/ a, P! a# | N# ^$ y9 y, h! D" d: K- R) U
从上图中我们可以看出,总体人群最终在玩与否的事件上被分成了四个群组。而分组是依据一些特征变量实现的。用来分组的具体指标有很多,比如Gini,information Gain, Chi-square,entropy。 B1 q, D, Z4 d6 {8 c: R( |* c. b2 ?) O7 l5 ` m) c
4 S; {6 V& S6 o7 P
from sklearn import tree % D; ]4 G5 L3 W, a/ x# L0 }; a; c' e+ g: I
L4 K- s" t, m% |; w
# Create tree object 6 f1 y1 r# A$ l U( M* T7 `1 qmodel = tree.DecisionTreeClassifier(criterion='gini') # for classification, here you can change the algorithm as gini or entropy (information gain) by default it is gini ( H6 f( x, I) u* A# p" F r2 E' j5 b
# model = tree.DecisionTreeRegressor() for regression, c+ k! q- _" x) u/ q# r8 ?
# G1 C% S- ?" [) M( X, k1 n# Train the model using the training sets and check score : u- J- n" i0 D7 qmodel.fit(X, y)' F- G b% l9 A3 c+ t& z
model.score(X, y) y9 g# A7 j# E2 u6 h1 }9 ]% y& T6 W0 ]' D6 I8 q, n
#Predict Output( L2 \( `, I5 o, k3 B- [- e( ^
predicted= model.predict(x_test)) F. N4 U2 j/ O8 e2 C. K
4. 支持向量机(SVM) ( ~) O5 o* ?. Y3 b. }" ~这是一个分类算法。在这个算法中我们将每一个数据作为一个点在一个n维空间上作图(n是特征数),每一个特征值就代表对应坐标值的大小。比如说我们有两个特征:一个人的身高和发长。我们可以将这两个变量在一个二维空间上作图,图上的每个点都有两个坐标值(这些坐标轴也叫做支持向量)。 3 O5 I: B$ ?8 E) ?/ `: r' X5 E: p, J e1 t
现在我们要在图中找到一条直线能最大程度将不同组的点分开。两组数据中距离这条线最近的点到这条线的距离都应该是最远的。" x& _: L' U% i4 B; y9 }& ?& q
0 b6 M+ [5 x" j. q' s# y
: I$ t# L/ F* h& ?8 U9 v
" J/ A# i9 W5 u* y
在上图中,黑色的线就是最佳分割线。因为这条线到两组中距它最近的点,点A和B的距离都是最远的。任何其他线必然会使得到其中一个点的距离比这个距离近。这样根据数据点分布在这条线的哪一边,我们就可以将数据归类。 0 N+ c: D$ p9 \2 B& Z( D 0 ?' B: \4 B$ p#Import Library/ v$ R6 X% Y7 q
from sklearn import svm* r( f# c3 o" `' [1 h+ h
#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset 5 I7 I5 Q' f- [! H- c/ E0 _2 Z# Create SVM classification object % l) I1 e0 H8 E8 }$ H7 R
# r7 n% V: e I% i5 a @model = svm.svc() # there is various option associated with it, this is simple for classification. You can refer link, for mo# re detail. Q/ B" f6 Z- d0 ~/ s/ Y8 B9 u" [# S' @$ e9 K1 m; s) k) v
# Train the model using the training sets and check score 0 i4 a3 x, \0 |0 ~/ P- p) Ymodel.fit(X, y)8 I6 Y. k. ?2 c
model.score(X, y)0 b! b; b2 ~ G7 `# U
! z6 O1 g& p( S: C/ m3 s. y那么,P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60>0.5,说明这个概率值更大。 + A, Z+ D* g9 R8 j9 l6 e. [8 h R, H6 d$ U6 A+ o B
当有多种类别和多种特征时,预测的方法相似。朴素贝叶斯通常用于文本分类和多类别分类问题。 ) T( I- G; t w6 |3 |" j- o : v7 w6 _ H7 C, a; n/ f#Import Library 9 ], N% `2 N1 n; Afrom sklearn.naive_bayes import GaussianNB9 T _, U7 s/ \2 f$ `; b* ~
#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset5 \1 t4 D+ B, i
: S6 ], h' r$ a
# Create SVM classification object model = GaussianNB() # there is other distribution for multinomial classes like Bernoulli Naive Bayes, Refer link( L% o/ C0 D9 v. P
% V' [; d2 @' b1 R% j# Train the model using the training sets and check score x$ }9 | x6 z( o
model.fit(X, y) ' {8 k% G& [; Z) U2 U/ d6 E5 N: b& V8 m! L* K m
#Predict Output d& L2 P/ x$ O' o
predicted= model.predict(x_test) I) `/ W. n4 r& f& P7 }; i
6.KNN(K-邻近算法)8 O$ e2 c# I2 F% [$ _
这个算法既可以解决分类问题,也可以用于回归问题,但工业上用于分类的情况更多。 KNN先记录所有已知数据,再利用一个距离函数,找出已知数据中距离未知事件最近的K组数据,最后按照这K组数据里最常见的类别预测该事件。 2 D% m7 [: X1 i ? + Q* l( \, g; K1 V0 Q2 ]距离函数可以是欧式距离,曼哈顿距离,闵氏距离 (Minkowski Distance), 和汉明距离(Hamming Distance)。前三种用于连续变量,汉明距离用于分类变量。如果K=1,那问题就简化为根据最近的数据分类。K值的选取时常是KNN建模里的关键。) _* @6 J a4 d" A
- x/ x8 j# u" f$ J2 x 8 k T8 n R' w" {4 p- z( j% A2 q- o" M
KNN在生活中的运用很多。比如,如果你想了解一个不认识的人,你可能就会从这个人的好朋友和圈子中了解他的信息。* v* M! r: U; U
2 J4 H" K4 D* a如果有M个特征变量,那么选取数m << M,从而在每个节点上随机选取m个特征变量来分割该节点。m在整个森林养成中保持不变。 4 G) I! m/ m6 o: \6 ~3 W) P* c0 `) @, k4 ~+ e
每个决策树都最大程度上进行分割,没有剪枝。 0 J! \+ z8 e" G) q* f2 a ( b C, K3 |9 Q: ^, [#Import Library$ B. g: g7 V1 C4 P' {5 }. k1 I' t5 u
from sklearn.ensemble import RandomForestClassifier ; i2 E0 l7 U4 I n1 y* N#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset2 S f9 T l4 M( e+ E! }
* H$ x, e" u7 j1 k T# Create Random Forest object 8 p: m! E6 `# [9 I, A. N3 P! smodel= RandomForestClassifier()7 d+ i7 P- ~1 i
9 C* ^/ R3 |, i; T6 y# Y
# Train the model using the training sets and check score2 s( r. Z, }) M* d- v/ C
model.fit(X, y) $ W r. ^) z! k ) y w/ B0 @3 b' F, A( H) a9 g#Predict Output0 @4 b( I t. Y$ H8 n6 V
predicted= model.predict(x_test) a% p, p; K4 W5 |& ]2 v9.降维算法(Dimensionality Reduction Algorithms) 4 w: d9 B- V& K* b1 b! y+ P在过去的4-5年里,可获取的数据几乎以指数形式增长。公司/政府机构/研究组织不仅有了更多的数据来源,也获得了更多维度的数据信息。: r- p3 g& `; N5 ]$ d
3 H9 k* \ r) z& I
例如:电子商务公司有了顾客更多的细节信息,像个人信息,网络浏览历史,个人喜恶,购买记录,反馈信息等,他们关注你的私人特征,比你天天去的超市里的店员更了解你。 d$ c8 T" Z% o, z A' V
4 V, z, I" \7 G" t! ?
作为一名数据科学家,我们手上的数据有非常多的特征。虽然这听起来有利于建立更强大精准的模型,但它们有时候反倒也是建模中的一大难题。怎样才能从1000或2000个变量里找到最重要的变量呢?这种情况下降维算法及其他算法,如决策树,随机森林,PCA,因子分析,相关矩阵,和缺省值比例等,就能帮我们解决难题。) S/ q) {- Z" G5 I" K! J
6 T5 G" D* p1 n, H( P# v/ @6 `2 e0 E0 k' q, l, _* H
#Import Library 1 U% a# a& @: m: F; xfrom sklearn import decomposition % J4 [! K# R; O; [8 R* N#Assumed you have training and test data set as train and test9 f' Q/ n' N! `3 F+ s, A
# Create PCA obeject pca= decomposition.PCA(n_components=k) #default value of k =min(n_sample, n_features) * v! w/ o/ r; |! B6 |7 C# For Factor analysis- A! H) i& y8 U U8 @
#fa= decomposition.FactorAnalysis() 5 g6 p! h) T# ^8 J# Reduced the dimension of training dataset using PCA & [6 |& k6 Z: w% J/ [* a+ H0 g2 G6 q* g4 L" p7 ~3 M/ e7 }
train_reduced = pca.fit_transform(train)) ~; B0 k+ G2 ~, i8 `
/ z& l2 i, j9 S1 x1 [#Reduced the dimension of test dataset* [; N1 B h- t9 h
test_reduced = pca.transform(test)' b) O* E) D6 N- l% i7 J! h
10.Gradient Boosing 和 AdaBoost 2 z2 l8 R3 o7 u( O) PGBM和AdaBoost都是在有大量数据时提高预测准确度的boosting算法。Boosting是一种集成学习方法。它通过有序结合多个较弱的分类器/估测器的估计结果来提高预测准确度。这些boosting算法在Kaggle,AV Hackthon, CrowdAnalytix等数据科学竞赛中有出色发挥。 " Y2 p* o- h$ e8 V# k7 J4 G$ D ! T+ S* q# D( A. r1 W* T1 v#Import Library + p& Q A( X2 c5 @from sklearn.ensemble import GradientBoostingClassifier ( z& r$ M1 W4 u- U& X#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset : w. _$ Y/ X& U& _3 g* h# Create Gradient Boosting Classifier object5 }. g! ^) V7 L% p
model= GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0) & F& b B$ V4 G. h % j9 M7 n9 i! n# Y# Train the model using the training sets and check score 9 C( E; R$ r% m+ ]% vmodel.fit(X, y) f @5 _1 K& q2 J K5 x
#Predict Output # s" `, P" i+ F. {2 Bpredicted= model.predict(x_test) % f" {5 I G, G0 W% ~- @8 fGradientBoostingClassifier 和随机森林是两种不同的boosting分类树。人们经常提问 这两个算法有什么不同。- E& X- q k: F; a8 j
9 d% P( E% _4 Q0 q) f! O& R原文链接:http://blog.csdn.net/han_xiaoyang/article/details/51191386 & v0 G1 s N' b" K———————————————— , `, `# C1 W/ T- L. z, e版权声明:本文为CSDN博主「_小羊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 4 T- ^0 c/ X$ j/ c$ u6 k! B- O* p原文链接:https://blog.csdn.net/qq_39303465/article/details/79176075 - G. _# j: N, k" f+ W8 a+ S G$ L4 D) ^/ g% ~: D