#What is Numpy?
#Support for large multidimensional arrays and matrices,
#It provides a large, high-level library of mathematical functions for manipulating these.
http://rest-term.com/archives/2999/
http://wbhappy.hatenablog.jp/entry/2015/02/06/210000
# ndarray.flags Memory layout information for array data
# ndarray.Number of dimensions of ndim array
# ndarray.number of elements in the size array
# ndarray.shape Number of elements in each dimension
# ndarray.itemsize Number of bytes per element
# ndarray.strides Number of bytes required to move to the next element in each dimension
# ndarray.nbytes Number of bytes in the entire array
# ndarray.dtype Array element data type(numpy.dtype)
>>> import numpy as np
>>> np.version.full_version
'1.8.0rc1'
>>> a = np.array([0,1,2,3,4,5])
>>> a
array([0, 1, 2, 3, 4, 5])
>>> a.ndim
1
>>> a.shape
(6,)
>>> b = a.reshape((3,2))
>>> b
array([[0, 1],
[2, 3],
[4, 5]])
>>> b.ndim
2
>>> b.shape
(3, 2)
>>> b[1][0] = 77
>>> b
array([[ 0, 1],
[77, 3],
[ 4, 5]])
>>> a
array([ 0, 1, 77, 3, 4, 5])
>>> c = a.reshape((3,2)).copy()
>>> c
array([[ 0, 1],
[77, 3],
[ 4, 5]])
>>> c[0][0] = -99
>>> a
array([ 0, 1, 77, 3, 4, 5])
>>> c
array([[-99, 1],
[ 77, 3],
[ 4, 5]])
>>> a*2
array([ 0, 2, 154, 6, 8, 10])
>>> a**2
array([ 0, 1, 5929, 9, 16, 25])
>>> a[np.array([2,3,4])]
array([77, 3, 4])
>>> a>4
array([False, False, True, False, False, True], dtype=bool)
>>> a[a>4]
array([77, 5])
>>> a[a>4]=4
>>> a
array([0, 1, 4, 3, 4, 4])
>>> a.clip(0,4)
array([0, 1, 4, 3, 4, 4])
>>> c = np.array([1,2,np.NAN, 3,4]) #Assuming you read from a text file
>>> c
array([ 1., 2., nan, 3., 4.])
>>> np.isnan(c) #Replace missing values
array([False, False, True, False, False], dtype=bool)
>>> c[~np.isnan(c)]
array([ 1., 2., 3., 4.])
>>> np.mean(c[~np.isnan(c)])
2.5
>>> import timeit
>>> normal_py_sec = timeit.timeit('sum(x*x for x in xrange(1000))',number=10000)
>>> Naive_np_sec = timeit.timeit('sum(na*na)',setup="import numpy as np; na=np.arange(1000)", number=10000)
>>> good_np_sec = timeit.timeit('na.dot(na)',setup="import numpy as np; na=np.arange(1000)", number=10000)
>>> print("Normal Python: %f sec"%normal_py_sec)
Normal Python: 0.836571 sec
>>> print("Naive Numpy: %f sec"%Naive_np_sec)
Naive Numpy: 4.806356 sec
>>> print("Good Numpy: %f sec"%good_np_sec)
Good Numpy: 0.039245 sec
>>> a = np.array([1,2,3])
>>> a.dtype
dtype('int64')
>>> np.array([1, "stringry"])
array(['1', 'stringry'],
dtype='|S8')
>>> np.array([1, "stringy", set([1,2,3])])
array([1, 'stringy', set([1, 2, 3])], dtype=object)
# http://rest-term.com/archives/2999/Than
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) #Array generation
>>> a
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
>>> a.flags #Memory layout information for array data
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
>>> a.ndim #Number of dimensions
2
>>> a.size #Element count
12
>>> a.shape #Number of elements in each dimension(Number of lines,Number of columns)
(4, 3)
>>> a.itemsize #Number of bytes per element
8
>>> a.strides #24 bytes for the next row, 8 bytes for the next column
(24, 8)
>>> a.nbytes #Number of bytes in the entire array
96
>>> a.dtype #Element data type
dtype('int64')
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.ones([2,3])
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> np.identity(3) #Identity matrix
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.eye(3) #Identity matrix that allows you to specify the number of columns
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.arange(10) # range()Same as
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.araange(1,2,0.2) #start point,end point,incremental
array([ 1. , 1.2, 1.4, 1.6, 1.8])
>>> np.linspace(1,4,6) #Range that can specify the number of elements()
array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ])
>>> np.logspace(2,3,4) #Logarithm
array([ 100. , 215.443469 , 464.15888336, 1000. ])
>>> np.logspace(2,4,4, base=2) #Bottom 2
array([ 4. , 6.34960421, 10.0793684 , 16. ])
>>> np.tile([0,1,2,3,4], 2) #Returns an array of generated and repeated elements
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
>>> a,b = np.meshgrid([1,2,3],[4,5,6,7]) #A grid array that is evenly spaced vertically and horizontally
>>> a
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
>>> b
array([[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7]])
>>> np.tri(3) #Triangular matrix
array([[ 1., 0., 0.],
[ 1., 1., 0.],
[ 1., 1., 1.]])
>>> a = np.array([[0,1,2],[3,4,5],[6,7,8]])
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> np.diag(a) #An array with diagonal elements extracted from the input array
array([0, 4, 8])
>>> np.empty(5) #It is not initialized only by securing the area
array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323, 1.97626258e-323])
>>> a = np.array([1,2,3])
>>> b = a.copy()
>>> b
array([1, 2, 3])
>>> np.random.randint(0,100,10) #Range of random numbers to generate(minimum value,Maximum value,Element count)Specify
array([67, 65, 61, 15, 48, 57, 42, 21, 49, 57])
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a = np.arange(10)
>>> b = a.reshape((2,5)) #Change of array shape(In this case to a two-dimensional array)
>>> b
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> a.resize((2,5)) #Change to a two-dimensional array with the same number of elements
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> a = np.tile(np.arange(3),3) # range(3)3-row grid
>>> a
array([0, 1, 2, 0, 1, 2, 0, 1, 2])
>>> np.argmax(a) #The smallest index of the maximum value elements
2
>>> np.argmin(a) #The smallest index of the minimum element
0
>>> a = np.eye(3) #3 array
>>> a
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.nonzero(a) #Returns an index array of nonzero elements(In this case, it is a two-dimensional array, so two)
(array([0, 1, 2]), array([0, 1, 2]))
>>> a = np.arange(15).reshape((3,5))
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> np.where(a%2==0) #Index array of elements that match the conditions
(array([0, 0, 0, 1, 1, 2, 2, 2]), array([0, 2, 4, 1, 3, 0, 2, 4]))
>>> a = np.arange(10)
>>> np.select([a<3, a>5],[a, a**2]) #Multiple condition search first argument:Array of conditions Second argument:An array of values to set in the index of the element that matches the condition
array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
>>> a = np.arange(9).reshape((3,3))
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> b = np.arange(8,-1,-1).reshape((3,3))
>>> b
array([[8, 7, 6],
[5, 4, 3],
[2, 1, 0]])
>>> np.dstack((a,b)) #Combine two-dimensional arrays into a three-dimensional array
array([[[0, 8],
[1, 7],
[2, 6]],
[[3, 5],
[4, 4],
[5, 3]],
[[6, 2],
[7, 1],
[8, 0]]])
>>> np.hstack((a,b)) #Join in column direction
array([[0, 1, 2, 8, 7, 6],
[3, 4, 5, 5, 4, 3],
[6, 7, 8, 2, 1, 0]])
>>> np.vstack((a,b)) #Join in row direction
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[8, 7, 6],
[5, 4, 3],
[2, 1, 0]])
>>> a = np.arange(16).reshape(2,2,4)
>>> a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
>>> np.dsplit(a,2) #Split 3D array
[array([[[ 0, 1],
[ 4, 5]],
[[ 8, 9],
[12, 13]]]), array([[[ 2, 3],
[ 6, 7]],
[[10, 11],
[14, 15]]])]
>>> a = np.arange(16).reshape(4,4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> np.hsplit(a,2) #Split in column direction
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
>>> np.vsplit(a,2) #Split in the row direction
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]
>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
[3, 4]])
>>> np.transpose(a) #Transpose the array
array([[1, 3],
[2, 4]])
>>> a = np.array([[1,2,3]])
>>> np.swapaxes(a,0,1) #Shaft replacement
array([[1],
[2],
[3]])
>>> a = np.random.randint(0,500,20)
>>> a
array([444, 97, 324, 492, 275, 95, 157, 336, 51, 249, 363, 409, 299,
432, 41, 469, 201, 308, 85, 455])
>>> np.amax(a) #Maximum value
492
>>> np.amin(a) #minimum value
41
>>> np.ptp(a) #Range of values(Maximum value-minimum value)
451
>>> np.mean(a) #Arithmetic mean
279.10000000000002
>>> np.median(a) #Median
303.5
>>> np.std(a) #standard deviation
146.4031761950539
>>> np.var(a) #Distributed
21433.889999999999
>>> b = np.random.randint(0,500,20)
>>> b
array([375, 207, 495, 320, 472, 481, 491, 133, 279, 480, 232, 261, 492,
183, 168, 424, 95, 236, 176, 332])
>>> np.corrcoef(a,b) #Correlation coefficient
array([[ 1. , 0.12452095],
[ 0.12452095, 1. ]])
>>> c = np.random.randint(0,10,20)
>>> c
array([6, 5, 9, 7, 9, 6, 4, 0, 1, 4, 6, 3, 2, 7, 9, 3, 4, 9, 4, 8])
>>> np.histogram(c) #histogram
(array([1, 1, 1, 2, 4, 1, 3, 2, 1, 4]), array([ 0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ]))
#What is SciPy?
#Provides many algorithms
# cluster :Hierarchical clustering/Vector quantization/K-means
# constants :Physics math constant
# fftpack :Fourier definition
# integrate :Integral
# interpolate :interpolation(linear,Cubic etc.)
# io :Data input / output
# linalg :Linear algebra routines using BLAS and LAPACK libraries
# maxentropy :Entropy distribution
# ndimage :n-dimensional image package
# odr :Total least squares
# optimize :optimisation
# signal :Signal processing
# sparse :Sparse matrix
# spatial :Spatial data structures and algorithms
# special :Special functions such as Bessel function and Yakkobian
# stats :statistics
>>> import scipy, numpy
>>> scipy.version.full_version
'0.13.0b1'
>>> scipy.dot is numpy.dot
True #Namespace is the same as Numpy
>>> data = sp.genfromtxt("sample/ch01/data/web_traffic.tsv", delimiter='\t')
>>> print(data.shape)
(743, 2)
>>> print(data[:10])
>>> import scipy as sp
>>> data = sp.genfromtxt("./sample/ch01/data/web_traffic.tsv", delimiter="\t") #Data read
>>> print(data[:10])
[ 2.00000000e+00 1.65600000e+03]
[ 3.00000000e+00 1.38600000e+03]
[ 4.00000000e+00 1.36500000e+03]
[ 5.00000000e+00 1.48800000e+03]
[ 6.00000000e+00 1.33700000e+03]
[ 7.00000000e+00 1.88300000e+03]
[ 8.00000000e+00 2.28300000e+03]
[ 9.00000000e+00 1.33500000e+03]
[ 1.00000000e+01 1.02500000e+03]]
>>> print(data.shape)
(743, 2)
>>> x = data[:,0] #elapsed time(SciPy:Extract the 0th dimension)
>>> y = data[:,1] #Number of accesses
>>> sp.sum(sp.isnan(y)) #Inappropriate value
0
>>> x = x[~sp.isnan(y)] #Get rid of inappropriate values
>>> y = y[~sp.isnan(y)] #Get rid of inappropriate values
>>> import matplotlib.pyplot as plt #Scatter plot
>>> plt.scatter(x,y)
<matplotlib.collections.PathCollection object at 0x11192e5d0>
>>> plt.title("Web traffic over the last month")
<matplotlib.text.Text object at 0x1118f7c90>
>>> plt.xlabel("Time")
<matplotlib.text.Text object at 0x111636090>
>>> plt.ylabel("Hits/hour")
<matplotlib.text.Text object at 0x111649fd0>
>>> plt.xticks([w*7*24 for w in range(10)], ['week %i' %w for w in range(10)])
([<matplotlib.axis.XTick object at 0x10e349710>, <matplotlib.axis.XTick object at 0x111653450>, <matplotlib.axis.XTick object at 0x11192edd0>, <matplotlib.axis.XTick object at 0x1119514d0>, <matplotlib.axis.XTick object at 0x111951c10>, <matplotlib.axis.XTick object at 0x113505390>, <matplotlib.axis.XTick object at 0x113505ad0>, <matplotlib.axis.XTick object at 0x11350e250>, <matplotlib.axis.XTick object at 0x11350e990>, <matplotlib.axis.XTick object at 0x11351a110>], <a list of 10 Text xticklabel objects>)
>>> plt.autoscale(tight=True)
>>> plt.grid()
>>> plt.show()
#Curve fitting
# polyfit(x,y,n) :Functions used for regression analysis(Regression analysis of two variables with n-th order equation)
#regression analysis...A method for obtaining a prediction formula (regression line) for predicting future values from one of two variables that are thought to have a correlation or causality.
>>> def error(f, x, y): #Error assuming the model function f exists
... return sp.sum((f(x)-y)**2)
>>> fp1, residuals, rank, sv, rcond = sp.polyfit(x, y, 1, full=True) #x with polyfit,Get the coefficients of a model that approximates y to the least squares
>>> print("Model parameters: %s" % fp1)
Model parameters: [ 2.57152281 1002.10684085]
>>> print(residuals) #Surplus
[ 3.19874315e+08]
>>> print(rank) #Matrix rank
2
>>> print(rcond) #Reciprocal of conditional number
1.64979141459e-13
reference: http://ktadaki.hatenablog.com/entry/2015/10/29/155340
I put values in 5 variables, but I only use the first fp1. The contents of fp1 look like this.
[ 2.59619213 989.02487106]
In other words, I got this formula.
f(x)=2.59619213x+989.02487106
>>> f1 = sp.poly1d(fp1) #Create model function
>>> print(error(f1,x,y))
319874314.777
>>> import matplotlib.pyplot as plt #Scatter plot
>>> plt.scatter(x,y)
<matplotlib.collections.PathCollection object at 0x11192e5d0>
>>> plt.title("Web traffic over the last month")
<matplotlib.text.Text object at 0x1118f7c90>
>>> plt.xlabel("Time")
<matplotlib.text.Text object at 0x111636090>
>>> plt.ylabel("Hits/hour")
<matplotlib.text.Text object at 0x111649fd0>
>>> plt.xticks([w*7*24 for w in range(10)], ['week %i' %w for w in range(10)]) #Rewrite the x-axis scale. In the argument, specify "where" and "what" to display in a list.
([<matplotlib.axis.XTick object at 0x10e349710>, <matplotlib.axis.XTick object at 0x111653450>, <matplotlib.axis.XTick object at 0x11192edd0>, <matplotlib.axis.XTick object at 0x1119514d0>, <matplotlib.axis.XTick object at 0x111951c10>, <matplotlib.axis.XTick object at 0x113505390>, <matplotlib.axis.XTick object at 0x113505ad0>, <matplotlib.axis.XTick object at 0x11350e250>, <matplotlib.axis.XTick object at 0x11350e990>, <matplotlib.axis.XTick object at 0x11351a110>], <a list of 10 Text xticklabel objects>)
>>> plt.autoscale(tight=True)
<matplotlib.legend.Legend object at 0x10c587ad0>
>>> fx = sp.linspace(0, x[-1], 1000) #For plotting"x value"Generate a
>>> plt.plot(fx, f1(fx), linewidth=4) #Draw the list as a graph
[<matplotlib.lines.Line2D object at 0x10c587850>]
>>> plt.legend(["d=%i" % f1.order], loc="upper left") #Show legend
>>> plt.grid()
>>> plt.show()
>>> f2p = sp.polyfit(x, y, 2)
>>> print(f2p)
[ 1.04688184e-02 -5.21727812e+00 1.96921629e+03]
>>> f2 = sp.poly1d(f2p)
>>> print(error(f2, x, y))
182006476.432
# f(x) = 0.0105322215 * x**2 - 5.26545650 * x + 1974.76802
>>> plt.plot(fx, f2(fx), linewidth=4)
#↑ Incorporate into the previous one
#More accurate curve, but complex function
#Degree-3,10,Tried at 100 → Overfitting
#I tried it with degree 1 → unlearned
#3 on the first straight line.Learning with data older than 5 weeks,The second straight line uses the data after that
>>> inflection = 3.5*7*24 #Calculate the time of change point
>>> xa = x[:inflection] #Data point before the change point
>>> ya = y[:inflection]
>>> xb = x[:inflection] #After the change point
>>> yb = y[:inflection]
>>> fa = sp.poly1d(sp.polyfit(xa, ya, 1))
>>> fb = sp.poly1d(sp.polyfit(xb, yb, 1))
>>> fa_error = error(fa, xa, ya)
>>> fb_error = error(fb, xb, yb)
>>> print("Error inflection=%f" % (fa_error + fb_error))
Error inflection=218985429.871767
# plt.plot(fx, fa(fx), linewidth=4)
# plt.plot(fx, fb(fx), linewidth=4)Display the figure in the same way
#Calculate the error using test data for the model trained using the data after the change point
>>> frac = 0.3 #Ratio of data used for the test
>>> split_idx = int(frac * len(xb))
>>> shuffled = sp.random.permutation(list(range(len(xb)))) #30 of all data%Randomly select
>>> test = sorted(shuffled[:split_idx]) #Index array of test data
>>> train = sorted(shuffled[split_idx:]) #Data index array for training
>>> #Train using each training data
>>> fbt1 = sp.poly1d(sp.polyfit(xb[train], yb[train], 1))
>>> fbt2 = sp.poly1d(sp.polyfit(xb[train], yb[train], 2))
>>> fbt3 = sp.poly1d(sp.polyfit(xb[train], yb[train], 3))
>>> fbt10 = sp.poly1d(sp.polyfit(xb[train], yb[train], 10))
>>> fbt100 = sp.poly1d(sp.polyfit(xb[train], yb[train], 100))
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/polynomial.py:579: RuntimeWarning: overflow encountered in multiply
scale = NX.sqrt((lhs*lhs).sum(axis=0))
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/polynomial.py:587: RankWarning: Polyfit may be poorly conditioned
warnings.warn(msg, RankWarning)
>>> #Evaluate using each training data
>>> for f in [fbt1, fbt2, fbt3, fbt10, fbt100]:
... print("Error d=%i: %f" % (f.order, error(f, xb[test], yb[test])))
...
Error d=1: 33618254.181783
Error d=2: 31298428.161162
Error d=3: 30849423.817712
Error d=10: 28969336.428648
Error d=55: 28919778.656526
#100 requests per hour,Expected to exceed 000-Find the solution of the quadratic equation
#100 from polynomial,Subtract 000 to create a new polynomial and find the root for that new polynomial
>>> print(fbt2)
2
0.004136 x - 1.662 x + 1677
>>> print(fbt2-100000)
2
0.004136 x - 1.662 x - 9.832e+04
>>> from scipy.optimize import fsolve
>>> reached_max = fsolve(fbt2-100000, 800)/(7*24)
>>> print("100,000 hits/hour expected at week %f" % reached_max[0])
100,000 hits/hour expected at week 30.241873
#Chapter 2 P27
#Classification/Supervised learning
#Iris dataset
#Extra small amount
#triangle:Setosa Maru:Versucikir Punishment:Virginica
>>> from matplotlib import pyplot as plt
>>> from sklearn.datasets import load_iris
>>> import numpy as np
>>> data = load_iris() #load from sklearn_Load data using iris function
>>> features = data['data']
>>> feature_names = data['feature_names']
>>> target = data['target']
>>> target_names = data['target_names']
>>> labels = target_names[target] # ?
>>> for t,marker,c in zip(range(3), ">ox","rgb"):
... plt.scatter(features[target == t,0],
... features[target == t,1],
... marker = marker,
... c = c) #Plot with markers of different colors for each class
...
<matplotlib.collections.PathCollection object at 0x10a5ec668>
<matplotlib.collections.PathCollection object at 0x10a287208>
<matplotlib.collections.PathCollection object at 0x10a5fa908>
#The "petal length" is stored third in the array.
>>> plength = features[:, 2]
>>> is_setosa = (labels == 'setosa') #Generate a boolean array of whether setosa or not
>>> max_setosa = plength[is_setosa].max()
>>> min_non_setosa = plength[~is_setosa].min()
>>> print('Maximum of setosa: {0}.'.format(max_setosa))
Maximum of setosa: 1.9. #Maximum petal length->1.9
>>> print('Minimum of others: {0}.'.format(min_non_setosa))
Minimum of others: 3.0. #Minimum petal length->3.0
>>> def apply_model( example ):
... if example[2] < 2:
... print("Iris Setosa")
... else:
... print("Iris Virginica or Itis Versicolor")
#Make the difference between other irises in the best possible way
>>> features = features[~is_setosa]
>>> labels = labels[~is_setosa]
>>> virginica = (labels == 'virginica')
>>> best_acc = -1.0
>>> best_fi = -1.0
>>> best_t = -1.0
>>> for fi in range(features.shape[1]): #Generate threshold candidates for each extra minute
... thresh = features[:,fi].copy()
... thresh.sort()
... for t in thresh: #Test at all thresholds
... pred = (features[:,fi] > t)
... acc = (labels[pred] == 'virginica').mean()
... if acc > best_acc:
... best_acc = acc
... best_fi = fi
... best_t = t
>>> def apply_model( example ):
... if(example[best_fi] > best_t):
... print("virginica")
... else:
... print("virsicolor")
# heldout.Start py
# python3 ./sample/ch02/heldout.py
>>> from threshold import learn_model, apply_model, accuracy
>>> for ei in range(len(features)): #All data except ei th will be used
... training = np.ones(len(features), bool)
... training[ei] = False
... testing = ~training
... model = learn_model(features[training], virginica[training])
... predictions = apply_model(features[testing], virginica[testing], model)
... error += np.sum(predictions != virginica[testing])
Recommended Posts