以下是课本对应的一些练习,但有两个问题
1.最后交叉验证部分我没做出来,估计我用的是python3.8的原因有大佬做出来的可以提意见
2.最后Facebook的例子没有做,因为网址打不开,需要
需要python3 advancedclassify.py代码的见我另一篇
##加载数据集
import advancedclassify
#from advancedclassify import *
agesonly=advancedclassify.loadmatch('agesonly.csv',allnum=True)
matchmaker=advancedclassify.loadmatch('matchmaker.csv')
# noinspection PyStatementEffect
matchmaker[0].data
# noinspection PyStatementEffect
matchmaker[0].match
#散点图
import importlib
importlib.reload(advancedclassify)
advancedclassify.plotagematches(agesonly)
#线性分类器
import importlib
importlib.reload(advancedclassify)
avgs=advancedclassify.lineartrain(agesonly)
avgs
advancedclassify.dpclassify([30,30],avgs)
advancedclassify.dpclassify([30,25],avgs)
advancedclassify.dpclassify([25,40],avgs)
advancedclassify.dpclassify([48,20],avgs)
#计算距离
import importlib
importlib.reload(advancedclassify)
#advancedclassify.getlocation('1 alewife center, cambridge, ma')
#advancedclassify.milesdistance('cambridge, ma','new york,ny')
#构造新的数据集
import importlib
importlib.reload(advancedclassify)
numericalset=advancedclassify.loadnumerical()
# noinspection PyStatementEffect
numericalset[0].data
#对数据进行缩放
import importlib
importlib.reload(advancedclassify)
scaledset,scalef=advancedclassify.scaledata(numericalset)
avgs=advancedclassify.lineartrain(scaledset)
# noinspection PyStatementEffect
numericalset[0].match
advancedclassify.dpclassify(scalef(numericalset[0].data),avgs)
# noinspection PyStatementEffect
numericalset[11].match
advancedclassify.dpclassify(scalef(numericalset[11].data),avgs)
#一个python会话的例子
from libsvm.svmutil import *
# Specify training set
prob = svm_problem([1,-1],[[1,0,1],[-1,0,-1]])
# Train the model
m = svm_train(prob, '-t 0 -c 1')
# Make a prediction
predicted_labels, _, _ = svm_predict([-1],[[1,1,1]],m)
# Predicted label for input [1,1,1] is predicted_labels[0]
print ("Predicted value: " + str(predicted_labels[0]))
#SVM婚姻数据集
from sklearn.svm._libsvm import cross_validation
from pandas.core.frame import DataFrame
import untitled0
from libsvm.svmutil import *
import numpy as np
#先将数据集转化成svm_model所要求的列表元组
answers,inputs=[r.match for r in untitled0.scaledset],[r.data for r in untitled0.scaledset]
print(answers)
print(inputs)
#选择径向基函数作为核方法
param=svm_parameter('-t 2')
prob=svm_problem(answers,inputs)
m=svm_train(prob,param)
#利用缩放处理函数,将待预测的数据按比例进行缩放,使相应的变量与构造模型时所用的变量处于相同的尺度范围内
# 对“具备给定特征集的人是否能够匹配成功”进行预测
newrow=[28.0,-1,-1,26.0,-1,1,2,0.8] #男士不想要小孩,女士想要
# m.svm_predict(untitled0.scalef(newrow))
predicted_labels1, _, _ =svm_predict([1],[untitled0.scalef(newrow)],m)
print(predicted_labels1)
newrow=[28.0,-1,1,26.0,-1,1,2,0.8] #双方都想要小孩
predicted_labels2, _, _ =svm_predict([1],[untitled0.scalef(newrow)],m)
print(predicted_labels2)