机器学习总结

朴素贝叶斯

from sklearn import datasets
iris = datasets.load_iris()
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
y_pred = gnb.fit(iris.data, iris.target).predict(iris.data)

支持向量机

from sklearn import svm
X = [[0, 0], [1, 1]]
y = [0, 1]
clf = svm.SVC()
clf.fit(X, y)  


>>> # get support vectors
>>> clf.support_vectors_
array([[ 0.,  0.],
       [ 1.,  1.]])
>>> # get indices of support vectors
>>> clf.support_ 
array([0, 1]...)
>>> # get number of support vectors for each class
>>> clf.n_support_ 
array([1, 1]...)

决策树

from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)

线性回归的核心代码

from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(ages_train,net_worths_train) # 训练回归直线

reg.coef_可以获取斜率
reg.intercept_可以获取截距
reg.score(target,data)可以获取r平方分数,这个是用来衡量这个拟合程度的一个变量.
这个数值介于0-1之间,如果是接近于1,意味着越好.

文本学习

# 提取词干的操作.
from nltk.stem.snowball import SnowballStemmer
stemmer = SnowballStemmer("english")
for word in word_list:
    words = words + ' ' + stemmer.stem(word)
# 获取词袋的操作.
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
bags_of_words = vectorizer.fit(word_data)
bags_of_words = vectorizer.transform(word_data)
from sklearn.feature_extraction.text import CountVectorizer  
  
#语料  
corpus = [  
    'This is the first document.',  
    'This is the second second document.',  
    'And the third one.',  
    'Is this the first document?',  
]  
#将文本中的词语转换为词频矩阵  
vectorizer = CountVectorizer()  
#计算个词语出现的次数  
X = vectorizer.fit_transform(corpus)  
#获取词袋中所有文本关键词  
word = vectorizer.get_feature_names()  
print word  
#查看词频结果  
print X.toarray()  
  
from sklearn.feature_extraction.text import TfidfTransformer  
  
#类调用  
transformer = TfidfTransformer()  
print transformer  
#将词频矩阵X统计成TF-IDF值  
tfidf = transformer.fit_transform(X)  
#查看数据结构 tfidf[i][j]表示i类文本中的tf-idf权重  
print tfidf.toarray()  
[u'and', u'document', u'first', u'is', u'one', u'second', u'the', u'third', u'this']
[[0 1 1 1 0 0 1 0 1]
 [0 1 0 1 0 2 1 0 1]
 [1 0 0 0 1 0 1 1 0]
 [0 1 1 1 0 0 1 0 1]]
TfidfTransformer(norm=u'l2', smooth_idf=True, sublinear_tf=False,
         use_idf=True)
[[ 0.          0.43877674  0.54197657  0.43877674  0.          0.
   0.35872874  0.          0.43877674]
 [ 0.          0.27230147  0.          0.27230147  0.          0.85322574
   0.22262429  0.          0.27230147]
 [ 0.55280532  0.          0.          0.          0.55280532  0.
   0.28847675  0.55280532  0.        ]
 [ 0.          0.43877674  0.54197657  0.43877674  0.          0.
   0.35872874  0.          0.43877674]]

偏差,方差以及特征选择的状况

高偏差差对训练数据关系很少,是一种过度的简化.
高方差相反.它不能很好的推广到没有见过的情况.(往往会过拟合的状况.)

总结:

image.png

image.png

在回归中使用正则化(lasso回归)

image.png

使用sklearn lasso回归代码

import sklearn.linear_model.Lasso
regression = Lasso()
regression.fit(feature,labels)
regression.predict([2,4])
print regression.ceof_

为什么要使用lasso回归:


image.png

主要成分分析(PCA)

成分转换之后的形式:


image.png

PCA的sklearn代码形式:


image.png

使用PCA成分分析的作用:


image.png

交叉验证

直接从sklearn中导入的情况:

from sklearn.model_selection import train_test_split
iris = datasets.load_iris() # 导入数据
features = iris.data # 特征
labels = iris.target # 标记
features_train, features_test, labels_train, labels_test = train_test_split(features,labels,test_size=0.4,random_state=0) # 直接运用这个方法
clf = SVC(kernel="linear", C=1.)
clf.fit(features_train, labels_train)# 训练
print clf.score(features_test, labels_test) # 测试成绩

一般的步骤:


image.png

交叉验证的代码:


image.png
import numpy as np
from sklearn.model_selection import KFold
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
y = np.array([1, 2, 3, 4])
kf = KFold(n_splits=4) # 这个n_splits是指把整体的数据分为多少个小的子集
print kf.get_n_splits(X)

print list(kf.split(X))
for train_index, test_index in kf.split(X):
   print("TRAIN:", train_index, "TEST:", test_index)
   X_train, X_test = X[train_index], X[test_index]
   y_train, y_test = y[train_index], y[test_index]

>>>
4
[(array([1, 2, 3]), array([0])), (array([0, 2, 3]), array([1])), (array([0, 1, 3]), array([2])), (array([0, 1, 2]), array([3]))]
('TRAIN:', array([1, 2, 3]), 'TEST:', array([0]))
('TRAIN:', array([0, 2, 3]), 'TEST:', array([1]))
('TRAIN:', array([0, 1, 3]), 'TEST:', array([2]))
('TRAIN:', array([0, 1, 2]), 'TEST:', array([3]))

自动调整算法的参数

from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV

iris = datasets.load_iris()
parameters = {'kernel': ('linear', 'rbf'), 'C': [1, 10]}
svc = svm.SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)

print sorted(clf.cv_results_.keys())
print clf.best_params_ # 直接打印最佳的参数

你可能感兴趣的:(机器学习总结)