Question

Convert THE CODE BELOW to a 3-class classifier (or rewrite it in Python in the same...

Convert THE CODE BELOW to a 3-class classifier (or rewrite it in Python in the same style – level of abstraction) for the Iris dataset. Decide the best subset of 2-variables for the classification task.

close all
clear
load fisheriris
ntr=75;
nts = size(meas,1) - ntr;

%rand('seed',12345)
rp=randperm(150);

usevars = [1, 2];
predvar = 4;
trainx=[ones(ntr,1) meas(rp(1:ntr),usevars)];
trainr=meas(rp(1:ntr),predvar);

testx=[ones(nts,1) meas(rp(ntr+1:150),usevars)];
testr=meas(rp(ntr+1:150),predvar);

H=50;
I=size(trainx,2);

w=rand(H,I)*0.2-0.1;
v=rand(1,H+1)*0.02-0.01;
v(1)=mean(trainr);

Nepochs=1000;
MSEtrain = zeros(1,Nepochs);
MSEtest = zeros(1,Nepochs);

lr = 0.01;
for epoch = 1:Nepochs
    %dw = zeros(size(w));
    for t = randperm(ntr)
        
        x = trainx(t,:)';
        z = logsig(w*x);
        z = [1; z];
        y = v * z;
        r = trainr(t);
        
        dv = (r-y)*z';
        
        for h = 2:H+1
            for i = 1 : I
                dw(h-1,i) = (r - y) * v(h) * z(h)*(1-z(h)) * x(i);
            end
        end
        v = v + lr*dv;
        w = w + lr*dw;
%        MSEtrain(epoch) = MSEtrain(epoch) + (r - y).^2;
    end    
    
    for t = 1:ntr 
        x = trainx(t,:)';
        z = logsig(w*x);
        z = [1; z];
        ytest = v * z;
        MSEtrain(epoch) = MSEtrain(epoch) + ((trainr(t) - ytest).^2);
    end
    
    for t = 1:nts
        x = testx(t,:)';
        z = logsig(w*x);
        z = [1; z];
        ytest = v * z;
        MSEtest(epoch) = MSEtest(epoch) + ((testr(t) - ytest).^2);
    end
end

MSEtrain = MSEtrain / ntr;
MSEtest = MSEtest / nts;
MSEdummy = mean((testr-mean(trainr)).^2);

plot(MSEtrain,'b'), hold on
plot(MSEtest,'r'), 
plot([1 Nepochs],[MSEdummy MSEdummy])
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: See the code for 3-class classification of iris dataset in python.

-------------------------------------------

#imports
import pandas as pd
import sklearn as skl

#import dataset iris
iris_dataset = skl.datasets.load_iris()
#print(len(iris_dataset))

#input data
input_data = iris_dataset.data
#output data
output_data = iris_dataset.target

#preparing data for classification
iris_inputdata = pd.DataFrame(input_data,columns=['SepalLength','SepalWidth','PetalLength','PetalWidth'])
iris_classdata = pd.DataFrame(output_data,columns=['Species'])
iris_data = pd.concat([iris_inputdata,iris_classdata],axis=1)

#perform classification using LogisticsRegression
logsig_reg = skl.linear_model.LogisticRegression()
input_train, input_test, output_train, output_test = skl.model_selection.train_test_split(input_data,output_data,test_size = 0.2,random_state = 3)
logsig_reg.fit(input_train,output_train) #perform training

#perform prediction
output_predicted = logsig_reg.predict(input_test)

#test accuracy of classification
class_accuracy = skl.metrics.accuracy_score(output_test,output_predicted)
print("Accuracy of classification: ",class_accuracy)

-----------------------------------------------

Note: In this code, inbuilt dataset iris in scikit-learn library is used.

Add a comment
Know the answer?
Add Answer to:
Convert THE CODE BELOW to a 3-class classifier (or rewrite it in Python in the same...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42,...

    Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42, 27, -1, 2.0, 'hello', [2, 4], 23] print("L1 =",L1) odds =[] evens=[] list=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',',','.'] no=0 for i in L1: i=str(i) for j in list: if i.find(j)>=0: no=1 if no==1: None else: i=int(i) if i%2==0: evens.append(i) else: odds.append(i) no=0      ...

  • LMS project Using the notes discussed in class: Implementing the LMS Algorithm First generate some signals clear all c...

    LMS project Using the notes discussed in class: Implementing the LMS Algorithm First generate some signals clear all close al1: Generate signals for testing the LMS Algorithm 1000 Fs Sampling frequency Sample time 1/Fs 10000: = L Length of signal S Time vector (0:L-1) *T ; Sum of a 50 Hz sinusoid and a 120 Hz sinusoid 0.7 sin (2*pi*50*t); inuside X d+ 10 randn (size (t)); Sinusoids 5O0000000L plus noise fiqure (1) plot (Fs*t (1:150),x (1:1500)) title('Signal Corrupted with...

  • LMS project Using the notes discussed in class: Implementing the LMS Algorithm First generate some signals...

    LMS project Using the notes discussed in class: Implementing the LMS Algorithm First generate some signals clear all close al1: Generate signals for testing the LMS Algorithm 1000 Fs Sampling frequency Sample time 1/Fs 10000: = L Length of signal S Time vector (0:L-1) *T ; Sum of a 50 Hz sinusoid and a 120 Hz sinusoid 0.7 sin (2*pi*50*t); inuside X d+ 10 randn (size (t)); Sinusoids 5O0000000L plus noise fiqure (1) plot (Fs*t (1:150),x (1:1500)) title('Signal Corrupted with...

  • please explain all rhanks Search 19:24 If the probability that head is 1/2 and the probability...

    please explain all rhanks Search 19:24 If the probability that head is 1/2 and the probability that the back is 1/2, coin is repeatedly throws twice w1 (H,H) w2=(T,H) w3 (H,T) w4= (T,T) The sample space is {w1,w2,w3,w4} The random variable X: R and the random variable Y: R for all we, the probability P is and is defined P ((w)) X (w) 0 for we{w1,w3} X(w)=1, for w e (w2, w4} Y (w) 0, for we{w1,w2} Y(w)1, for w{w3,...

  • please provide matlab solution too 3. Butterball recommends the following cooking times for turkeys at 325...

    please provide matlab solution too 3. Butterball recommends the following cooking times for turkeys at 325 °C. size, (lbs) un-stuffed t, (h) stuffed t, (h) 2.00 2.25 6. 2.50 2.75 10 3.00 3.50 18 3.50 4.50 22 4.00 5.00 24 4.50 5.50 30 5.00 6.25 (a) Plot the recommended cooking time as a function of turkey size for un-stuffed and stuffed turkeys on the same plot. (b) For each of the two menu options, find the third-order interpolating polynomial (by...

  • I want to know what is wrong with this code, and how to fix it please....

    I want to know what is wrong with this code, and how to fix it please. i keep getting Error using contour (line 48) Z must be at least a 2x2 matrix. Error in Projectrevised (line 42) curve= contour(b,FOS1,h,'ShowText','on'); %% Constants w = 450000; l = 10; n = 6; SOM = 505000000; max = 10; min = 2; diff = (max-min)/n; FOS1 = zeros(n,n); FOS2 = zeros(n,n); i = 1; j = 1; Vm = (w*l)/2; %% FOS@different base&height...

  • CAN YOU PLEASE CHECK WHATS WRONG? function[F]=func(t,h) global Atank Ahole kOne kTwo g rho %F=zeros(2,1) %NL=y(1)...

    CAN YOU PLEASE CHECK WHATS WRONG? function[F]=func(t,h) global Atank Ahole kOne kTwo g rho %F=zeros(2,1) %NL=y(1) %NG=y(2) F=(kOne+kTwo*cos(2*pi*t/24)-rho*Ahole*sqrt(2*g*h))/(rho*Atank); close all clear all %clear all %close all global Atank Ahole kOne kTwo g rho Atank = 3.13;% Atank value Ahole =0.06; %Ahole value kOne = 300; % K1 value kTwo = 200; % K2 value g = 9.81; % Gravity rho = 1000; % rho value t0=0; tf=150; %range of time %Initial value h0_2=h(1); h0_1=h(2); h0_0=h(3); n=input('Enter the number of steps:');...

  • Hello, i have this matlab code where i have to track a red ball from and...

    Hello, i have this matlab code where i have to track a red ball from and uploaded video, and plot its direction in x-y direction My Question is how can i add x vs time plot and y vs time plot in this code. Thank you if exist('V','var') delete(V); end clear;clc; V=VideoReader('video.mp4'); figure(1); x=[];y=[]; i=1; while hasFrame(V) J=readFrame(V); figure(1);imshow(J);title(['Frame No. ',num2str(i)]); if (i>=28 && i<=132) bw=J(:,:,1)>200 & J(:,:,2)<80 & J(:,:,3)<80; bw=imfill(bw,'holes'); A=regionprops(bw,'Centroid'); x(end+1)=A.Centroid(1); y(end+1)=A.Centroid(2); hold on; plot(x,y,'*k');hold off; end i=i+1;...

  • Write a python program that writes to an output file "decoder.txt" a table of letters as...

    Write a python program that writes to an output file "decoder.txt" a table of letters as follows: ___1_2_3_4_5 1: A B C D E 2: F G H I K 3: L M N O P 4: Q R S T U 5: V W X Y Z Note that the letter J has been removed. Use string.ascii_uppercase to start

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT