Outils pour utilisateurs

Outils du site


python:first_course_statistics

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
Prochaine révision Les deux révisions suivantes
python:first_course_statistics [2016/10/08 13:24]
Beretta, Anna Letizia
python:first_course_statistics [2016/10/17 07:52]
Francesco Beretta [Boxplot (p.9)]
Ligne 1: Ligne 1:
  
 +====== General instructions ======
  
 +Read following important documentation about:
 +  * pandas [[http://​pandas.pydata.org/​pandas-docs/​stable/​dsintro.html#​dataframe|dataframes]]
 +  * [[http://​matplotlib.org/​api/​pyplot_summary.html|matplotlib.pyplot]]
  
 +Save your scripts in a folder inside the data folder, calling the script folder '​my_scripts'​ or whaterver. If  '​my-scripts'​ is set as your [[python:​generic_features#​get_the_current_working_directory_address|current working directory]],​ then the data files are available under this address '​../​[data file]',​ for instantce: '​../​geyser1.TAB'​
 +\\
 ====== Eruptions of the "Old Faithful"​ geyser (p.5) ====== ====== Eruptions of the "Old Faithful"​ geyser (p.5) ======
  
 +\\
  
 ===== Histogram (p.5) ===== ===== Histogram (p.5) =====
  
-This is my first function.+<code python>​ 
 +import pandas as pd 
 +import matplotlib.pyplot as plt 
 +gys1 = pd.DataFrame(pd.read_csv('​../​geyser1.TAB',​ '​\t'​)) 
 +g_int = gys1['​Interval'​] 
 +ax = plt.gca() 
 +ax.hist(g_int,​ bins=20, color='​r'​) 
 +ax.set_xlabel('​Intereruption time'​) 
 +ax.set_ylabel('​Frequency'​) 
 +ax.set_title('​Histogram'​) 
 +plt.show()  
 +</​code>​ 
 + 
 + 
 +\\ 
 + 
 +===== Boxplot (p. 6) =====
  
 <code python> <code python>
-# fake code – to be deleted +import matplotlib.pyplot as plt 
-import ​csv+import ​pandas as pd 
 +gysr1_boxplot = pd.read_csv('​...\geyser1.TAB',​ '​\t'​) 
 +data_gysr1 = gysr1_boxplot['​Interval'​] 
 +plt.boxplot(data_gysr1) 
 +ax = plt.gca() 
 +ax.set_xlabel('​222 cases'​) 
 +ax.set_ylabel('​Interruption time ( minutes'​) 
 +ax.set_title('​Box and Whisker Plot'​) 
 +plt.show() 
 +</​code>​
  
-csvfile = open('​D:​\data-wrangling-master\data-wrangling-master\data\chp3\data-text.csv',​ '​rb'​) 
-reader = csv.reader(csvfile) 
  
-for row in reader: +\\ 
-    print(row)+ 
 +===== ScatterPlot (p. 7) ===== 
 + 
 +AB: Put face- and edgecolor to change both of them. You can also have two different colors ​for the in- and outside of each dot. 
 + 
 +<code python>​ 
 +import matplotlib.pyplot as plt 
 +import pandas as pd 
 +geysr1_scatterplot = pd.read_csv('​...\geyser1.TAB',​ '​\t'​) 
 +geysr1_data_Xax = geysr1_scatterplot['​Duration'​] 
 +geysr1_data_Yax = geysr1_scatterplot['​Interval'​] 
 +plt.scatter(geysr1_data_Xax,​ geysr1_data_Yax,​ facecolor='​y',​ edgecolor='​y'​) 
 +ax = plt.gca() 
 +ax.set_xlabel('​Eruption duration time (minutes)'​) 
 +ax.set_ylabel('​Interuption time (minutes)'​) 
 +ax.set_title('​Scatter Plot of INTERVAL vs DURATION'​) 
 +plt.show()
 </​code>​ </​code>​
  
 +===== Descriptive statistics =====
 +
 +Note: try different examples, ie. the whole population or only those where '​Duration'​ <= 3
 +
 +[[http://​pandas.pydata.org/​pandas-docs/​stable/​basics.html#​descriptive-statistics|doc]] – [[http://​www.marsja.se/​pandas-python-descriptive-statistics/​|example]]
 +
 +<code python>
 +import pandas as pd
 +gysr1 = pd.read_csv('​../​geyser1.tab',​ '​\t'​)
 +gysr1['​Duration'​][gysr1['​Duration'​] <= 3].describe()
 +</​code>​
 +
 +
 +===== Boxplot (p.9) =====
 +
 +Selecting rows in a dataframe: [[http://​pandas.pydata.org/​pandas-docs/​stable/​indexing.html#​the-where-method-and-masking|doc]] / [[http://​stackoverflow.com/​questions/​17071871/​select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas|example]]
 +
 +<code python>
 +import matplotlib.pyplot as plt
 +import pandas as pd
 +gysr1 = pd.read_csv('​../​geyser1.tab',​ '​\t'​)
 +gysr1_inf3 = gysr1.loc[gysr1['​Duration'​] <= 3]
 +gysr1_sup3 = gysr1.loc[gysr1['​Duration'​] > 3]
 +plt.boxplot([gysr1_inf3['​Interval'​],​gysr1_sup3['​Interval'​]],​ labels= ['​inf3','​sup3'​])
 +</​code>​
  
 ====== International adoption rates (p.13) ====== ====== International adoption rates (p.13) ======
  
python/first_course_statistics.txt · Dernière modification: 2017/09/26 08:54 par Francesco Beretta