Part 7: Data Manipulation
Go to Maple Portal Previous Tutorial Next Tutorial
|
Introduction
|
|
Maple's Tutorials are designed to help you get started with Maple, learn about the key tools available in Maple, and lead you through a series of problems.
In Part 7: Data Manipulation, learn to import and export data. Explore Maple's tools for statistics, visualization, and data analysis.
To try this material on your own, start with an empty Maple document. Perform the steps described in the left column of each table below. The results of the steps are displayed in the right column for reference.
Refer to Help>Quick Reference for basic getting started tips.
Note for non-Windows users: The keystrokes given in this document are for Windows. There will be differences for other platforms. If you are using a different platform, see Help>Quick Help for the list of the most common keystrokes.
|
|
Importing and Exporting Data
|
|
Import and export data with interactive tools or with a command. With Maple, you can import data from many formats and export data back to files.
|
Importing Data
|
|
Steps
|
Result
|
Using the Import Data Assistant
You can import data from a file. Supported file formats include Excel, MATLAB, Image, Audio, Matrix Market and Delimited.
Example: From the Tools>Assistants menu, choose Import Data...
Locate the data file ExcelData.xls. This file is located in the data/portal subdirectory of your Maple installation.
Choose format Excel.
Click Next, Next, and Next. If desired, add a name so you can refer to this data later, and click Done.
The data is imported as an Array.
Note: After completing these steps to import the data, you can double-click on the summary to browse the data.
Now, use Plot Builder to plot the data.
Right-click on the imported data and from the context menu select Plots>Plot Builder.
Click Plot.
|
|
Using the ExcelTools Package
You can also use the ExcelTools package to import and export data stored in Microsoft Excel format.
Example:
Import the Excel file ExcelData.xls.
If a file is not located in the current directory, you need to enter the full path to the data file.
In this case, the data is located in the data/portal directory of your Maple installation. The command kernelopts(datadir) returns the location of the data directory. Then, the cat command is used to concatenate the two strings to give the full path to the data file.
View the first row.
Return the number of elements in the Array.
Plot the data using plots[pointplot].
|
| (2.1.1) |
| (2.1.2) |
| (2.1.3) |
| (2.1.4) |
|
Using the readdata Command
The readdata command reads numeric data from a text file. The calling sequences is readdata("fileID", n), where n is the number of columns of data.
The output from readdata is a list of elements.
This command returns a list, so use list selection to view entries in Data1. Then, plot the data.
|
| (2.1.5) |
| (2.1.6) |
| (2.1.7) |
| (2.1.8) |
|
The ImportMatrix command reads a data file just like readdata, but the output from ImportMatrix is a Matrix instead of a list.
|
| (2.1.9) |
|
|
|
|
|
Exporting Data
|
|
Steps
|
Result
|
Exporting an Excel File
Using ExcelTools, export to a file. By default, the file is exported to the current directory. To find what is the current directory, use the command currentdir().
As a test, import the first 10 rows back into Maple.
|
| (2.2.1) |
|
Exporting a Text File
The writedata command writes data to a text file. The calling sequence is writedata(fileID,data,format), where filename is the name given to the data file, data is the data itself, and format specifies how the data is to be written. The options for format are integer, float, or string.
Example:
Create a list, filling in the entries with the seq command.
Write the data to a file.
As a test, import the data file back into Maple.
|
| (2.2.2) |
| (2.2.3) |
|
|
|
|
|
|
Random Distributions
|
|
Steps
|
Result
|
To generate a random number, use the rand command. The simplest calling sequence generates a random 12 digit positive integer.
The rand(a..b) calling sequence returns a procedure which then generates numbers between a and b.
In order to ensure that a different procedure is created every time the code is run, the randomize command is used to reset the seed for the random number generator.
Example:
Generate random numbers between -10 and 10.
The rand command provides a simple interface to the RandomTools package, which consists of a large collection of tools and algorithms for random number and random object generation.
|
| (3.1) |
| (3.2) |
| (3.3) |
| (3.4) |
|
Maple has a built-in Statistics package that provides a large number of continuous and discrete distributions that can be used to generate random numbers. To use these distributions, load the Statistics package.
Example:
Use a Normal distribution with a mean value of 5 and a standard deviation of 1 to generate 100 random numbers.
Create a line chart of the sample data.
Right-click on the generated data. From the context menu, select Statistics>Visualization>Histogram.
After previewing the graph, click Quit to return the Histogram to your document.
|
| (3.5) |
| (3.6) |
|
You can also define a random number generator using a distribution.
Here, X1 is a procedure that takes n as an argument, where n is the number of random numbers to generate.
Each time you use X1, it generates new random numbers.
Example:
Generate 10 random numbers using X1(10) and then create a line chart of the data using the LineChart command.
|
| (3.7) |
| (3.8) |
| (3.9) |
| (3.10) |
| (3.11) |
|
|
|
|
|
Statistics, Regression, and Curve Fitting
|
|
|
Basic Tools
|
|
Steps
|
Results
|
With Maple, it is easy to use statistics and to do curve fitting and regression analysis.
Many useful commands are available directly from the context menu.
Easy Access to Statistics Operations
For a 1-dimensional array, vector or list, when you right-click on the data, the context menu includes Statistics options. Under Statistics, the categories Data Manipulation, Quantities, Summary and Tabulation, and Visualization offer many Statistics commands.
Example:
Define a list as shown. Right-click on the output. Choose Statistics>Quantities>Mean.
|
| (4.1.1) |
|
The Curve Fitting Assistant is also available through the context menu.
Example:
In this example, we start by listing the pairs of data.
Right click on the data and select Curve Fitting from the context menu. Choose Curve Fitting>Interactive Curve Fitting. Select OK when prompted to specify the variable name. This opens the Curve Fitting Assistant which provides an easy way to access Maple's curve fitting commands.
You can choose the type of curve and preview the result. The function is displayed below the plot. Select the Splines curve, and select the corresponding Plot button.
You can opt to return the function (interpolant) or the plot.In the drop-down menu near the bottom left corner of the window, change the selection from Interpolant to Plot. Click Done.
The plot is displayed.
|
| (4.1.2) |
|
You can also call the Curve Fitting Assistant with a command. This allows you to pass data that was defined earlier in the Maple session.
Example: Use the DataSet that was imported in the Importing Data section of this tutorial. (If you have not done so already, go to this section first and follow the steps to import and define the data.)
1.
|
Enter the command "CurveFitting[Interactive](DataSet)". The Curve Fitting Assistant opens.
|
2.
|
Under Least Squares, click Plot to see the least squares curve.
|
3.
|
At the bottom of the window, set the option to return the plot. In the drop-down menu near the bottom left corner of the window, change the selection from Interpolant to Plot. Click Done.
|
The plot is displayed.
|
|
|
|
|
|
Advanced Example: Noisy Signal
|
|
Steps
|
Results
|
In this example, you will use a random number generator to add noise to a signal.
Define the noise as a random number generator with mean value 1 and standard deviation 0.5.
We use a sample size of 200 to create a vector of noise values.
We can then extract entries from the vector. The vector indices go from 1 to 200. To select the th entry, use the selection operator: PureNoiseVector[i].
|
| (4.2.1) |
|
Define a signal.
Create a noisy signal by adding noise to the original signal:
[i]
Generate a data set using the seq command.
Plot the signal and noisyData together.
|
|
Curve Fitting
To fit a functional model to the noisy data, use the Fit command from the Statistics package.
The calling sequence is Fit(f, X, Y, v), where f is the function model, X and Y are the x and y data, and v is the name of the independent variable in the component function.
Create two lists Xdata and Ydata from the data in the two columns of noisyData.
Tip: It's often useful to do something for each element of a data structure. Here, we do this by i=1..nops(noisyData). Recall, nops is the number of elements.
The function model is .
Plot and the noisyData together to visualize the fit.
Tip: The Statistics and CurveFitting packages contain a number of commands for fitting curves to data points including linear, exponential, polynomial, least squares, and spline.
|
| (4.2.2) |
|
|
|
Exercise: Repeat the noisy signal example using a random number generator based on a Weibull distribution.
|
|
|
See Also
|
|
Array, cat, currentdir, Curve Fitting (Maple Portal topic), CurveFitting package, kernelopts(datadir), list, map, rand, seq, Statistics
|
Go to Maple Portal Previous Tutorial Next Tutorial
|