#!/usr/bin/env python # coding: utf-8 # In[7]: 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 #------------------------------------------------ # In[16]: #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 #------------------------------------------------ # In[17]: # 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) # In[29]: #Drawing y=sin(x) and y= 3x^2+10 #When trying this example for canopy delete the line below get_ipython().run_line_magic('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() # In[37]: #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 get_ipython().run_line_magic('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()