import numpy as np
# Define a random list including 20 random integers between [1,56)
random_list = np.random.randint(1,56,20)
#-----------------------------------------------
#Assign elements bigger than 28 in random_list to new_list
new_list = [i for i in random_list if i>28 ]
print "Random Original List:", random_list
print "New list with the condition:", new_list
#------------------------------------------------
# Create a binary list including 1 or 0 according to the upper condition of elements in the random list.
new_list_binary = [1 if i>28 else 0 for i in random_list]
print "New binary list with the condition:", new_list_binary
#------------------------------------------------
Random Original List: [22 45 27 33 54 11 30 53 12 10 50 31 11 31 38 18 1 39 55 12] New list with the condition: [45, 33, 54, 30, 53, 50, 31, 31, 38, 39, 55] New binary list with the condition: [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0]
#Creating a list including list elements within a changing k range;
#------------------------------------------------
# List Comprehension Version
k= 9
new_list_comp = [[i for i in range(n)] for n in range (1,k+2)]
print "Creating a list with list comprehension, range: 9" ,new_list_comp
print ""
#------------------------------------------------
# Loop Version
new_list_loop = []
k=11
for n in range (1,k+2):
temp_list = []
for i in range(n):
temp_list.append(i)
new_list_loop.append(temp_list)
print "Creating a list with loop, range: 11", new_list_loop
#------------------------------------------------
Creating a list with list comprehension, range: 9 [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] Creating a list with loop, range: 11 [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]
# Using different new features of numpy
import numpy as np
# Creating a 5x6 (rowxcolumn) array including random integers between [1,26)
new_matrix = np.random.randint(1,26,size=(5,6))
print new_matrix
print ""
print "Get the element at row 3 and column 4:", new_matrix[3,4]
print "Get the elements at row 1,2,3 and column 4:", new_matrix[1:4,4]
print "Get the elements at row 2 and column 3:4", new_matrix[2,3:5]
print ""
print "Max is ", new_matrix.max()
print "Min is ", new_matrix.min()
print "Mean is ", new_matrix.mean()
print ""
print "Find row based max:", new_matrix.max(axis=1)
print "Find column based max:", new_matrix.max(axis=0)
[[19 22 2 16 12 24] [15 25 5 13 2 14] [25 14 21 19 11 14] [ 5 15 4 20 13 3] [ 4 3 21 13 21 14]] Get the element at row 3 and column 4: 13 Get the elements at row 1,2,3 and column 4: [ 2 11 13] Get the elements at row 2 and column 3:4 [19 11] Max is 25 Min is 2 Mean is 13.6333333333 Find row based max: [24 25 25 20 21] Find column based max: [25 25 21 20 21 24]
#Drawing y=sin(x) and y= 3x^2+10
#When trying this example for canopy delete the line below
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 30) #array of 30 points from 0 to 10
y_sin = np.sin(x)
y_curve=3*(x**2)+10
ax1 = plt.subplot(211)
plt.plot(x, y_curve, 'ro-', label='Curve : y=3x^2+10')
plt.legend(loc = 'lower right')
plt.xlabel("X axis")
plt.ylabel("Y axis")
ax2= plt.subplot(212)
plt.plot(x, y_sin, 'b-', label='A sine wave')
plt.legend(loc = 'lower right')
plt.xlabel("X axis")
plt.ylabel("Y axis")
#When trying to show this example for canopy add the line below
#plt.show()
<matplotlib.text.Text at 0xefc3fd0>
#Drawing a histogram plot of ages distribution of a list containing 1000 people’s ages.
#When trying this example for canopy delete the line below
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
#Loop ----------------------------------------
ages = []
for i in range(1000):
ages.append(np.random.randint(10,61))
histogram = plt.hist(ages, bins=10)
#---------------------------------------------
#List Comprehension---------------------------
ages = [np.random.randint(10,61) for i in range(1000)]
histogram = plt.hist(ages, bins=10)
#----------------------------------------------
#Pure numpy features --------------------------
ages = np.random.randint(10,61,1000)
histogram = plt.hist(ages, bins=10)
#----------------------------------------------
#When trying to show this example for canopy add the line below
#plt.show()