网站关键词太多好不好,网络整合营销理论是指什么,妇科医院网站建设怎么做,中小企业网站建设报告现在最常用的数据分析的编程语言为R和Python。每种语言都有自己的特点#xff0c;Python因为Scikit-Learn库赢得了优势。Scikit-Learn有完整的文档#xff0c;并实现很多机器学习算法#xff0c;而每种算法使用的接口几乎相同#xff0c;可以非常快的测试其它学习算法。 Pa…现在最常用的数据分析的编程语言为R和Python。每种语言都有自己的特点Python因为Scikit-Learn库赢得了优势。Scikit-Learn有完整的文档并实现很多机器学习算法而每种算法使用的接口几乎相同可以非常快的测试其它学习算法。 Pandas一般和Scikit-Learn配合使用它是基于Numpy构建的含有更高级数据结构和工具的数据统计工具可以把它当成excel。 加载数据 首先把数据加载到内存。下载UCI数据集 1 2 3 4 5 6 7 8 9 10 11 import numpy as np import urllib # 数据集的url url http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data # 下载文件 raw_data urllib.urlopen(url) # 把数据加载到numpy matrix dataset np.loadtxt(raw_data, delimiter,) # 分离数据集 X dataset[:,0:7] # 属性集 y dataset[:,8] # 标签 数据标准化 在开始应用学习算法之前应首先对数据执行标准化这是为了确保特征值的范围在0-1。对数据进行预处理 1 2 3 4 5 from sklearn import preprocessing # normalize normalized_X preprocessing.normalize(X) # standardize standardized_X preprocessing.scale(X) 分类 ExtraTreesClassifier(基于树) 1 2 3 4 5 6 from sklearn import metrics from sklearn.ensemble import ExtraTreesClassifier model ExtraTreesClassifier() model.fit(X, y) # 显示属性的相对重要性 print(model.feature_importances_) LogisticRegression 1 2 3 4 5 6 7 8 9 from sklearn.feature_selection import RFE from sklearn.linear_model import LogisticRegression model LogisticRegression() rfe RFE(model, 3) rfe rfe.fit(X, y) print(rfe.support_) print(rfe.ranking_) 机器学习算法 Logistic regression 通常用来解决分类问题binary但是也支持多个分类。这个算法会给出属于某一分类的概率 1 2 3 4 5 6 7 8 9 10 11 from sklearn import metrics from sklearn.linear_model import LogisticRegression model LogisticRegression() model.fit(X, y) print(model) # 做预测 expected y predicted model.predict(X) # 输出 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) 朴素贝叶斯-Naive Bayes 这也是广为人知的机器学习算法用来学习数据分布的密度在多分类问题中可以提供高质量的预测结果。 1 2 3 4 5 6 7 8 9 10 11 from sklearn import metrics from sklearn.naive_bayes import GaussianNB model GaussianNB() model.fit(X, y) print(model) # 预测 expected y predicted model.predict(X) # 结果 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) KNN算法(KNearest Neighbours) 使用Python实现K-Nearest Neighbor算法它通常用在更复杂分类算法的一部分它在回归问题中可以提供很好的结果。 决策树Decision Trees 能很好的处理回归和分类问题。 1 2 3 4 5 6 7 8 9 10 11 12 from sklearn import metrics from sklearn.tree import DecisionTreeClassifier # fit a CART model to the data model DecisionTreeClassifier() model.fit(X, y) print(model) # 预测 expected y predicted model.predict(X) # 结果 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) 支持向量机Support Vector Machines 使用Python实现Support Vector Machine算法 1 2 3 4 5 6 7 8 9 10 11 12 from sklearn import metrics from sklearn.svm import SVC # fit a SVM model to the data model SVC() model.fit(X, y) print(model) # 预测 expected y predicted model.predict(X) # 结果 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) Scikit-Learn还提供了一堆更复杂的算法包括clusteringBagging 和 Boosting。 转载于:https://www.cnblogs.com/gejuncheng/p/8127446.html