seting up jupytrnotebook in mac https://mattgosden.medium.com/set-up-your-mac-for-python-and-jupyter-using-virtual-environments-730bf2888e05
Jupyter Notebooks Python Modules Not Found https://stackoverflow.com/a/49274223 Is it acceptable and safe to run pip install under sudo? Use a virtual environment: $ sudo pip install virtualenv $ virtualenv myenv .. some output .. $ source myenv/bin/activate (myenv) $ pip install what-i-want (myenv) $ deactivate $
Numbers: type() + - / * ** (pow) += *= Boolean: type(), True, False, and or not != ==String String + int : err --> string + str(int) String * int : StringStringStringStringString... String[int] : charat String[-1] : last, -2 next to last String+String=String String[int?:int?] : substring start to before stop"assume"[4:6] me "assume"[4:] me "assume"[:2] as String.split() : gives words String.find(Stirng, index?) : index or -1 search after int index using """ triple quotes you can define strings that span across multiple lines str(int) The string equivalent of an integer used for concatenation to stringstr = "%s %s %d" % ("hello", "world", 12) # formatting str.strip() # truncate
functions
Tuples/Lists/Dictionaries/Sets
dictionaryphonebook = {'Andrew':8806336, 'Emily':6784346, 'Peter':7658344, 'Lewis':1122345}phonebook['Man'] = 1234567 # add new key/value del phonebook['Andrew'] if phonebook.has_key('Man'): print "exists" phonebook.keys() phonebook.values().sort() for k, v in d.iteritems(): print # Sort dictionary by values and get keys list ordered by values sorted_keys = sorted(my_dict, key=my_dict.get, reverse=True) my_dict.get('a key', a_default_value_if_not_found)
Iterable, generator
It is just the same except you used def fibo(top): prev = 0 yield 0 #print(0) print('After first yield!') prev2 = 1 count = 1 while count < top: next = prev + prev2 yield next #print(next) prev2 = prev prev = next count += 1 for fval in fibo(10): print("working with {}".format(fval)) Classes
__dict__: Dictionary containing the class's namespace.
ModulesA module is a python file that (generally) has only defenitions of variables, functions, and classes. As you see, a module looks pretty much like your normal python program.
with open('/Users/mshahriarinia/Documents/ner/ner-testing.json') as f:for line in f:<do something with line> Save Objects (Pickles)
try/except try: a = input('Enter a number to subtract from > ') b = input('Enter the number to subtract > ') except NameError:print "\nYou cannot subtract a letter" continue except SyntaxError:print "\nPlease enter a number only." continue print a - b try: loop = input('Press 1 to try again > ') except (NameError, SyntaxError):loop = 0 except err: pass ## Python Numpy## SetsA set is an unordered collection of distinct elements. As a simple example, consider the following:
As usual, everything you want to know about sets can be found in the documentation.
## TuplesA tuple is an (immutable) ordered list of values. A tuple is in many ways similar to a list; one of the most important differences is that tuples can be used as keys in dictionaries and as elements of sets, while lists cannot. Here is a trivial example:
The documentation has more information about tuples. ## FunctionsPython functions are defined using the
We will often define functions to take optional arguments, like this:
There is a lot more information about Python classes in the documentation. ## .## NumpyNumpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. If you are already familiar with MATLAB, you might find this tutorial useful to get started with Numpy. ## ArraysA numpy array is a grid of values, all of the same type, and is indexed by a tuple of
nonnegative integers. The number of dimensions is the We can initialize numpy arrays from nested Python lists, and access elements using square brackets:
Numpy also provides many functions to create arrays:
You can read about other methods of array creation in the documentation. ## Array indexingNumpy offers several ways to index into arrays.
You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank than the original array. Note that this is quite different from the way that MATLAB handles array slicing:
For brevity we have left out a lot of details about numpy array indexing; if you want to know more you should read the documentation. ## DatatypesEvery numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype. Here is an example:
You can read all about numpy datatypes in the documentation. ## Array mathBasic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy module:
Note that unlike MATLAB,
Numpy provides many useful functions for performing computations on
arrays; one of the most useful is
You can find the full list of mathematical functions provided by numpy in the documentation. Apart from computing mathematical functions using arrays, we frequently
need to reshape or otherwise manipulate data in arrays. The simplest example
of this type of operation is transposing a matrix; to transpose a matrix,
simply use the
Numpy provides many more functions for manipulating arrays; you can see the full list in the documentation. ## BroadcastingBroadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to use the smaller array multiple times to perform some operation on the larger array. For example, suppose that we want to add a constant vector to each row of a matrix. We could do it like this:
This works; however when the matrix
Numpy broadcasting allows us to perform this computation without actually
creating multiple copies of
The line Broadcasting two arrays together follows these rules: - If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length.
- The two arrays are said to be
*compatible*in a dimension if they have the same size in the dimension, or if one of the arrays has size 1 in that dimension. - The arrays can be broadcast together if they are compatible in all dimensions.
- After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays.
- In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension
If this explanation does not make sense, try reading the explanation from the documentation or this explanation. Functions that support broadcasting are known as Here are some applications of broadcasting:
Broadcasting typically makes your code more concise and faster, so you should strive to use it where possible. ## Numpy DocumentationThis brief overview has touched on many of the important things that you need to know about numpy, but is far from complete. Check out the numpy reference to find out much more about numpy. ## SciPyNumpy provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays. SciPy builds on this, and provides a large number of functions that operate on numpy arrays and are useful for different types of scientific and engineering applications. The best way to get familiar with SciPy is to browse the documentation. We will highlight some parts of SciPy that you might find useful for this class. ## Image operationsSciPy provides some basic functions to work with images. For example, it has functions to read images from disk into numpy arrays, to write numpy arrays to disk as images, and to resize images. Here is a simple example that showcases these functions:
Left: The original image.
Right: The tinted and resized image.
## MATLAB filesThe functions ## Distance between pointsSciPy defines some useful functions for computing distances between sets of points. The function
You can read all the details about this function in the documentation. A similar function ( ## MatplotlibMatplotlib is a plotting library.
In this section give a brief introduction to the ## PlottingThe most important function in matplotlib is
Running this code produces the following plot: With just a little bit of extra work we can easily plot multiple lines at once, and add a title, legend, and axis labels:
You can read much more about the ## SubplotsYou can plot different things in the same figure using the
You can read much more about the ## ImagesYou can use the
## Remote DebuggingDownload Eclipse Download PyDev over Eclipse Create a PyDev project, Update both pythons of server and client (eclipse) to same version. Link for centos python update Copy pydev plugin to server. The files are part of the eclipse plugin. To find the path on a mac $ find /Applications/eclipse/plugins -name 'org.python.pydev_*' # e.g. /Applications/eclipse/plugins/org.python.pydev_2.8.2.2013090511 On server, find an appropriate location for the pydev files $ python -c "import sys from pprint import pprint as pp pp(sys.path)" # e.g. /usr/local/lib/python2.7/dist-packagesssh msnia@freeplay-02.cise.ufl.edu mkdir pysrc scp -r /Users/morteza/Downloads/transfer/apps/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev_4.4.0.201510052309/pysrc/* msnia@freeplay-02.cise.ufl.edu:pysrc ssh msnia@freeplay-02.cise.ufl.edu 'sudo cp -R pysrc/* /usr/local/lib/python2.7/dist-packages/' ssh msnia@freeplay-02.cise.ufl.edu rm -r pysrc Configure path mapping. On the remote machine, edit /usr/local/lib/python2.7/dist-packages/ pydevd_file_utils.py to define the mapping between local and remote files. Here my remote is a vm and I've mounted the remote /home/vagrant at '/Users/brian/sandbox/vagrant/example.dev/nfs-vagrant' PATHS_FROM_ECLIPSE_TO_PYTHON = [(r''/Users/morteza/zproject/workspaces/spokenlanguageunderstanding, r'/home/msnia/zproject/spokenlanguageunderstanding'), ] |

Software Engineering ➼ Machine learning ➼ Data Science ➼ Product Leadership 🎯 > Tools - Business >