一、sklearn库介绍
scikit-learn是数据挖掘与分析的简单而有效的工具。 依赖于NumPy, SciPy和matplotlib。它主要包含以下几部分内容:
(1)从功能来分:
(2)从API模块来分:
就我目前的菜鸟级别,感觉经常用到的有clustering, classification(svm, tree, linear regression 等)
二、cluster聚类
阅读sklearn.cluster的API发现主要有两个内容:一个是各种聚类方法的class如cluster.KMeans,另一个是可以直接使用的聚类方法的函数如:
所以实际使用中,对应也有两种方法。
在sklearn.cluster共有9种聚类方法,分别是
Kmeans聚类介绍
采用类构造器,来构造Kmeans聚类器
sklearn.cluster.KMeans(n_clusters=8, #簇的个数,即你想聚成几类 init='k-means++', #初始簇中心的获取方法 n_init=10, #获取初始簇中心的更迭次数 max_iter=300, #最大迭代次数 tol=0.0001, #容忍度,即kmeans运行准则收敛的条件 precompute_distances='auto', #是否需要提前计算距离 verbose=0, #冗长模式 random_state=None, #随机生成簇中心的状态条件 copy_x=True, #True即复制了就不会修改原数据 n_jobs=1, #并行设置 algorithm='auto' #kmeans算法:'auto', 'full', 'elkan',其中 'full'表示用EM方式 )
虽然有很多参数,但是都已经给出了默认值。所以我们一般不需要去传入这些参数,参数的。可以根据实际需要来调用。下面给一个简单的例子:
import numpy as np from sklearn.cluster import KMeans data = np.random.rand(100, 3) #生成一个随机数据,样本大小为100, 特征数为3
#假如我要构造一个聚类数为3的聚类器 estimator = KMeans(n_clusters=3) #构造聚类器 estimator.fit(data) #对样本数据进行聚类计算
estimator.fit_predict(data) #输出样本数据的聚类结果
estimator.fit_transform(data) #Compute clustering and transform X to cluster-distance space
estimator.predict(data) #Predict the closest cluster each sample in X belongs to.
estimator.score(data) #Opposite of the value of X on the K-means objective.
estimator.transform(data) #Transform X to a cluster-distance space. label_pred = estimator.labels_ #获取聚类标签,即聚类结果 centroids = estimator.cluster_centers_ #获取聚类中心 inertia = estimator.inertia_ # 获取所有样本至聚类中心的距离总和
直接采用kmeans函数:
import numpy as np from sklearn import cluster data = np.random.rand(100, 3) #生成一个随机数据,样本大小为100, 特征数为3 k = 3 # 假如我要聚类为3个clusters [centroid, label, inertia] = cluster.k_means(data, k) #分别返回聚类中心点,样本聚类结果,样本至聚类中心的距离总和
三、classification分类
分类是数据挖掘或者机器学习中最重要的一个部分。不过由于经典的分类方法机制比较特性化,所以好像sklearn并没有特别定制一个分类器这样的class。
常用的分类方法有:
1.KNN最近邻案例(主要是Nearest Neighbors Classification)
from sklearn import neighbors, datasets # import some data to play with iris = datasets.load_iris() n_neighbors = 15 X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target weights = 'distance' # also set as 'uniform' clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights) clf.fit(X, y) # if you have test data, just predict with the following functions # for example, xx, yy is constructed test data x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Z is the label_pred
2.svm案例
from sklearn import svm X = [[0, 0], [1, 1]] y = [0, 1] #建立支持向量分类模型 clf = svm.SVC() #拟合训练数据,得到训练模型参数 clf.fit(X, y) #对测试点[2., 2.], [3., 3.]预测 res = clf.predict([[2., 2.],[3., 3.]]) #输出预测结果值 print(res) #get support vectors print("support vectors:", clf.support_vectors_) #get indices of support vectors print("indices of support vectors:", clf.support_) #get number of support vectors for each class print("number of support vectors for each class:", clf.n_support_)
当然SVM还有对应的回归模型SVR
from sklearn import svm X = [[0, 0], [2, 2]] y = [0.5, 2.5] clf = svm.SVR() clf.fit(X, y) res = clf.predict([[1, 1]]) print(res)
3.逻辑回归
from sklearn import linear_model X = [[0, 0], [1, 1]] y = [0, 1] logreg = linear_model.LogisticRegression(C=1e5) #we create an instance of Neighbours Classifier and fit the data. logreg.fit(X, y) res = logreg.predict([[2, 2]]) print(res)