markdown
stringlengths
0
37k
code
stringlengths
1
33.3k
path
stringlengths
8
215
repo_name
stringlengths
6
77
license
stringclasses
15 values
Using SPP models (signatures from different sizes) For the SPP models, we can use images of any size as input, to obtain a feature vector of a fixed size. Note that in the paper we obtained better results by padding small images to a fixed canvas size, and processed larger images in their original size. More informatio...
from preprocess.normalize import remove_background # To illustrate that images from any size can be used, let's process the signatures just # by removing the background and inverting the image normalized_spp = 255 - remove_background(original) plt.imshow(normalized_spp) # Note that now we need to use lists instead...
interactive_example.ipynb
luizgh/sigver_wiwd
bsd-2-clause
if/else, for, while, pass, break, continue
p=[] for i in xrange(2,100): isprime=1 for j in p: if(i%j==0): isprime=0 break if isprime: p.append(i) print p for i in xrange(10): pass i=10 while i>0: i=i-1 print i x=['text',"str",''' Hello World\\n '''] print x
notebook/Somkiat's Basic Python.ipynb
wasit7/PythonDay
bsd-3-clause
List: access, update, del, len(), + , in, for, slicing, append(), insert(), pop(), remove()
x=['a','b','c'] #access print x[0] #update x[0]='d' print x print "size of x%d is"%len(x) y=['x','y','z'] z=x+y gamma=y+x print z print gamma print 'a' in x print y y.remove('y')# remove by vavlue print y print y y.pop(0)# remove by index print y y.insert(0,'x') y.insert(1,'y') print y x=[i*i for i in xrange(10)]...
notebook/Somkiat's Basic Python.ipynb
wasit7/PythonDay
bsd-3-clause
Dictionary: access, update, del, in
x={} x={'key':'value'} x['foo']='bar' x x['foo']='Hello' x x['m']=123 x['foo','key'] keys=['foo','key'] [x[k] for k in keys] print x del x print x
notebook/Somkiat's Basic Python.ipynb
wasit7/PythonDay
bsd-3-clause
Function: function definition, pass by reference, keyword argument, default argument, lambda a built-in immutable type: str, int, long, bool, float, tuple
def foo(x): x=x+1 y=2*x return y print foo(3) x=3 print foo(x) print x def bar(x=[]): x.append(7) print "in loop: {}".format(x) x=[1,2,3] print x bar(x) print x def func(x=0,y=0,z=0):#defualt input argument return x*100+y*10+z func(1,2) func(y=2,z=3,x=1)#keyword input argument f=func f(...
notebook/Somkiat's Basic Python.ipynb
wasit7/PythonDay
bsd-3-clause
Module: class, from, import, reload(), package and init
class Obj: def __init__(self, _x, _y): self.x = _x self.y = _y def update(self, _x, _y): self.x += _x self.y += _y def __str__(self): return "x:%d, y:%d"%(self.x,self.y) a=Obj(5,7)#call __init__ print a#call __str__ a.update(1,2)#call update print a ...
notebook/Somkiat's Basic Python.ipynb
wasit7/PythonDay
bsd-3-clause
The code is inside the box (known as a code cell), and the computer's response (called the output of the code) is shown below the box. As you can see, the computer printed the message that we wanted. Arithmetic We can also print the value of some arithmetic operation (such as addition, subtraction, multiplication, or ...
print(1 + 2)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
We can also do subtraction in python. The next code cell subtracts 5 from 9 and prints the result, which is 4.
print(9 - 5)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
You can actually do a lot of calculations with python! See the table below for some examples. <table style="width: 100%;"> <tbody> <tr><th><b>Operation</b></th><th><b>Symbol</b></th><th><b>Example</b></th></tr> <tr> <td>Addition</td> <td>+</td> <td>1 + 2 = 3</td> </tr> <tr> <td>Subtraction</td> <td>-</td> <td>5 - 4 = ...
print(((1 + 3) * (9 - 2) / 2) ** 2)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
In general, Python follows the PEMDAS rule when deciding the order of operations. Comments We use comments to annotate what code is doing. They help other people to understand your code, and they can also be helpful if you haven't looked at your own code in a while. So far, the code that we have written is very short...
# Multiply 3 by 2 print(3 * 2)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
To indicate to Python that a line is comment (and not Python code), you need to write a pound sign (#) as the very first character. Once Python sees the pound sign and recognizes that the line is a comment, it is completely ignored by the computer. This is important, because just like English or Hindi (or any other ...
Multiply 3 by 2
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
Variables So far, you have used code to make a calculation and print the result, and the result isn't saved anywhere. However, you can imagine that you might want to save the result to work with it later. For this, you'll need to use variables. Creating variables The next code cell creates a variable named test_var a...
# Create a variable called test_var and give it a value of 4+5 test_var = 4 + 5 # Print the value of test_var print(test_var)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
In general, to work with a variable, you need to begin by selecting the name you want to use. Variable names are ideally short and descriptive. They also need to satisfy several requirements: - They can't have spaces (e.g., test var is not allowed) - They can only include letters, numbers, and underscores (e.g., test...
# Set the value of a new variable to 3 my_var = 3 # Print the value assigned to my_var print(my_var) # Change the value of the variable to 100 my_var = 100 # Print the new value assigned to my_var print(my_var)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
Note that in general, whenever you define a variable in a code cell, all of the code cells that follow also have access to the variable. For instance, we use the next code cell to access the values of my_var (from the code cell above) and test_var (from earlier in this tutorial).
print(my_var) print(test_var)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
The next code cell tells Python to increase the current value of my_var by 3. To do this, we still need to use my_var = like before. And also just like before, the new value we want to assign to the variable is to the right of the = sign.
# Increase the value by 3 my_var = my_var + 3 # Print the value assigned to my_var print(my_var)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
Using multiple variables It's common for code to use multiple variables. This is especially useful when we have to do a long calculation with multiple inputs. In the next code cell, we calculate the number of seconds in four years. This calculation uses five inputs.
# Create variables num_years = 4 days_per_year = 365 hours_per_day = 24 mins_per_hour = 60 secs_per_min = 60 # Calculate number of seconds in four years total_secs = secs_per_min * mins_per_hour * hours_per_day * days_per_year * num_years print(total_secs)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
As calculated above, there are 126144000 seconds in four years. Note it is possible to do this calculation without variables as just 60 * 60 * 24 * 365 * 4, but it is much harder to check that the calculation without variables does not have some error, because it is not as readable. When we use variables (such as nu...
# Update to include leap years days_per_year = 365.25 # Calculate number of seconds in four years total_secs = secs_per_min * mins_per_hour * hours_per_day * days_per_year * num_years print(total_secs)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
Note: You might have noticed the .0 added at the end of the number, which might look unnecessary. This is caused by the fact that in the second calculation, we used a number with a fractional part (365.25), whereas the first calculation multipled just numbers with no fractional part. You'll learn more about this in L...
print(hours_per_dy)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
When you see NameError like this, it's an indication that you should check how you have spelled the variable that it references as "not defined". Then, to fix the error, you need only correct the spelling.
print(hours_per_day)
notebooks/intro_to_programming/raw/tut1.ipynb
Kaggle/learntools
apache-2.0
Now we have the weatherData object with its data variable containing four extra columns, i.e. Month, _LastYearsAvg, _LastYearsStd, and _ThisYear. The columns _LastYearsAvg and _LastYearsStd contain the average and standard deviation of all previous years of month Month respectively. Columns _ThisYear contains the avera...
weatherData.data.head()
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
First let's remove the early years in which we are not interested in. Note also that there might be months containing -9999. This can happen if they constitute the first months in which the measurement was available (and no last years are available). In addition create a new column containing the difference of the meas...
data = weatherData.data[ weatherData.data["Year"] >= 1980 ] data = data[ np.isclose(data["_LastYearsStd"], -9999.0) == False ] data["avg_diff"] = data["_LastYearsAvg"] - data["_ThisYear"]
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
In order get a feel for a good classification for extreme climate events we can look at the distribution of the difference between the average values and the distribution of the standard deviations. I will take the maximum temperature reading as an example.
data_tmax = data[ data["Element"] == "TMAX" ] hist_avg, bin_edges_avg = np.histogram( np.abs(np.abs(np.asarray( data_tmax["avg_diff"] ))), bins=100 ) hist_std, bin_edges_std = np.histogram( np.abs(np.asarray( data_tmax["_LastYearsStd"] )), bins=100 ) fig = plt.figure(figsize=(10, 8), dpi=200) ax = fig.add_subplot(111...
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
This already gives us an indication that there are events that are extreme in the sense that they deviate by more than 1-2 standard deviations from the average of past years. However this still does not give us a robust and good indicator of when to call a climate event extreme. A relative measure will be much more hel...
data["avg_diff_fold"] = np.abs(data["avg_diff"]) / data["_LastYearsStd"] data_tmax = data[ data["Element"] == "TMAX" ]
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
For plotting we will remove the few events that deviate extremely and would render the plotting impossible
tmpData = np.abs(np.asarray( data_tmax["avg_diff_fold"] )) tmpData = tmpData[ tmpData < np.percentile(tmpData, 99.9) ] hist_avg_fold, bin_edges_avg_fold = np.histogram(tmpData, bins=100, density=True)
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
Here I will take prior knowlede (I looked already at the plot and went back one step) and assume that the distribution will look like a normal distribution. To visually emphasize this point we can fit the distribution and add the fit to the plot.
mu, std = norm.fit(np.concatenate((-tmpData,tmpData), axis=1)) x = np.linspace(0, 5, 100) p = norm.pdf(x, mu, std) print("Fitted a normal distribution at %.1f with standard deviation %.2f" %(mu, std)) fig = plt.figure(figsize=(10, 8), dpi=200) ax = fig.add_subplot(111) ax.tick_params(axis='both', which='major', label...
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
From the plot we can see that there is no obvious cutoff point that we could choose so we will have to use common sense. I would argue that a good measure would be to declare the 25% highest values as extreme. This will give us a cuttoff point of:
cutoff = np.percentile(tmpData, 85) print("The cutoff point is set to %.2f" %cutoff)
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
What the plot above is not telling us is how the individual bins of the histogram are populated in time. By that I mean that each event in the histogram is linked to the year in which the measurement was taken. We can now ask the question if events that deviate far from the all time averages are more likely to have occ...
bin_years = list() for i in range(1,len(bin_edges_avg_fold)): start, end = bin_edges_avg_fold[i-1], bin_edges_avg_fold[i] tmp = data_tmax[ data_tmax["avg_diff_fold"] > start ] tmp = tmp[ tmp["avg_diff_fold"] < end ] bin_years.append(tmp["Year"]) avg_time = [ np.average(item) for item in bin_years ] a...
notebooks/Load climate data.ipynb
nberliner/delveData
gpl-2.0
B Write a function, variance, which computes the variance of a list of numbers. The function takes one argument: a list or 1D NumPy array of numbers. It returns one floating-point number: the variance of all the numbers. Recall the formula for variance: $$ variance = \frac{1}{N - 1} \sum_{i = 1}^{N} (x_i - \mu_x)^2 $$ ...
try: variance except: assert False else: assert True import numpy as np np.random.seed(5987968) x = np.random.random(8491) v = x.var(ddof = 1) np.testing.assert_allclose(v, variance(x)) np.random.seed(4159) y = np.random.random(25) w = y.var(ddof = 1) np.testing.assert_allclose(w, variance(y))
assignments/A7/A7_Q1.ipynb
eds-uga/csci1360-fa16
mit
C The lecture on statistics mentions latent variables, specifically how you cannot know what the underlying process is that's generating your data; all you have is the data, on which you have to impose certain assumptions in order to derive hypotheses about what generated the data in the first place. To illustrate this...
import numpy as np np.random.seed(5735636) sample1 = np.random.normal(loc = 10, scale = 5, size = 10) sample2 = np.random.normal(loc = 10, scale = 5, size = 1000) sample3 = np.random.normal(loc = 10, scale = 5, size = 1000000) ######################### # DON'T MODIFY ANYTHING # # ABOVE THIS BLOCK # #############...
assignments/A7/A7_Q1.ipynb
eds-uga/csci1360-fa16
mit
Here we look at some Chang'e 5 low data rate telemetry received with Allen Telescope array on 2021-01-23, during its transfer to the Sun-Earth L1 point. The recorded data corresponds to the frequency 8471.2 MHz, which corresponds to the lander. The frames are CCSDS concatenated frames with a frames size of 220 bytes.
def load_frames(path): frame_size = 220 frames = np.fromfile(path, dtype = 'uint8') frames = frames[:frames.size//frame_size*frame_size].reshape((-1, frame_size)) return frames frames = load_frames('ATA_2021-01-23/ce5_frames_1.u8')
CE5/CE-5 frame analysis ATA 2021-01-23.ipynb
daniestevez/jupyter_notebooks
gpl-3.0
AOS frames AOS frames come from spacecraft 91 and virtual channels 1 and 2. Other combinations are most likely to corruted frames despite the fact that the Reed-Solomon decoder was successful.
aos = [CE5_AOSFrame.parse(f) for f in frames] collections.Counter([a.primary_header.transfer_frame_version_number for a in aos]) collections.Counter([a.primary_header.spacecraft_id for a in aos if a.primary_header.transfer_frame_version_number == 1]) collections.Counter([a.primary_header.virtual_...
CE5/CE-5 frame analysis ATA 2021-01-23.ipynb
daniestevez/jupyter_notebooks
gpl-3.0
Virtual channel 1 The vast majority of frames belong to virtual channel 1, which seems to send real-time telemetry.
[a.primary_header for a in aos if a.primary_header.virtual_channel_id == 1][:10] vc1 = [a for a in aos if a.primary_header.virtual_channel_id == 1] fc = np.array([a.primary_header.virtual_channel_frame_count for a in vc1]) [a.insert_zone for a in aos[:10]] t_vc1 = np.datetime64('2012-08-01') + np.timedelta64(1, 's') ...
CE5/CE-5 frame analysis ATA 2021-01-23.ipynb
daniestevez/jupyter_notebooks
gpl-3.0
We need to sort the data, since the different files we've loaded up are not in chronological order.
vc1_packets = list(ccsds.extract_space_packets(vc1, 108, 1, get_timestamps = True)) vc1_sp_headers = [ccsds.SpacePacketPrimaryHeader.parse(p[0]) for p in vc1_packets]
CE5/CE-5 frame analysis ATA 2021-01-23.ipynb
daniestevez/jupyter_notebooks
gpl-3.0
There are space packets in may APIDs. The contents of each APID are shown belown in plot form, but it's not easy to guess what any of the values mean.
vc1_apids = collections.Counter([p.APID for p in vc1_sp_headers]) vc1_apids vc1_by_apid = {apid : [p for h,p in zip(vc1_sp_headers, vc1_packets) if h.APID == apid] for apid in vc1_apids} plot_apids(vc1_by_apid, 108, 1)
CE5/CE-5 frame analysis ATA 2021-01-23.ipynb
daniestevez/jupyter_notebooks
gpl-3.0
๋งŒ๋“ค์–ด์ง„ ndarray ๊ฐ์ฒด์˜ ํ‘œํ˜„์‹(representation)์„ ๋ณด๋ฉด ๋ฐ”๊นฅ์ชฝ์— array()๋ž€ ๊ฒƒ์ด ๋ถ™์–ด ์žˆ์„ ๋ฟ ๋ฆฌ์ŠคํŠธ์™€ ๋™์ผํ•œ ๊ตฌ์กฐ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ์‹ค์ œ๋กœ 0, 1, 2, 3 ์ด๋ผ๋Š” ์›์†Œ๊ฐ€ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งŒ๋“ ๋‹ค.
L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(L)) L
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
๊ทธ๋Ÿฌ๋‚˜ ndarray ํด๋ž˜์Šค ๊ฐ์ฒด a์™€ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค ๊ฐ์ฒด b๋Š” ๋งŽ์€ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. ์šฐ์„  ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค ๊ฐ์ฒด๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ linked list์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ๊ฐ๊ฐ์˜ ์›์†Œ๊ฐ€ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ndarray ํด๋ž˜์Šค ๊ฐ์ฒด๋Š” C์–ธ์–ด์˜ ํ–‰๋ ฌ์ฒ˜๋Ÿผ ์—ฐ์†์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๋ฐฐ์น˜๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์›์†Œ๊ฐ€ ๊ฐ™์€ ์ž๋ฃŒํ˜•์ด์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ œ์•ฝ์„ ๊ฐ€์ง€๋Š” ๋Œ€์‹  ๋‚ด๋ถ€์˜ ์›์†Œ์— ๋Œ€ํ•œ ์ ‘๊ทผ๊ณผ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰์ด ๋นจ๋ผ์ง„๋‹ค. ndarray ํด๋ž˜์Šค์˜ ๋˜ ๋‹ค๋ฅธ ํŠน์„ฑ์€ ํ–‰๋ ฌ์˜ ๊ฐ ์›์†Œ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฒกํ„ฐํ™” ์—ฐ์‚ฐ(vectorized operation)์„ ์ง€์›ํ•œ๋‹ค๋Š” ์ ...
a = np.arange(1000) #arange : ๊ทธ๋ƒฅ array range์ž„ array๋กœ ๋ฐ”๊ฟˆ %time a2 = a**2 a1 = np.arange(10) print(a1) print(2 * a1)
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
L = range(1000) %time L2 = [i**2 for i in L]
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
๊ฐ๊ฐ์˜ ์ฝ”๋“œ ์‹คํ–‰์‹œ์— IPython์˜ %time ๋งค์ง ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ ๊ฒฐ๊ณผ ndarray์˜ ์œ ๋‹ˆ๋ฒ„์„ค ์—ฐ์‚ฐ ์‹คํ–‰ ์†๋„๊ฐ€ ๋ฆฌ์ŠคํŠธ ๋ฐ˜๋ณต๋ฌธ ๋ณด๋‹ค ๋น ๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ndarray์˜ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•œ ๋ฒˆ์— ํ•˜๋Š” ๊ฒƒ๋„ ๋นจ๋ผ์ง„ ์ด์œ ์˜ ํ•˜๋‚˜์ด๊ณ  ์œ ๋‹ˆ๋ฒ„์„ค ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด NumPy ๋‚ด๋ถ€์ ์œผ๋กœ ๊ตฌํ˜„๋œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰ ์ž์ฒด๋„ ๋นจ๋ผ์ง„๋‹ค. ๋”ฐ๋ผ์„œ Python์˜ ์„ฑ๋Šฅ ๊ฐœ์„ ์„ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ์ง€์ผœ์•ผํ•˜๋Š” ์ฝ”๋”ฉ ๊ด€๋ก€ ์ค‘์˜ ํ•˜๋‚˜๊ฐ€ NumPy์˜ ndarray์˜ ๋ฒกํ„ฐํ™” ์—ฐ์‚ฐ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” Python ์ž์ฒด์˜ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด...
L = range(10) print(L) print(2 * L)
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
๋‹ค์ฐจ์› ํ–‰๋ ฌ์˜ ์ƒ์„ฑ ndarray ๋Š” N-dimensional Array์˜ ์•ฝ์ž์ด๋‹ค. ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ndarray ํด๋ž˜์Šค๋Š” ๋‹จ์ˆœ ๋ฆฌ์ŠคํŠธ์™€ ์œ ์‚ฌํ•œ 1์ฐจ์› ํ–‰๋ ฌ ์ด์™ธ์—๋„ 2์ฐจ์› ํ–‰๋ ฌ, 3์ฐจ์› ํ–‰๋ ฌ ๋“ฑ์˜ ๋‹ค์ฐจ์› ํ–‰๋ ฌ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์ง€์›ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ 2์ฐจ์› ํ–‰๋ ฌ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ 3์ฐจ์› ํ–‰๋ ฌ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
a = np.array([0, 1, 2]) a b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array b a = np.array([0, 0, 0, 1]) a c = np.array([[[1,2],[3,4]],[[5,6],[7,8]]]) # 2 x 2 x 2 array c
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
ํ–‰๋ ฌ์˜ ์ฐจ์› ๋ฐ ํฌ๊ธฐ๋Š” ndim ์†์„ฑ๊ณผ shape ์†์„ฑ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
print(a.ndim) print(a.shape) a = np.array([[1,2,3 ],[3,4,5]]) a a.ndim a.shape print(b.ndim) print(b.shape) print(c.ndim) print(c.shape)
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
๋‹ค์ฐจ์› ํ–‰๋ ฌ์˜ ์ธ๋ฑ์‹ฑ ndarray ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ๋‹ค์ฐจ์› ํ–‰๋ ฌ์˜ ์›์†Œ ํ•˜๋‚˜ ํ•˜๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝค๋งˆ(comma ,)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„๋œ ์ฐจ์›์„ ์ถ•(axis)์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค. ํ”Œ๋กฏ์˜ x์ถ•๊ณผ y์ถ•์„ ๋– ์˜ฌ๋ฆฌ๋ฉด ๋  ๊ฒƒ์ด๋‹ค.
a = np.array([[0, 1, 2], [3, 4, 5]]) a a[0,0] # ์ฒซ๋ฒˆ์งธ ํ–‰์˜ ์ฒซ๋ฒˆ์งธ ์—ด a[0,1] # ์ฒซ๋ฒˆ์งธ ํ–‰์˜ ๋‘๋ฒˆ์งธ ์—ด a[-1, -1] # ๋งˆ์ง€๋ง‰ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์—ด
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
๋‹ค์ฐจ์› ํ–‰๋ ฌ์˜ ์Šฌ๋ผ์ด์‹ฑ ndarray ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ๋‹ค์ฐจ์› ํ–‰๋ ฌ์˜ ์›์†Œ ์ค‘ ๋ณต์ˆ˜ ๊ฐœ๋ฅผ ์ ‘๊ทผํ•˜๋ ค๋ฉด ์ผ๋ฐ˜์ ์ธ ํŒŒ์ด์ฌ ์Šฌ๋ผ์ด์‹ฑ(slicing)๊ณผ comma(,)๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
a = np.array([[0, 1, 2, 3], [4, 5, 6, 7]]) a a[0, :] # ์ฒซ๋ฒˆ์งธ ํ–‰ ์ „์ฒด a[:, 1] # ๋‘๋ฒˆ์งธ ์—ด ์ „์ฒด a[1, 1:] # ๋‘๋ฒˆ์งธ ํ–‰์˜ ๋‘๋ฒˆ์งธ ์—ด๋ถ€ํ„ฐ ๋์—ด๊นŒ์ง€
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ NumPy ndarray ํด๋ž˜์Šค์˜ ๋˜๋‹ค๋ฅธ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์€ ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ(fancy indexing)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š” ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ(array indexing) ๋ฐฉ๋ฒ•์ด๋‹ค. ์ธ๋ฑ์‹ฑ์ด๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์—ˆ์ง€๋งŒ ์‚ฌ์‹ค์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์งˆ์˜(Query) ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ์—์„œ๋Š” ๋Œ€๊ด„ํ˜ธ(Bracket, [])์•ˆ์˜ ์ธ๋ฑ์Šค ์ •๋ณด๋กœ ์ˆซ์ž๋‚˜ ์Šฌ๋ผ์ด์Šค๊ฐ€ ์•„๋‹Œ ndarray ํ–‰๋ ฌ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ์ด ํ–‰๋ ฌ์„ ํŽธ์˜์ƒ ์ธ๋ฑ์Šค ํ–‰๋ ฌ์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ๋‹ค. ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ์˜ ๋ฐฉ์‹์—์€ ๋ถˆ๋ฆฌ์•ˆ(Boolean) ํ–‰๋ ฌ ๋ฐฉ์‹๊ณผ ์ •์ˆ˜ ํ–‰๋ ฌ ๋ฐฉ์‹ ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋จผ์ € ๋ถˆ๋ฆฌ์•ˆ ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ ๋ฐฉ์‹์€ ์ธ๋ฑ...
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) idx = np.array([True, False, True, False, True, False, True, False, True, False]) a[idx]
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค.
a[a % 2 == 0] a[a % 2] # 0์ด True, 1์ด False
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
2์ฐจ์› ์ด์ƒ์˜ ์ธ๋ฑ์Šค์ธ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด
a = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) [a % 2 == 0] a[[a % 2 == 0]] a[a % 2]
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
์ •์ˆ˜ ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ์—์„œ๋Š” ์ธ๋ฑ์Šค ํ–‰๋ ฌ์˜ ์›์†Œ ๊ฐ๊ฐ์ด ์›๋ž˜ ndarray ๊ฐ์ฒด ์›์†Œ ํ•˜๋‚˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ธ๋ฑ์Šค ์ •์ˆ˜์ด์—ฌ์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1์ฐจ์› ํ–‰๋ ฌ์—์„œ ํ™€์ˆ˜๋ฒˆ์งธ ์›์†Œ๋งŒ ๊ณจ๋ผ๋‚ด๋ ค๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค
a = np.array([0, 1, 2, 3, 4, 10, 6, 7, 8, 9]) * 10 idx = np.array([0, 5, 7, 9, 9]) #์œ„์น˜๋ฅผ ๋œปํ•จ a[idx]
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
์ •์ˆ˜ ํ–‰๋ ฌ ์ธ๋ฑ์Šค์˜ ํฌ๊ธฐ๋Š” ์›๋ž˜์˜ ํ–‰๋ ฌ ํฌ๊ธฐ์™€ ๋‹ฌ๋ผ๋„ ์ƒ๊ด€์—†๋‹ค. ๊ฐ™์€ ์›์†Œ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒฝ์šฐ์—๋Š” ์›๋ž˜์˜ ํ–‰๋ ฌ๋ณด๋‹ค ๋” ์ปค์ง€๊ธฐ๋„ ํ•œ๋‹ค.
a = np.array([0, 1, 2, 3]) * 10 idx = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2]) a[idx] a[0]
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ ๋ถˆ๋ฆฌ์•ˆ(Boolean) ๋ฐฉ์‹ ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ True์ธ ์›์†Œ๋งŒ ์„ ํƒ ์ธ๋ฑ์Šค์˜ ํฌ๊ธฐ๊ฐ€ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ์œ„์น˜ ์ง€์ • ๋ฐฉ์‹ ํ–‰๋ ฌ ์ธ๋ฑ์‹ฑ ์ง€์ •๋œ ์œ„์น˜์˜ ์›์†Œ๋งŒ ์„ ํƒ ์ธ๋ฑ์Šค์˜ ํฌ๊ธฐ๊ฐ€ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ๋‹ฌ๋ผ๋„ ๋œ๋‹ค.
joobun = np.array(["BSY","PJY","PJG","BSJ"]) idx = np.array([0,0,0,1,1,1,2,2,2,3,3,3,0,1,2,3]) joobun[idx] a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) a[(a % 2 == 0) & (a % 3 == 1)]
06. ๊ธฐ์ดˆ ์„ ํ˜•๋Œ€์ˆ˜/01. NumPy ์†Œ๊ฐœ.ipynb
zzsza/Datascience_School
mit
ๅŠ ่ฝฝๅ’Œๅ‡†ๅค‡ๆ•ฐๆฎ ๆž„ๅปบ็ฅž็ป็ฝ‘็ปœ็š„ๅ…ณ้”ฎไธ€ๆญฅๆ˜ฏๆญฃ็กฎๅœฐๅ‡†ๅค‡ๆ•ฐๆฎใ€‚ไธๅŒๅฐบๅบฆ็บงๅˆซ็š„ๅ˜้‡ไฝฟ็ฝ‘็ปœ้šพไปฅ้ซ˜ๆ•ˆๅœฐๆŽŒๆกๆญฃ็กฎ็š„ๆƒ้‡ใ€‚ๆˆ‘ไปฌๅœจไธ‹ๆ–นๅทฒ็ปๆไพ›ไบ†ๅŠ ่ฝฝๅ’Œๅ‡†ๅค‡ๆ•ฐๆฎ็š„ไปฃ็ ใ€‚ไฝ ๅพˆๅฟซๅฐ†่ฟ›ไธ€ๆญฅๅญฆไน ่ฟ™ไบ›ไปฃ็ ๏ผ
data_path = 'Bike-Sharing-Dataset/hour.csv' rides = pd.read_csv(data_path) rides.head()
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
ๆ•ฐๆฎ็ฎ€ไป‹ ๆญคๆ•ฐๆฎ้›†ๅŒ…ๅซ็š„ๆ˜ฏไปŽ 2011 ๅนด 1 ๆœˆ 1 ๆ—ฅๅˆฐ 2012 ๅนด 12 ๆœˆ 31 ๆ—ฅๆœŸ้—ดๆฏๅคฉๆฏๅฐๆ—ถ็š„้ช‘่ฝฆไบบๆ•ฐใ€‚้ช‘่ฝฆ็”จๆˆทๅˆ†ๆˆไธดๆ—ถ็”จๆˆทๅ’Œๆณจๅ†Œ็”จๆˆท๏ผŒcnt ๅˆ—ๆ˜ฏ้ช‘่ฝฆ็”จๆˆทๆ•ฐๆฑ‡ๆ€ปๅˆ—ใ€‚ไฝ ๅฏไปฅๅœจไธŠๆ–น็œ‹ๅˆฐๅ‰ๅ‡ ่กŒๆ•ฐๆฎใ€‚ ไธ‹ๅ›พๅฑ•็คบ็š„ๆ˜ฏๆ•ฐๆฎ้›†ไธญๅ‰ 10 ๅคฉๅทฆๅณ็š„้ช‘่ฝฆไบบๆ•ฐ๏ผˆๆŸไบ›ๅคฉไธไธ€ๅฎšๆ˜ฏ 24 ไธชๆก็›ฎ๏ผŒๆ‰€ไปฅไธๆ˜ฏ็ฒพ็กฎ็š„ 10 ๅคฉ๏ผ‰ใ€‚ไฝ ๅฏไปฅๅœจ่ฟ™้‡Œ็œ‹ๅˆฐๆฏๅฐๆ—ถ็งŸ้‡‘ใ€‚่ฟ™ไบ›ๆ•ฐๆฎๅพˆๅคๆ‚๏ผๅ‘จๆœซ็š„้ช‘่กŒไบบๆ•ฐๅฐ‘ไบ›๏ผŒๅทฅไฝœๆ—ฅไธŠไธ‹็ญๆœŸ้—ดๆ˜ฏ้ช‘่กŒ้ซ˜ๅณฐๆœŸใ€‚ๆˆ‘ไปฌ่ฟ˜ๅฏไปฅไปŽไธŠๆ–น็š„ๆ•ฐๆฎไธญ็œ‹ๅˆฐๆธฉๅบฆใ€ๆนฟๅบฆๅ’Œ้ฃŽ้€Ÿไฟกๆฏ๏ผŒๆ‰€ๆœ‰่ฟ™ไบ›ไฟกๆฏ้ƒฝไผšๅฝฑๅ“้ช‘่กŒไบบๆ•ฐใ€‚ไฝ ้œ€่ฆ็”จไฝ ็š„ๆจกๅž‹ๅฑ•็คบๆ‰€ๆœ‰่ฟ™ไบ›ๆ•ฐๆฎใ€‚
rides[:24*10].plot(x='dteday', y='cnt', figsize=(10,4))
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
ๆŸฅ็œ‹ๆฏๅคฉ็š„้ช‘่กŒๆ•ฐๆฎ๏ผŒๅฏนๆฏ”2011ๅนดๅ’Œ2012ๅนด
day_rides = pd.read_csv('Bike-Sharing-Dataset/day.csv') day_rides = day_rides.set_index(['dteday']) day_rides.loc['2011-08-01':'2011-12-31'].plot(y='cnt', figsize=(10,4)) day_rides.loc['2012-08-01':'2012-12-31'].plot(y='cnt', figsize=(10,4))
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
่™šๆ‹Ÿๅ˜้‡๏ผˆๅ“‘ๅ˜้‡๏ผ‰ ไธ‹้ขๆ˜ฏไธ€ไบ›ๅˆ†็ฑปๅ˜้‡๏ผŒไพ‹ๅฆ‚ๅญฃ่Š‚ใ€ๅคฉๆฐ”ใ€ๆœˆไปฝใ€‚่ฆๅœจๆˆ‘ไปฌ็š„ๆจกๅž‹ไธญๅŒ…ๅซ่ฟ™ไบ›ๆ•ฐๆฎ๏ผŒๆˆ‘ไปฌ้œ€่ฆๅˆ›ๅปบไบŒ่ฟ›ๅˆถ่™šๆ‹Ÿๅ˜้‡ใ€‚็”จ Pandas ๅบ“ไธญ็š„ย get_dummies() ๅฐฑๅฏไปฅ่ฝปๆพๅฎž็Žฐใ€‚
dummy_fields = ['season', 'weathersit', 'mnth', 'hr', 'weekday'] for each in dummy_fields: dummies = pd.get_dummies(rides[each], prefix=each, drop_first=False) rides = pd.concat([rides, dummies], axis=1) fields_to_drop = ['instant', 'dteday', 'season', 'weathersit', 'weekday', 'atemp', 'mnth...
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
่ฐƒๆ•ด็›ฎๆ ‡ๅ˜้‡ ไธบไบ†ๆ›ด่ฝปๆพๅœฐ่ฎญ็ปƒ็ฝ‘็ปœ๏ผŒๆˆ‘ไปฌๅฐ†ๅฏนๆฏไธช่ฟž็ปญๅ˜้‡ๆ ‡ๅ‡†ๅŒ–๏ผŒๅณ่ฝฌๆขๅ’Œ่ฐƒๆ•ดๅ˜้‡๏ผŒไฝฟๅฎƒไปฌ็š„ๅ‡ๅ€ผไธบ 0๏ผŒๆ ‡ๅ‡†ๅทฎไธบ 1ใ€‚ ๆˆ‘ไปฌไผšไฟๅญ˜ๆข็ฎ—ๅ› ๅญ๏ผŒไปฅไพฟๅฝ“ๆˆ‘ไปฌไฝฟ็”จ็ฝ‘็ปœ่ฟ›่กŒ้ข„ๆต‹ๆ—ถๅฏไปฅ่ฟ˜ๅŽŸๆ•ฐๆฎใ€‚ ๅฐ†ๆ•ฐๆฎๆ‹†ๅˆ†ไธบ่ฎญ็ปƒใ€ๆต‹่ฏ•ๅ’Œ้ชŒ่ฏๆ•ฐๆฎ้›† ๆˆ‘ไปฌๅฐ†ๅคง็บฆๆœ€ๅŽ 21 ๅคฉ็š„ๆ•ฐๆฎไฟๅญ˜ไธบๆต‹่ฏ•ๆ•ฐๆฎ้›†๏ผŒ่ฟ™ไบ›ๆ•ฐๆฎ้›†ไผšๅœจ่ฎญ็ปƒๅฎŒ็ฝ‘็ปœๅŽไฝฟ็”จใ€‚ๆˆ‘ไปฌๅฐ†ไฝฟ็”จ่ฏฅๆ•ฐๆฎ้›†่ฟ›่กŒ้ข„ๆต‹๏ผŒๅนถไธŽๅฎž้™…็š„้ช‘่กŒไบบๆ•ฐ่ฟ›่กŒๅฏนๆฏ”ใ€‚
# Save data for approximately the last 21 days test_data = data[-21*24:] # Now remove the test data from the data set data = data[:-21*24] # Separate the data into features and targets target_fields = ['cnt', 'casual', 'registered'] features, targets = data.drop(target_fields, axis=1), data[target_fields] test_feat...
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
ๆˆ‘ไปฌๅฐ†ๆ•ฐๆฎๆ‹†ๅˆ†ไธบไธคไธชๆ•ฐๆฎ้›†๏ผŒไธ€ไธช็”จไฝœ่ฎญ็ปƒ๏ผŒไธ€ไธชๅœจ็ฝ‘็ปœ่ฎญ็ปƒๅฎŒๅŽ็”จๆฅ้ชŒ่ฏ็ฝ‘็ปœใ€‚ๅ› ไธบๆ•ฐๆฎๆ˜ฏๆœ‰ๆ—ถ้—ดๅบๅˆ—็‰นๆ€ง็š„๏ผŒๆ‰€ไปฅๆˆ‘ไปฌ็”จๅކๅฒๆ•ฐๆฎ่ฟ›่กŒ่ฎญ็ปƒ๏ผŒ็„ถๅŽๅฐ่ฏ•้ข„ๆต‹ๆœชๆฅๆ•ฐๆฎ๏ผˆ้ชŒ่ฏๆ•ฐๆฎ้›†๏ผ‰ใ€‚
# Hold out the last 60 days or so of the remaining data as a validation set train_features, train_targets = features[:-60*24], targets[:-60*24] val_features, val_targets = features[-60*24:], targets[-60*24:]
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
ๅผ€ๅง‹ๆž„ๅปบ็ฝ‘็ปœ ไธ‹้ขไฝ ๅฐ†ๆž„ๅปบ่‡ชๅทฑ็š„็ฝ‘็ปœใ€‚ๆˆ‘ไปฌๅทฒ็ปๆž„ๅปบๅฅฝ็ป“ๆž„ๅ’Œๅๅ‘ไผ ้€’้ƒจๅˆ†ใ€‚ไฝ ๅฐ†ๅฎž็Žฐ็ฝ‘็ปœ็š„ๅ‰ๅ‘ไผ ้€’้ƒจๅˆ†ใ€‚่ฟ˜้œ€่ฆ่ฎพ็ฝฎ่ถ…ๅ‚ๆ•ฐ๏ผšๅญฆไน ้€Ÿ็އใ€้š่—ๅ•ๅ…ƒ็š„ๆ•ฐ้‡๏ผŒไปฅๅŠ่ฎญ็ปƒไผ ้€’ๆ•ฐ้‡ใ€‚ <img src="assets/neural_network.png" width=300px> ่ฏฅ็ฝ‘็ปœๆœ‰ไธคไธชๅฑ‚็บง๏ผŒไธ€ไธช้š่—ๅฑ‚ๅ’Œไธ€ไธช่พ“ๅ‡บๅฑ‚ใ€‚้š่—ๅฑ‚็บงๅฐ†ไฝฟ็”จ S ๅž‹ๅ‡ฝๆ•ฐไฝœไธบๆฟ€ๆดปๅ‡ฝๆ•ฐใ€‚่พ“ๅ‡บๅฑ‚ๅชๆœ‰ไธ€ไธช่Š‚็‚น๏ผŒ็”จไบŽ้€’ๅฝ’๏ผŒ่Š‚็‚น็š„่พ“ๅ‡บๅ’Œ่Š‚็‚น็š„่พ“ๅ…ฅ็›ธๅŒใ€‚ๅณๆฟ€ๆดปๅ‡ฝๆ•ฐๆ˜ฏย $f(x)=x$ใ€‚่ฟ™็งๅ‡ฝๆ•ฐ่Žทๅพ—่พ“ๅ…ฅไฟกๅท๏ผŒๅนถ็”Ÿๆˆ่พ“ๅ‡บไฟกๅท๏ผŒไฝ†ๆ˜ฏไผš่€ƒ่™‘้˜ˆๅ€ผ๏ผŒ็งฐไธบๆฟ€ๆดปๅ‡ฝๆ•ฐใ€‚ๆˆ‘ไปฌๅฎŒๆˆ็ฝ‘็ปœ็š„ๆฏไธชๅฑ‚็บง๏ผŒๅนถ่ฎก็ฎ—ๆฏไธช็ฅž็ปๅ…ƒ็š„่พ“ๅ‡บใ€‚ไธ€ไธชๅฑ‚็บง็š„ๆ‰€ๆœ‰่พ“ๅ‡บๅ˜ๆˆไธ‹ไธ€ๅฑ‚็บง็ฅž็ปๅ…ƒ็š„่พ“ๅ…ฅใ€‚่ฟ™ไธ€ๆต็จ‹ๅซๅšๅ‰ๅ‘ไผ ๆ’ญ๏ผˆforward pro...
class NeuralNetwork(object): def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate): # Set number of nodes in input, hidden and output layers. self.input_nodes = input_nodes self.hidden_nodes = hidden_nodes self.output_nodes = output_nodes # Initialize w...
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
ๅ•ๅ…ƒๆต‹่ฏ• ่ฟ่กŒ่ฟ™ไบ›ๅ•ๅ…ƒๆต‹่ฏ•๏ผŒๆฃ€ๆŸฅไฝ ็š„็ฝ‘็ปœๅฎž็Žฐๆ˜ฏๅฆๆญฃ็กฎใ€‚่ฟ™ๆ ทๅฏไปฅๅธฎๅŠฉไฝ ็กฎไฟ็ฝ‘็ปœๅทฒๆญฃ็กฎๅฎž็Žฐ๏ผŒ็„ถๅŽๅ†ๅผ€ๅง‹่ฎญ็ปƒ็ฝ‘็ปœใ€‚่ฟ™ไบ›ๆต‹่ฏ•ๅฟ…้กปๆˆๅŠŸๆ‰่ƒฝ้€š่ฟ‡ๆญค้กน็›ฎใ€‚
import unittest inputs = np.array([[0.5, -0.2, 0.1]]) targets = np.array([[0.4]]) test_w_i_h = np.array([[0.1, -0.2], [0.4, 0.5], [-0.3, 0.2]]) test_w_h_o = np.array([[0.3], [-0.1]]) class TestMethods(unittest.TestCase): ########## # Un...
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
่ฎญ็ปƒ็ฝ‘็ปœ ็Žฐๅœจไฝ ๅฐ†่ฎพ็ฝฎ็ฝ‘็ปœ็š„่ถ…ๅ‚ๆ•ฐใ€‚็ญ–็•ฅๆ˜ฏ่ฎพ็ฝฎ็š„่ถ…ๅ‚ๆ•ฐไฝฟ่ฎญ็ปƒ้›†ไธŠ็š„้”™่ฏฏๅพˆๅฐไฝ†ๆ˜ฏๆ•ฐๆฎไธไผš่ฟ‡ๆ‹Ÿๅˆใ€‚ๅฆ‚ๆžœ็ฝ‘็ปœ่ฎญ็ปƒๆ—ถ้—ดๅคช้•ฟ๏ผŒๆˆ–่€…ๆœ‰ๅคชๅคš็š„้š่—่Š‚็‚น๏ผŒๅฏ่ƒฝๅฐฑไผš่ฟ‡ไบŽ้’ˆๅฏน็‰นๅฎš่ฎญ็ปƒ้›†๏ผŒๆ— ๆณ•ๆณ›ๅŒ–ๅˆฐ้ชŒ่ฏๆ•ฐๆฎ้›†ใ€‚ๅณๅฝ“่ฎญ็ปƒ้›†็š„ๆŸๅคฑ้™ไฝŽๆ—ถ๏ผŒ้ชŒ่ฏ้›†็š„ๆŸๅคฑๅฐ†ๅผ€ๅง‹ๅขžๅคงใ€‚ ไฝ ่ฟ˜ๅฐ†้‡‡็”จ้šๆœบๆขฏๅบฆไธ‹้™ (SGD) ๆ–นๆณ•่ฎญ็ปƒ็ฝ‘็ปœใ€‚ๅฏนไบŽๆฏๆฌก่ฎญ็ปƒ๏ผŒ้ƒฝ่Žทๅ–้šๆœบๆ ทๆœฌๆ•ฐๆฎ๏ผŒ่€Œไธๆ˜ฏๆ•ดไธชๆ•ฐๆฎ้›†ใ€‚ไธŽๆ™ฎ้€šๆขฏๅบฆไธ‹้™็›ธๆฏ”๏ผŒ่ฎญ็ปƒๆฌกๆ•ฐ่ฆๆ›ดๅคš๏ผŒไฝ†ๆ˜ฏๆฏๆฌกๆ—ถ้—ดๆ›ด็Ÿญใ€‚่ฟ™ๆ ท็š„่ฏ๏ผŒ็ฝ‘็ปœ่ฎญ็ปƒๆ•ˆ็އๆ›ด้ซ˜ใ€‚็จๅŽไฝ ๅฐ†่ฏฆ็ป†ไบ†่งฃ SGDใ€‚ ้€‰ๆ‹ฉ่ฟญไปฃๆฌกๆ•ฐ ไนŸๅฐฑๆ˜ฏ่ฎญ็ปƒ็ฝ‘็ปœๆ—ถไปŽ่ฎญ็ปƒๆ•ฐๆฎไธญๆŠฝๆ ท็š„ๆ‰นๆฌกๆ•ฐ้‡ใ€‚่ฟญไปฃๆฌกๆ•ฐ่ถŠๅคš๏ผŒๆจกๅž‹ๅฐฑไธŽๆ•ฐๆฎ่ถŠๆ‹Ÿๅˆใ€‚ไฝ†ๆ˜ฏ๏ผŒๅฆ‚ๆžœ่ฟญไปฃๆฌกๆ•ฐๅคชๅคš๏ผŒๆจกๅž‹ๅฐฑๆ— ๆณ•ๅพˆๅฅฝๅœฐๆณ›ๅŒ–ๅˆฐๅ…ถไป–ๆ•ฐๆฎ๏ผŒ่ฟ™ๅซๅš่ฟ‡ๆ‹Ÿๅˆใ€‚ไฝ ้œ€่ฆ้€‰ๆ‹ฉไธ€ไธชไฝฟ่ฎญ็ปƒๆŸๅคฑๅพˆไฝŽ...
import sys ### Set the hyperparameters here ### iterations = 4000 learning_rate = 0.5 hidden_nodes = 20 output_nodes = 1 N_i = train_features.shape[1] network = NeuralNetwork(N_i, hidden_nodes, output_nodes, learning_rate) losses = {'train':[], 'validation':[]} for ii in range(iterations): # Go through a random ...
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
ๆฃ€ๆŸฅ้ข„ๆต‹็ป“ๆžœ ไฝฟ็”จๆต‹่ฏ•ๆ•ฐๆฎ็œ‹็œ‹็ฝ‘็ปœๅฏนๆ•ฐๆฎๅปบๆจก็š„ๆ•ˆๆžœๅฆ‚ไฝ•ใ€‚ๅฆ‚ๆžœๅฎŒๅ…จ้”™ไบ†๏ผŒ่ฏท็กฎไฟ็ฝ‘็ปœไธญ็š„ๆฏๆญฅ้ƒฝๆญฃ็กฎๅฎž็Žฐใ€‚
fig, ax = plt.subplots(figsize=(8,4)) mean, std = scaled_features['cnt'] predictions = network.run(test_features).T*std + mean ax.plot(predictions[0], label='Prediction') ax.plot((test_targets['cnt']*std + mean).values, label='Data') ax.set_xlim(right=len(predictions)) ax.legend() dates = pd.to_datetime(rides.ix[test...
first-neural-network/Your_first_neural_network.ipynb
xaibeing/cn-deep-learning
mit
Initialize dataset, print available variable names. In order to be able to access the data, save your API key in the file APIKEY and put it into the same folder where you run this notebook.
apikey = open('APIKEY').readlines()[0].strip() dh = datahub_main(apikey) ds = dataset('ecmwf_era5',dh,debug=True) ds.variable_names() # Choose location, variable lon = 26 lat = 58 variable = '2_metre_temperature_surface' d1 = ds.get_json_data_in_pandas(count=100000,**{'vars':'2_metre_temperature_surface','lon':lon,'...
api-examples/ERA5_tutorial.ipynb
planet-os/notebooks
mit
Wind energy example As a more advanced use case, let's try to analyse potential wind energy production in three different locations. For this, we take wind speed at 100 m height, apply a simplified energy production curve to hourly data, and get a statistics for hourly and weekly average (wa) data.
def production_curve(wind_speed): """ Production curve of a turbine/farm can be simplified as a cube of wind speed, with production starting from a particular wind speed 'v_cut_in' getting maximum output power at wind speed 'v_rated' production halted at very strong wind sp...
api-examples/ERA5_tutorial.ipynb
planet-os/notebooks
mit
split_element <b>split_element(func, in_stream, out_streams)</b> <br> <br> where <ol> <li><b>func</b> is a function with an argument which is an element of a single input stream and that returns a list with one element for each out_stream. <i>func</i> may have additional keyword arguments and may also have a state....
def simple_example_of_split_element(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions def f(v): return [v+100, v*2] # Create agent with input stream x and output streams y, z. split_element(func=f, in_stream=x, out_streams=[y,z]) ...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Using Lambda Expressions Lambda expressions in split_element can be convenient as shown in this example which is essentially the same as the previous one.
def example_of_split_element_with_lambda(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Create agent with input stream x and output streams y, z. split_element(lambda v: [v+100, v*2], x, [y,z]) # Put test values in the input streams. x.extend(list(range(5)))...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of the decorator @split_e The decorator <b>@split_e</b> operates the same as split_element, except that the agent is created by calling the decorated function. <br> Compare this example with the first example which used <i>split_element</i>. The two examples are almost identical. The difference is in the way th...
def simple_example_of_split_e(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions @split_e def f(v): return [v+100, v*2] # Create agent with input stream x and output streams y, z. f(in_stream=x, out_streams=[y,z]) # Put te...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of functional forms You may want to use a function that returns the streams resulting from a split instead of having the streams specified in out_streams, i.e. you may prefer to write: <br> <br> a, b, c = h(u) <br> <br> where <i>u</i> is a stream that is split into streams <i>a</i>, <i>b</i>, and <i>c</i>, ins...
def simple_example_of_functional_form(): # ------------------------------------------------------ # Specifying a functional form # The functional form takes a single input stream and returns # three streams. def h(w): # Specify streams x = Stream('x') y = Stream('y') ...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example with keyword arguments This example shows how to pass keyword arguments to <i>split_element</i>. In the example, <i>addend</i> and <i>multiplicand</i> are arguments of <i>f</i> the encapsulated function, and these arguments are passed as keyword arguments to <i>split_element</i>.
def example_of_split_element_with_keyword_args(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions def f(v, addend, multiplicand): return [v+addend, v*multiplicand] # Create agent with input stream x and output streams y, z. sp...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Split element with state This example shows how to create an agent with state. The encapsulated function takes two arguments --- an element of the input stream and a <b>state</b> --- and it returns two values: a list of elements corresponding to the output streams and the <b>next state</b>. The function may have additi...
def example_of_split_element_with_state(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions def f(v, state): next_state = state+1 return ([v+state, v*state], next_state) # Create agent with input stream x and output streams ...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example with state and keyword arguments This example shows an encapsulated function with a state and an argument called <i>state_increment</i> which is passed as a keyword argument to <i>split_element</i>.
def example_of_split_element_with_state_and_keyword_args(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions def f(v, state, state_increment): next_state = state + state_increment return ([v+state, v*state], next_state) # Cr...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example with StreamArray and NumPy arrays
import numpy as np from IoTPy.core.stream import StreamArray def example_of_split_element_with_stream_array(): # Specify streams x = StreamArray('x') y = StreamArray('y') z = StreamArray('z') # Specify encapsulated functions def f(v, addend, multiplier): return [v+addend, v*multiplier]...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of split list split_list is the same as split_element except that the encapsulated function operates on a <i>list</i> of elements of the input stream rather than on a single element. Operating on a list can be more efficient than operating sequentially on each of the elements of the list. This is especially imp...
def example_of_split_list(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions def f(lst, addend, multiplier): return ([v+addend for v in lst], [v*multiplier for v in lst]) # Create agent with input stream x and output streams y, z. ...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of split list with arrays In this example, the encapsulated function <i>f</i> operates on an array <i>a</i> which is a segment of the input stream array, <i>x</i>. The operations in <i>f</i> are array operations (not list operations). For example, the result of <i>a * multiplier </i> is specified by numpy multi...
def example_of_split_list_with_arrays(): # Specify streams x = StreamArray('x') y = StreamArray('y') z = StreamArray('z') # Specify encapsulated functions def f(a, addend, multiplier): # a is an array # return two arrays. return (a + addend, a * multiplier) # Create...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Test of unzip unzip is the opposite of zip_stream. <br> <br> An element of the input stream is a list or tuple whose length is the same as the number of output streams; the <i>j</i>-th element of the list is placed in the <i>j</i>-th output stream. <br> <br> In this example, when the unzip agent receives the triple (1,...
def simple_test_unzip(): # Specify streams w = Stream('w') x = Stream('x') y = Stream('y') z = Stream('z') # Create agent with input stream x and output streams y, z. unzip(in_stream=w, out_streams=[x,y,z]) # Put test values in the input streams. w.extend([(1, 10, 100), (2, 20,...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of separate <b>separate</b> is the opposite of <b>mix</b>. <br> The elements of the input stream are pairs (index, value). When a pair <i>(i,v)</i> arrives on the input stream the value <i>v</i> is appended to the <i>i</i>-th output stream. <br> <br> In this example, when (0, 1) and (2, 100) arrive on the input...
def simple_test_separate(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') w = Stream('w') # Create agent with input stream x and output streams y, z. separate(in_stream=x, out_streams=[y,z,w]) # Put test values in the input streams. x.extend([(0,1), (2, 100)...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of separate with stream arrays. This is the same example as the previous case. The only difference is that since the elements of the input stream are pairs, the dimension of <i>x</i> is 2.
def test_separate_with_stream_array(): # Specify streams x = StreamArray('x', dimension=2) y = StreamArray('y') z = StreamArray('z') # Create agent with input stream x and output streams y, z. separate(in_stream=x, out_streams=[y,z]) # Put test values in the input streams. x.extend...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example of split window The input stream is broken up into windows. In this example, with <i>window_size</i>=2 and <i>step_size</i>=2, the sequence of windows are <i>x[0, 1], x[2, 3], x[4, 5], ....</i>. <br> <br> The encapsulated function operates on a window and returns <i>n</i> values where <i>n</i> is the number of ...
def simple_example_of_split_window(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Specify encapsulated functions def f(window): return (max(window), min(window)) # Create agent with input stream x and output streams y, z. split_window(func=f, in_stream=x, out_st...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example that illustrates zip followed by unzip is the identity. zip_stream followed by unzip returns the initial streams.
from IoTPy.agent_types.merge import zip_stream def example_zip_plus_unzip(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') u = Stream('u') v = Stream('v') # Create agents zip_stream(in_streams=[x,y], out_stream=z) unzip(in_stream=z, out_streams=[u,v]) # ...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example that illustrates that mix followed by separate is the identity.
from IoTPy.agent_types.merge import mix def example_mix_plus_separate(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') u = Stream('u') v = Stream('v') # Create agents mix(in_streams=[x,y], out_stream=z) separate(in_stream=z, out_streams=[u,v]) # Put test...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Simple example of timed_unzip An element of the input stream is a pair (timestamp, list). The sequence of timestamps must be increasing. The list has length n where n is the number of output streams. The m-th element of the list is the value of the m-th output stream associated with that timestamp. For example, if an e...
def test_timed_unzip(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') # Create agent with input stream x and output streams y, z. timed_unzip(in_stream=x, out_streams=[y,z]) # Put test values in the input streams. x.extend([(1, ["A", None]), (5, ["B", "a"]), (7,...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Example that illustrates that timed_zip followed by timed_unzip is the identity.
from IoTPy.agent_types.merge import timed_zip def test_timed_zip_plus_timed_unzip(): # Specify streams x = Stream('x') y = Stream('y') z = Stream('z') u = Stream('u') v = Stream('v') # Create agents timed_zip(in_streams=[x,y], out_stream=z) timed_unzip(in_stream=z, out_streams=[u,v]...
examples/ExamplesOfSplit.ipynb
AssembleSoftware/IoTPy
bsd-3-clause
Let's continue with the necessary imports:
import calendar import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import networkx as nx from scipy import stats import pandas as pd import statsmodels.api as sm from typecheck import typecheck import sys sys.path.append("../lib")
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
High-level plotting For our purposes, we will define high-level plotting as anything in the matplotlib world which: Utilizes matplotlib APIs under the covers In order to accomplish complicated plotting tasks And provide an API for those tasks to the user, to employ either in conjunction with matplotlib directly, or in...
from matplotlib import sankey fig = plt.figure(figsize=(18, 22)) ax = fig.add_subplot( 1, 1, 1, xticks=[], yticks=[]) ax.set_title(("Rankine Power Cycle\nExample 8.6 from Moran and Shapiro\n" "$Fundamentals \ of \ Engineering \ Thermodynamics$, " "6th ed., 2008"), fonts...
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
A more common example is code you would write in your own projects. For instance, if you recall from the notebook covering the matplotlib APIs, we created a module to demonstrate the object-oriented API for programmatic workflows. That little module is a perfect example of high-level plotting with matplotlib. NetworkX ...
import lanl from networkx.drawing.nx_agraph import graphviz_layout # Set up the plot's figure instance plt.figure(figsize=(14,14)) # Generate the data graph structure representing the route relationships G = lanl.get_routes_graph(debug=True) # Perform the high-level plotting operations in NetworkX pos = graphviz_lay...
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Like we saw when creating a graph that would neatly render the matplotlib module layout in accordance with the philosophy of its architecture, Aric Hagberg had to do something similar when rendering the Internet routes from Los Alamos National Laboratory. We've put this code in the lanl module for this notebook repo; i...
graphviz_layout
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Taking a look at that file, we can see that graphviz_layout wraps the pygraphviz_layout function. From there, we see that NetworkX is converting pygraphviz's node data structure to something general that can be used for all backends. We're already several layers deep in NetworkX's high-level API internals. Next, let's ...
nx.draw
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Now we're getting close to matplotlib! The nx_pylab module's draw function makes direct use of matplotlib.pyplot in order to: Get the current figure from pyplot Or, if it exists, from the axes object Hold and un-hold the matplotlib figures Call a matplotlib draw function It also makes a subsequent call to the Network...
from scipy.stats import norm, rayleigh a = rayleigh.rvs(loc=5, scale=2, size=1000) + 1 b = rayleigh.rvs(loc=5, scale=2, size=1000) c = rayleigh.rvs(loc=5, scale=2, size=1000) - 1
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Create a Pandas data structure instance:
data = pd.DataFrame({"a": a, "b": b, "c": c}, columns=["a", "b", "c"])
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Let's take a look at a histogram plot of this data:
axes = data.plot(kind="hist", stacked=True, bins=30, figsize=(16, 8)) axes.set_title("Fabricated Wind Speed Data", fontsize=20) axes.set_xlabel("Mean Hourly Wind Speed (km/hr)", fontsize=16) _ = axes.set_ylabel("Velocity Counts", fontsize=16)
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
So what's going on here? Well, if you take a dive into the Pandas codebase, you'll find that there are wrappers in pandas.tools.plotting that do a bunch of work under the covers to make plotting from the DataFrame object an exercise in simplicity. In particular, look at plotting.plot_frame and plotting._plot. Grammar o...
from bokeh.plotting import output_notebook output_notebook()
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
The Bokeh example has been saved to this notebook's repo (and modified slightly); you can load it up with the following:
import burtin
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Since this series of notebooks are focused on matplotlib (and not Bokeh!), we won't go into too much detail, but it is definitely worth mentioning that Bokeh provides a matplotlib compatibility layer. It doesn't cover 100% of all matplotlib API usage a given project may entail, but enough so that one should be able to ...
import ggplot from ggplot import components, geoms, scales, stats from ggplot import exampledata
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Here's a quick look at some movie data collected over the 20th century:
data = exampledata.movies aesthetics = components.aes(x='year', y='budget') (ggplot.ggplot(aesthetics, data=data) + stats.stat_smooth(span=.15, color='red', se=True) + geoms.ggtitle("Movie Budgets over Time") + geoms.xlab("Year") + geoms.ylab("Dollars"))
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Views of data collected on the cuts and qualities of diamonds:
data = exampledata.diamonds aesthetics = components.aes(x='price', fill='cut') (ggplot.ggplot(aesthetics, data=data) + geoms.geom_density(alpha=0.25) + geoms.facet_wrap("clarity"))
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
ggplot offers some nice color options that, when used effectively, can be more revealing of data in hidden relationships. The following are examples of ggplot's support for ColorBrewer:
aesthetics = components.aes(x='price', y='carat', color='clarity') plot = ggplot.ggplot(aesthetics, data=data) point = geoms.geom_point(alpha=0.6) (plot + point + scales.scale_color_brewer(type='qual', palette='Set1')) (plot + point + scales.scale_color_brewer(type='seq', palette='YlOrRd')) (plot + point + scal...
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
There is a great deal more to explore with ggplot. Be sure to check out: * the main site * the docs * the Github repo New styles in matplotlib matplotlib has recently added support for ggplot styling. Let's contrast it with the defaults. First, import the demo code:
import mplggplot
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Now let's take a look at the default styling of some plots:
figure, axes = plt.subplots(ncols=2, nrows=2, figsize=(10, 10)) mplggplot.demo(axes) plt.show()
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Though we are looking at the ggplot style here, there are actually several styles to choose from. Here's the list of the available ones:
plt.style.available
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Now we'll select "ggplot" and re-render our demo plots:
plt.style.use('bmh') figure, axes = plt.subplots(ncols=2, nrows=2, figsize=(10, 10)) mplggplot.demo(axes) plt.show()
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
It's not really a wonder that ggplot has so much appeal in the community :-) Seaborn As noted, the development of seaborn has been greatly inspired by the Grammar of Graphics and R's ggplot in particular. Here's what the notes say on the seaborn site: <blockquote> Seaborn aims to make visualization a central part of ex...
import seaborn as sns pallete_name = "husl" colors = sns.color_palette(pallete_name, 8) colors.reverse() cmap = mpl.colors.LinearSegmentedColormap.from_list(pallete_name, colors)
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Scatter Plot Matrices Scatter plots offer a unique view on multivariate data sets, allowing one to see what sorts of correlations exist between variables, if any. Here is the famous "iris" data set in a seaborn pairplot scatter plot matrix which focuses on pair-wise relationships:
sns.set() data_frame = sns.load_dataset("iris") _ = sns.pairplot(data_frame, hue="species", size=2.5)
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Facet Grids When you want to split up a data set by one or more variables, and then group subplots of these separated variables, you probably want to use a facet grid. Another use case is for examining repeated runs of an experiment to reveal potentially conditional relationships between variables. Below is a concocted...
import seademo sns.set(style="ticks") data = seademo.get_data_set() grid = sns.FacetGrid(data, col="walk", hue="walk", col_wrap=5, size=2) grid.map(plt.axhline, y=0, ls=":", c=".5") grid.map(plt.plot, "step", "position", marker="o", ms=4) grid.set(xticks=np.arange(5), yticks=[-3, 3], xlim=(-.5, 4.5), ylim=(...
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0
Violin Plots Violin plots are similar to box plots (the latter of which we will see more use of below in the "Data Analysis" section). Where box plots display variation in samples of a statistical population with different parts of the box indicating the degree of spread (among other things), the violin plot provide in...
sns.set(style="whitegrid") df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0) used_networks = [1, 3, 4, 5, 6, 7, 8, 11, 12, 13, 16, 17] level_values = df.columns.get_level_values("network").astype(int) used_columns = level_values.isin(used_networks) df = df.loc[:, used_columns] corr_df = df.corr()...
github/MasteringMatplotlib/mmpl-high-level.ipynb
moonbury/pythonanywhere
gpl-3.0