当前位置:   article > 正文

常用的几种机器学习算法回归模型python代码实现_回归预测常用机器学习算法代码

回归预测常用机器学习算法代码

       由于在论文实验过程中一直使用的是python语言完成的论文实验,所以在论文需要使用机器学习方法时就考虑使用了scikit-learn。

        scikit-learn是一款很好的Python机器学习库,它包含以下的特点:

        (1)简单高效的数据挖掘和数据分析工具;

        (2)可供大家使用,可在各种环境中重复使用;

        (3)建立在NumPy, SciPy和matplotlib上;

        (4)开放源码,可商业使用;

scikit-learn官方英文使用文档地址:http://scikit-learn.org/stable/index.html

scikit-learn中文文档地址:http://sklearn.apachecn.org/cn/0.19.0/index.html

          在本文中将把我在论文实验过程中使用几种机器学习方法源码贴出来方便调用,但每种机器学习方法的原理就不赘述了,可以参考官方给出的文档。这几种方法使用的测试数据均为如下所示:


一、贝叶斯岭回归

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn import datasets, linear_model
  4. from sklearn.cross_validation import train_test_split
  5. from sklearn import metrics
  6. from sklearn import preprocessing
  7. from sklearn.naive_bayes import GaussianNB
  8. from sklearn import linear_model
  9. from sklearn import metrics
  10. def Bayes(path):
  11. data = pd.read_excel(path)
  12. data.dropna(inplace=True)
  13. array=data.values
  14. X=array[:,1:len(data.columns)-1]
  15. y=array[:,len(data.columns)-1]
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
  17. reg=linear_model.BayesianRidge()
  18. reg_=reg.fit(X_train, y_train)
  19. y_pred = reg.predict(X_test)
  20. return (X_test,y_pred)
  21. x,y=Bayes("./test.xls")
  22. print (x,y)

运行代码,可以看到结果如下所示:


二、Logistic回归

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn import datasets, linear_model
  4. from sklearn.cross_validation import train_test_split
  5. from sklearn import metrics
  6. from sklearn import preprocessing
  7. from sklearn.linear_model import LogisticRegression
  8. from sklearn.svm import l1_min_c
  9. from sklearn import metrics
  10. def Logist(path):
  11. data = pd.read_excel(path)
  12. data.dropna(inplace=True)
  13. array=data.values
  14. X=array[:,1:len(data.columns)-1]
  15. y=array[:,len(data.columns)-1]
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
  17. cls = LogisticRegression(C=1.0,tol=1e-6)
  18. rbf=cls.fit(X_train, y_train)
  19. y_pred = cls.predict(X_test)
  20. return (X_test,y_pred)
  21. x,y=Logist("./test.xls")
  22. print(x,y)

运行代码,可以看到结果如下所示:


三、多层感知器

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn import datasets, linear_model
  4. from sklearn.cross_validation import train_test_split
  5. from sklearn import metrics
  6. from sklearn import preprocessing
  7. from sklearn.linear_model import Perceptron
  8. from sklearn import metrics
  9. def Percep(path):
  10. data = pd.read_excel(path)
  11. data.dropna(inplace=True)
  12. array=data.values
  13. X=array[:,1:len(data.columns)-1]
  14. y=array[:,len(data.columns)-1]
  15. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
  16. per=Perceptron()
  17. rbf=per.fit(X_train, y_train)
  18. y_pred = per.predict(X_test)
  19. return (X_test,y_pred)
  20. #X=preprocessing.scale(X)
  21. #y=preprocessing.scale(y)
  22. x,y=Percep("./test.xls")
  23. print(x,y)

运行代码,可以看到结果如下所示:


四、支持向量机回归

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn import datasets, linear_model
  4. from sklearn.cross_validation import train_test_split
  5. from sklearn import svm
  6. from sklearn import metrics
  7. from sklearn import preprocessing
  8. def SVM(path):
  9. data = pd.read_excel(path)
  10. data.dropna(inplace=True)
  11. array=data.values
  12. X=array[:,1:len(data.columns)-1]
  13. y=array[:,len(data.columns)-1]
  14. #X=preprocessing.scale(X)
  15. #y=preprocessing.scale(y)
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
  17. rbf_svc = svm.SVR(kernel='rbf') #此处使用的是径向基内核
  18. rbf_svc.tol=1
  19. rbf=rbf_svc.fit(X_train, y_train)
  20. y_pred = rbf_svc.predict(X_test)
  21. return (X_test,y_pred)
  22. x,y=SVM("./test.xls")
  23. print(x,y)

运行代码,可以看到结果如下所示:


五、决策树回归

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn import datasets, linear_model
  4. from sklearn.cross_validation import train_test_split
  5. from sklearn import metrics
  6. from sklearn import preprocessing
  7. from sklearn import tree
  8. def Tree(path):
  9. data = pd.read_excel(path)
  10. data.dropna(inplace=True)
  11. array=data.values
  12. X=array[:,1:len(data.columns)-1]
  13. y=array[:,len(data.columns)-1]
  14. #X=preprocessing.scale(X)
  15. #y=preprocessing.scale(y)
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
  17. clf = tree.DecisionTreeRegressor()
  18. rbf=clf.fit(X_train, y_train)
  19. y_pred = rbf.predict(X_test)
  20. return (X_test,y_pred)
  21. x,y=Tree("./test.xls")
  22. print(x,y)

运行代码,可以看到结果如下所示:


六、最近邻回归

  1. import numpy as np
  2. import pandas as pd
  3. from sklearn import datasets, linear_model
  4. from sklearn.cross_validation import train_test_split
  5. from sklearn import metrics
  6. from sklearn import preprocessing
  7. from sklearn import neighbors
  8. from sklearn import metrics
  9. from sklearn.model_selection import cross_val_predict
  10. def KNN(path):
  11. data = pd.read_excel(path)
  12. data.dropna(inplace=True)
  13. array=data.values
  14. X=array[:,1:len(data.columns)-1]
  15. y=array[:,len(data.columns)-1]
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
  17. knn = neighbors.KNeighborsRegressor(1, weights="uniform") //修改第一个参数的值可以变为KNN_N近邻
  18. knn=knn.fit(X_train, y_train)
  19. y_pred = knn.predict(X_test)
  20. return(X_test,y_pred)
  21. x,y=KNN("./test.xls")
  22. print(x,y)

运行代码,可以看到结果如下所示:


本文中的源代码下载地址:https://github.com/XiaoYaoNet/Machine-Learning

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/128005
推荐阅读
相关标签
  

闽ICP备14008679号