Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1515484
  • 博文数量: 289
  • 博客积分: 11086
  • 博客等级: 上将
  • 技术积分: 3291
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-22 17:06
个人简介

徐小玉的博客。

文章分类

全部博文(289)

文章存档

2023年(6)

2022年(1)

2021年(2)

2020年(9)

2019年(9)

2018年(6)

2017年(10)

2016年(10)

2014年(3)

2013年(4)

2011年(12)

2010年(16)

2009年(14)

2008年(119)

2007年(48)

2006年(20)

我的朋友

分类: Python/Ruby

2023-05-19 14:59:19

安装准备:

安装
matplotlib 数据可视化

      没有: pip show matplotlib

      安装: pip install matplotlib

安装pandas

        pip show pandas
        pip install pandas

至此,机器学习需要的基础库准备就绪:

import sys

import scipy

import numpy as np

import sklearn

import pandas

import matplotlib

1: 线性回归:

#导入所需库matplotlib,numpy

import matplotlib.pyplot as plt

import numpy as np

#scikit-learn导入线性模型的线性回归算法

from sklearn import linear_model

#生成数据集

x = np.linspace(-3,3,30)

y = 2*x + 1

# 把序列变矩阵,scikit-learnfit函数需要的输入参数是矩阵。

x=[[i] for i in x]

y=[[i] for i in y]

#训练线性回归模型

model = linear_model.LinearRegression()

#为模型传入训练参数

model.fit(x,y)

# predict

#model.predict(x_)

# 预测一下,选4

x_=[[1],[2],[3],[5]]

print ( model.predict(x_))

#数据集绘制,这里并不影响预测,纯粹是为了看一下。

plt.scatter(x,y)

plt.show()


output:

[[ 3.]

 [ 5.]

 [ 7.]

 [11.]]


2: logistic 回归

# ------------logistic回归分类算法-----------

#------------------------------------------

print ("\n------ logistic回归 ------\n")

#scikit-learn导入线性模型的logistic回归算法

from sklearn.linear_model import LogisticRegression

#鸢尾花分类数据集,是一个分类问题的数据集

from sklearn.datasets import load_iris

#载入鸢尾花数据集

X,y = load_iris(return_X_y=True)

#训练模型

#clf=LogisticRegression().fit(X,y)  出错了。因为增大迭代

#max_iter=10000

clf=LogisticRegression(max_iter=3000).fit(X,y)

#使用模型预测

print ('---to Predict:\n')

#clf.predict(X)

print(clf.predict(X))

print ('---to evaluate:\n')

print(clf.score(X,y))



output:


---to Predict:

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1

 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 2 2 2

 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

 2 2]

---to evulate:

0.9733333333333334


KNN分类算法:

# ------------KNN分类算法-----------

#------------------------------------------

print ("\n------ KNN ------\n")

from sklearn.datasets import load_iris

#近邻模型KNN算法

from sklearn.neighbors import KNeighborsClassifier

#载入鸢尾花数据集

X,y = load_iris(return_X_y=True)

clf=KNeighborsClassifier().fit(X,y)

print(clf.predict(X))

print(clf.score(X,y))



output :

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1

 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 2 2 2

 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

 2 2]

0.9666666666666667


 



阅读(6577) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~