forked from hanbt/learn_dl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add]add class to classify iris flowers
- Loading branch information
1 parent
50c2a3b
commit b8e3eda
Showing
4 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
5.1,3.5,1.4,0.2,Iris-setosa | ||
4.9,3.0,1.4,0.2,Iris-setosa | ||
4.7,3.2,1.3,0.2,Iris-setosa | ||
4.6,3.1,1.5,0.2,Iris-setosa | ||
5.0,3.6,1.4,0.2,Iris-setosa | ||
5.4,3.9,1.7,0.4,Iris-setosa | ||
4.6,3.4,1.4,0.3,Iris-setosa | ||
5.0,3.4,1.5,0.2,Iris-setosa | ||
4.4,2.9,1.4,0.2,Iris-setosa | ||
4.9,3.1,1.5,0.1,Iris-setosa | ||
5.4,3.7,1.5,0.2,Iris-setosa | ||
4.8,3.4,1.6,0.2,Iris-setosa | ||
4.8,3.0,1.4,0.1,Iris-setosa | ||
4.3,3.0,1.1,0.1,Iris-setosa | ||
5.8,4.0,1.2,0.2,Iris-setosa | ||
5.7,4.4,1.5,0.4,Iris-setosa | ||
5.4,3.9,1.3,0.4,Iris-setosa | ||
5.1,3.5,1.4,0.3,Iris-setosa | ||
5.7,3.8,1.7,0.3,Iris-setosa | ||
5.1,3.8,1.5,0.3,Iris-setosa | ||
5.4,3.4,1.7,0.2,Iris-setosa | ||
5.1,3.7,1.5,0.4,Iris-setosa | ||
4.6,3.6,1.0,0.2,Iris-setosa | ||
5.1,3.3,1.7,0.5,Iris-setosa | ||
4.8,3.4,1.9,0.2,Iris-setosa | ||
5.0,3.0,1.6,0.2,Iris-setosa | ||
5.0,3.4,1.6,0.4,Iris-setosa | ||
5.2,3.5,1.5,0.2,Iris-setosa | ||
5.2,3.4,1.4,0.2,Iris-setosa | ||
4.7,3.2,1.6,0.2,Iris-setosa | ||
4.8,3.1,1.6,0.2,Iris-setosa | ||
5.4,3.4,1.5,0.4,Iris-setosa | ||
5.2,4.1,1.5,0.1,Iris-setosa | ||
5.5,4.2,1.4,0.2,Iris-setosa | ||
4.9,3.1,1.5,0.1,Iris-setosa | ||
5.0,3.2,1.2,0.2,Iris-setosa | ||
5.5,3.5,1.3,0.2,Iris-setosa | ||
4.9,3.1,1.5,0.1,Iris-setosa | ||
4.4,3.0,1.3,0.2,Iris-setosa | ||
5.1,3.4,1.5,0.2,Iris-setosa | ||
5.0,3.5,1.3,0.3,Iris-setosa | ||
4.5,2.3,1.3,0.3,Iris-setosa | ||
4.4,3.2,1.3,0.2,Iris-setosa | ||
5.0,3.5,1.6,0.6,Iris-setosa | ||
5.1,3.8,1.9,0.4,Iris-setosa | ||
4.8,3.0,1.4,0.3,Iris-setosa | ||
5.1,3.8,1.6,0.2,Iris-setosa | ||
4.6,3.2,1.4,0.2,Iris-setosa | ||
5.3,3.7,1.5,0.2,Iris-setosa | ||
5.0,3.3,1.4,0.2,Iris-setosa | ||
7.0,3.2,4.7,1.4,Iris-versicolor | ||
6.4,3.2,4.5,1.5,Iris-versicolor | ||
6.9,3.1,4.9,1.5,Iris-versicolor | ||
5.5,2.3,4.0,1.3,Iris-versicolor | ||
6.5,2.8,4.6,1.5,Iris-versicolor | ||
5.7,2.8,4.5,1.3,Iris-versicolor | ||
6.3,3.3,4.7,1.6,Iris-versicolor | ||
4.9,2.4,3.3,1.0,Iris-versicolor | ||
6.6,2.9,4.6,1.3,Iris-versicolor | ||
5.2,2.7,3.9,1.4,Iris-versicolor | ||
5.0,2.0,3.5,1.0,Iris-versicolor | ||
5.9,3.0,4.2,1.5,Iris-versicolor | ||
6.0,2.2,4.0,1.0,Iris-versicolor | ||
6.1,2.9,4.7,1.4,Iris-versicolor | ||
5.6,2.9,3.6,1.3,Iris-versicolor | ||
6.7,3.1,4.4,1.4,Iris-versicolor | ||
5.6,3.0,4.5,1.5,Iris-versicolor | ||
5.8,2.7,4.1,1.0,Iris-versicolor | ||
6.2,2.2,4.5,1.5,Iris-versicolor | ||
5.6,2.5,3.9,1.1,Iris-versicolor | ||
5.9,3.2,4.8,1.8,Iris-versicolor | ||
6.1,2.8,4.0,1.3,Iris-versicolor | ||
6.3,2.5,4.9,1.5,Iris-versicolor | ||
6.1,2.8,4.7,1.2,Iris-versicolor | ||
6.4,2.9,4.3,1.3,Iris-versicolor | ||
6.6,3.0,4.4,1.4,Iris-versicolor | ||
6.8,2.8,4.8,1.4,Iris-versicolor | ||
6.7,3.0,5.0,1.7,Iris-versicolor | ||
6.0,2.9,4.5,1.5,Iris-versicolor | ||
5.7,2.6,3.5,1.0,Iris-versicolor | ||
5.5,2.4,3.8,1.1,Iris-versicolor | ||
5.5,2.4,3.7,1.0,Iris-versicolor | ||
5.8,2.7,3.9,1.2,Iris-versicolor | ||
6.0,2.7,5.1,1.6,Iris-versicolor | ||
5.4,3.0,4.5,1.5,Iris-versicolor | ||
6.0,3.4,4.5,1.6,Iris-versicolor | ||
6.7,3.1,4.7,1.5,Iris-versicolor | ||
6.3,2.3,4.4,1.3,Iris-versicolor | ||
5.6,3.0,4.1,1.3,Iris-versicolor | ||
5.5,2.5,4.0,1.3,Iris-versicolor | ||
5.5,2.6,4.4,1.2,Iris-versicolor | ||
6.1,3.0,4.6,1.4,Iris-versicolor | ||
5.8,2.6,4.0,1.2,Iris-versicolor | ||
5.0,2.3,3.3,1.0,Iris-versicolor | ||
5.6,2.7,4.2,1.3,Iris-versicolor | ||
5.7,3.0,4.2,1.2,Iris-versicolor | ||
5.7,2.9,4.2,1.3,Iris-versicolor | ||
6.2,2.9,4.3,1.3,Iris-versicolor | ||
5.1,2.5,3.0,1.1,Iris-versicolor | ||
5.7,2.8,4.1,1.3,Iris-versicolor | ||
6.3,3.3,6.0,2.5,Iris-virginica | ||
5.8,2.7,5.1,1.9,Iris-virginica | ||
7.1,3.0,5.9,2.1,Iris-virginica | ||
6.3,2.9,5.6,1.8,Iris-virginica | ||
6.5,3.0,5.8,2.2,Iris-virginica | ||
7.6,3.0,6.6,2.1,Iris-virginica | ||
4.9,2.5,4.5,1.7,Iris-virginica | ||
7.3,2.9,6.3,1.8,Iris-virginica | ||
6.7,2.5,5.8,1.8,Iris-virginica | ||
7.2,3.6,6.1,2.5,Iris-virginica | ||
6.5,3.2,5.1,2.0,Iris-virginica | ||
6.4,2.7,5.3,1.9,Iris-virginica | ||
6.8,3.0,5.5,2.1,Iris-virginica | ||
5.7,2.5,5.0,2.0,Iris-virginica | ||
5.8,2.8,5.1,2.4,Iris-virginica | ||
6.4,3.2,5.3,2.3,Iris-virginica | ||
6.5,3.0,5.5,1.8,Iris-virginica | ||
7.7,3.8,6.7,2.2,Iris-virginica | ||
7.7,2.6,6.9,2.3,Iris-virginica | ||
6.0,2.2,5.0,1.5,Iris-virginica | ||
6.9,3.2,5.7,2.3,Iris-virginica | ||
5.6,2.8,4.9,2.0,Iris-virginica | ||
7.7,2.8,6.7,2.0,Iris-virginica | ||
6.3,2.7,4.9,1.8,Iris-virginica | ||
6.7,3.3,5.7,2.1,Iris-virginica | ||
7.2,3.2,6.0,1.8,Iris-virginica | ||
6.2,2.8,4.8,1.8,Iris-virginica | ||
6.1,3.0,4.9,1.8,Iris-virginica | ||
6.4,2.8,5.6,2.1,Iris-virginica | ||
7.2,3.0,5.8,1.6,Iris-virginica | ||
7.4,2.8,6.1,1.9,Iris-virginica | ||
7.9,3.8,6.4,2.0,Iris-virginica | ||
6.4,2.8,5.6,2.2,Iris-virginica | ||
6.3,2.8,5.1,1.5,Iris-virginica | ||
6.1,2.6,5.6,1.4,Iris-virginica | ||
7.7,3.0,6.1,2.3,Iris-virginica | ||
6.3,3.4,5.6,2.4,Iris-virginica | ||
6.4,3.1,5.5,1.8,Iris-virginica | ||
6.0,3.0,4.8,1.8,Iris-virginica | ||
6.9,3.1,5.4,2.1,Iris-virginica | ||
6.7,3.1,5.6,2.4,Iris-virginica | ||
6.9,3.1,5.1,2.3,Iris-virginica | ||
5.8,2.7,5.1,1.9,Iris-virginica | ||
6.8,3.2,5.9,2.3,Iris-virginica | ||
6.7,3.3,5.7,2.5,Iris-virginica | ||
6.7,3.0,5.2,2.3,Iris-virginica | ||
6.3,2.5,5.0,1.9,Iris-virginica | ||
6.5,3.0,5.2,2.0,Iris-virginica | ||
6.2,3.4,5.4,2.3,Iris-virginica | ||
5.9,3.0,5.1,1.8,Iris-virginica | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# !/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
|
||
import pandas as pd | ||
|
||
from python3.perceptron import Perceptron | ||
|
||
|
||
# 定义激活函数f | ||
def active_function(x): | ||
# return x | ||
if x < 0.5: | ||
return 0 | ||
elif x > 0.5 and x < 2: | ||
return 1 | ||
elif x > 2: | ||
return 2 | ||
|
||
|
||
class LinearUnit(Perceptron): | ||
def __init__(self, input_num): | ||
'''初始化线性单元,设置输入参数的个数''' | ||
Perceptron.__init__(self, input_num, activator_fun=active_function) | ||
|
||
|
||
def get_training_dataset(): | ||
''' | ||
捏造5个人的收入数据 | ||
''' | ||
# 构建训练数据 | ||
# 输入向量列表,每一项是工作年限 | ||
data = pd.read_csv('iris.data', header=None) | ||
# take [0, 1] columns of data to be x, take 4 column and do categorical codes as y. | ||
# y now is an array composed by 0 (stands for Iris-setosa), 1 (stands for Iris-versicolor), and | ||
# 2 (stands for Iris-virginica) | ||
x, y = data[[0, 1, 2, 3]], pd.Categorical(data[4]).codes | ||
# pd.to_list | ||
|
||
# print(x.values.tolist()) | ||
input_vecs = [x.values.tolist()][0] | ||
labels = y.tolist() | ||
print(type(labels), labels) | ||
# labels = y. | ||
print(input_vecs) | ||
# 期望的输出列表,月薪,注意要与输入一一对应 | ||
# labels = [5500, 2300, 7600, 1800, 11400] | ||
return input_vecs, labels | ||
|
||
|
||
def train_linear_unit(): | ||
''' | ||
使用数据训练线性单元 | ||
''' | ||
# 创建感知器,输入参数的特征数为1(工作年限) | ||
lu = LinearUnit(4) | ||
# 训练,迭代10轮, 学习速率为0.01 | ||
input_vecs, labels = get_training_dataset() | ||
lu.train(input_vecs, labels, 10000, 0.001) | ||
# 返回训练好的线性单元 | ||
return lu | ||
|
||
|
||
# def plot(linear_unit): | ||
# import matplotlib.pyplot as plt | ||
# input_vecs, labels = get_training_dataset() | ||
# fig = plt.figure() | ||
# ax = fig.add_subplot(111) | ||
# ax.scatter(map(lambda x: x[0], input_vecs), labels) | ||
# weights = linear_unit.weights | ||
# bias = linear_unit.bias | ||
# x = range(0,12,1) | ||
# y = map(lambda x:weights[0] * x + bias, x) | ||
# ax.plot(x, y) | ||
# plt.show() | ||
|
||
|
||
if __name__ == '__main__': | ||
'''训练线性单元''' | ||
|
||
# data = pd.read_csv('iris.data', header=None) | ||
# take [0, 1] columns of data to be x, take 4 column and do categorical codes as y. | ||
# y now is an array composed by 0 (stands for Iris-setosa), 1 (stands for Iris-versicolor), and | ||
# 2 (stands for Iris-virginica) | ||
# x, y = data[[0, 1, 2, 3]], pd.Categorical(data[4]).codes | ||
# pd.to_list | ||
|
||
# print(x, y) | ||
linear_unit = train_linear_unit() | ||
print("predict") | ||
print(linear_unit.predict([5.1, 3.5, 1.4, 0.2])) | ||
print(linear_unit.predict([5.9, 3.0, 5.1, 1.8])) | ||
print(linear_unit.predict([6.0,2.2,5.0,1.5])) | ||
print(linear_unit.predict([6.0, 3.4, 4.5, 1.6])) | ||
|
||
# 打印训练获得的权重 |