1 / 35

Array and File Handling for Data Analysis

Learn how to process and store data using arrays and files. This tutorial covers loading data from files, representing lists of objects using arrays, working with array subscripts, and more.

kwhyte
Download Presentation

Array and File Handling for Data Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. All our knowledge brings us nearer to our ignorance, All our ignorance brings us nearer to death, But nearness to death, no nearer to God. Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information? The cycles of Heaven in twenty centuries Bring us farther from God and nearer to the Dust. - T. S. Eliot, Choruses From ‘The Rock’, Selected Poems (New York: Harcourt, Brace & World, 1964), p. 107.

  2. Arrays and Files • Example • One-Dimensional Arrays • Loading Arrays from Files • Multi-dimensional Arrays

  3. Example: Analysis (1) • We’d like to present relative national population data for Central America. • We should see the data elements geographically. • A sample image is shown here. map from commons.wikimedia.org

  4. Example: Analysis (1) • The program should: • Represent the lists of data; • Process the elements of those data; • Store the data permanently; • Present the data geographically. map from commons.wikimedia.org

  5. Representing Lists of Objects • Frequently, applications must store lists of objects of the same type. • Variables represent one object at a time so a list would require separate variables: String country1, country2, country3; • Processing represents lists of objects using arrays.

  6. Array Definition Pattern ElementType[] arrayName; or ElementType[] arrayName = new ElementType[length]; or ElementType[] arrayName = arrayLiteral; • ElementType is any type (including an array type); • arrayName is the handle for the array object being defined – if there is no assignment clause in the statement, the handle value is set to null; • length is an expression specifying the number of elements in the array; • arrayLiteral is the list of literal of type ElementType, enclosed in curly braces ({}).

  7. array1 array2 [0] [1] [2] [3] ? ? ? ? array3 [0] [1] “Bashful” “Doc” Array Definitions double[] array1; final int SIZE = 4; int[] array2 = new int[SIZE]; String[] array3 = { "Bashful", "Doc" };

  8. anArray [0] [1] “Grumpy” “Happy” Array Subscripts String[] anArray = new String[2]; anArray[0] = "Grumpy"; anArray[1] = "Happy"; println(anArray[1]);

  9. anArray [0] [1] “Grumpy” “Happy” Working with Arrays String[] anArray = new String[2]; anArray[0] = "Grumpy"; anArray[1] = "Happy"; for (inti = 0; i < anArray.length; i++) { println(anArray[i]); }

  10. Iteration 1 (cont.) • Analysis • Design • Implementation • Test

  11. String[] countries = { "Belize", "Costa Rica", "El Salvador", "Guatemala", "Honduras", "Nicaragua", "Panama" }; int[] populations = { 294385, 4133884, 6948073, 12728111, 7483763, 5675356, 3242173 }; for (inti = 0; i < countries.length; i++) { println(countries[i] + ": " + populations[i]); }

  12. Iteration 2 • Analysis • Design • Implementation • Test

  13. Arrays as Parameters intcomputeTotal(int[] values) { int result = 0; for (inti = 0; i < values.length; i++) { result += values[i]; } return result; }

  14. Reference Values as Parameters void draw() { String[] sa = {"Grumpy", "Happy"}; changeStringArray(sa); println(sa); } void changeStringArray(String[] sa) { sa[0] = "Dopey"; sa[1] = "Sleepy"; println(sa); }

  15. Exercises

  16. Persistent Storage • Problems with our current approach to data processing: • Data must be hard-coded into the program; • Data stored during the execution of a program is lost when the program exits. • Processing supports persistent storage using files.

  17. Saving Text Data Program: dwarfOutput.pde String[] dwarves = new String[2]; dwarves[0] = "Grumpy"; dwarves[1] = "Happy"; saveStrings("data/dwarves.txt", dwarves); Output: dwarves.txt Grumpy Happy

  18. Loading Text Data Input: dwarves.txt Bashful Doc Program: dwarfInput.pde String[] dwarves; dwarves = loadStrings("data/dwarves.txt");

  19. Loading Numeric Data Input: ages.txt 127 324 Program: agesInput.pde String[] ageLines; ageLines = loadStrings("data/ages.txt"); int[] ages = new int[ageLines.length]; for (inti = 0; i < ageLines.length; i++) { ages[i] = int(ageLines[i]); }

  20. Loading Mixed Data Input: dwarfData.txt Bashful 127 Doc 324 Program: dwarfDataInput.pde String[] dwarfLines, tokens; dwarfLines = loadStrings("data/dwarfData.txt"); int[] dwarfAges = new int[dwarfLines.length]; String[] dwarfNames = new String[dwarfLines.length]; for (inti = 0; i < dwarfLines.length; i++) { tokens = split(dwarfLines[i], ""); dwarfNames[i] = tokens[0]; dwarfAges[i] = int(tokens[1]); }

  21. Iteration 3 • Analysis • Design • Implementation • Test

  22. void loadPopulationData(String dataFilename) { String[] dataLines = loadStrings(dataFilename); populationCountries = new String[dataLines.length]; populationValues = new int[dataLines.length]; String[] tokens; for (inti = 0; i < dataLines.length; i++) { tokens = split(dataLines[i], " "); populationCountries[i] = tokens[0]; populationValues[i] = int(tokens[1]); } }

  23. Iteration 4 • Analysis • Design • Implementation • Test

  24. Iteration 5 • Analysis • Design • Implementation • Test

  25. Multi-Dimensional Data • Some data sets cannot be represented with single-dimensional arrays. • Examples:

  26. Multi-Dimensional Arrays • Some data collections, like the Sudoku grid can be viewed as multi-dimensional data. • Declaring 2-D arrays: type[][] identifier = arrayExpression; • Constructing 2-D arrays: new type[totalRows][totalColumns] • Accessing 2-D array elements: identifier[someRow][someColumn] • This can be generalized to more dimensions.

  27. Iteration 6 • Analysis • Design • Implementation • Test

  28. Data Management • Storing, retrieving and manipulating data sets are very common problems. • Database Management Systems are designed to address these problems. Database Files Records Fields Characters/Strings/Integers Bits

  29. The Relational Data Model • Most current DBMSs use the relational data model. • Relational models separate the structure of the data from the data itself. Schema: Data:

  30. Structured Query Language • SQL is the standard query language for relational databases. • Example: SQL> SELECT firstName, lastName FROM StudentTable WHERE ID > 600000000; FIRSTNAME LASTNAME --------- -------- Jeff Plantinga Ryan Tuuk

  31. Edgar F. Codd (1923-2003)Relational Data Model • Codd developed the relational model in the early 1970s. • Included features for: • Data definition • Data queries • Most current database systems use the relational model. image from wikipedia, June, 2006

  32. What’s the Big Idea Privacy • Database systems allow us to build and maintain large data sets: • This can be useful for many applications. • It can also be dangerous. • Guidelines: • Collect only what you need to collect. • Have a clear policy on what information is collected and on how it is used. • Keep the data accurate and secure.

More Related