![]() |
|
P is for Practical | |
PerlMonks |
RFC: 101 Perl PDL Exercises for Data Analysisby thechartist (Monk) |
on May 06, 2019 at 23:36 UTC ( #1233413=perlmeditation: print w/replies, xml ) | Need Help?? |
101 Perl PDL Exercises for Data Analysis (May 2019 with PDL 2.019) Before "data science" became a fashionable topic in computing, Perl hackers have been cleaning and analyzing data since Perl was written. The following tutorial provides Perl examples to the problems posed in: 101 NumPy Exercises for Data Analysis My purpose is to demonstrate that Perl has the necessary tools to complete common data analysis tasks with minimal effort. The philosophy of Perl has always been "There is more than one way to do it." Data analysis is no exception. While PDL has excellent functionality "out of the box", you might find it more effective to use individual CPAN modules to solve particular problems. This document assumes you know some basic programming -- loops, conditionals, variables, etc. Perl syntax is similar to any C derived language. Perl has a few fundamental data types:
Examples:
There are others (typeglobs and references), but they will not be needed for the exercises that follow. As always in Perl, there is more than one way to do anything. For PDL, one can enter simply invoke the Perl interpreter at the command line (like any other Perl script), or use a REPL (Read, Evaluate, Print, Loop) interface for interactive analysis. This exercise will show the Perl PDL one liner entered at the command line, but code in between quotation marks should work at the REPL also. Exercise 1 1. Import PDL and print the version. Answer:
2. Create a 1D array of numbers from 0 to 9 Answer:
3. Q. Create a 3×3 numpy array of all True’s Answer:
4. Q. Q. Extract all odd numbers from arr = [0,1,2,3,4,5,6,7,8,9]. Answer:
5. Q. Replace all odd numbers in arr (from question 4) with -1.
Answer: perl -MPDL -e "$arr = sequence(10); $odd = $arr->where($arr % 2 == 1); $odd .= -1 ; print $arr;" 6.Replace all odd numbers in arr with -1 without changing arr
Answer: $ perl -MPDL -e "$arr = sequence(10); $out = sequence(10); $odd = $arr->where($arr %2 == 1); $odd .= -1; print $out, $arr;" Note: the '.=' operator is a special type of assignment operator in the PDL context. Ordinarily this is used for string concatenation. 7. Q. Convert a 1D array to a 2D array with 2 rows.
Answer:
8. Q. Stack arrays a and b vertically
Answer:
9. Q. Stack the arrays a and b horizontally.
Answer:
Back to
Meditations
|
|