. n3 ?# M0 @% @* Z; D$ i2 p! J A8 \- t' i7 s' k/ k( D# Create tree object , n7 B( e9 U) J/ v- t/ Dmodel = tree.DecisionTreeClassifier(criterion='gini') # for classification, here you can change the algorithm as gini or entropy (information gain) by default it is gini 6 I7 u" R2 U. @! V8 C
# a; E7 S0 t' J5 ~' b# model = tree.DecisionTreeRegressor() for regression * G4 ~& K, M. {' \0 J ; C8 m" E7 k# h# m5 J& C( A# Train the model using the training sets and check score5 u' O7 u+ {0 `
model.fit(X, y); P0 B$ O6 ]3 |
model.score(X, y) / c* w3 d& }2 D5 n6 o $ o; H# S0 R; i' m9 F0 `8 e$ J#Predict Output ! d/ W: j. U) K T" [, ~8 O3 Bpredicted= model.predict(x_test)2 |/ ~. c; j5 V7 S- R& X+ } r& T k
4. 支持向量机(SVM)7 \' V5 `/ i& h3 r. k) ^9 K! F
这是一个分类算法。在这个算法中我们将每一个数据作为一个点在一个n维空间上作图(n是特征数),每一个特征值就代表对应坐标值的大小。比如说我们有两个特征:一个人的身高和发长。我们可以将这两个变量在一个二维空间上作图,图上的每个点都有两个坐标值(这些坐标轴也叫做支持向量)。 " X$ }8 h5 Q. p4 O9 j' b4 r* S1 f S, }4 |' z& {
现在我们要在图中找到一条直线能最大程度将不同组的点分开。两组数据中距离这条线最近的点到这条线的距离都应该是最远的。 , a% o2 S1 n) H, Z- [ G. c4 c. P6 U5 K: v
- U6 l% O+ w6 D. ~0 c4 \; j' t- t3 \
在上图中,黑色的线就是最佳分割线。因为这条线到两组中距它最近的点,点A和B的距离都是最远的。任何其他线必然会使得到其中一个点的距离比这个距离近。这样根据数据点分布在这条线的哪一边,我们就可以将数据归类。1 o/ U% `8 ]% t, Y/ y
" d4 l5 E5 q5 Y
#Import Library8 M$ z% T9 O( R; w( x
from sklearn import svm ( j, d: }1 X( F/ q#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset9 U% }3 P- G5 o, |
# Create SVM classification object 6 ]5 F6 K2 I% x: F% j
% u, X9 c: i4 ]# o, H' g; Qmodel = svm.svc() # there is various option associated with it, this is simple for classification. You can refer link, for mo# re detail.& [; S+ | E+ O `. E
% \' {. {4 M' h+ R+ S
# Train the model using the training sets and check score + P! g+ ]# e3 _8 Q7 Zmodel.fit(X, y)& G7 T. a0 Y+ d& ^7 A& _3 \6 R
model.score(X, y), Q) M. W8 u" @! y, E) F. G, V/ Z
- E3 b" r5 |7 V5 V
#Predict Output ; u% b8 Z' Y& Y4 Ypredicted= model.predict(x_test) S; {7 M+ k0 S# _3 t/ S5. 朴素贝叶斯 ' [2 v" c0 b) M! O! J7 \. {这个算法是建立在贝叶斯理论上的分类方法。它的假设条件是自变量之间相互独立。简言之,朴素贝叶斯假定某一特征的出现与其它特征无关。比如说,如果一个水果它是红色的,圆状的,直径大概7cm左右,我们可能猜测它为苹果。即使这些特征之间存在一定关系,在朴素贝叶斯算法中我们都认为红色,圆状和直径在判断一个水果是苹果的可能性上是相互独立的。, S8 o* S1 h0 E, F2 N2 N0 A
: `9 g! y2 Z* z4 K$ j 0 o. A: G2 e4 t5 A怎样确定K的值:5 r4 w7 q9 O" ?) N0 a# f! X! r
/ N( |* c% e/ U
如果我们在每个集群中计算集群中所有点到质心的距离平方和,再将不同集群的距离平方和相加,我们就得到了这个集群方案的总平方和。% D3 ]6 J+ v0 _ }$ p; h
. ~- b! |+ k6 z9 Y% P7 M
我们知道,随着集群数量的增加,总平方和会减少。但是如果用总平方和对K作图,你会发现在某个K值之前总平方和急速减少,但在这个K值之后减少的幅度大大降低,这个值就是最佳的集群数。 & _: K9 H9 y* I3 A 9 U, ^$ g% `0 L; I$ u% z* d8 f" N. _9 n, R' B9 p. J6 y
#Import Library & D" K0 [, `% Q- J, Sfrom sklearn.cluster import KMeans4 N- w$ s: f7 i& D. t. p! R
h1 x+ p9 i; y, \& H) Z#Assumed you have, X (attributes) for training data set and x_test(attributes) of test_dataset9 t; L2 r0 D% I; j8 [/ R( k
# Create KNeighbors classifier object model * ]6 j, J5 D+ l5 D( Q, ^1 \
k_means = KMeans(n_clusters=3, random_state=0)- @5 o% G" @+ ]" K
( D) j2 X% d* P9 T# Train the model using the training sets and check score+ Z. a( Y# [, E4 z
model.fit(X) 2 P* c& y, I/ V, ^ 8 v5 x' o1 V, ?: o#Predict Output 7 ?' {0 x- L$ O' C. w) }6 Y5 q mpredicted= model.predict(x_test)5 Z, H! _4 b! Y1 l- m/ y8 |
8.随机森林# V; ^& f H* C
随机森林是对决策树集合的特有名称。随机森林里我们有多个决策树(所以叫“森林”)。为了给一个新的观察值分类,根据它的特征,每一个决策树都会给出一个分类。随机森林算法选出投票最多的分类作为分类结果。+ \2 D, _* P2 B; I0 z7 w
2 n1 H+ ^/ W2 N& M7 T4 v' r怎样生成决策树:) I8 H P$ y# j7 _/ k, D
2 D) u& f& R; C每个决策树都最大程度上进行分割,没有剪枝。3 e' J4 u- r" \; ^; [- p8 @
4 t7 }8 l0 \$ P C* L# [7 ^
#Import Library7 [3 D, W" V4 o) m/ ?% L
from sklearn.ensemble import RandomForestClassifier' T" p5 o" ]- t% t0 C
#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset % o0 x: I. ^- D, h: d% U% Y9 {8 I% d- Q- [$ J& \6 u. Y
# Create Random Forest object6 ^- V% [" k& x0 G# j9 H1 N
model= RandomForestClassifier()8 _, h- g/ D N2 @9 ?- C
- t! I7 c+ d7 {2 a8 o# o+ ]# Train the model using the training sets and check score 8 N; t5 J2 _! a/ qmodel.fit(X, y) " c4 f( D f8 u4 z% E; x3 j8 o+ ^( z
#Predict Output & l' L9 j0 _8 V2 lpredicted= model.predict(x_test) % I3 ~: ] Y" Z5 x8 p) p6 f5 V9.降维算法(Dimensionality Reduction Algorithms)2 I. ]# P* H% ]4 b4 y, O
在过去的4-5年里,可获取的数据几乎以指数形式增长。公司/政府机构/研究组织不仅有了更多的数据来源,也获得了更多维度的数据信息。9 j7 n2 q C: g& p8 J( P c
& h6 w" P ~$ G- S3 O' X$ K" G例如:电子商务公司有了顾客更多的细节信息,像个人信息,网络浏览历史,个人喜恶,购买记录,反馈信息等,他们关注你的私人特征,比你天天去的超市里的店员更了解你。 - s; W4 U% x/ t) R { 3 l. k7 t1 R6 t4 A( x7 y% W) J作为一名数据科学家,我们手上的数据有非常多的特征。虽然这听起来有利于建立更强大精准的模型,但它们有时候反倒也是建模中的一大难题。怎样才能从1000或2000个变量里找到最重要的变量呢?这种情况下降维算法及其他算法,如决策树,随机森林,PCA,因子分析,相关矩阵,和缺省值比例等,就能帮我们解决难题。/ q. O1 U1 r0 l j& A8 _
+ Q0 L5 V/ F: Z, x5 | W" d$ x" ~% [" ?% e* q, P; ~% ~' f
#Import Library , [0 P/ ~( {2 `- C5 dfrom sklearn import decomposition, {3 s- ^9 w" n/ b( v! P% H
#Assumed you have training and test data set as train and test9 e4 d6 T0 w5 n* `/ g; X
# Create PCA obeject pca= decomposition.PCA(n_components=k) #default value of k =min(n_sample, n_features)4 V1 k8 r% `+ m) g
# For Factor analysis & ]9 I9 V9 R" q5 Y#fa= decomposition.FactorAnalysis() 4 k( ~, C* o/ X1 J- c' K* O& K# Reduced the dimension of training dataset using PCA( J9 z% L" u3 r5 a% q! s
8 N2 d" b/ Y" G
train_reduced = pca.fit_transform(train) - V! ~. T0 S2 U- z9 ] }: o. M: D; ~9 r0 {# M( C
#Reduced the dimension of test dataset& U9 Z$ m0 C, Q1 n
test_reduced = pca.transform(test) , k: q6 W$ L" S# i% I( \10.Gradient Boosing 和 AdaBoost& f. y; a2 `% {
GBM和AdaBoost都是在有大量数据时提高预测准确度的boosting算法。Boosting是一种集成学习方法。它通过有序结合多个较弱的分类器/估测器的估计结果来提高预测准确度。这些boosting算法在Kaggle,AV Hackthon, CrowdAnalytix等数据科学竞赛中有出色发挥。4 y% f0 |/ N2 X) I4 m8 x3 Q
$ [% k% x* }/ M2 D/ [0 w
#Import Library3 }! `) o c$ C. l( n1 V8 J3 @
from sklearn.ensemble import GradientBoostingClassifier 5 `# M, n4 U9 M) X: @) X5 _9 q#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset 0 d- k6 p- C7 S; I# Create Gradient Boosting Classifier object0 L4 I. g$ E& _
model= GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)2 F- m p" e5 F- B' {9 t/ ]! e
4 W- _& U/ ^# C# Train the model using the training sets and check score6 Y/ A$ L H( |4 X( |, E7 R( K
model.fit(X, y) / `$ N, y0 `: h+ y( I; ^ Y+ c#Predict Output * Q4 h# ^! C) u- \) spredicted= model.predict(x_test) * U9 _6 ~+ B+ Z/ ^GradientBoostingClassifier 和随机森林是两种不同的boosting分类树。人们经常提问 这两个算法有什么不同。 ' m, v Y4 N6 g/ [1 C# ~. i( h8 `% ]" ?; b8 F! ^/ T( c
原文链接:http://blog.csdn.net/han_xiaoyang/article/details/511913864 f2 f5 V4 R. _7 e" l
————————————————7 K( h0 ~* ~8 y
版权声明:本文为CSDN博主「_小羊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。- ^' o& X4 Z& j
原文链接:https://blog.csdn.net/qq_39303465/article/details/79176075* c4 O' R5 K" V; a; v5 I
7 y9 R- J) V+ R. Q/ ? 0 g% j/ y5 l: d+ t5 n+ d V9 j