Question
Need help completing code. (In python)
Problem 2. Prepare the data for the lab In this problem youll split the dataset into a training dataset and a test dataset and store them into separate arrays to work with: (a) Create a training set dataTR consisting of the first 500 and last 500 rows of the original dataset and a set dataTS with the remaining (middle) 372 rows. (b) Create 2D numpy arrays X-tr with rows [1.zi(1), Zi(2) , Zi(3) , Zi(4)] with İ = 0, 1, . . . , 999( m,r = 1000 ) and X-t s with rows [1, zXI), zr2)丙(3)刑41 with j = 0, 1, . . . , 371 ( Tnts = 372). c) Create corresponding 1D arrays y tr and y ts with the labels y and y of the data samples. dataTRnp.zeros ( (1000, 5), double) print dataTR.shape, n dataTr(0:500,:) = data(0:500,:] dataTR[500:1000,:data [872:1372,: print dataTR[497:503,:dataTR[497:503,n print data[499:501,:1data[499:501,:, n print dataTR[997:1000,dataTR[997:1000,:, n dataTS# np . zeros((372, # keep going 5), double)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assuming your original data is given in the form (initial columns have the values for inputs(x) and the last columns have the output(y) ).

Which is data[:,:-1] = Your x values

and

data[:,-1:] = Your y values

Solution in python.

#Part a of the answer

#Making dataTR and dataTS
dataTR = np.zeros((1000,5),'double')
dataTS = np.zeros((372,5),'double')

#Assigning Respective Values
dataTR[0:500,:] = data[0:500,:]
dataTR[500:1000,:] = data[872:1372,:]
dataTS[0:372,:] = data[500:872,:]


#Part b and c of the answer
#The questions wants you to split your newly created DataTR and DataTS into
#x_tr and x_ts.

#Part b
#Making X_tr and X_ts
X_tr = np.zeros((1000,5),'double')
X_ts = np.zeros((372,5),'double')

#Initiallizing X_tr and X_ts

#You want the first column to be a ones and rest input x.
#You will notice the last column is not taken from the dataTR because
# this column contains output values y.
X_tr[:,1:] = dataTR[:,:-1]
X_tr[:,0] = numpy.ones(1000,'double')

X_ts[:,1:] = dataTS[:,:-1]
X_ts[:,0] = numpy.ones(372,'double')

#Part c
#Making y_tr and y_ts
#This has 1-d array because the output has a single column.
y_tr = np.zeros((1000),'double')
y_ts = np.zeros((372),'double')

#Initiallizing y_tr and y_ts
#Here, the last column is taken into account because it has outputs.
#Using notation of -1.
y_tr[:] = dataTR[:,-1]
y_ts[:] = dataTS[:,-1]

Screenshot of Solution:

import numpy # Creating a random dataset just for the purpose of illustrating datanumpy.random.rand (1372, 5) #Part a of the answe #Making dataTR and dataTS dataTR np. zeros (( 1000,5), double) dataTSnp.zeros ( (372,5), double) #Assigning Respective Values dataTR[0:500,:] - data [0:500,:] dataTR [ 500: 1000,:] = data [872: 1372,:] dataTS [0:372,:] - data [500:872,:] #Part b and c of the answer #The questions wants you to split your newly created DataTR and DataTS into #x tr and xts - #Part b aking Xtr and X ts X trnp.zeros ((1000,5), double) X tsnp.zeros ( (372,5), double) #Initiallizing Xtr and X-ts #You want the first column to be a ones and rest input x #You will notice the last column is not taken from the dataTR because # this column contains output values y x tr[:,0]numpy.ones (1000, double) X ts [ : , 1: ] = dataTS [ : , :-1] xt s [ : , Э] -numpy . ones ( 372, double ) #Pa rt c #Making ytr and yts #This has 1-d array because the output has a single column y trnp.zeros ((1000),double y tsnp.zeros ( (372), double) #Initiallizing y_tr and y_ts #Here, the last column is taken into account because it has outputs #Using notation of -1

Outputs:

Confirming X_ts is in correct shape.I hope this satisfies you.Thanks for using Chegg.

Add a comment
Know the answer?
Add Answer to:
Need help completing code. (In python) Problem 2. Prepare the data for the lab In this...
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
  • Python, given a code in class i just need help with the third bullet point ; using a and n (defin...

    Python, given a code in class i just need help with the third bullet point ; using a and n (defined in the second picture of python code) find the first digit for k! for k =1,...,n. you dont have to type in all this code just help me write the code in the first picture where it says: def benford(a): b = [0 for d in range(1,10)] #Do everthything in here return b 2.2 Generating data In this assignment...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main functi...

    C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main function int main() {    //seeding a random number    srand(time(0));    //define the arrays for the wagered, winning amount, percent amount    double amounts[7] = { 1, 5, 10, 20, 50, 100, 1000 };    double payoff[7] = { 100, 500, 1000, 2000, 5000, 10000, 100000 };    double percent[7] = { 0.01, 0.05, 1, 2,...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that...

    Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and...

  • i need help making this program the skeleton of the code is below: //Include the following #include <iostream> #...

    i need help making this program the skeleton of the code is below: //Include the following #include <iostream> #include <string> #include <fstream> //you must include this library if you wish to do file i/o using namespace std; /********************************************************* //Following is the declaration of a order record **********************************************************/ class order_record { public: string cell_number; string item_number; double quantity; double price; int processing_plant; double tax_rate; double order_tax; double net_cost; double total_cost; }; //Prototypes for your functions: input, output, and process will go...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

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
Active Questions
ADVERTISEMENT