《机器学习》周志华 习题答案3.3

3.3 编程实现对率回归,并给出西瓜数据集3.0α上的结果。

本题我就调用了sklearn的逻辑回归库来测试。

#!/usr/bin/python
# -*- coding:utf-8 -*-
from sklearn import linear_model,datasets
import numpy as np
from  matplotlib import pyplot as plt
file1 = open(‘c:\quant\watermelon.csv‘,‘r‘)
data = [line.strip(‘\n‘).split(‘,‘) for line in file1]
X = [[float(raw[-3]), float(raw[-2])] for raw in data[1:]]
Y = [1 if raw[-1]==‘\xca\xc7‘ else 0 for raw in data[1:]]
X = np.array(X)
# import some data to play with
h = .002  # step size in the mesh

logreg = linear_model.LogisticRegression(C=1e5)

# we create an instance of Neighbours Classifier and fit the data.
logreg.fit(X, Y)

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
test = np.c_[xx.ravel(), yy.ravel()]
#print test
Z = logreg.predict(test)
#print Z
#np.c_() 连接多个数组为一个数组
# Put the result into a color plot
Z = Z.reshape(xx.shape)
#print Z
#plt.figure(1, figsize=(4, 3))
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors=‘k‘, cmap=plt.cm.Paired)
plt.xlabel(‘Density‘)
plt.ylabel(‘Sugar rate‘)

plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())

plt.show()

结果如下:

西瓜数据集如下:

编号,色泽,根蒂,敲声,纹理,脐部,触感,密度,含糖率,好瓜
1,青绿,蜷缩,浊响,清晰,凹陷,硬滑,0.697,0.46,是
2,乌黑,蜷缩,沉闷,清晰,凹陷,硬滑,0.774,0.376,是
3,乌黑,蜷缩,浊响,清晰,凹陷,硬滑,0.634,0.264,是
4,青绿,蜷缩,沉闷,清晰,凹陷,硬滑,0.608,0.318,是
5,浅白,蜷缩,浊响,清晰,凹陷,硬滑,0.556,0.215,是
6,青绿,稍蜷,浊响,清晰,稍凹,软粘,0.403,0.237,是
7,乌黑,稍蜷,浊响,稍糊,稍凹,软粘,0.481,0.149,是
8,乌黑,稍蜷,浊响,清晰,稍凹,硬滑,0.437,0.211,是
9,乌黑,稍蜷,沉闷,稍糊,稍凹,硬滑,0.666,0.091,否
10,青绿,硬挺,清脆,清晰,平坦,软粘,0.243,0.267,否
11,浅白,硬挺,清脆,模糊,平坦,硬滑,0.245,0.057,否
12,浅白,蜷缩,浊响,模糊,平坦,软粘,0.343,0.099,否
13,青绿,稍蜷,浊响,稍糊,凹陷,硬滑,0.639,0.161,否
14,浅白,稍蜷,沉闷,稍糊,凹陷,硬滑,0.657,0.198,否
15,乌黑,稍蜷,浊响,清晰,稍凹,软粘,0.36,0.37,否
16,浅白,蜷缩,浊响,模糊,平坦,硬滑,0.593,0.042,否
17,青绿,蜷缩,沉闷,稍糊,稍凹,硬滑,0.719,0.103,否
时间: 2024-10-22 18:23:59

《机器学习》周志华 习题答案3.3的相关文章

《机器学习》周志华 习题答案6.2

原题是分别采用线性核和高斯核对西瓜数据集进行SVM的训练,周老师推荐的是LIMSVM,这里我使用的仍然是sklearn. #!/usr/bin/python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets file1 = open('c:\quant\watermelon.csv','r') data = [line.strip(

《机器学习》周志华 习题答案9.4

原题采用Kmeans方法对西瓜数据集进行聚类.我花了一些时间居然没找到西瓜数据集4.0在哪里,于是直接采用sklearn给的例子来分析一遍,更能说明Kmeans的效果. #!/usr/bin/python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import BaggingClassifier from sklearn.tree import D

《机器学习》周志华 习题答案3.5

编程实现判别分析,并给出西瓜数据集上的结果. 数据集如下 编号,色泽,根蒂,敲声,纹理,脐部,触感,密度,含糖率,好瓜 1,青绿,蜷缩,浊响,清晰,凹陷,硬滑,0.697,0.46,是 2,乌黑,蜷缩,沉闷,清晰,凹陷,硬滑,0.774,0.376,是 3,乌黑,蜷缩,浊响,清晰,凹陷,硬滑,0.634,0.264,是 4,青绿,蜷缩,沉闷,清晰,凹陷,硬滑,0.608,0.318,是 5,浅白,蜷缩,浊响,清晰,凹陷,硬滑,0.556,0.215,是 6,青绿,稍蜷,浊响,清晰,稍凹,软粘,0

《机器学习》周志华 习题答案8.3

原题,以不剪枝的决策树作为基学习器,对西瓜数据集实现Adaboost分类. #!/usr/bin/python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import AdaBoostClassifier from sklearn.tree import DecisionTreeClassifier file1 = open('c:\quant\w

《机器学习》周志华 习题答案8.5

用Bagging,以决策树为树桩,在西瓜数据集上实现. #!/usr/bin/python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import BaggingClassifier from sklearn.tree import DecisionTreeClassifier file1 = open('c:\quant\watermelon.c

《机器学习》周志华 习题答案5.5

原题是写一个BP神经网络来拟合西瓜数据集,西瓜数据集我已经数值化了如下: 编号,色泽,根蒂,敲声,纹理,脐部,触感,密度,含糖率,好瓜 1,1,1,3,1,1,1,0.697,0.46,1 2,2,1,2,1,1,1,0.774,0.376,1 3,2,1,3,1,1,1,0.634,0.264,1 4,1,1,2,1,1,1,0.608,0.318,1 5,3,1,3,1,1,1,0.556,0.215,1 6,1,2,3,1,2,2,0.403,0.237,1 7,2,2,3,2,2,2,0

《机器学习》周志华 习题答案4.3

原题,对西瓜数据集用决策树来进行划分,此处我只选取了西瓜的密度和含糖率这两个连续属性来进行划分, #!/usr/bin/python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from matplotlib import colors from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis file1 =

《机器学习》周志华 习题答案3.6

原题:线性判别分析仅在线性可分数据上能获得理想结果,试设计一个改进方法,使其能够用于非线性可分数据. 这里我采用二次判别分析来对原来的西瓜数据集进行分类,同样采用sklearn里的二次判别库. #!/usr/bin/python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from matplotlib import colors from sklearn.discriminant_analy

【读书笔记】机器学习-周志华 & 机器学习实战(Python)

这两本放在一起看吧.当然了,我觉得Spark上面的实践其实是非常棒的.有另一个系列文章讨论了Spark. /Users/baidu/Documents/Data/Interview/机器学习-数据挖掘/<机器学习_周志华.pdf> 一共442页.能不能这个周末先囫囵吞枣看完呢.哈哈哈. P1 一般用模型指全局性结果(例如决策树),用模式指局部性结果(例如一条规则). P3 如果预测的是离散值,那就是分类-classification:如果预测的是连续值,那就叫回归-regression. P3