python sklearn的快速使用
鸢尾花识别是一个经典的机器学习分类问题,它的数据样本中包括了4个特征变量,1个类别变量,样本总数为150。
它的目标是为了根据花萼长度(sepallength)、花萼宽度(sepalwidth)、花瓣长度(petallength)、花瓣宽度(petalwidth)这四个特征来识别出鸢尾花属于山鸢尾(iris-setosa)、变色鸢尾(iris-versicolor)和维吉尼亚鸢尾(iris-virginica)中的哪一种。
#引入数据集,sklearn包含众多数据集
fromsklearnimportdatasets
#将数据分为测试集和训练集
fromsklearn.model_selectionimporttrain_test_split
#利用邻近点方式训练数据
fromsklearn.neighborsimportKNeighborsClassifier
#引入数据,本次导入鸢尾花数据,iris数据包含4个特征变量
iris=datasets.load_iris()
#特征变量
iris_X=iris.data
#print(iris_X)
print('特征变量的长度',len(iris_X))
#目标值
iris_y=iris.target
print('鸢尾花的目标值',iris_y)
#利用train_test_split进行训练集和测试机进行分开,test_size占30%
X_train,X_test,y_train,y_test=train_test_split(iris_X,iris_y,test_size=0.3)
#我们看到训练数据的特征值分为3类
#print(y_train)
'''
[1102000222102021010201002120010010000
2221112020111122122202220101001222111
2001021201222121001001110211022]
'''
#训练数据
#引入训练方法
knn=KNeighborsClassifier()
#进行填充测试数据进行训练
knn.fit(X_train,y_train)
params=knn.get_params()
print(params)
'''
{'algorithm':'auto','leaf_size':30,'metric':'minkowski',
'metric_params':None,'n_jobs':None,'n_neighbors':5,
'p':2,'weights':'uniform'}
'''
score=knn.score(X_test,y_test)
print("预测得分为:%s"%score)
'''
预测得分为:0.9555555555555556
[1211221000012010200022022221222122120
21211021]
[1211221000012010200012022221122122120
21211021]
'''
#预测数据,预测特征值
print(knn.predict(X_test))
'''
[0222200002202021202102101220210211202
12021012]
'''
#打印真实特征值
print(y_test)
'''
[1222211112111121102111020200202020220
22010200]
'''
以上内容为大家介绍了pythonsklearn的快速使用,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中的filter函数功能是什么?
python中的filter函数功能是什么?在python中,面对众多的数据,我们要过滤筛选出我们需要的数据。python中的filter函数就是起到了过滤筛选的作...详情>>
2023-11-10 20:37:27
pythontime模块是什么
pythontime模块是什么在python中使用时间,就免不了和time模块打交道,另外两个模块这个暂时先不做介绍。做time模块的使用上,我们可以用它来对...详情>>
2023-11-10 15:53:16
python是什么编程语言
python是什么编程语言1、说明是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Pyt...详情>>
2023-11-10 15:21:05
python异常处理的两种技巧
python异常处理的两种技巧1、传递异常有时我们会在捕捉到一个异常后重新引发它(传递异常),实现起来很简单,使用不带参数的raise语句即可。deff...详情>>
2023-11-10 14:49:39热门推荐
python中的filter函数功能是什么?
沸python delattr函数如何使用?
热python中pdb模块怎么用?
热Python如何截图保存?
新python中缺少module怎么办?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python对象的三要素是什么
pythonGIL在Python多线程的应用
python如何对多个CSV文件进行读取
pythonif嵌套命令如何理解?
python对列表进行永久性或临时排序的方法
python生成器调用方法引发异常
技术干货






