1 + 2 * 3
7
2**3
8
x = "Machine" + "Learning"
print(x)
MachineLearning
x.upper()
'MACHINELEARNING'
x.lower()
'machinelearning'
len(x)
15
a = 2 + 3
b = a * 4
c = b - 7
L = [a,b,c]
print(L)
[5, 20, 13]
M = [10, 2, "a", 3.5]
N = L + M
print(N)
[5, 20, 13, 10, 2, 'a', 3.5]
len(N)
7
N[2]
13
N[:3]
[5, 20, 13]
N[3:]
[10, 2, 'a', 3.5]
N[3:6]
[10, 2, 'a']
# This is what a comment looks like
# Dictionaries contain name-value pairs
studentIds = {'knuth': 42.0, 'turing': 56.0, 'nash': 92.0 }
studentIds['turing']
56.0
studentIds['ada'] = 97.0
studentIds
{'ada': 97.0, 'knuth': 42.0, 'nash': 92.0, 'turing': 56.0}
del studentIds['knuth']
studentIds
{'ada': 97.0, 'nash': 92.0, 'turing': 56.0}
studentIds['knuth'] = [42.0,'forty-two']
studentIds
{'ada': 97.0, 'knuth': [42.0, 'forty-two'], 'nash': 92.0, 'turing': 56.0}
studentIds.keys()
dict_keys(['turing', 'nash', 'ada', 'knuth'])
list(studentIds)
['turing', 'nash', 'ada', 'knuth']
list(studentIds.values())
[56.0, 92.0, 97.0, [42.0, 'forty-two']]
list(studentIds.items())
[('turing', 56.0), ('nash', 92.0), ('ada', 97.0), ('knuth', [42.0, 'forty-two'])]
list(studentIds)
len(studentIds)
4
fruits = ['apples','oranges','pears','bananas']
for fruit in fruits:
print (fruit + ' for sale')
apples for sale oranges for sale pears for sale bananas for sale
# Accessing dict element
fruitPrices = {'apples': 2.00, 'oranges': 1.50, 'pears': 1.75}
for fruit, price in fruitPrices.items():
if price < 2.00:
print ('%s cost %f a pound' % (fruit, price))
else:
print (fruit + ' are too expensive!')
pears cost 1.750000 a pound oranges cost 1.500000 a pound apples are too expensive!
# Example of python's list comprehension construction:
nums = [1,2,3,4,5,6]
plusOneNums = [x+1 for x in nums]
print("plusOneNums =", plusOneNums)
oddNums = [x for x in nums if x % 2 == 1]
print("oddNums = ", oddNums)
oddNumsPlusOne = [x+1 for x in nums if x % 2 ==1]
print("oddNumsPlusOn = ", oddNumsPlusOne)
plusOneNums = [2, 3, 4, 5, 6, 7] oddNums = [1, 3, 5] oddNumsPlusOn = [2, 4, 6]
import numpy as np
# Creating a 1-d array
A = np.array([1,2,3,4,5])
A
array([1, 2, 3, 4, 5])
# Creating a 1-d array from an existing Python list
L = [1, 2, 3, 4, 5, 6]
A = np.array(L)
A
array([1, 2, 3, 4, 5, 6])
A / 5
array([0.2, 0.4, 0.6, 0.8, 1. , 1.2])
np.array([1.0,2,3,4,5]) / 5
array([0.2, 0.4, 0.6, 0.8, 1. ])
M = np.array([2, 3, 4, 5, 6, 7])
P = L * M
P
array([ 2, 6, 12, 20, 30, 42])
P.sum()
112
P.mean()
18.666666666666668
# Dot product of two vectors
np.dot(L, M)
112
# Creating a 2-d array
A = np.array([[1,2,3],[4,5,6],[7,8,9],[8,7,6]])
A
array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [8, 7, 6]])
A.shape
(4, 3)
A[0,1]
2
A[2,1]
8
A[:,1]
array([2, 5, 8, 7])
A[0,:]
array([1, 2, 3])
A[1:,1:]
array([[5, 6], [8, 9], [7, 6]])
A[1:3,1:]
array([[5, 6], [8, 9]])
A[1:,0:2]
array([[4, 5], [7, 8], [8, 7]])
A[-1,:]
array([8, 7, 6])
# Transpose a 2-d array
print(A)
print()
print(A.T)
[[1 2 3] [4 5 6] [7 8 9] [8 7 6]] [[1 4 7 8] [2 5 8 7] [3 6 9 6]]
C = np.array([1,10,20])
print("A: \n", A)
print()
print("C: \n", C)
print()
print("A * C: \n", A * C)
print()
print("A dot C: \n", np.dot(A,C))
A: [[1 2 3] [4 5 6] [7 8 9] [8 7 6]] C: [ 1 10 20] A * C: [[ 1 20 60] [ 4 50 120] [ 7 80 180] [ 8 70 120]] A dot C: [ 81 174 267 198]
A = np.mat(A)
C = np.mat(C)
print(A * C)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-83-bc847f7489f1> in <module>() 2 C = np.mat(C) 3 ----> 4 print(A * C) C:\Anaconda\envs\py35\lib\site-packages\numpy\matrixlib\defmatrix.py in __mul__(self, other) 307 if isinstance(other, (N.ndarray, list, tuple)) : 308 # This promotes 1-D vectors to row vectors --> 309 return N.dot(self, asmatrix(other)) 310 if isscalar(other) or not hasattr(other, '__rmul__') : 311 return N.dot(self, other) ValueError: shapes (4,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
print("C Transpose: \n", C.T)
print()
print("A * C: \n",A * C.T)
C Transpose: [[ 1] [10] [20]] A * C: [[ 81] [174] [267] [198]]
# Using arrays to index into other arrays (e.g., Boolean masks)
A = np.array([1, 2, 3, 4, 5, 6])
A > 3
array([False, False, False, True, True, True])
A[A > 3]
array([4, 5, 6])
sum(A[A > 3])
15
Ind = (A > 1) & (A < 6)
print(Ind)
A[Ind]
[False True True True True False]
array([2, 3, 4, 5])