Julia Quick Syntax Reference - A Pocket Guide for Data Science Programming

von: Antonello Lobianco

Apress, 2019

ISBN: 9781484251904 , 223 Seiten

Format: PDF, Online Lesen

Kopierschutz: Wasserzeichen

Mac OSX,Windows PC für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Online-Lesen für: Mac OSX,Linux,Windows PC

Preis: 46,99 EUR

eBook anfordern eBook anfordern

Mehr zum Inhalt

Julia Quick Syntax Reference - A Pocket Guide for Data Science Programming


 

Table of Contents

4

About the Author

10

About the Technical Reviewer

11

Acknowledgments

12

Introduction

13

Part I: Language Core

16

Chapter 1: Getting Started

17

1.1 Why Julia?

17

1.2 Installing Julia

19

1.3 Running Julia

22

1.4 Miscellaneous Syntax Elements

24

1.5 Packages

25

1.5.1 Using the Package Manager

26

1.5.2 Using Packages

27

1.6 Help System

29

Chapter 2: Data Types and Structures

31

2.1 Simple Types (Non-Containers)

32

2.1.1 Basic Mathematic Operations

33

2.1.2 Strings

33

Concatenation

34

2.2 Arrays (Lists)

35

2.2.1 Multidimensional and Nested Arrays

39

2.3 Tuples

44

2.4 Named Tuples

45

2.5 Dictionaries

46

2.6 Sets

48

2.7 Memory and Copy Issues

48

2.8 Various Notes on Data Types

52

2.8.1 Random Numbers

53

2.8.2 Missing, Nothing, and NaN

53

Chapter 3: Control Flow and  Functions

55

3.1 Code Block Structure and Variable Scope

55

3.2 Repeated Iteration: for and while Loops, List Comprehension, Maps

57

3.3 Conditional Statements: if Blocks, Ternary Operator

58

3.4 Functions

59

3.4.1 Arguments

61

3.4.2 Return Value

63

3.4.3 Multiple-Dispatch (aka Polymorphism)

63

3.4.4 Templates (Type Parameterization)

64

3.4.5 Functions as Objects

65

3.4.6 Call by Reference/Call by Value

65

3.4.7 Anonymous Functions (aka “Lambda” Functions)

66

3.4.8 Broadcasting Functions

67

3.5 Do Blocks

68

3.6 Exiting Julia

68

Chapter 4: Custom Types

70

4.1 Primitive Type Definition

71

4.2 Structure Definition

72

4.3 Object Initialization and Usage

73

4.4 Abstract Types and Inheritance

74

4.4.1 Implementation of the Object-Oriented Paradigm in Julia

76

4.5 Some Useful Functions Related to Types

79

Chapter 5: Input/Output

80

5.1 Reading (Input)

81

5.1.1 Reading from the Terminal

81

5.1.2 Reading from a File

82

Importing Data for a Matrix

83

Parsing Comma Separated Value (CSV) Files

83

5.1.3 Importing Data from Excel

84

5.1.4 Importing Data from JSON

85

5.1.5 Accessing Web Resources (HTTP)

87

5.2 Writing (Output)

88

5.2.1 Writing to the Terminal

88

5.2.2 Writing to a File

89

5.2.3 Exporting to CSV

90

5.2.4 Exporting Data to Excel

91

5.2.5 Exporting Data to JSON

92

5.3 Other Specialized IO

93

Chapter 6: Metaprogramming and Macros

94

6.1 Symbols

95

6.2 Expressions

96

6.2.1 Creating Expressions

97

Parse a String

97

Colon Prefix Operator

97

Quote Block

97

Use the Exp Constructor with a Tree

98

6.2.2 Evaluating Symbols and Expressions

98

6.3 Macros

100

6.3.1 Macro Definition

100

6.3.2 Macro Invocation

101

6.3.3 String Macros

102

Chapter 7: Interfacing Julia with Other Languages

104

7.1 Julia ? C

105

7.2 Julia ? C++

107

7.2.1 Interactive C++ Prompt

107

7.2.2 Embed C++ Code in a Julia Program

108

7.2.3 Load a C++ Library

111

7.3 Julia ? Python

113

7.3.1 Embed Python Code in a Julia Program

114

7.3.2 Use Python Libraries

115

7.3.3 PyJulia: Using Julia in Python

116

Installation

116

Usage

117

7.4 Julia ? R

119

7.4.1 Interactive R Prompt

120

7.4.2 Embed R Code in a Julia Program

120

7.4.3 Use R Libraries

121

7.4.4 JuliaCall: Using Julia in R

122

Installation

122

Usage

122

Chapter 8: Effectively Write Efficient Code

125

8.1 Performance

126

8.1.1 Benchmarking

126

8.1.2 Profiling

128

8.1.3 Type Stability

132

8.1.4 Other Tips to Improve Performance

134

Avoid Using Global Variables and Run Performance-Critical Code Within Functions

134

Annotate the Type of Data Structures

134

Annotate the Fields of Composite Types

134

Loop Matrix Elements by Column and Then by Row

135

8.2 Code Parallelization

136

8.2.1 Adding and Removing Processes

136

8.2.2 Running Heavy Computations on a List of Items

138

8.2.3 Aggregate Results

139

8.3 Debugging

140

8.3.1 Introspection Tools

141

8.3.2 Debugging Tools

142

8.4 Managing Runtime Errors (Exceptions)

145

Part II: Packages Ecosystem

146

Chapter 9: Working with Data

147

9.1 Using the DataFrames Package

148

9.1.1 Installing and Importing the Library

148

9.1.2 Creating a DataFrame or Loading Data

149

9.1.3 Getting Insights About the Data

151

9.1.4 Filtering Data (Selecting or Querying Data)

152

9.1.5 Editing Data

155

9.1.6 Editing Structure

156

Merging/Joining/Copying Datasets

158

9.1.7 Managing Missing Values

158

9.1.8 The Split-Apply-Combine Strategy

159

Aggregating

160

Computing the Cumulative Sum by Categories

162

9.1.9 Pivoting Data

163

Stacking Columns

165

Unstacking

165

The Pivot Function

167

Sorting

169

9.1.10 Dataframe Export

169

Exporting to CSV

169

Exporting to the OpenDocument Spreadsheet File

169

Exporting to a Matrix

170

Exporting to a Dict

170

Exporting to the hdf5 Format

171

9.2 Using IndexedTables

171

9.2.1 Creating an IndexedTable (NDSparse)

172

9.2.2 Row Filtering

173

9.2.3 Editing/Adding Values

174

9.3 Using the Pipe Operator

174

9.4 Plotting

176

9.4.1 Installation and Backends

176

9.4.2 The Plot Function

179

9.4.3 Plotting from DataFrames

182

9.4.4 Saving

185

Chapter 10: Mathematical Libraries

186

10.1 JuMP, an Optimization Framework

187

10.1.1 The Transport Problem: A Linear Problem

189

The Problem

189

Importing the Libraries

190

Defining the Sets

190

Defining the Parameters

191

Declaring the Model

192

Declaring the Model Variables

193

Declaring the Model Constraints

193

Declaring the Model Objective

194

Human-Readable Visualization of the Model (Optional)

194

Resolution of the Model

194

Visualization of the Results

195

10.1.2 Choosing Between Pizzas and Sandwiches, a Non-Linear Problem

197

The Problem

197

Importing the Libraries and Declaring the Model

198

Declaring the Model Variables, Constraints, and Objectives

198

Resolution of the Model and Visualization of the Results

199

10.2 SymPy, a CAS System

200

10.2.1 Loading the Library and Declaring Symbols

200

10.2.2 Creating and Manipulating Expressions

201

10.2.3 Solving a System of Equations

202

10.2.4 Retrieving Numerical Values

202

10.3 LsqFit, a Data Fit Library

203

10.3.1 Loading the Libraries and Defining the Model

204

10.3.2 Parameters

204

10.3.3 Fitting the Model

205

10.3.4 Retrieving the Parameters and  Comparing them with the Observations

205

Chapter 11: Utilities

207

11.1 Weave for Dynamic Documents

207

11.2 Zip Files

211

11.2.1 Writing a Zip Archive

211

11.2.2 Reading from a Zipped Archive

212

11.3 Interact and Mux: Expose Interacting Models on the Web

214

11.3.1 Importing the Libraries

214

11.3.2 Defining the Logic of the Model

214

11.3.3 Defining Controls and Layout

215

11.3.4 Providing Widgets to Web Users

216

Index

218