-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
101 lines (77 loc) · 2.85 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 24 13:38:24 2019
Iris dataset linear regression
@author: DuangSUSE
"""
from pandas import Series, read_csv, options as pdopts
iris = read_csv('Iris.csv', encoding='utf-8', parse_dates=[], index_col=False)
iris['id'] = Series().astype(int)
###
pdopts.mode.chained_assignment = None # default 'warn'
###
###
def vectorize(w,i, cname='w', cid='id', iris=iris): iris.loc[iris[cname]== w, cid] = i
vectorize('setosa', 0); vectorize('versicolor', 1); vectorize('virginica', 2)
from sklearn.model_selection import train_test_split
iris_ds = iris.copy()
trainset, testset, trainsetid, testsetid = train_test_split(iris_ds, iris_ds['id'], train_size = 0.6)
del trainset['w'], trainset['id']
###
from sklearn.linear_model import LinearRegression
#from math import floor
lreg = LinearRegression()
lreg.fit(trainset, trainsetid)
testset_truthw, testset_truthid = testset['w'], testset['id']
del testset['w'], testset['id']
testset['predict'] = lreg.predict(testset)
testset['id'] = testset_truthid
##
from pandas import DataFrame
def verifyRegressionAccuracy(ts: DataFrame, emax: float = 0.1, npredict = 'predict', ntruth = 'real') -> float:
predicteds, truths = ts[npredict], ts[ntruth]
acceptables = [t for (r, t) in zip(truths, predicteds) if abs(t - r) <emax]
return (len(acceptables) / len(predicteds)) *100
print("Error region 1.0", verifyRegressionAccuracy(testset, 1.0, 'predict', 'id'),sep=':')
print("0.1", verifyRegressionAccuracy(testset, 1.0, ntruth='id'),sep=':')
###: testset
vectorize('setosa', 1); vectorize('versicolor', 2); vectorize('virginica', 3)
iris_ds = iris.copy()
trainset, testset, trainsetid, testsetid = train_test_split(iris_ds, iris_ds['id'], train_size = 0.6)
del trainset['w'], trainset['id']
lreg = LinearRegression()
lreg.fit(trainset, trainsetid)
testset_truthw = testset['w']; testset_truthid = testset['id']
del testset['w'], testset['id']
testset['predict'] = lreg.predict(testset)
testset['id'] = testset_truthid
from math import floor
def flowername(w):
value_map = {-1: 'setosa', 0: 'setosa', 1: 'versicolor', 2: 'virginica', 3: 'err'}
return value_map[floor(w)]
testset['guess'] = testset['predict'].map(flowername)
testset['w'] = testset_truthw
testset.describe()
print(testset.head(100))
###
testset['predict'] = testset['predict'].map(round)
print(verifyRegressionAccuracy(testset, 1, 'predict', 'id'))
print(testset.head(43))
###
from sys import stderr
print('RPPL, Read-Predict-Print-Loop.')
print('Format: x y z')
while True:
try:
line = input('⇝')
x,y,z = [float(nv) for nv in line.split(None, 2)]
except ValueError:
print('Failed to split inpit ', line, file=stderr)
continue
except KeyboardInterrupt:
break
except EOFError:
break
predication = lreg.predict([[x,y,z]])
print('Predict', predication, flowername(predication), sep=' = ')