text:
# 0. Python Tutorial¶

### About the notebook/slides¶

• The slides are programmed as a Jupyter/IPython notebook.
• Feel free to try them and experiment on your own by launching the notebooks.

• You can run the notebook online:

If you are using nbviewer you can change to slides mode by clicking on the icon:

# Introduction¶

• Python is a great general-purpose programming language,...
• ...and with the help of a libraries such as numpy, scipy and matplotlib (among many others) it becomes a powerful environment for scientific computing.

# Scripta volant, codex manent

Python main characteristics:

• dynamic type system
• interpreted (actually: compiled to bytecode, *.pyc files)
• multi-paradigm: imperative, procedural, object-oriented, (functional), literate; do whatever you want
• indentation is important!

I assume that you have some programming experience:

• Java (static type system, compiled, object-oriented, verbose)
• C/C++ (static type system, compiled, multi-paradigm, low-level)
• Matlab? R?

I also expect that many of you will have some experience with Python and numpy; for the rest of you, this section will serve as a quick crash course both on the Python programming language and on the use of Python for scientific computing.

Some of you may have previous knowledge in Matlab, in which case we also recommend the numpy for Matlab users page (https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html).

# In this tutorial¶

• Basic Python: Basic data types, containers, loops, functions and classes.
• numpy: arrays, array indexing, datatypes, array math and broadcasting.
• matplotlib: plotting, subplots, images.
• Jupyter and IPython: Creating notebooks and typical workflows.

# Basics of Python¶

• Python is a high-level, dynamically typed multiparadigm programming language.
• Python code is often said to be almost like pseudocode, since it allows you to express very powerful ideas in very few lines of code while being very readable.

Required Python packages (libraries) to run this notebook:

• Python
• Jupyter/IPython - interactive Python shell
• numpy - linear algebra library
• matplotlib - plotting library

Scientific Python Distributions (available for almost every platform):

As an example, here is an implementation of the classic quicksort algorithm in Python:

In [1]:
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[int(len(arr) / 2)]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)

In [2]:
quicksort([3,6,8,10,1,2,1])

Out[2]:
[1, 1, 2, 3, 6, 8, 10]

# Python versions¶

• There are currently two different supported versions of Python, 2.x and 3.x.
• Python 3.0 introduced many backwards-incompatible changes to the language, so code written for 2.x may not work under 3.x and vice versa.
• In our class we will be using Python 3.x.

Note: You can check your Python version at the command line by running python --version.

# Basic data types¶

## Numeric types¶

Integers and floats work as you would expect from other languages:

In [3]:
x = 3
print(x, type(x))

3 <class 'int'>

In [4]:
print(x + 1)   # Addition;
print(x - 1)   # Subtraction;
print(x * 2)   # Multiplication;
print(x ** 2)  # Exponentiation.

4
2
6
9

Autoincrements:

In [5]:
x += 1
print(x)  # Prints "4"
x *= 2
print(x)  # Prints "8"

4
8

Note that unlike many languages, Python does not have unary increment (x++) or decrement (x--) operators.

Python also has built-in types for long integers and complex numbers; you can find all of the details in the documentation.

Real numbers (float)

In [6]:
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"

<class 'float'>
2.5 3.5 5.0 6.25

## Booleans¶

Python implements all of the usual operators for Boolean algebra, but uses English words rather than symbols (&&, ||, !, etc.):

In [7]:
t, f, aa, bb = True, False, True, False # Hmm... yes you can do this in Python
print(t, f, type(t))

True False <class 'bool'>

Now we let's look at the operations:

In [8]:
print(t and f) # Logical AND;
print(t or f)  # Logical OR;
print(not t)   # Logical NOT;
print(t != f)  # Logical XOR;

False
True
False
True

## Booleans and truth Testing¶

• In programming we invariably need some concept of conditions to allow a program to react differently to different situations.
• Python uses a combination of Boolean variables, which evaluate to either True or False, and if statements.
In [5]:
day = "Sunday"
if day == 'Sunday':
print('Sleep!!!')
else:
print('Go to work')

Sleep!!!

There is no switch or case statements

In [10]:
if day == 'Monday':
print('Week end is over!')
elif day == 'Sunday' or day =='Saturday':
print('Sleep!')
else:
print('Meeeh')

Week end is over!

In [11]:
var_1 = 234
if var_1:
print('Do something with', var_1)
else:
print('Nothing to do')

Do something with 234

In [12]:
1 == 2

Out[12]:
False
In [13]:
50 == 2*25

Out[13]:
True

There is another boolean operator is, that tests whether two objects are the same type:

In [14]:
1 is 1

Out[14]:
True

But not...

In [15]:
1 is int

Out[15]:
False
In [16]:
print(type(1), type(int))

<class 'int'> <class 'type'>

In [17]:
1 is 1.0

Out[17]:
False

## Strings¶

String literals can use single quotes ('') or or double quotes(""); it does not matter.

In [18]:
hello = 'hello'
world = "world"
print(hello, len(hello))

hello 5

In [19]:
hw = hello + ' ' + world  # String concatenation
print(hw)

hello world

String formatting

In [20]:
hw12 = '%s %s! your number is: %d' % (hello, world, 12)  # sprintf style string formatting
print(hw12)

hello world! your number is: 12

Note: Checkout https://docs.python.org/3/library/string.html#formatspec for string formatting specs.

String objects have a bunch of useful methods; for example:

In [21]:
s = "hello"
print(s.capitalize())           # Capitalize a string; prints "Hello"
print(s.upper())                # Convert a string to uppercase; prints "HELLO"
print(s.rjust(7))               # Right-justify a string, padding with spaces; prints "  hello"
print(s.center(7))              # Center a string, padding with spaces; prints " hello "
print(s.replace('l', '(ell)'))  # Replace all instances of one substring with another;
# prints "he(ell)(ell)o"
print('  world '.strip())       # Strip leading and trailing whitespace; prints "world"

Hello
HELLO
hello
hello
he(ell)(ell)o
world

Again, you can find a list of all string methods in the documentation.

# Containers¶

Python includes several built-in container types: lists, dictionaries, sets, and tuples.

## Lists¶

A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:

In [22]:
xs = [3, 1, 2]    # Create a list
print(xs, xs[2])
print(xs[-1])     # Negative indices count from the end of the list; prints "2"

[3, 1, 2] 2
2

In [23]:
xs[2] = 'foo'    # Lists can contain elements of different types
print(xs)

[3, 1, 'foo']

In [24]:
xs.append('bar') # Add a new element to the end of the list
print(xs)

[3, 1, 'foo', 'bar']

In [25]:
xs =  xs + ['thing1', 'thing2'] # Adding lists (the += op works too)
print(xs)

[3, 1, 'foo', 'bar', 'thing1', 'thing2']

Python lists also implement queue and stack data structures.

In [26]:
x = xs.pop()     # Remove and return the last element of the list
print(x, xs)

thing2 [3, 1, 'foo', 'bar', 'thing1']

As usual, you can find all the gory details about lists in the documentation.

## Slicing¶

In addition to accessing list elements one at a time, Python provides concise syntax to access sublists; this is known as slicing:

In [27]:
nums = list(range(5)) # range is a built-in function (more on this later)
print(nums)

[0, 1, 2, 3, 4]

In [28]:
print(nums[2:4])    # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:])     # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:2])     # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(nums[:])      # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print(nums[:-1])    # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8, 9]  # Assign a new sublist to a slice
print(nums)         # Prints "[0, 1, 8, 9, 4]"

[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]

# Loops¶

You can loop over the elements of a list like this:

In [29]:
animals = ['cat', 'dog', 'monkey']
for animal in animals:
aa = animal + ' :)'
print(aa)

cat :)
dog :)
monkey :)

If you want access to the index of each element within the body of a loop, use the built-in enumerate function:

In [30]:
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print('Item number %d is a %s' % (idx + 1, animal))

Item number 1 is a cat
Item number 2 is a dog
Item number 3 is a monkey

## Conditional loops¶

In Python we have only a while loop.

In [31]:
i = 0
while i < 3:
print(i)
i += 1

0
1
2

# List comprehensions¶

When programming, frequently we want to transform one type of data into another.

For example, consider the following code that computes square numbers:

In [32]:
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
squares.append(x ** 2)
print(squares)

[0, 1, 4, 9, 16]

You can make this code simpler using a list comprehension:

In [33]:
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print(squares)

[0, 1, 4, 9, 16]

List comprehensions can also contain conditions:

In [34]:
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares)

[0, 4, 16]

# Dictionaries¶

A dictionary stores (key, value) pairs, similar to a Map in Java or an object in Javascript. You can use it like this:

In [35]:
d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data

In [36]:
print(d['cat'])       # Get an entry from a dictionary; prints "cute"
print('cat' in d)     # Check if a dictionary has a given key; prints "True"

cute
True

In [37]:
d['fish'] = 'wet'     # Set an entry in a dictionary
print(d['fish'])      # Prints "wet"

wet

In [38]:
print(d['monkey'])  # KeyError: 'monkey' not a key of d

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-38-e3ac4f3aa8c2> in <module>()
----> 1 print(d['monkey'])  # KeyError: 'monkey' not a key of d

KeyError: 'monkey'
In [39]:
print(d.get('monkey', 'N/A'))  # Get an element with a default; prints "N/A"
print(d.get('fish', 'N/A'))    # Get an element with a default; prints "wet"

N/A
wet

In [40]:
del d['fish']               # Remove an element from a dictionary
print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"

N/A

It is easy to iterate over the keys in a dictionary:

In [7]:
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print('A %s has %d legs' % (animal, legs))

A person has 2 legs
A cat has 4 legs
A spider has 8 legs

## Dictionary comprehensions¶

These are similar to list comprehensions, but allow you to easily construct dictionaries. For example:

In [42]:
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print(even_num_to_square)

{0: 0, 2: 4, 4: 16}

# Sets¶

A set is an unordered collection of distinct elements. As a simple example, consider the following:

In [43]:
animals = {'cat', 'dog'}
print(animals)

{'dog', 'cat'}

In [44]:
print('cat' in animals)   # Check if an element is in a set; prints "True"
print('fish' in animals)  # prints "False"

True
False

In [45]:
animals.add('fish')      # Add an element to a set
print('fish' in animals)
print(len(animals))       # Number of elements in a set;

True
3

In [46]:
animals.add('cat')           # Adding an element that is already in the set does nothing
print(animals, len(animals))
animals.remove('cat')        # Remove an element from a set
print(animals,len(animals))

{'dog', 'cat', 'fish'} 3
{'dog', 'fish'} 2

## Loops in sets¶

• Iterating over a set has the same syntax as iterating over a list;
• however since sets are unordered, you cannot make assumptions about the order in which you visit the elements of the set.
In [47]:
animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
# Prints "#1: fish", "#2: dog", "#3: cat"

#1: dog
#2: cat
#3: fish

## Set comprehensions¶

Like lists and dictionaries, we can easily construct sets using set comprehensions:

In [48]:
from math import sqrt
print('Set:', {int(sqrt(x)) for x in range(30)})
print('List:', [int(sqrt(x)) for x in range(30)])

Set: {0, 1, 2, 3, 4, 5}
List: [0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5]

## Tuples¶

• An (immutable) ordered list of values;
• 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,

Here is a trivial example:

In [9]:
t = (5, 6)
print(t, type(t))

(5, 6) <class 'tuple'>
5 6

In [50]:
d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
print(d)

{(0, 1): 0, (1, 2): 1, (2, 3): 2, (3, 4): 3, (4, 5): 4, (5, 6): 5, (6, 7): 6, (7, 8): 7, (8, 9): 8, (9, 10): 9}

In [51]:
print(d[t])
print(d[(1, 2)])

5
1

In [52]:
t[0] = 1 # Produces TypeError 'tuple' object does not support item assignment

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-5757bc7a932f> in <module>()
----> 1 t[0] = 1 # Produces TypeError 'tuple' object does not support item assignment

TypeError: 'tuple' object does not support item assignment

# Functions¶

Python functions are defined using the def keyword. For example:

In [53]:
def sign(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'

In [54]:
for x in [-1, 0, 1]:
print(sign(x))

negative
zero
positive

We will often define functions to take optional keyword arguments, like this:

In [55]:
def hello(name, loud=False):
if loud:
print('HELLO, %s' % name.upper())
else:
print('Hello, %s!' % name)

In [56]:
hello('Bob')
hello('Fred', loud=True)

Hello, Bob!
HELLO, FRED

## $\lambda$-constructors (inline functions)¶

In [57]:
func = lambda x: x**x + 2
print(type(func))

<class 'function'>

In [58]:
func(3)

Out[58]:
29
In [59]:
x = [1, 5, 3, 2, 7, 8, 3, 6]

print([func(elem) for elem in x])
list(map(lambda x: x**x + 2, x)) # note the map() function

[3, 3127, 29, 6, 823545, 16777218, 29, 46658]

Out[59]:
[3, 3127, 29, 6, 823545, 16777218, 29, 46658]

# Classes and object oriented programming¶

• The syntax for defining classes in Python is straightforward.
• Remember to include self as the first parameter of the class methods.
In [20]:
class Greeter():
# Constructor
def __init__(self, name):
self.name = name  # Create an instance variable

# Instance method
def greet(self, loud=False):
if loud:
print('HELLO, %s!' % self.name.upper())
else:
print('Hello, %s' % self.name)

In [23]:
g = Greeter('Fred')  # Construct an instance of the Greeter class
g.greet()            # Call an instance method; prints "Hello, Fred"
g.greet(loud=True)   # Call an instance method; prints "HELLO, FRED!"

Hello, Fred
HELLO, FRED!

For more information check the Python docs

# Exceptions¶

As in many other modern languages Python can handle exceptions.

In [25]:
def my_func(a):
return a * a

In [26]:
print(my_func(2))

4

In [27]:
my_func('AAAAA')

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-c43b8d3fab9a> in <module>()
----> 1 my_func('AAAAA')

<ipython-input-25-77fcce2d5fdd> in my_func(a)
1 def my_func(a):
----> 2     return a * a

TypeError: can't multiply sequence by non-int of type 'str'
In [64]:
try:
print(my_func("A"))
except TypeError as e:
print('Caught exception:', e)

Caught exception: can't multiply sequence by non-int of type 'str'

# The import statement¶

• We have seen already the import statement in action.
• Python has a huge number of libraries included with the distribution.
• Most of these variables and functions are not accessible from a normal Python interactive session.
• Instead, you have to import them.

# Modules¶

• A module allows you to logically organize your Python code;
• groups related code into a module makes the code easier to understand and use.

• A module is a file consisting of Python code, can define functions, classes and variables.

• A module can also include runnable code.

Note: A module is also a Python object with arbitrarily named attributes that you can bind and reference.

## Importing complete modules¶

Importing whole modules with an additional short name:

In [65]:
import scipy.spatial.distance

In [66]:
scipy.spatial.distance.euclidean((1,1), (2,2))

Out[66]:
1.4142135623730951
In [67]:
import scipy.spatial.distance as dists

In [68]:
dists.euclidean((1,1), (2,2))

Out[68]:
1.4142135623730951

## Importing a particular function or class.¶

For example, there is a math module containing many useful functions. To access, say, the square root function, you can

In [69]:
from math import sqrt

and then

In [70]:
sqrt(81)

Out[70]:
9.0

# numpy: scientific computing with Python¶

• Numpy is the core library for scientific computing in Python.
• It provides a high-performance multidimensional array object, and tools for working with these arrays.

To use numpy, we first need to import numpy package:

In [71]:
import numpy as np

## Arrays¶

A 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 rank of the array;
• The shape of an array is a tuple of integers giving the size of the array along each dimension.
• useful linear algebra, Fourier transform, random number capabilities and more.
• Complemented by scipy.

We can initialize numpy arrays from nested Python lists, and access elements using square brackets:

In [72]:
a = np.array([1, 2, 3])  # Create a rank 1 array
print(type(a), a.shape, a[0], a[1], a[2])
a[0] = 5  # Change an element of the array
print(a)

<class 'numpy.ndarray'> (3,) 1 2 3
[5 2 3]

In [73]:
b = np.array([[1, 2, 3], [4, 5, 6]])  # Create a rank 2 array
print(b)

[[1 2 3]
[4 5 6]]

In [74]:
print(b.shape)
print(b[0, 0], b[0, 1], b[1, 0])

(2, 3)
1 2 4

Numpy provides many shortcut functions to create arrays:

In [75]:
a = np.zeros((2,2))  # Create an array of all zeros
print(a)

[[ 0.  0.]
[ 0.  0.]]

In [76]:
b = np.ones((1,2))   # Create an array of all ones
print(b)

[[ 1.  1.]]

In [77]:
c = np.full((2,2), 7)
print(c)

[[7 7]
[7 7]]

In [78]:
d = np.eye(2)        # Create a 2x2 identity matrix
print(d)

[[ 1.  0.]
[ 0.  1.]]

In [79]:
e = np.random.random((2,2)) # Create an array filled with random values
print(e)

[[ 0.09845823  0.09568912]
[ 0.18341987  0.72051223]]

## Array indexing¶

Numpy offers several ways to index into arrays.

Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:

In [80]:
# Create the following rank 2 array with shape (3, 4)
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
a

Out[80]:
array([[ 1,  2,  3,  4],
[ 5,  6,  7,  8],
[ 9, 10, 11, 12]])

Use slicing to pull out the subarray consisting of the first 2 rows and columns 1 and 2; b is the following array of shape (2, 2):

In [81]:
b = a[:2, 1:3]
print(b)

[[2 3]
[6 7]]

A slice of an array is a view into the same data, so modifying it will modify the original array.

In [82]:
print(a[0, 1])
b[0, 0] = 77      # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1])

2
77

You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank than the original array.

Note: This is quite different from the way that MATLAB handles array slicing.

In [83]:
# Create the following rank 2 array with shape (3, 4)
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a)

[[ 1  2  3  4]
[ 5  6  7  8]
[ 9 10 11 12]]

Two ways of accessing the data in the middle row of the array. Mixing integer indexing with slices yields an array of lower rank, while using only slices yields an array of the same rank as the original array:

In [84]:
row_r1 = a[1, :]    # Rank 1 view of the second row of a
row_r2 = a[1:2, :]  # Rank 2 view of the second row of a
row_r3 = a[[1], :]  # Rank 2 view of the second row of a
print(row_r1, row_r1.shape)
print(row_r2, row_r2.shape)
print(row_r3, row_r3.shape)

[5 6 7 8] (4,)
[[5 6 7 8]] (1, 4)
[[5 6 7 8]] (1, 4)

We can make the same distinction when accessing columns of an array:

In [85]:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape)
print(col_r2, col_r2.shape)

[ 2  6 10] (3,)
[[ 2]
[ 6]
[10]] (3, 1)

Integer array indexing

• When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array.
• In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array.

Here is an example:

In [86]:
a = np.array([[1,2], [3, 4], [5, 6]])
print(a)

[[1 2]
[3 4]
[5 6]]

An example of integer array indexing. The returned array will have shape (3,).

In [87]:
a[[0, 1, 2], [0, 1, 0]]

Out[87]:
array([1, 4, 5])

The above example of integer array indexing is equivalent to this:

In [88]:
np.array([a[0, 0], a[1, 1], a[2, 0]])

Out[88]:
array([1, 4, 5])

When using integer array indexing, you can reuse the same element from the source array:

In [89]:
a[[0, 0], [1, 1]]

Out[89]:
array([2, 2])

Equivalent to the previous integer array indexing example

In [90]:
np.array([a[0, 1], a[0, 1]])

Out[90]:
array([2, 2])

One useful trick with integer array indexing is selecting or mutating one element from each row of a matrix:

Create a new array from which we will select elements

In [91]:
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(a)

[[ 1  2  3]
[ 4  5  6]
[ 7  8  9]
[10 11 12]]

Create an array of indices

In [92]:
b = np.array([0, 2, 0, 1])

Select one element from each row of a using the indices in b.

In [93]:
print(a[np.arange(4), b])  # Prints "[ 1  6  7 11]"

[ 1  6  7 11]

Mutate one element from each row of a using the indices in b.

In [94]:
a[np.arange(4), b] += 10
print(a)

[[11  2  3]
[ 4  5 16]
[17  8  9]
[10 21 12]]

Boolean array indexing: Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition. Here is an example:

In [95]:
a = np.array([[1,2], [3, 4], [5, 6]])

Find the elements of a that are bigger than 2:

In [96]:
bool_idx = (a > 2)
print(bool_idx)

[[False False]
[ True  True]
[ True  True]]

This returns a numpy array of Booleans of the same shape as a, where each slot of bool_idx tells whether that element of a is > 2.

We use boolean array indexing to construct a rank 1 array consisting of the elements of a corresponding to the True values of bool_idx.

In [97]:
print(a[bool_idx])

[3 4 5 6]

We can do all of the above in a single concise statement:

In [98]:
print(a[a > 2])

[3 4 5 6]

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.

## Datatypes¶

Every 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:

In [30]:
import numpy as np
x = np.array([1, 2])                  # Let numpy choose the datatype
y = np.array([1.0, 2.0])              # Let numpy choose the datatype
z = np.array([1, 2], dtype=np.float32)  # Force a particular datatype

print(x.dtype, y.dtype, z.dtype)

int64 float64 float32

You can read all about numpy datatypes in the documentation.

## Array math¶

Basic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy module:

In [100]:
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

In [101]:
print(x + y)

[[  6.   8.]
[ 10.  12.]]

In [102]:
print(np.add(x, y))

[[  6.   8.]
[ 10.  12.]]

Elementwise difference

In [103]:
print(x - y)
print(np.subtract(x, y))

[[-4. -4.]
[-4. -4.]]
[[-4. -4.]
[-4. -4.]]

Elementwise product

In [104]:
print(x * y)
print(np.multiply(x, y))

[[  5.  12.]
[ 21.  32.]]
[[  5.  12.]
[ 21.  32.]]

Elementwise division - both produce the same result.

In [105]:
print(x / y)
print(np.divide(x, y))

[[ 0.2         0.33333333]
[ 0.42857143  0.5       ]]
[[ 0.2         0.33333333]
[ 0.42857143  0.5       ]]

Elementwise square root;

In [106]:
print(np.sqrt(x))

[[ 1.          1.41421356]
[ 1.73205081  2.        ]]

## Important note:¶

• Unlike MATLAB, * is elementwise multiplication, not matrix multiplication.
• We instead use the numpy.dot() function to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices.
• dot() is available both as a function in the numpy module and as an instance method of array objects.
In [107]:
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
print(v.dot(w))
print(np.dot(v, w))

219
219

In [108]:
# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))

[29 67]
[29 67]

In [109]:
# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
#  [43 50]]
print(x.dot(y))
print(np.dot(x, y))

[[19 22]
[43 50]]
[[19 22]
[43 50]]

Numpy provides many useful functions for performing computations on arrays; one of the most useful is sum:

In [110]:
x = np.array([[1,2],[3,4]])
print(x)

[[1 2]
[3 4]]

In [111]:
np.sum(x)  # Compute sum of all elements; prints "10"

Out[111]:
10
In [112]:
np.sum(x, axis=0)  # Compute sum of each column; prints "[4 6]"

Out[112]:
array([4, 6])
In [113]:
np.sum(x, axis=1)  # Compute sum of each row; prints "[3 7]"

Out[113]:
array([3, 7])

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 T attribute of an array object:

In [114]:
print(x)
print(x.T)

[[1 2]
[3 4]]
[[1 3]
[2 4]]

In [115]:
v = np.array([1,2,3])
print(v)
print(v.T)

[1 2 3]
[1 2 3]

Is numpy actually faster than 'plain' Python?

In [116]:
import random
import numpy as np

In [117]:
%timeit sum([random.random() for _ in range(random.randint(10000,10100))])

2.1 ms ± 93 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [118]:
%timeit np.sum(np.random.random(random.randint(10000,10100)))

177 µs ± 3.67 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Broadcasting 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:

In [119]:
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
x

Out[119]:
array([[ 1,  2,  3],
[ 4,  5,  6],
[ 7,  8,  9],
[10, 11, 12]])
In [120]:
v = np.array([1, 0, 1])
v

Out[120]:
array([1, 0, 1])
In [121]:
y = np.empty_like(x)   # Creates an empty matrix with the same shape as x

# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
y[i, :] = x[i, :] + v

In [122]:
y

Out[122]:
array([[ 2,  2,  4],
[ 5,  5,  7],
[ 8,  8, 10],
[11, 11, 13]])
• This works; however when the matrix x is very large, computing an explicit loop in Python could be slow.

• Note that adding the vector v to each row of the matrix x is equivalent to forming a matrix vv by stacking multiple copies of v vertically, then performing elementwise summation of x and vv.

We could implement this approach like this:

In [123]:
vv = np.tile(v, (4, 1))  # Stack 4 copies of v on top of each other
vv

Out[123]:
array([[1, 0, 1],
[1, 0, 1],
[1, 0, 1],
[1, 0, 1]])
In [124]:
y = x + vv  # Add x and vv elementwise
y

Out[124]:
array([[ 2,  2,  4],
[ 5,  5,  7],
[ 8,  8, 10],
[11, 11, 13]])

Numpy broadcasting allows us to perform this computation without actually creating multiple copies of v. Consider this version, using broadcasting:

In [125]:
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = x + v  # Add v to each row of x using broadcasting
y

Out[125]:
array([[ 2,  2,  4],
[ 5,  5,  7],
[ 8,  8, 10],
[11, 11, 13]])

The line y = x + v works even though x has shape (4, 3) and v has shape (3,) due to broadcasting; this line works as if v actually had shape (4, 3), where each row was a copy of v, and the sum was performed elementwise.

Broadcasting two arrays together follows these rules:

1. 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.
2. 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.
3. The arrays can be broadcast together if they are compatible in all dimensions.
4. After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays.
5. 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 universal functions. You can find the list of all universal functions in the documentation.

## Some applications of broadcasting:¶

Compute outer product of vectors

In [126]:
v = np.array([1,2,3])  # v has shape (3,)
w = np.array([4,5])    # w has shape (2,)

To compute an outer product, we

• reshape v to be a column vector of shape (3, 1), and
• broadcast it against w to yield an output of shape (3, 2), which is the outer product of v and w:
In [127]:
print(np.reshape(v, (3, 1)) * w)

[[ 4  5]
[ 8 10]
[12 15]]

Add a vector to each row of a matrix

In [128]:
x = np.array([[1,2,3], [4,5,6]])

x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3), producing the following matrix:

In [129]:
print(x + v)

[[2 4 6]
[5 7 9]]

Add a vector to each column of a matrix x has shape (2, 3) and w has shape (2,).

• If we transpose x then it has shape (3, 2) and can be broadcast against w to yield a result of shape (3, 2);
• transposing this result yields the final result of shape (2, 3) which is the matrix x with the vector w added to each column.

This gives the following matrix:

In [130]:
print((x.T + w).T)

[[ 5  6  7]
[ 9 10 11]]

Another solution is to reshape w to be a row vector of shape (2, 1); we can then broadcast it directly against x to produce the same output.

In [131]:
print(x + np.reshape(w, (2, 1)))

[[ 5  6  7]
[ 9 10 11]]

Multiply a matrix by a constant

• x has shape (2, 3).
• numpy treats scalars as arrays of shape ();
• they can be broadcasted together to shape (2, 3),
• producing the following array:
In [132]:
print(x * 2)

[[ 2  4  6]
[ 8 10 12]]

Broadcasting typically makes your code more concise and faster, so you should strive to use it where possible.

So far, we 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.
• If you are already familiar with MATLAB, you might find this tutorial useful to get started with numpy.
• Nicolas P. Rougier's Numpy tutorial highly recommended.

# Matplotlib¶

• Matplotlib is a plotting library.
• Provides a plotting system similar -and in my opinion better- to that of MATLAB.
In [133]:
import matplotlib.pyplot as plt

By running this special IPython notebook magic command, we will be displaying plots inline and with appropiate resolutions for retina displays. Check the docs for more options.

In [134]:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

## Plotting¶

The most important function in matplotlib is plot, which allows you to plot 2D data. Here is a simple example:

In [135]:
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)

# Plot the points using matplotlib
plt.plot(x, y_sin);

With just a little bit of extra work we can easily plot multiple lines at once, and add a title, legend, and axis labels:

In [136]:
y_cos = np.cos(x)
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine']);

## Subplots¶

You can plot different things in the same figure using the subplot function. Here is an example:

In [137]:
plt.subplot(2, 1, 1)
plt.plot(x, y_sin)
plt.title('Sine')
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
plt.tight_layout()

Check out Nicolas P. Rougier's Matplotlib tutorial.

# What's next¶

• Install Python on your own computer (remember to install version 3.x)
• Retrive this notebook from the class GitHub repository: http://github.com/lmarti/machine-learning.
• If you are not familiar with the Git version control system you can use GitHub desktop.
• Try running this notebook.

# Acknowledgments¶

In [138]:
%load_ext version_information
%version_information scipy, numpy, matplotlib

Out[138]:
SoftwareVersion
Python3.6.1 64bit [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
IPython6.0.0
OSDarwin 16.5.0 x86_64 i386 64bit
scipy0.19.0
numpy1.12.1
matplotlib2.0.0
Thu Apr 27 23:03:44 2017 -03
In [1]:
# this code is here for cosmetic reasons
from IPython.core.display import HTML
from urllib.request import urlopen


text:
# Machine learning using neuroimaging data

Suppose I have collected the hemodynamic responses of participants when they were performing cognitive tasks (e.g. n-back) using a 16-channel functional near-infrared spectroscopy device. I would like to extract some features of the data and perform machine learning to classify participants (e.g. demented vs. not demented / ASD vs. non-ASD).

Can the oxygenated hemoglobin data in all 16 channels of each participant treated as 16 distinct observations? That is, if I have 3 participants, the sample size = 3 participants x 16 channels = 48. I wonder if that is possible? (the data in each channel may correlate with each others)

And, if I use this way to increase the sample size, will it violate any assumptions of machine learning?

• Your data sound photograph-like (or x-ray). Is that fair to say? – Dave Feb 8 at 11:24
• The data is in numerical instead of a figure/photograph. All the channels were placed on the forehead and it measured the oxy-hemoglobin in these 16 channels, so each participant got 16 data points. – TLL Feb 8 at 14:29
• What is your concern about using the $16$ variables? This seems like a standard regression-type problem where you make a prediction based on measurements you take, whether they are your $16$ variables or the pixels in a photograph. Do you mean as $16$ distinct observations so that $3$ patients means $n=48$? – Dave Feb 8 at 16:04
• Sorry for the confusion. What I want to ask is what you mentioned: Can the data in the 16 channels treated as 16 distinct observations so that 3 patients means n=48? – TLL Feb 9 at 2:05

format: plain


text:
Index: The Book of Statistical ProofsGeneral Theorems ▷ Bayesian statistics ▷ Probabilistic modeling ▷ Full probability model

Definition: Consider measured data $y$ and some unknown latent parameters $\theta$. The combination of a generative model for $y$ and a prior distribution on $\theta$ is called a full probability model $m$:

$\label{eq:fpm} m: \, y \sim \mathcal{D}(\theta), \, \theta \sim \mathcal{D}(\lambda) \; .$

Sources:

Metadata: ID: D30 | shortcut: fpm | author: JoramSoch | date: 2020-03-03, 16:16.


text:
# How to extract phase angle from sinusoid

I'm doing some electric circuit calcualtions and I'm trying to get the phasor representation of some arbitrary function of Sin or Cos. Could be complex like:

(0. - 10. I) Sin[1000 t]

For instance, if I have:

Vin = 10Cos[1000t- 90Degree]

which is:

Vin = 10Re[Exp[-I*90Degree]*Exp[I*1000t]]

I want to get the magnitude angle form. i.e $10\angle-90^\circ$

I can get the magnitude with:

MaxValue[Abs[Vin], t]

but I can't get the phase angle correctly. I've tried:

Re[N[ArcCos[(Vin/MaxValue[Vin, t]) /. t -> 0]/Degree]]

but that gives $90^\circ$ not $-90^\circ$

So I have 2 questions actually:

1. Can I force mathematica to keep my function in $\cos\left(something - 90^\circ\right)$ form instead of changing it to $\sin\left(something + 0^\circ\right)$?
2. And how can I correctly obtain the phase angle
-

Maybe you could expand the Vin to Fourier series to "normalize" it.

For example there are three of them kind:

VinSet = {10 Cos[1000 t - π/2], 9 Cos[400 t + π/4], Cos[t + 3.45]};

coeffSet = FourierCoefficient[# /. Times[ω_?NumericQ t] :> t, t, 1] & /@ VinSet

$\left\{-5 i,\frac{9 \sqrt[4]{-1}}{2}, -0.476409-0.151771 i\right \}$

{2 Abs[#], Arg[#]} & /@ coeffSet

{$\left\{10,-\frac{\pi }{2}\right\}$,$\left\{9,\frac{\pi }{4}\right\}$,$\{1.,-2.83319\}$}

Note $2\pi-2.83319 \approx 3.45$.

-

If you specify the angle as a real number (rather than an exact integer), it does not do the transformation to Sin. For instance

Vin = 10 Cos[1000 t - Pi/2.0]

and

Vin = 10 Cos[1000 t - 90.0 Degree]

both do what you ask.

-
Great that works, any idea how to obtain the phase angle? –  Wayne Apr 26 '13 at 21:51

As for your second question, here's one way that is very similar to what we'd do by hand. Use the exponential form and then identify the phase and magnitude.

Clear[A, p, t]
Vin = 10 Exp[-Pi/2]*Exp[1000 t I];
{A, p} = Replace[Vin, A_ Exp[p_] -> {A Exp[Re[p]], Im[p]}]

A now holds the amplitude and p holds the phase. If you have the trigonometric form you can use FullSimplify to bring it into the exponential form first.

To get rid of Im[t] and Re[t] we can tell Mathematica that the variable t is a real variable:

Refine[{A, p}, Assumptions :> t \[Element] Reals]

-


text:
# Building an Exercise and Weight Display: Part 1

[I’ve called this Part 1. Let’s see if I get around to writing parts 2–4.]

While doing some overplanning in the last few weeks of the year, I built a few text files and spreadsheets. In the process, I decided that some sort of display would be worthwhile for keeping track of a few goals that I had developed. I thought about Dashboard widgets, but they seem to be waning. And then I thought about NerdTool/GeekTool, but those never worked very well for me when switching monitor resolutions, which I do often. And then a friend mentioned Übersicht, which I didn’t think I had heard of, but Brett Terpstra has written a couple of widgets. So, I’ve probably at least heard of it.

So, I played with some of the widgets from the gallery, and developed the beginnings of the heads up display that I wanted. But, one of the things I wanted was a way to track weight and exercise. I’ve been tracking my running for the last few years in RunKeeper, and I’ve been tracking my weight, off and on, in Weighbot. So, the first question was how to get the data out.

Weighbot presented a problem. It is mostly an iOS app, although it has a cloud backup service. The cloud backup service will let you download all your data in a CSV file from a website. This could probably be automated. But, my tests suggested that Weighbot usually did a backup immediately upon app launch. My usual usage pattern is that once a day I launch the app, enter my weight, and then close the app. The backup on launch, though, meant that the data set I downloaded was always missing today’s data point, unless I forced an additional backup. Then I realized, though, that RunKeeper could also track weight. I put a couple weeks worth of data points in, and I was off to the races.

RunKeeper has an API called HealthGraph that lets other apps access their data. I assumed that access to this API would be simple, like for Flickr or Forecast.io. But it’s a little more involved. First, you register, and they give you a Client ID and a Client Secret. You then use the Client ID to get RunKeeper to generate a Code. You then use the Client ID, Client Secret, and Code to generate an Access Token, which finally lets you access the data. Figuring all this out and making it work seamlessly is probably worthwhile if you are building an app for the masses, but it was quite a lot of trouble for a one-off data display. Luckily, this blog post walks you through the steps and provides the necessary snippets of code.

Then, I was off to the races. Starting by stealing some API access code from Dr. Drang, I used my new Access Token to create some Python code that downloads my run data and my weight data. Basically, you request a URL with a couple extra headers (including the Access Token) in the HTTP request, and you get back JSON blobs of data. I then wrote code that processed those blobs into a graph and extracted the bits of raw data that I wanted to display. At that point, I needed to get the raw data over to Übersicht to format for display. I puzzled for a bit about how to do this, and I wound up with an obvious solution—another JSON blob. Since Übersicht uses a JavaScript-related language called CoffeeScript, I assumed that processing the JSON would be straightforward, and it was.

Here’s the code that downloads the necessary JSON blobs. It requires a bit of explanation. First, it gets the Access Token from a file called .runkeeper that I keep in my home directory. Then, it makes an initial call to the RunKeeper API to get a JSON blob of user information. In fact, with the exception of a User ID, this blob contains almost no actual user information. What it contains, instead, are pointers to the URLs to use to obtain the desired JSON blobs. In fact, these seem to be static strings, but the API documentation insists that they should not be hardcoded. Thus, the next two calls to the RunKeeper API use the information from the user blob to request the fitness_activities blob and the weight blob.

#!/usr/bin/python
# coding=utf-8

from __future__ import division
from __future__ import unicode_literals
import json
import urllib2
from os import environ

# Get my Runkeeper Access Token
try:
with open(environ['HOME'] + '/.runkeeper') as rcfile:
for line in rcfile:
k, v = line.split(':')
if k.strip() == 'access_token':
AccessToken = v.strip()
except (IOError, NameError):
print "Failed to get Access Token"
exit()

# Get the user from Runkeeper
try:
userReq = urllib2.Request('http://api.runkeeper.com/user')
userReq.add_header('Authorization', 'Bearer ' + AccessToken)
userReq.add_header('Accept', 'application/vnd.com.runkeeper.User+json')
userJsonString = urllib2.urlopen(userReq).read()
user = json.loads(userJsonString);
except (IOError, ValueError):
print "Connection failure for user."
exit()

# Get the fitness activities from Runkeeper
try:
fitnessReq = urllib2.Request('http://api.runkeeper.com' + user['fitness_activities'])
fitnessReq.add_header('Authorization', 'Bearer ' + AccessToken)
fitnessReq.add_header('Accept', 'application/vnd.com.runkeeper.FitnessActivityFeed+json')
fitnessJsonString = urllib2.urlopen(fitnessReq).read()
fitness = json.loads(fitnessJsonString);
except (IOError, ValueError):
print "Connection failure for fitness activities."
exit()

# Get the weight from Runkeeper
try:
weightReq = urllib2.Request('http://api.runkeeper.com' + user['weight'])
weightReq.add_header('Authorization', 'Bearer ' + AccessToken)
weightReq.add_header('Accept', 'application/vnd.com.runkeeper.WeightSetFeed+json')
weightJsonString = urllib2.urlopen(weightReq).read()
weight = json.loads(weightJsonString);
except (IOError, ValueError):
print "Connection failure for weight data."
exit()

Each call to ‘json.loads()’ parses a JSON blob and turns it into a Python dictionary. The structure of these dictionaries (JavaScript Objects) is explained in the HealthGraph API documentation. Here is the documentation of the fitness_activities blob, and here is the documentation for the weight blob. There is no real error checking, and your mileage may vary, etc.

It’s been a fun little project. If I were to write a second part of this blog post, it would be on extracting the running data for display (and retrieving my training plan from a text file for display, as well). If I were to write a third part, it would be on extracting the weight data, computing my weight goals, and creating a plot of both the data and the goals with Matplotlib. And if I were to write a fourth part, it would be on writing the HTML, CoffeeScript, and CSS to display everything as an Übersicht widget, which was by far the most painful part of the whole process. But I’m a professor, so I may leave those tasks as an exercise for the reader.

# Bits and Pieces

• Blogging 101 wasn’t for me. I sortof knew that it was for new bloggers, but I thought that I could make it work. I couldn’t or didn’t. The assignments didn’t appeal to me. Besides, November (and the first half of December) is a busy time in academia. So, it didn’t really happen. So it goes.

• But, I’ve given up social media for Advent. So, maybe while I’m absent from Twitter I’ll blog more. Maybe. Do people give things up for Advent? Not that I know of. But I have to take periodic breaks from social media or it starts to make me crazy. Advent and Lent seemed like one way to do it; both are seasons of waiting and preparation.

• If you miss me that much, though, I’ve decided that Instagram doesn’t count. (Mostly because I follow only a few people on Instagram and the volume of posts is light.) Will I blog more often or Instagram more often? Yes. Maybe.

• Every December for the last several years I’ve hit Instapaper zero. It happened by accident the first time or two—Winter Break is conducive to reading all the things—but for the last couple of years it has been intentional. And as I started making my way through my queue this year, I finally read Ta-Nehisi Coates’s piece on reparations from last June. Wow. There is so much about the history of racism in the United States that I was never taught. The piece was widely lauded, so I’m late to the party, but it’s probably the best thing I’ve read this year.

• On a lighter note, I’m a long time fan of Merlin Mann, and hence a long time listener to his podcast with Dan Benjamin, Back to Work. If I may be so bold, it is a lot of nonsense punctuated by moments of brilliance. And in the most recent episode, a little after 1:27, Merlin says, “The problem is you still have hope about email. That is hell. Hell is believing that there is hope in email. The only thing that can actually help you is to realize that it is completely hopeless.” My name is Allen, and I have an email problem.

• And today, after finishing up that episode, I was listening to this fantastic interview of Ira Glass by Alec Baldwin. So, so good. I don’t listen to Alec Baldwin’s show very often, but maybe I should. (I wrote a whole post about his interview of Billy Joel back in 2012.)


text:
# Zero Evolution12

Posted by Loren Shure,

Today's post will take us on an historical tour of the function zeros, and pertains, in various ways, to the related functions ones, eye, false, true, rand, randn, and complex.

You might think "How can a function like zeros need to evolve at all?" - but you will see some of the MATLAB changes that lead to the evolution.

### Contents

#### Original Behavior of zeros

In Cleve's original Fortran MATLAB, there was no zeros function. However, the function ones had the following help entry:

  ONES  All ones. ONES(N) is an N by N matrix of ones.  ONES(M,N)
is an M by N matrix of ones.  ONES(A) is the same size as
A and all ones.

When I first started using MATLAB in 1987(version 3.05a), the behavior for the function zeros matched this ones design, with the obvious difference being the matrix had values of 0 instead of 1 for all of its entries.

Notice what this design means. I always get an array the same size as A unless A is a scalar. And then we get something with size A by A. This means that if we want to ever get a scalar value for zeros, we must constantly program defensively and ask if A is a scalar, and then create zeros(1), else create zeros(A). Constantly. This was a source of both confusion and bugs in code where people didn't recognize the discontinuity in behavior when they were programming.

#### Ambiguity Led to Change around MATLAB Version 4

To alleviate this nuisance, in MATLAB Version 4, we no longer used the syntax zeros(A) but instead expected the inputs to be non-negative integers. MATLAB still only had 2-dimensional double arrays (with an attribute describing when to interpret the values as characters). With this design, there was no more need for such defensive programming.

#### Extension to N Dimensions in MATLAB Version 5

Once we introduced higher dimensional arrays in MATLAB (version 5), we changed the input syntax for zeros to allow 2 scalar non-negative integer inputs OR a vector of such values for N-Dimensional arrays. While it was nice to be able to create higher dimensional arrays this way, it was tricky to create them for non-double datatypes, another new feature in that release of MATLAB.

The most common way to generate an array of zeros of another type was to first create the correctly sized double array and then convert it to, let's say uint8. This was inefficient, first creating an array using 8 times more memory than necessary, and then converting it to the uint8 array, with its smaller datatype.

Of course there were ways around this too. You could create a scalar zero of the right datatype, e.g., and then set that to be the final element in a newly created array, like this for your 4-D array:

     B(m,n,p,q) = uint8(zeros(1));

Effective, but perhaps a bit cryptic.

#### Ability to Create zeros of Particular Class in MATLAB R14 (Version 7)

In MATLAB 7, we introduced another new syntax for zeros and companions, allowing you to specify the datatype of the output, provided that it was one of the built-in datatypes. Here's a snippet of the relevant information.

   X = zeros(..., classname) classname restricted to built in types:
'double', 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32',
'uint32', 'int64', or 'uint64'

This was definitely more memory efficient than creating an array of doubles and then coverting them. And less mysterious than setting the end element of a new array to a zero of the right type.

There was a remaining limitation however. It was still cumbersome to create an array of zeros of a user-defined datatype, including classes that shipped as part of our products.

#### Latest Syntax Updates in MATLAB R2013a (Version 8.1)

In addition to creating zero-arrays in all the ways possible since MATLAB Version 4, you can now create an array of zeros to be like another array, saving you the hassle of finding out that array's size and type and passing that information along. And it also allows you to create arrays of any datatype, built-in or user-defined. How do you do this? Here's one of the possible syntax choices.

    X = zeros(sz1,...,szN,'like',p)

#### Have You Been Able to Capitalize on These Changes?

Have you been able to take advantage of some of these changes over time? Did you know about them? I'd love to hear your thoughts here.

Get the MATLAB code

Published with MATLAB® R2013a

### Note

adam replied on : 1 of 12

Interesting to see the evolution. I use these type of functions quite a lot for pre-sizing arrays, but even after 8 years using Matlab I have to say the syntax of e.g. zeros(N) is still not intuitive to me. I keep making the mistake of using it when I want a vector of N zeros (where I don’t really care if it is a row or a column) before realising I have a square matrix instead!

Andrew Newell replied on : 2 of 12

If I want to have an array of zeros the same size as an array A, I just use B = 0*A. That also ensures that B is in the same class as A. Is there any reason to use zeros instead?

Drazick replied on : 3 of 12

Thank you for the historical review.

I still wonder why don’t you allow creating a matrix with no initialization which should be much faster.
Namely just the memory allocation, with no need to run through it to set a value.

Thanks.

Sky replied on : 4 of 12

I often have bits of code that look something like x = zeros(size(A)); or x = ones(size(A));

So, the behavior that would be most convenient for me is for x = zeros(‘like’,A); to be the same as x = zeros(size(A),’like’,A); instead of returning a scalar. What’s the reasoning behind returning a scalar? Typing x = zeros(1,’like’,A); would still be possible if a scalar is desired.

Loren Shure replied on : 5 of 12

Thanks for the replies.

Adam, Yes – zeros(n) has indeed caused confusion.

Andrew, Not sure if the performance will differ at all – it might especially if you have non-finite elements in your matrix. And then you might not get all 0s as well.

Drazick, MATLAB doesn’t have arrays with uninitialized values in its definition, at least for now. We’d need to add some library functions, at least one, to ask if the array was uninitialized. That might clutter most input syntax checking – being sure inputs have values.

Sky, I think we were trying to unify the older and newer behaviors with similar (parallel) syntaxes.

–Loren

Andrew replied on : 6 of 12

The pattern B(m,n,p,q) = uint8(0); (or even just B(m,n,p,q) = 0) still works and can be used as a “trick” for fast zero allocation of large arrays. It only explicitly assigns the last element of the array in order to set the size. This only works for zero/false values of a datatype. The one major caveat is that, unlike the zeros function that writes (or overwrites) a zero in each element, this can only be used safely if the matrix to allocated does not already exist.

Andrew replied on : 7 of 12

You mentioned complex, rand, and randn as being “related functions. However, these don’t seem to have received the ‘like’ syntax extension in R2013a. It would be nice if all of the RandStream class functions supported this for the appropriate types. I often try to check for single/double precision and use the appropriate allocation as, from experience, there’s nothing worse than a function needlessly ignoring a datatype.

Loren Shure replied on : 8 of 12

Andrew-

I said rand and randn since they use the single input syntax to create a 2-D output. They do not have the like syntax since they only generate singles or doubles and not the long list of possible datatypes. I mentioned complex since that is one of the attributes that the ‘like’ syntax will inspect.

You are right that the trick of initializing a matrix to zero values by setting its end element to a zero of the right type will only work correctly for a non-existing array.

–Loren

Loren Shure replied on : 9 of 12

NOTE: I just got a note from Yair Altman suggesting I mention another change to zeros, et al. that is not visible in the syntax but is a documented change – that is, we made these functions multi-threaded for large enough outputs in MATLAB R2007A – thereby speeding them up.

Thanks, Yair!
–Loren

Yair Altman replied on : 10 of 12

I just posted a detailed analysis of the performance of zeros() vs. ones() for different allocation sizes, showing a surprising effect of multi-threading on zeros:
http://UndocumentedMatlab.com/blog/allocation-performance-take-2/

There is definitely more to zeros (and ones) than one would naively assume. This functions are deceivingly simple. We can only speculate how much more effort has been invested in more complex built-in functions, if such simple functions as zeros() and ones() have so much more internal complexity than meets the eye.

– Yair

Michelle Hirsch replied on : 11 of 12

Interesting assessment Yair, but it turns out that the reasons for the behavior changes aren’t what you thought. The performance change for zeros in R2008b resulted from a change in the underlying MATLAB memory management architecture at that time.

Eric replied on : 12 of 12

Michelle, do you have any thoughts on if there’s a way to improve the performance of ‘ones’ for >100k elements, as shown by Yair?


text: Consider the behavior of a quadratic function as it approaches its vertex. This makes sense, if you think about it. If a > 0 in ax2 + bx + c = 0, then the parabola is opening upwards and if a < 0, then the parabola is opening downwards. CTRL + SPACE for auto-complete. In this tutorial, we are going to learn how to find the vertex, focus, and directrix of a parabola. Step 1. is a mirror-symmetrical, plane curve and typically U-shaped. If the quadratic is written in the form y = a(x – h)2 + k, then the vertex is the point (h, k). I simply need to calculate what the vertex of the parabola is that goes through these three points. Well, it is quite easy. Whew, that was a lot of shuffling numbers around! Find the simultaneous equations formed by the condition that the equation y = a x 2 + b x + c passes through the points (2, − 1 0), (0, 2) and (4, 1 4) and … Improve your math knowledge with free questions in "Find the vertex of a parabola" and thousands of other math skills. To find the vertex, I look at the coefficients a, b, and c. The formula for the vertex gives me: h = –b / 2a = –(1) / 2(3) = –1 / 6. + 5, the value of his 1, and k is 5. The vertex of a parabola is the highest or lowest point, also known as the maximum or minimum of a. Interactive simulation the most controversial math riddle ever! There are two ways to do it, and they're both easy. The "Ask A Scientist" website provides this answer: The general form of a parabola is given by the equation: A * x^2 + B * x + C = y where A, B, and C are arbitrary Real constants. Axis of Symmetry and Vertex of a Parabola. For the horizontal axis of symmetry, the equation will be y = 4b or y = -4b. How do you find the vertex on a graph? The squared part is always positive (for a right-side-up parabola… Step 2. It is a point (h, k) on the parabola. Each parabola has a vertical line or axis of symmetry that … If a is positive then the parabola opens upwards like a regular "U". You have three pairs of points that are (x,y) ordered pairs. There are chances of mistakes which can arise from taking the wrong sign of the ‘h.’ For example equation, y = (x – 7), + 8, we noticed that the ‘h’ is 7, but it is often mistaken that the x-coordinate of the vertex is -7. Keep Learning!! The vertex is the point (h, k) where the parabola crosses its axis of symmetry. If #a>0#, the vertex is the minimum point and the parabola opens upward.If #a<0#, the vertex is the maximum point and the parabola opens downward.. To find the vertex, you need to find the x- and y-coordinates. Vertex Form of Equation. Finding the focus of a parabola given its equation . One way to find the vertex of a parabola is to turn the standard form of the quadratic equation into vertex form by completing the square. Let L be a normal to the parabola y 2 = 4 x. -b/2a is x-coordinate and -v/2u is y-coordinate. Parabola finding vertex and X intercepts. The vertex of the parabola is at (0, 2). That given point is termed as “Focus” of the Parabola. Description: This lesson shows how to determine the coordinate points of the vertex of a parabola on a graph. Write CSS OR LESS and hit save. Improve your math knowledge with free questions in "Find the vertex of a parabola" and thousands of other math skills. the coordinates of the vertex, $$\begin{pmatrix}h,k\end{pmatrix}$$, and: ; the coordinates another point $$P$$ through which the parabola passes. Latest Trends of Information Technology in Education, 8 examples of technology that utilizes the Internet of Things, 6 Examples Of Smart Home Devices That Utilize IoT Technology, 6 Reasons Why Companies Need IoT solutions, How Digital Healthcare is revolutionized with IoT. Learn about the parts of a parabola. Fortunately, converting equations in the other direction (from vertex to standard form) is a lot simpler. Co-ordinates of Vertex of a parabola can be found form the coefficients a,b,c of the standard form of quadratic. Example 3 : Find the vertex of the parabola y = x ² + 4 x. Use the equation found in part (a) to find the depth of the cooker. The origin or fixed point of Parabola can be found by pairing the h value with the k value, to give the coordinate (h, k). The vertex is on the axis of symmetry, so its x -coordinate is − b 2a − b 2 a. Remember, at the y-intercept the value of $$x$$ is zero. If L passes through the point (9, 6), the L is given by. Given the two zeros at a and b, write the parabola's equation as f(x) = (x-a)(x-b) = x² - (a+b)x + ab .....[1] Given a parabola of the form f(x) = Ax²+Bx+C, the x-coordinate of Distance between vertex and focus = a. Determine the horizontal or vertical axis of symmetry. The vertex is in the 2nd quadrant. What Is Vertex Form? Example 1 : Find the equation of the parabola if the vertex is (4, 1) and the focus is (4, − 3) Solution : From the given information the parabola is symmetric about y -axis and open downward For FREE. MIT grad explains how to find the vertex of a parabola. The vertex form of a parabola's equation is generally expressed as: y = a(x-h) 2 +k (h,k) is the vertex as you can see in the picture below If a is positive then the parabola opens upwards like a regular "U". Step 4. Steps to Find Vertex Focus and Directrix Of The Parabola. … As we know, the Parabola equation and vertex (h,k) are given to us. a = 1, b = 6 Vertex and X-Intercept of a Parabola Parabolas have a highest or a lowest point, known as their vertex, which represents its turning point on a graph. The vertex of a parabola is the point at the intersection of the parabola and its line of symmetry. See More. Notice that here we are working with a parabola with a vertical axis of symmetry, so the x-coordinate of the focus is the same as the x-coordinate of the vertex. In order to graph a parabola, we need to find several pieces of information, including the vertex. I'm going to assume the OP wanted the vertex and focus of the original tilted parabola since they already had rotated it to a standard form where those values are easier to find. View more. Standard Form:  y = ax2 + bx + c & x = uy2 + vy + w for y-axis and x-axis respectively. depending upon the orientation of the parabola. A parabola is the arc a ball makes when you throw it, or the cross-section of a satellite dish. To find the zeros, Vertex, Min and Max we first need to understand the basic's of a parabola. View solution. Try Our College Algebra Course. Hence, the axis of symmetry is going to be a vertical line. View solution. Find the vertex and graph the parabola. Directrix is always perpendicular to the axis of symmetry of a Parabola. Now play around with some measurements until you have another dot that is exactly the same distance from the focus and the straight line. + k, the negative in front of the parenthesis tells us that the parabola is pointed downward. Hi there, There are already few answers given to this question. + k, here (h,k) are the points on the x-axis and y-axis respectively. Recognizing a Parabola Formula If you see a quadratic equation in two variables, of the form  y = ax2 + bx + c , where a ≠ 0, then congratulations! Next, calculate the midway point, which will lie directly in between the two roots of the equation. To find the vertex of a quadratic equation, start by identifying the values of a, b, and c. Then, use the vertex formula to figure out the x-value of the vertex. For a parabola whose equation is given in standard form, the vertex will be the minimum (lowest point) of the graph if and the maximum (highest point) of the graph if. The Parabola is a set of all points in a plane which are equidistant from a given point and a given line. The standard form of a parabola equation is . – 2x), {Take half of the coefficient of the x-term inside the parentheses, square it and add both side}, While solving geometry, you might encounter problems such as. This is as vertical parabola. We tried to cover maximum basic things which one should know about parabola. … Example – Input : 5 3 2 Output : Vertex:(-0.3, 1.55) Focus: (-0.3, 1.6) Directrix: y=-198 Consult the … , It would be better if we understand the meaning of Parabola, basic things related to it and related terms. How do you find the vertex on a graph? This is NOT completing the square.


text:
# Peeter Joot's (OLD) Blog.

• ## Archives

 Adam C Scott on avoiding gdb signal noise… Ken on Scotiabank iTrade RESP …… Alan Ball on Oops. Fixing a drill hole in P… Peeter Joot's B… on Stokes theorem in Geometric… Exploring Stokes The… on Stokes theorem in Geometric…

• 287,841

## PHY452H1S Basic Statistical Mechanics. Lecture 10: Continuing review of thermodynamics. Taught by Prof. Arun Paramekanti

Posted by peeterjoot on February 14, 2013

[Click here for a PDF of this post with nicer formatting (especially if my latex to wordpress script has left FORMULA DOES NOT PARSE errors.)]

# Disclaimer

Peeter’s lecture notes from class. May not be entirely coherent.

# Continuing review of thermodynamics

We have energy conservation split into two types of energy

\begin{aligned}dE = \underbrace{d W }_{\text{Organized macroscopic variable X}}+ \underbrace{d Q}_{\text{disorganized}}\end{aligned} \hspace{\stretch{1}}(1.2.1)

In fig. 1 we plot changes that are adiabatic processes ($d Q = 0$) and heating and cooling processes (with $d W = 0$).

Given a dimensionality of $d_w + 1$, a cyclic change is that for which we have

\begin{aligned}\{ X_{\mathrm{initial}} \} \rightarrow \{ X_{\mathrm{final}} \} \end{aligned} \hspace{\stretch{1}}(1.0.2a)

\begin{aligned}E_{\mathrm{initial}} \rightarrow E_{\mathrm{final}}\end{aligned} \hspace{\stretch{1}}(1.0.2b)

\begin{aligned}\Delta W \ne 0\end{aligned} \hspace{\stretch{1}}(1.0.2c)

\begin{aligned}\Delta Q \ne 0\end{aligned} \hspace{\stretch{1}}(1.0.2d)

\begin{aligned}\Delta E = 0\end{aligned} \hspace{\stretch{1}}(1.0.2e)

Such a cyclic process could be represented as in fig. 2.

Fig2: Cyclic process

Here we’ve labeled the level curves with a parameter $\sigma$, as yet undefined. We call $\sigma$ the thermodynamic entropy, and say that

\begin{aligned}\left( {\sigma, \{x_i\}} \right),\end{aligned} \hspace{\stretch{1}}(1.0.3)

specifies the state of the system.

Example: Pushing a block against a surface with friction.

Equilibrium

Considering two systems in contact as in fig. 3.

Fig3: Two systems in contact

We require

• Mechanical equilibrium.

requires balance of the forces $f_i$

\begin{aligned}\frac{\partial {E}}{\partial {x_i}} = f_i,\end{aligned} \hspace{\stretch{1}}(1.0.4)

(Note the neglect of the sign here, the direction of the force isn’t really of interest).

and

\begin{aligned}\frac{\partial {E_1}}{\partial {x_i}} = \frac{\partial {E_2}}{\partial {x_i}} \end{aligned} \hspace{\stretch{1}}(1.0.5)

• Thermal stability

\begin{aligned}\frac{\partial {E_1}}{\partial {\sigma}} = \frac{\partial {E_2}}{\partial {\sigma}} \end{aligned} \hspace{\stretch{1}}(1.0.6)

We must have some quantity that characterizes the state of the system in a non-macroscopic fashion. The identity eq. 1.0.6 is a statement that we have equal temperatures.

We define temperature as

\begin{aligned}T \equiv \frac{\partial {E}}{\partial {\sigma}}.\end{aligned} \hspace{\stretch{1}}(1.0.7)

We could potentially define different sorts of temperature, for example, perhaps $T^3 \equiv {\partial {E}}/{\partial {\sigma}}$. Should we do this, we effectively also define $\sigma$ in a specific way. The definition eq. 1.0.7 effectively defines this non-macroscopic parameter $\sigma$ (the entropy) in the simplest possible way.

Cyclic state variable verses non-state variables

\begin{aligned}\{x_i\}, \sigma \rightarrow \text{state variable''}\end{aligned} \hspace{\stretch{1}}(1.0.8)

A non-cyclic process changes these, whereas a cyclic process takes $\sigma, \{x_i\}$ back to the initial values. This is characterized by

\begin{aligned}\oint d\sigma = 0\end{aligned} \hspace{\stretch{1}}(1.0.9a)

\begin{aligned}\oint dx_i = 0.\end{aligned} \hspace{\stretch{1}}(1.0.9b)

This doesn’t mean that the closed loop integral of other qualities, such as temperature are necessarily zero

\begin{aligned}\oint T d\sigma = \oint d Q \ne 0\end{aligned} \hspace{\stretch{1}}(1.0.10a)

\begin{aligned}\oint f_i dx_i = \oint d W \ne 0.\end{aligned} \hspace{\stretch{1}}(1.0.10b)

Note that the identification of $d Q = T d\sigma$ follows from our definition

\begin{aligned}\left( { \frac{\partial {E}}{\partial {\sigma}} } \right)_x = T\end{aligned} \hspace{\stretch{1}}(1.0.17)

so that with $d W = 0$ we have

\begin{aligned}dE = T d \sigma\end{aligned} \hspace{\stretch{1}}(1.0.17)

Graphically we have for a cyclic process fig. 4.

Fig4: Cyclic process

We have

\begin{aligned}d W_{\rightarrow} = -d W_{\leftarrow} \end{aligned} \hspace{\stretch{1}}(1.0.17)

\begin{aligned}d Q_{\rightarrow} = -d Q_{\leftarrow} \end{aligned} \hspace{\stretch{1}}(1.0.17)

so that

\begin{aligned}\Delta Q_{12}^{(\mathrm{A})} + \Delta Q_{21}^{(\mathrm{B})} \ne 0,\end{aligned} \hspace{\stretch{1}}(1.0.17)

or

\begin{aligned}\Delta Q_{12}^{(\mathrm{A})} \ne \Delta Q_{12}^{(\mathrm{B})}.\end{aligned} \hspace{\stretch{1}}(1.0.17)

Irreversible and reversible processes

Reversible means that an undoing of the macroscopic quantities brings us back to the initial state. A counter example is a block on a spring as illustrated in fig. 5.

Fig 5: Heat loss and irreversibility

In such a system the block will hit gas atoms as it moves. It’s hard to imagine that such gas particles will somehow spontaneously reorganize itself so that they return to their initial positions and velocities. This is the jist of the Second law of thermodynamics. Real processes introduce a degree of irreversibility with

\begin{aligned}\text{Energy}_1 \rightarrow \text{Energy}_2\end{aligned} \hspace{\stretch{1}}(1.0.17)

\begin{aligned}\text{Work} \rightarrow \text{Heat}\end{aligned} \hspace{\stretch{1}}(1.0.17)

but not all

\begin{aligned}\text{Heat} \rightarrow \text{Work}.\end{aligned} \hspace{\stretch{1}}(1.0.17)


text:
# Tag Info

7

CLS stands for classification and its there to represent sentence-level classification. In short in order to make pooling scheme of BERT work this tag was introduced. I suggest reading up on this blog where this is also covered in detail.

6

[CLS] stands for classification. It is added at the beginning because the training tasks here is sentence classification. And because they need an input that can represent the meaning of the entire sentence, they introduce a new tag. They can’t take any other word from the input sequence, because the output of that is the word representation. So they add a ...

5

Your model is overfitting. You should try standard methods people use to prevent overfitting: Larger dropout (up to 0.5), in low-resource setups word dropout (i.e., randomly masking input tokens) also sometimes help (0.1-0.3 might be reasonable values). If you have many input classes, label smoothing can help. You can try a smaller model dimension. If you ...

5

In order to get accurate results, you should not oversample the test set! Otherwise you are simply evaluating on synthetic samples that you yourself have created. The support on your classification report should mirror the imbalance in your dataset. From what I understand you have 3500 samples, then you did some oversampling (probably brought them to around ...

5

Let me first clarify the general principle of classification with text data. Note that I'm assuming that you're using a "traditional" method (like decision trees), as opposed to Deep Learning (DL) method. As you correctly understand, each individual text document (instance) has to be represented as a vector of features, each feature representing a ...

4

Scikit-learn has compose.ColumnTransformer which allows different columns or column subsets of the input to be transformed separately and the features generated by each transformer will be concatenated to form a single feature space. This is useful for heterogeneous or columnar data, to combine several feature extraction mechanisms or transformations into a ...

3

To build on the previous answer: In transfer learning, the goal is to use a pre-trained model and tweak the model to then specialise it to suit a certain task. So, what we do is, as SrJ has eluded to, keep the main model's architecture in tact. So this would be the 6 CNN layers (and possibly the three linear layers, if they were also involved in pre-training)...

3

The nDCG depends on the relevance of each document as you can see on the Wikipedia definition. I guess you could use 0 and 1 as relevance scores, but then all relevant documents would have the same score of 1, and then it wouldn't make much sense to apply the nDCG penalty discounts. A similar measure often used with binary relevance scores is the mean ...

2

In general this doesn't work well, since it's almost unavoidable that the classifier won't be able to distinguish all the categories from each other. I'd suggest tying to reduce the number of categories (for instance discard the least common ones). In any case I'm not aware of any specific model to deal with a high number of classes, it's regular text ...

2

Well, obviously the use cases depends on the industry. Also, I am assuming you are thinking of use cases that are somehow useful. But let's think of some examples: I once worked with a book distributor that tagged each book they sold with keywords (Fantasy, Horror, etc). You can automate the tagging process if you have a sufficiently large dataset of ...

2

First of all, you use two terms Clustering and Classification interchangably and I would like to draw your attention to this. Your problem is purely Clustering. Secondly, you asked for testing accuracy. As your problem is pure Clustering, there is no evaluation for that. The last but not least is the problem of "Short Text Understanding". In short ...

2

What you need is simply a language model. This is a very common task so you should be able to find code and data easily. This question gives some pointers for Python (be careful, the accepted answer is incorrect according to the two other answers). Applying the language model to a sentence gives you a probability (or a perplexity score, which works the ...

2

This is called an open-class text classification problem, it's used in particular for some author identification problems. I don't have any recent pointers but from a quick search I found this article: https://www.aclweb.org/anthology/N16-1061.pdf In the field of author classification there is a similar problem called author verification, which can be ...

2

There seems to be a mistake in your method: I read about the use of downsampling and upsampling, so I applied them before training and testing the dataset. It's incorrect to change the distribution of the test set. When resampling, the resampling should be applied only on the training set. The goal is to force the model to take into account the two classes,...

2

You could train a character-level language model, e.g. an LSTM, on the real short texts, and use the perplexity as the signal to know whether a piece of text is real or not. In order to find an appropriate perplexity threshold, you can have a look at the distribution of perplexities over a validation holdout dataset. UPDATE: There are multiple ...

2

What you are proposing is a heuristic method, because you define the rules manually in advance. From a Machine Learning (ML) point of view the "training" is the part where you observe some data and decide which rules to apply, and the "testing" is when you run a program which applies these rules to obtain a predicted label. As you ...

2

First, congratulations for thinking to do a qualitative analysis of the results :) I know it should be obvious, but so many people just assume that the system works and don't bother checking their output. Now, strictly speaking what you're seeing is not a bug. These are errors made by a statistical system. A statistical system is not meant to get everything ...

2

In order to detect overfitting you need to separate your data in a training set - that you use to estimate the parameters of you model - and a test set - where you evaluate your model keeping the parameters fixed, this is usually called cross-validation. I understand from your results that you are not doing such separation of data so, you're not able to ...

2

Very interesting question. Easy, but probably lazy answer When using pre-trained models, it is always advised to feed it data similar to what it was trained with. Basically, if it matters, don't remove them, and if it doesn't matter, it doesn't hurt to keep them in. Obviously, if you can, try with or without stopwords, and see what works best for your ...

1

In neural networks meant for classification, you need a linear layer before the softmax to project the internal representation, which has some dimensionality $d_i$, to the output space, which has dimensionality $d_o$ equal the number of choices (5 in your case). So you either place a Dense(5) layer after the BiLSTM or you take the output of the BiLSTM "...

1

Accuracy is not the best measure for imbalanced data. Prefer precision and recall. Do undersampling/oversampling to get equal samples for each class and try XGBoost. Or else you can use SVC with class weights, give lower class weight to classes with more samples and vice versa.

1

Try to use the concatenated vector of average word2vec and max pool word2vec. You may get better results than tfidf and PCA. Remove the words like stop words. Also, try with lemmatized words. If you okay with computational time, go for transformer-based transfer learning embedding like BERT, ALBERT etc...

1

You have tagged this as text-classification, but describe your efforts to cluster the documents. My answer is based on the assumption that you don't know how the documents are grouped for you to classify new documents. The first step would be to determine the clusters/classes/groups and then classify new documents. There are at least 2 things one can think ...

1

It can be improved in many ways. However, the best start would be if you elaborate on your pipeline together with choosen parameters, as well as your data.

1

There are different ways to address the task that you describe: If the goal is simply to predict the author among a set of predefined authors, then this is not a clustering task (unsupervised) but a classification task (supervised). This implies that you would split the data between training and test set (or use cross-validation), train a model using the ...

1

Okay so keeping it very short and precisely in context of your question- Accuracy tells us, out of all the documents how many are classified correctly. Precision tells us out of all documents which are predicted in a category, how often its correct. Uni -gram- "nasa", "is" "space" , "agency" bi-gram- "nasa is", "space agency" Now lets go over the ...

1

For the most part, as you add more classes to your multiclass classification problem, it becomes more difficult to construct a model. All algorithms can run into trouble because there are fewer examples of each class to learn from. In case the 27 class target feature does not work well for you, you can decrease the number of classes even more. To do this: ...

1

I too am working on same problem, found these below links very useful in getting started on oversampling and under sampling- https://machinelearningmastery.com/random-oversampling-and-undersampling-for-imbalanced-classification/ https://en.wikipedia.org/wiki/Oversampling_and_undersampling_in_data_analysis

1

Regarding the first question, to the best of my knowledge, there is no SOTA approach. It depends on the task. For the second one, did you try to combine the numerical values with the TFIDF vectors, and then feed the final vector(s) to the classifier? Probably not. I usually try this way. But, if your vocabulary is too large, try to reduce it by considering ...

Only top voted, non community-wiki answers of a minimum length are eligible


text:
Free Version
Moderate

# Squaring a Binomial

ACTMAT-TJZOEE

Which of the following is equivalent to ${ (3p-4q) }^{ 2 }$?

A

$9{ p }^{ 2 }-16{ q }^{ 2 }$

B

$9{ p }^{ 2 }+16{ q }^{ 2 }$

C

$9{ p }^{ 2 }-12p{ q }+16{ q }^{ 2 }$

D

$9{ p }^{ 2 }-24pq+16{ q }^{ 2 }$

E

$9{ p }^{ 2 }+24pq+16{ q }^{ 2 }$


text:
# Do odd perfect numbers exist? New results on an old problem

Euclid, from Rafael’s “School of Athens”, Vatican Museum, Rome, photo courtesy Clay Mathematics Institute

#### Perfect numbers

A perfect number is a positive integer whose divisors (not including itself) add up to the integer. The smallest perfect number is $6$, since $6 = 1 + 2 + 3$. The next is $28 = 1 + 2 + 4 + 7 + 14$, followed by $496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248$ and $8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64$ $+ 127 + 254 + 508 + 1016 + 2032 + 4064$.

The notion of a perfect number is at least 2300 years old. In approximately 300 BCE, Euclid showed (using modern notation) that if $2^p – 1$ is a prime number (which implies, by the way, that $p$ itself is prime), then $2^{p-1} (2^p – 1)$ is a perfect number. For example, when $p = 5$, we have $2^5 – 1 = 31$, which is prime, and $2^4 (2^5 – 1) = 16 \cdot 31 = 496$ is perfect. In approximately 100 CE, the Greek mathematician Nicomachus noted that 8128 is a perfect number, and stated, without proof, that every perfect number is of the form $2^{n-1} (2^n – 1)$ (also omitting the clear condition that $n$ must be prime). In the first century CE, the Hebrew theologian Philo of Alexandria asserted that Earth was created in $6$ days and the moon orbits Earth in $28$ days, since $6$ and $28$ are perfect. In the early fifth century CE, the Christian theologian Augustine of Hippo repeated the assertion that God created Earth in $6$ days because $6$ is the smallest perfect number.

In the 12th century, the Egyptian mathematician Ismail ibn Fallūs calculated the 5th, 6th and 7th perfect numbers $(33550336, 8589869056$ and $137438691328$), plus some additional ones that are incorrect. The first known mention of the 5th perfect number in European history is in a manuscript written by an unknown writer between 1456 and 1461. The 6th and 7th were identified by the Italian mathematician Pietro Cataldi in 1588; he also proved that every perfect number obtained using Euclid’s formula ends in $6$ or $8$.

In the 18th century, Euler proved that Euclid’s formula $2^{p-1} (2^p – 1)$, for prime $2^p – 1$, yields all even perfect numbers. He also introduced the $\sigma (N)$ notation (also known as the divisor function) for the sum of the divisors of $N$, including $N$ itself. Thus we may write his result as follows: If $N$ is a positive even integer, then $\sigma(N) = 2N$ (i.e., $N$ is perfect) if and only if $N = 2^{p-1} (2^p – 1)$ for some prime of the form $2^p – 1$. This result is now known as the Euclid-Euler theorem. For additional history and background on perfect numbers, see this Wikipedia article.

#### Odd perfect numbers

The Euclid-Euler theorem pretty well wrapped up the case for even perfect numbers. But what about odd perfect numbers (OPNs)? Do any such integers exist? This question has remained stubbornly unanswered for centuries. Numerous restrictions are known on the properties of any possible OPN, with more restrictions being proved with every passing year. Present-day mathematicians echo the sentiment of James Sylvester, who wrote

a prolonged meditation on the subject has satisfied me that the existence of any one such [odd perfect number] — its escape, so to say, from the complex web of conditions which hem it in on all sides — would be little short of a miracle.

Here are some of the known restrictions on any possible OPN $N$, listed in roughly chronological order:

1. $N$ must not be divisible by 105 (Sylvester, 1888).
2. If $N$ is not divisible by $3, 5$ or $7$, it must have at least $27$ prime factors (Norton, 1960).
3. $N$ must have at least seven distinct prime factors (Pomerance, 1974).
4. $N$ must have at least $75$ prime factors and at least $9$ distinct prime factors (Hare, 2005 and Nielsen, 2006). This was later extended to $101$ prime factors (Ochem and Rao, 2012).
5. $N$ must be congruent to either $1 (\bmod 12)$ or $117 (\bmod 468)$ or $81 (\bmod 324)$ (Roberts, 2008).
6. The largest prime factor $p$ of $N$ must satisfy $p > 10^8$ (Goto and Ohno, 2008).
7. $N \gt 10^{1500}$ (Ochem and Rao, 2012).

(See this MathWorld article for references to these and other items.)

#### Spoof odd perfect numbers

Recently mathematicians have taken a slightly different tack on the problem — searching for spoof odd perfect numbers, namely integers that resemble odd perfect numbers but don’t quite meet all the requirements. There is historical precedent here: in 1638 Rene Descartes observed that $198585576189 = 3^2 \cdot 7^2 \cdot 11^2 \cdot 13^2 \cdot 22021$ almost satisfies Euclid’s formula.

To see this, first note two properties of the sigma function: (a) $\sigma(pq) = \sigma(p)\sigma(q)$, if $p$ and $q$ are relatively prime; and (b) $\sigma(p^m) = 1 + p + p^2 + \cdots p^m$ if $p$ is prime and $m \gt 0$. With regards to Descartes’ number, one might be tempted to write $$\hat{\sigma}(198585576189) = \sigma(3^2) \sigma(7^2) \sigma(11^2) \sigma(13^2) \sigma(22021)$$ $$= (1 + 3 + 3^2)(1 + 7 + 7^2) (1 + 11 + 11^2) (1 + 13 + 13^2) (1 + 22021) = 397171152378 = 2 \cdot 198585576189.$$ However, this calculation is incorrect, since $22021$ is not prime; in fact $\sigma(22021) = 23622$, not $22022$ as in the above. Thus the above calculation, which appears at first glance to certify that $198585576189$ is an OPN, is invalid. The correct sigma value for Descartes’ number is $\sigma(198585576189) = 426027470778$.

In 1999, John Voight of Dartmouth University discovered a different variety of spoof odd perfect number: $−22017975903 = 3^4 \cdot 7^2 \cdot 11^2 \cdot 19^2 \cdot (−127)$. Here again one might be tempted to write $$\hat{\sigma}(−22017975903) = \sigma(3^4) \sigma(7^2) \sigma(11^2) \sigma(19^2) \sigma(-127)$$ $$= (1 + 3 + 3^2 + 3^3 + 3^4) (1 + 7 + 7^2) (1 + 11 + 11^2) (1 + 19 + 19^2) (1 + (-127)) = -44035915806 = 2 \cdot (−22017975903).$$ But again, this calculation is invalid, in this case because the integer in question is negative, and $\sigma(-127) = -128$ not $-126$ as used above. But in both cases, these “spoofs” are definitely interesting, and possibly might inspire a line of attack to certify that OPNs simply cannot exist.

#### Computer searches

The most recent development here is due to a team led by Pace Nielsen and Paul Jenkins of Brigham Young University, subsequently joined by Michael Griffin and Nick Andersen, who embarked on a computer search for additional spoof odd perfect numbers. After roughly 60 CPU-years of computing, the team found 21 spoofs with six or fewer prime bases. Here the team relaxed the criteria in several ways, allowing non-prime bases (as with Descartes), negative bases (as with Voight), and also spoofs whose bases share the same prime factors (by the rules, a given prime may appear only once in the factorization list).

Why the interest in spoofs? According to Nielsen,

Any behavior of the larger set has to hold for the smaller subset. … So if we find any behaviors of spoofs that do not apply to the more restricted class, we can automatically rule out the possibility of an OPN.

So far, they have discovered some interesting facts about the spoofs, but none of these properties would preclude the existence of real odd perfect numbers. For example, the team has found that every one of the 21 spoofs that they have uncovered, with the exception of Descartes’ example, has at least one negative base. If this numerical observation could be proven, then this would prove that no odd perfect numbers exist, since by definition the bases of odd perfect numbers must be both positive and prime numbers.

So the search continues. Will this approach work? Voight, for instance, is not sure that even with the BYU team’s result that we are close to a final attack on the problem. Paul Pollack of the University of Georgia adds,

It would be great if we could stare at the list of spoofs and see some property and somehow prove there are no OPNs with that property. That would be a beautiful dream if it works, but it seems too good to be true.

For additional details and background, see this Quanta article by Steve Nadis, from which some of the above information was taken.