Question

Python Help: Using the stock.py data, write a program that does the following: Create a NumPy...

Python Help:

Using the stock.py data, write a program that does the following:

  1. Create a NumPy array from the nasdaq list from stocks.py. Do the same with the other three lists from stocks.py. Thus, at the end of this tasks, you will have four NumPy arrays.
  2. Print out the type (that is, the object type, not the element type) of each array in #1 and the data type typecode of the elements in each array in #1. This task is just 8 print statements; there's no looping needed.
  3. Print out the shape of each array in #1.
  4. Print out whether the size of each of the four arrays in #1 is the same.
  5. Write code that creates a single 2-D array where the first column is the nasdaq array, the second column is the sp500 array, and the third column is the djia array.
  6. From the 2-D array in #5, extract the 2-D sub-array that consists of trading days 21 to 39 of the sp500column and djia columns. By "trading day 21" and "trading day 39," I mean the elements in the sp500column and djia columns that correspond to the value of trading_days equaling 21 and the value of trading_days equaling 39. I do not mean the twenty-first value of the trading_days list or the thirty-ninth value of the trading_days list. Also, when I say "consists of trading days 21 to 39," I mean, "includes the stock index values corresponding to trading day 21 through trading day 39, including trading day 21 and trading day 39." Print the shape of this extracted sub-array.

#==============================================================================

# Lists of stock index values

# There are three lists defined here:

# * nasdaq: Close of index at the end of each trading day in trading_days.

# * sp500: Close of index at the end of each trading day in trading_days.

# * djia: Close of index at the end of each trading day in trading_days.

# * trading_days: Number of trading days since Jun 1, 2016. Jun 1 is

# trading day 0.

#

# Values downloaded from http://finance.yahoo.com. Values of all lists are

# daily trading day closings from Jun 1-Aug 31, 2016, inclusive.

#==============================================================================

nasdaq = [4952.25,

4971.359863,

4942.52002,

4968.709961,

4961.75,

4974.640137,

4958.620117,

4894.549805,

4848.439941,

4843.549805,

4834.930176,

4844.919922,

4800.339844,

4837.209961,

4843.759766,

4833.319824,

4910.040039,

4707.97998,

4594.439941,

4691.870117,

4779.25,

4842.669922,

4862.569824,

4822.899902,

4859.160156,

4876.810059,

4956.759766,

4988.640137,

5022.819824,

5005.72998,

5034.060059,

5029.589844,

5055.779785,

5036.370117,

5089.930176,

5073.899902,

5100.160156,

5097.629883,

5110.049805,

5139.810059,

5154.97998,

5162.129883,

5184.200195,

5137.72998,

5159.740234,

5166.25,

5221.120117,

5213.140137,

5225.47998,

5204.580078,

5228.399902,

5232.890137,

5262.02002,

5227.109863,

5228.660156,

5240.149902,

5238.379883,

5244.600098,

5260.080078,

5217.689941,

5212.200195,

5218.919922,

5232.330078,

5222.990234,

5213.220215]

sp500 = [2099.330078,

2105.26001,

2099.129883,

2109.409912,

2112.129883,

2119.120117,

2115.47998,

2096.070068,

2079.060059,

2075.320068,

2071.5,

2077.98999,

2071.219971,

2083.25,

2088.899902,

2085.449951,

2113.320068,

2037.410034,

2000.540039,

2036.089966,

2070.77002,

2098.860107,

2102.949951,

2088.550049,

2099.72998,

2097.899902,

2129.899902,

2137.159912,

2152.139893,

2152.429932,

2163.75,

2161.73999,

2166.889893,

2163.780029,

2173.02002,

2165.169922,

2175.030029,

2168.47998,

2169.179932,

2166.580078,

2170.060059,

2173.600098,

2170.840088,

2157.030029,

2163.790039,

2164.25,

2182.870117,

2180.889893,

2181.73999,

2175.48999,

2185.790039,

2184.050049,

2190.149902,

2178.149902,

2182.219971,

2187.02002,

2183.870117,

2182.639893,

2186.899902,

2175.439941,

2172.469971,

2169.040039,

2180.379883,

2176.120117,

2170.949951]

djia = [17789.669922,

17838.560547,

17807.060547,

17920.330078,

17938.279297,

18005.050781,

17985.189453,

17865.339844,

17732.480469,

17674.820312,

17640.169922,

17733.099609,

17675.160156,

17804.869141,

17829.730469,

17780.830078,

18011.070312,

17400.75,

17140.240234,

17409.720703,

17694.679688,

17929.990234,

17949.369141,

17840.619141,

17918.619141,

17895.880859,

18146.740234,

18226.929688,

18347.669922,

18372.119141,

18506.410156,

18516.550781,

18533.050781,

18559.009766,

18595.029297,

18517.230469,

18570.849609,

18493.060547,

18473.75,

18472.169922,

18456.349609,

18432.240234,

18404.509766,

18313.769531,

18355,

18352.050781,

18543.529297,

18529.289062,

18533.050781,

18495.660156,

18613.519531,

18576.470703,

18636.050781,

18552.019531,

18573.939453,

18597.699219,

18552.570312,

18529.419922,

18547.300781,

18481.480469,

18448.410156,

18395.400391,

18502.990234,

18454.300781,

18400.880859]

trading_days = list(range(len(nasdaq)))

#- Check lengths of all lists are the same:

if (len(nasdaq) != len(sp500)) or (len(sp500) != len(djia)):

raise ValueError("bad data")

#===== end file =====

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HERE IS THE PYTHON PROGRAM,

import numpy
import stock

#===========#1=============#
nasdaq_array = numpy.array(stock.nasdaq)
sp500_array = numpy.array(stock.sp500)
djia_array = numpy.array(stock.djia)
trading_days_array = numpy.array(stock.trading_days)


#===========#2=============#
#Object Types
print("Object type of nasdaq_array: ",type(nasdaq_array))
print("Object type of sp500_array: ",type(sp500_array))
print("Object type of djia_array: ",type(djia_array))
print("Object type of trading_days_array: ",type(trading_days_array))

#Typecode of elements in the arrays
print("Data Type of elements in nasdaq_array: ",nasdaq_array.dtype)
print("Data Type of elements in sp500_array: ",sp500_array.dtype)
print("Data Type of elements in djia_array: ",djia_array.dtype)
print("Data Type of elements in trading_days_array: ",trading_days_array.dtype)

#===========#3=============#
print("Shape of nasdaq_array: ",nasdaq_array.shape)
print("Shape of sp500_array: ",sp500_array.shape)
print("Shape of djia_array: ",djia_array.shape)
print("Shape of trading_days_array: ",trading_days_array.shape)


#===========#4=============#
if(nasdaq_array.size==djia_array.size & sp500_array.size==trading_days_array.size & nasdaq_array.size==sp500_array.size):
   print("The size of all the arrays are same")
else:
   print("The sizes are not same")

#===========#5=============#
two_Darray = numpy.array([nasdaq_array,sp500_array,djia_array])

#===========#6=============#
subArray = numpy.array([two_Darray[1][trading_days_array[20]:trading_days_array[38]],two_Darray[2][trading_days_array[20]:trading_days_array[38]]])

print("Shape of subArray created: ",subArray.shape)

The program is in sequence to what has been asked to do.

#1. Numpy arrays are created using the corresponding lists from the stocks.py file with the method numpy.array().

#2. type() function is used to get the type of the Object the passed argument is. The dtype attribute of the numpy array is used to get the Data type of the elements store in a numpy array.

#3. To print the shapes of the array we simply use the shape attribute of the numpy array.

#4. With a simple IF-ELSE statement we determine whether the size of these arrays are equal or not using the built-in size attribute.

#5. A new 2D array is created using the above created arrays. The syntax is similar to creating a normal array but this time we passed a list with first, second and third element as the nasdaq, sp500 and djia arrays respectively.

#6. Basic array slicing is used to create the Sub Array.Note we have used 20 instead of 21 because the first day starts with 0 in the trading_days array.

Feel free to ask anything. Do uprate if you find this useful. Thanks.

Add a comment
Know the answer?
Add Answer to:
Python Help: Using the stock.py data, write a program that does the following: Create a NumPy...
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
  • need help with this python progam using numpy Create a 4x4 two dimensional array with numbers...

    need help with this python progam using numpy Create a 4x4 two dimensional array with numbers from 1 thru 16. show this then: Change the last element by dividing it in half. Show that the original array has changed. show this then: Set all values in row 2 to zero. Show the original array contains this change show this then: Set all values in column 1 to one. Shoe the original array contains this change. show this then: Display the...

  • D Question 12 1.5 pts Check the true statements about NumPy arrays: O A single instantiated NumPy array can store multi...

    D Question 12 1.5 pts Check the true statements about NumPy arrays: O A single instantiated NumPy array can store multiple types (e.g., ints and strings) in its individual element positions. A NumPy array object can be instantiated using multiple types (e.g., ints and strings) in the list passed to its constructor O Memory freeing will require a double-nested loop. The number of bits used to store a particular NumPy array object is fixed. O The numpy.append(my.array, new_list) operation mutates...

  • Can someone help me with these C program problems? Thanks 1. Write a C program that...

    Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...

  • 8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the...

    8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. The program you are going to write will produce a String containing a tabular representation of a 2D String array. For the 2D arrays, assume that the first...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • 1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n,...

    1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n, p): """Return a list of the run lengths in n tosses of a coin whose heads probability is p. Arguments: n--Number of tosses (a positive int), p--The probability of observing heads for any given toss (float between 0 and 1). """ For example, if the simulated sequence of coin tosses is HTTHHTHHHTTHHTTTTH, then the list of run lengths the function returns will be [1,...

  • The XYZ Company needs you to write a program that will allow them to enter the...

    The XYZ Company needs you to write a program that will allow them to enter the weekly sales for a salesperson and then the program will calculate things such as most sales (and what day on which they happened), least sales (and day on which they happened), total sales, and daily average. The program will need to do the following: • Validate the user input to ensure that it is not less than 0.0 (0.0 will be acceptable if there...

  • JAVA Using the data provided in the attachment, create a program that will display a child's...

    JAVA Using the data provided in the attachment, create a program that will display a child's predicted adult height. This program will need to create an array of objects to search. This program needs to validate the input for valid range and input values. If invalid data is given, the program needs to indicate that data is invalid and suggest appropriate values/ranges. However, you do not need to validate alphabetic data given for numeric input. 1. The program will need...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

  • C++ Write a program that illustrates enumeration types. Define an enumeration type for the months of...

    C++ Write a program that illustrates enumeration types. Define an enumeration type for the months of the year. This should be global. Put it above the main program. Declare a variable of the enumerated type called month; Declare an array of unsigned integers called year[12] and initialize the array to the days in each month. (Just assume 28 for February.) 1. Write a loop that will progress from January through December printing out the days in each month. The index...

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