1、安装Python
安装python-m pip安装–用户麻木scipy matplotlib IPython jupyter熊猫症状鼻[en]Install python-m pip install– user numpy scipy matplotlib ipython jupyter pandas sympy nose
pip install -U scikit-learn
效果图:[en]Effect picture:

运行结果:[en]Running result:

完整代码:[en]Complete code:
python;gutter:true;
from pandas import read_csv
from pandas.plotting import scatter_matrix
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC</p>
<p>print("------------------------------------------------")</p>
<h1>Load dataset</h1>
<h1>url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv"</h1>
<p>names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = read_csv("C:\Users\Administrator\Downloads\iris.data", names=names)</p>
<h1>shape</h1>
<p>print("------------------------------------------------")
print(dataset.shape)</p>
<h1>print(dataset.head(20))</h1>
<h1>descriptions</h1>
<p>print("------------------------------------------------")
print(dataset.describe())</p>
<h1>classdistribution</h1>
<p>print("------------------------------------------------")
print(dataset.groupby('class').size())</p>
<h1>boxand whisker plots</h1>
<p>print("------------------------------------------------")</p>
<h1>dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)</h1>
<h1>pyplot.show()</h1>
<p>print("------------------------------------------------")</p>
<h1>histograms</h1>
<h1>dataset.hist()</h1>
<h1>pyplot.show()</h1>
<p>print("------------------------------------------------")</p>
<h1>scatter plot matrix</h1>
<h1>scatter_matrix(dataset)</h1>
<h1>pyplot.show()</h1>
<p>print("------------------------------------------------")</p>
<h1>Split-out validation dataset</h1>
<p>array = dataset.values
X = array[:,0:4]
y = array[:,4]
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.20, random_state=1)</p>
<p>models = []
models.append(('LR', LogisticRegression(solver='liblinear', multi_class='ovr')))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))</p>
<h1>evaluate each model in turn</h1>
<p>results = []
names = []
for name, model in models:
kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
cv_results = cross_val_score(model, X_train, Y_train, cv=kfold, scoring='accuracy')
results.append(cv_results)
names.append(name)
print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))</p>
<h1>Compare Algorithms</h1>
<p>pyplot.boxplot(results, labels=names)
pyplot.title('Algorithm Comparison')
pyplot.show()</p>
<h1>Make predictions on validation dataset</h1>
<p>model = SVC(gamma='auto')
model.fit(X_train, Y_train)
predictions = model.predict(X_validation)</p>
<h1>Evaluate predictions</h1>
<p>print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
参考资料:[en]Reference:
英文:https://machinelearningmastery.com/machine-learning-in-python-step-by-step/
中文: https://www.jianshu.com/p/711488d85e00
Original: https://www.cnblogs.com/linlf03/p/16117231.html
Author: work hard work smart
Title: 手把手教你用python开始第一个机器学习项目
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/5931/
转载文章受原作者版权保护。转载请注明原作者出处!