[SOLVED] 程序代写代做代考 database python information theory In [12]:

30 $

File Name: 程序代写代做代考_database_python_information_theory_In [12]:.zip
File Size: 649.98 KB

SKU: 4978387892 Category: Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Or Upload Your Assignment Here:


In [12]:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import math
%matplotlib inline
In [13]:
from sklearn.datasets import load_iris
iris = load_iris()
print(iris[‘DESCR’])

.. _iris_dataset:

Iris plants dataset
——————–

**Data Set Characteristics:**

:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
– sepal length in cm
– sepal width in cm
– petal length in cm
– petal width in cm
– class:
– Iris-Setosa
– Iris-Versicolour
– Iris-Virginica

:Summary Statistics:

============== ==== ==== ======= ===== ====================
MinMax MeanSD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.37.9 5.84 0.830.7826
sepal width:2.04.4 3.05 0.43 -0.4194
petal length: 1.06.9 3.76 1.760.9490(high!)
petal width:0.12.5 1.20 0.760.9565(high!)
============== ==== ==== ======= ===== ====================

:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%[email protected])
:Date: July, 1988

The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher’s paper. Note that it’s the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.

This is perhaps the best known database to be found in the
pattern recognition literature.Fisher’s paper is a classic in the field and
is referenced frequently to this day.(See Duda & Hart, for example.)The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

.. topic:: References

– Fisher, R.A. “The use of multiple measurements in taxonomic problems”
Annual Eugenics, 7, Part II, 179-188 (1936); also in “Contributions to
Mathematical Statistics” (John Wiley, NY, 1950).
– Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.
(Q327.D83) John Wiley & Sons.ISBN 0-471-22361-1.See page 218.
– Dasarathy, B.V. (1980) “Nosing Around the Neighborhood: A New System
Structure and Classification Rule for Recognition in Partially Exposed
Environments”.IEEE Transactions on Pattern Analysis and Machine
Intelligence, Vol. PAMI-2, No. 1, 67-71.
– Gates, G.W. (1972) “The Reduced Nearest Neighbor Rule”.IEEE Transactions
on Information Theory, May 1972, 431-433.
– See also: 1988 MLC Proceedings, 54-64.Cheeseman et al”s AUTOCLASS II
conceptual clustering system finds 3 classes in the data.
– Many, many more …
In [14]:
from pandas.plotting import scatter_matrix
import pandas as pd
iris_data = pd.DataFrame(data=iris[‘data’],columns=iris[‘feature_names’])
iris_data[“target”] = iris[‘target’]
color_wheel = {1: “#0392cf”, 2: “#7bc043”, 3: “#ee4035”}
colors = iris_data[“target”].map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(iris_data, color=colors, alpha=0.6, figsize=(15, 15))


In [15]:
# Select first 2 flower classes (~100 rows)
# And first 2 features
sepal_len = iris[‘data’][:100,0]
sepal_wid = iris[‘data’][:100,1]
labels = iris[‘target’][:100]
# We will also center the data
# This is done to make numbers nice, so that we have no
# need for biases in our classification. (You might not
# be able to remove biases this way in general.)
sepal_len -= np.mean(sepal_len)
sepal_wid -= np.mean(sepal_wid)
In [16]:
# Plot Iris
plt.scatter(sepal_len,sepal_wid,c=labels,cmap=plt.cm.Paired)
plt.xlabel(“sepal length”)
plt.ylabel(“sepal width”)
Out[16]:
Text(0, 0.5, ‘sepal width’)


In [17]:
def plot_sep(w1, w2, color=’green’):
”’ Plot decision boundary hypothesis
w1 * sepal_len + w2 * sepal_wid = 0
in input space, highlighting the hyperplane ”’
plt.scatter(sepal_len,sepal_wid,c=labels,cmap=plt.cm.Paired)
plt.title(“Separation in Input Space”)
plt.ylim([-1.5,1.5])
plt.xlim([-1.5,2])
plt.xlabel(“sepal length”)
plt.ylabel(“sepal width”)
if w2 != 0:
m = -w1/w2
t = 1 if w2 > 0 else -1
plt.plot([-1.5,2.0],[-1.5*m, 2.0*m],’-y’,color=color)
plt.fill_between(
[-1.5, 2.0],
[m*-1.5, m*2.0],
[t*1.5, t*1.5],
alpha=0.2,
color=color)
if w2 == 0: # decision boundary is vertical
t = 1 if w1 > 0 else -1
plt.plot([0, 0],[-1.5, 2.0],’-y’,color=color)
plt.fill_between(
[0, 2.0*t],
[-1.5, -2.0],
[1.5, 2],
alpha=0.2,
color=color)
In [18]:
plot_sep(0, 1)


In [19]:
plot_sep(-0.5, 1)


In [20]:
# We’re going to hand pick one point and # analyze that point:
a1 = sepal_len[41]
a2 = sepal_wid[41]
print (a1, a2) # (-0.97, -0.79)
plot_sep(-0.5, 1)
plt.plot(a1, a2, ‘ob’) # highlight the point

-0.971000000000001 -0.7989999999999999
Out[20]:
[ ]


In [21]:
# Now let’s look at weight space
def plot_weight_space(sepal_len, sepal_wid, lab=1, color=’steelblue’,
maxlim=2.0):
plt.title(“Constraint(s) in Weight Space”)
plt.ylim([-maxlim,maxlim])
plt.xlim([-maxlim,maxlim])
plt.xlabel(“w1”)
plt.ylabel(“w2”)
if sepal_wid != 0:
m = -sepal_len/sepal_wid
t = 1*lab if sepal_wid > 0 else -1*lab
plt.plot([-maxlim, maxlim],
[-maxlim*m, maxlim*m],
‘-y’,
color=color)
plt.fill_between([-maxlim, maxlim], # x
[m*-maxlim, m*maxlim], # y-min
[t*maxlim, t*maxlim], # y-max alpha=0.2,
color=color)
if sepal_wid == 0: # decision boundary is vertical
t = 1*lab if sepal_len > 0 else -1*lab
plt.plot([0, 0],[-maxlim, maxlim],’-y’,color=color)
plt.fill_between([0, 2.0*t],
[-maxlim, -maxlim],
[maxlim, maxlim],
alpha=0.2,
color=color)
In [22]:
# Plot the constraint for the point identified earlier:
a1 = sepal_len[41]
a2 = sepal_wid[41]
print (a1, a2)
# Do this on the board first by hand
plot_weight_space(a1, a2, lab=1)
# Below is the hypothesis we plotted earlier
# Notice it falls outside the range.
plt.plot(-0.5, 1, ‘og’)

-0.971000000000001 -0.7989999999999999
Out[22]:
[ ]


In [23]:
w1 = -0.5 # + …
w2= 1 #+…
In [24]:
# This should bring the point closer to the boundary # In this case, the step brought the point into the # condition boundary
plot_weight_space(a1, a2, lab=1)
plt.plot(-0.5+a1, 1+a2, ‘og’)
# old hypothesis
plt.plot(-0.5, 1, ‘og’)
plt.plot([-0.5, -0.5+a1], [1, 1+a2], ‘-g’)
plt.axes().set_aspect(‘equal’, ‘box’)

/Users/Andy/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:7: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.In a future version, a new instance will always be created and returned.Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
import sys


In [25]:
# Which means that the point (a1, a2) in input
# space is correctly classified.
plot_sep(-0.5+a1, 1+a2)


In [26]:
sepal_len
Out[26]:
array([-0.371, -0.571, -0.771, -0.871, -0.471, -0.071, -0.871, -0.471,
-1.071, -0.571, -0.071, -0.671, -0.671, -1.171,0.329,0.229,
-0.071, -0.371,0.229, -0.371, -0.071, -0.371, -0.871, -0.371,
-0.671, -0.471, -0.471, -0.271, -0.271, -0.771, -0.671, -0.071,
-0.271,0.029, -0.571, -0.471,0.029, -0.571, -1.071, -0.371,
-0.471, -0.971, -1.071, -0.471, -0.371, -0.671, -0.371, -0.871,
-0.171, -0.471,1.529,0.929,1.429,0.029,1.029,0.229,
0.829, -0.571,1.129, -0.271, -0.471,0.429,0.529,0.629,
0.129,1.229,0.129,0.329,0.729,0.129,0.429,0.629,
0.829,0.629,0.929,1.129,1.329,1.229,0.529,0.229,
0.029,0.029,0.329,0.529, -0.071,0.529,1.229,0.829,
0.129,0.029,0.029,0.629,0.329, -0.471,0.129,0.229,
0.229,0.729, -0.371,0.229])
In [27]:
sepal_wid
Out[27]:
array([ 4.010e-01, -9.900e-02,1.010e-01,1.000e-03,5.010e-01,
8.010e-01,3.010e-01,3.010e-01, -1.990e-01,1.000e-03,
6.010e-01,3.010e-01, -9.900e-02, -9.900e-02,9.010e-01,
1.301e+00,8.010e-01,4.010e-01,7.010e-01,7.010e-01,
3.010e-01,6.010e-01,5.010e-01,2.010e-01,3.010e-01,
-9.900e-02,3.010e-01,4.010e-01,3.010e-01,1.010e-01,
1.000e-03,3.010e-01,1.001e+00,1.101e+00,1.000e-03,
1.010e-01,4.010e-01,5.010e-01, -9.900e-02,3.010e-01,
4.010e-01, -7.990e-01,1.010e-01,4.010e-01,7.010e-01,
-9.900e-02,7.010e-01,1.010e-01,6.010e-01,2.010e-01,
1.010e-01,1.010e-01,1.000e-03, -7.990e-01, -2.990e-01,
-2.990e-01,2.010e-01, -6.990e-01, -1.990e-01, -3.990e-01,
-1.099e+00, -9.900e-02, -8.990e-01, -1.990e-01, -1.990e-01,
1.000e-03, -9.900e-02, -3.990e-01, -8.990e-01, -5.990e-01,
1.010e-01, -2.990e-01, -5.990e-01, -2.990e-01, -1.990e-01,
-9.900e-02, -2.990e-01, -9.900e-02, -1.990e-01, -4.990e-01,
-6.990e-01, -6.990e-01, -3.990e-01, -3.990e-01, -9.900e-02,
3.010e-01,1.000e-03, -7.990e-01, -9.900e-02, -5.990e-01,
-4.990e-01, -9.900e-02, -4.990e-01, -7.990e-01, -3.990e-01,
-9.900e-02, -1.990e-01, -1.990e-01, -5.990e-01, -2.990e-01])
In [28]:
labels
Out[28]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In [29]:
sgn_labels=labels.copy()
In [30]:
sgn_labels
Out[30]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In [31]:
for i in range(0,sgn_labels.size,1):
if sgn_labels[i] == 0:
sgn_labels[i]=-1
In [32]:
sgn_labels
Out[32]:
array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])
In [33]:
def plr2d(x1,x2,t,w0_0,w1_0,w2_0,N,disable_w0):
w0=w0_0
w1=w1_0
w2=w2_0
for n in range(0,N,1):
mismatched = False
for i in range(0,t.size,1):
z=w0 + x1[i]*w1 + x2[i]*w2
if z*(t[i]) <= 0:mismatched = Trueif disable_w0:w0 = w0else:w0 = w0 + (t[i])*1w1 = w1 + (t[i])*(x1[i])w2 = w2 + (t[i])*(x2[i])if mismatched == False:print(“converged: n=”,n)breakprint(“learning done”)for i in range(0,t.size,1):z=w0 + x1[i]*w1 + x2[i]*w2if z*(t[i]) <= 0:print(“mismatch[“,i,”]”)return [w0,w1,w2]In [34]:[wt0,wt1,wt2]=plr2d(sepal_len,sepal_wid,sgn_labels,0,0,0,10,True)print([wt0,wt1,wt2])plot_sep(wt1,wt2)converged: n= 1learning done[0, 0.37100000000000133, -0.40100000000000025] In [35]:# reference ANDx1=np.array([0,0,1,1])x2=np.array([0,1,0,1])t=np.array([-1,-1,-1,1])[w0,w1,w2]=plr2d(x1,x2,t,0,0,0,100,False)print([w0,w1,w2])converged: n= 8learning done[-4, 3, 2]In [36]:# reference ORx1=np.array([0,0,1,1])x2=np.array([0,1,0,1])t=np.array([-1,1,1,1])[w0,w1,w2]=plr2d(x1,x2,t,0,0,0,100,False)print([w0,w1,w2])converged: n= 5learning done[-1, 2, 2]In [37]:# reference NOTx1=np.array([1,1])x2=np.array([0,1])t=np.array([1,-1])[w0,w1,w2]=plr2d(x1,x2,t,0,0,0,100,False)print([w0,w1,w2])converged: n= 4learning done[1, 1, -3]In [38]:# Vectorize plr2ddef plr2d_vectorize(X ,T, N):## YOUR CODE HERE# Note: W is shaped nx1 (for w_0,…w_n-1) that includes w0 e.g. [[w0], [w1], [w2], …]return W

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] 程序代写代做代考 database python information theory In [12]:
30 $