机器学习上机期末总结

机器学习常用代码总结
摘要由CSDN通过智能技术生成

目录

机器学习常用代码总结:

一:常用的数据集

二:机器学习方法及常用参数

2.1 线性回归

2.2 逻辑回归

2.3 朴素贝叶斯

2.4 K-均值聚类

2.5 K-近邻算法

2.6 朴素贝叶斯

2.7 神经网络

2.8 决策树

2.9 支持向量机

三:画图重点

3.1 柱状图

3.2 直线图

3.3 散点图

3.4 文本和标记的显示

3.5 颜色映射表的创建与使用

3.6 利用网格法绘制分类边界

四:评价指标

4.1 predict

4.2 predict_proba

4.3 精度


机器学习常用代码总结:

编程语言:python

机器学习库:sklearn

一:常用的数据集

"""
X:多维数组,每一个列表为一个样本,列表里面是该样本的特征。
Y:一维数组,保存的是X中每个样本对应的类别。
"""

'''make_moons:产生的每个样本中包含两个特征'''
from sklearn.datasets import make_moons
X,Y = make_moons(n_samples=100,noise=0.5)#指定100个噪声值为0.5的二维样本
#n_samples:产生的样本数;noise:设置的噪声值


'''load_iris:每行数据包含每个样本的四个特征和样本的类别信息,'''
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data[:, [0,2]]#任选2个特征分别作为横轴和纵轴,用不同的颜色标记不同品种的花
Y = iris.target#iris的每个样本都包含了品种信息,即目标属性(第5列,也叫target或label)
#其他常用属性
shape=iris.data.shape#data对应了样本的4个特征,150行4列
data=iris.data[:5]#显示样本特征的前5行
targetShape=iris.target.shape#target对应了样本的类别(目标属性),150行1列


'''make_blobs:可指定每个样本包含的特征数'''
'''官方网址:https://github.com/scikit-learn/scikit-learn/blob/a5ab948/sklearn/datasets/samples_generator.py#L680'''
from sklearn.datasets.samples_generator import make_blobs

X,Y = make_blobs(n_samples=10, n_features=2, centers=[[1,1], [2,2]], cluster_std=[0.6, 0.6])
#####centers:聚类中心;cluster_std:类别方差,n_features:每个样本的特征数
#n_samples:产生的样本数;n_features:每个样本的特征数;cluster_std:每个类别的方差;
#centers:[n_centers, n_features],n_centers和n_features可为一个列表,默认,centers=3,如下写法;
X,Y = make_blobs(n_samples=10,n_features=5,centers=3,cluster_std=[1,2,3])
#centers:类别数;


'''train_test_split:将数据分为训练集和测试集'''
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.4)
#test_size:训练集占的比例

二:机器学习方法及常用参数

2.1 线性回归

from sklearn.linear_model import LinearRegression #引入回归库
linreg = LinearRegression() #构建回归模型
linreg.fit(x_train, y_train) #数据拟合
w1,w2=linreg.coef_ #查看斜率
b=linreg.intercept_ #查看截距
#得到每个样本的预测值
y_predict=w1*x_train[:,0]+w2*x_train[:,1]+b
#将预测值与原始值在图中绘制进行比较
plt.plot(x_train[:,0],y_predict,linestyle='',marker='o',color='c')
plt.plot(x_train[:,0],y_train,linestyle='',marker='o',color='y')
for i,j in zip(x_train[:,0],range(len(y_predict))):
    plt.plot([i]*2,[y_predict[j],y_train[j]],color='r')
    plt.text(i,(y_predict[j]+y_train[j])/2,'%.2f'%((y_predict[j]+y_train[j])/2))
plt.show()
#求预测精度
score=linreg.score(x_test,y_test)#预测精度

2.2 逻辑回归

from sklearn.linear_model import LogisticRegression
'''
LogisticRegression(penalty='l2', dual=False, tol=0.0001, C=1.0, fit_intercept=True, 
                   intercept_scaling=1, class_weight=None, random_state=None, 
                   solver='liblinear', max_iter=100, multi_class=’ovr’, verbose=0, 
                   warm_start=False, n_jobs=1)
fit(x, y[, sample_weight]):拟合训练样本[x,y]。
predict(x):预测样本x的类别标识。
predict_proba(x):预测概率值。
score(x, y[, sample_weight]):输出结定样本和分类标识的平均准确度。
coef_: 回归系数,结构为(1, n_features) or (n_classes, n_features)。
intercept_ : 截距,两分类对应一个,多分类对应多个。
'''
LR=LogisticRegression() #模型构建
LR.fit(x_train,y_train) #数据拟合
LR.score(x_test,y_test)#预测精度
#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值