Quick Intro to Python

Math and Modules

In the space below and use Python as a calculator.

In [27]:

Now let's try some more advanced funtions.

In [28]:

The standard Python distribution only comes with the bare-bone capabilities. Other functionality can be accessed through modules using the import command. To access a function using the following syntax.

import <module>


Here's an example.

In [31]:
import math



What if I don't know how to use a function, you can access the documentation.

? <module>.<function>

Let's look at the documentation of math.log10

In [30]:
? math.log10


Strings are denoted by ' or ". Set the variable my_name equal to your name full name.

In [7]:
my_name =

In [32]:
intro = 'Hello, my name is '

print(intro + my_name + '.')

Strings can all be indexed.

In [34]:
first_initial = 'My first initial is '
print(first_initial + my_name[0] + '.')

Try and pick out all of your initials.

In [33]:
initials = 'My initials are '

print(initials + my_name[0] + my_name[] + ".")

SciPy and NumPy

SciPy and NumPy are two highly optimized core packages used for scientific computing in the Python community.

  • NumPy - Numerical computation in Python
  • SciPy - Scientific computation in Python

Let's import numpy and make a numpy array.

In [44]:
import numpy as np

B = np.ones((3, 3))

Use the cell below to manipulate the array we just created.

In [ ]:

Let's do some simple matrix multiplication using np.dot.

$$ \mathbf{A} \overrightarrow{x} = \overrightarrow{y}$$

First checkout the documentation of np.dot.

In [ ]:
? np.dot

In [43]:
N = 
A = np.eye(N) * 2
x = np.arange(N)

print('A =')
print('x =')

print('y =')

Use the cell below to call another function from NumPy.

In [ ]:


Scikit-Learn, a.k.a. sklearn, is a scientific toolkit (there are many others) for machine learning and it built on SciPy and NumPy.

Below is an example from scikit-learn for linear regression.

This example also using the plotting library matplotlib to display the results.

In [37]:
%matplotlib inline

# Code source: Jaques Grobler
# License: BSD 3 clause

import matplotlib.pyplot as plt
from sklearn import datasets, linear_model

# Load the diabetes dataset
diabetes = datasets.load_diabetes()

# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis]
diabetes_X_temp = diabetes_X[:, :, 2]

# Split the data into training/testing sets
diabetes_X_train = diabetes_X_temp[:-20]
diabetes_X_test = diabetes_X_temp[-20:]

# Split the targets into training/testing sets
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]

# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
# regr.fit(diabetes_X_train, diabetes_y_train)

# Predict result
# y = regr.predict(diabetes_X_test)

# The coefficients
print('Coefficients: \n', regr.coef_)
# The mean square error
print("Residual sum of squares: %.2f"
      % np.mean((y - diabetes_y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(diabetes_X_test, diabetes_y_test))

# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')
plt.plot(diabetes_X_test, y, color='blue', linewidth=3)



In [ ]: