zlj0406 Goto Github PK
Type: User
Type: User
#coding:gbk """ 利用决策树算法进行分类 作者:梁鹤逸 日期:2019.12.14 """ import pandas as pd # 调入需要用的库 import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm import seaborn as sb #%matplotlib inline # 调入数据 df = pd.read_csv('G:\\zy\\python‘s程序\\frenchwine.csv') df.columns = ['species','alcohol', 'malic_acid', 'ash', 'alcalinity ash', 'magnesium'] # 查看前5条数据 df.head() print(df.head()) # 查看数据描述性统计信息 df.describe() print(df.describe()) def scatter_plot_by_category(feat, x, y): #数据的可视化 alpha = 0.5 gs = df.groupby(feat) cs = cm.rainbow(np.linspace(0, 1, len(gs))) for g, c in zip(gs, cs): plt.scatter(g[1][x], g[1][y], color=c, alpha=alpha) plt.figure(figsize=(20,5)) plt.subplot(131) scatter_plot_by_category('species', 'alcohol', 'ash') plt.xlabel('alcohol') plt.ylabel('ash') plt.title('species') plt.show() plt.figure(figsize=(20, 10)) #利用seaborn库绘制三种Iris花不同参数图 for column_index, column in enumerate(df.columns): if column == 'species': continue plt.subplot(2, 3, column_index + 1) sb.violinplot(x='species', y=column, data=df) plt.show() # 首先对数据进行切分,即划分出训练集和测试集 from sklearn.model_selection import train_test_split #调入sklearn库中交叉检验,划分训练集和测试集 all_inputs = df[['alcohol', 'malic_acid', 'ash', 'alcalinity ash','magnesium']].values all_species = df['species'].values (X_train, X_test, Y_train, Y_test) = train_test_split(all_inputs, all_species, train_size=0.85, random_state=1)#80%的数据选为训练集 # 使用决策树算法进行训练 from sklearn.tree import DecisionTreeClassifier #调入sklearn库中的DecisionTreeClassifier来构建决策树 # 定义一个决策树对象 decision_tree_classifier = DecisionTreeClassifier() # 训练模型 model = decision_tree_classifier.fit(X_train, Y_train) # 输出模型的准确度 print(decision_tree_classifier.score(X_test, Y_test)) # 使用训练的模型进行预测,为了方便, # 案例直接把测试集里面的数据拿出来三条 print(X_test[0:3])#利用3个数据进行测试,即取3个数据作为模型的输入端 model.predict(X_test[0:3]) print(model.predict(X_test[0:3]))#输出测试的结果,即输出模型预测的结 for all_species in range(1): print('仙粉,黛西拉,赤霞珠')
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.