markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
And just for the sake of completion, let's temporarily kick out Tony from the table. Temporary, since it's not inplace. | df.drop('Tony', axis = 0)
# Renaming Columns
df.rename(columns={'Jan': 'January'}, inplace=True)
df
df.rename(columns={'Feb': 'February', 'Mar': 'March', 'Apr': 'April'}, inplace=True)
df | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Dataframe from a Dictionry
Let's create a new dataframe from a dictionary, and then apply some of the selection techniques we just learnt. | dict1 = {'first_name': ['Erlich', 'Richard', "Dinesh", 'Gilfoyle', 'Nelson'],
'second_name': ['Bachman', 'Hendricks', np.nan, np.nan, 'Bighetti'],
'occupation': ['Investor', 'Entrepreneur', 'Coder', 'Coder', 'Bench Warmer'],
'age': [40, 30, 28, 29, 28]}
df = pd.DataFrame(dict1, columns = ['firs... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Exercise | np.random.seed(42)
np.random.randn(4,4)
np.random.seed(42)
df = pd.DataFrame(np.random.randn(4,4), index = "Peter,Clarke,Bruce,Tony".split(","), columns = "Jan,Feb,Mar,Apr".split(","))
df
# Who scored greater than 0 in Apr?
df[df>0][["Apr"]]
# Who scored below 0 in March?
# In which month/months did Clarke score a... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Handling Missing Data
Pay special attention to this section. If needed, spend some extra time to cover all the relevant techniques. <br>
Never in my experience have I come across a 100% clean data set "in the wild". What that means is that of course you will find that most data sets that you train with to be complete, ... | df = pd.DataFrame({'NYC':[3,np.nan,7,9,6],
'SF':[4,3,8,7,15],
'CHI':[4,np.nan,np.nan,14,6],
'MIA':[3, 9,12,8,9]}, index = ['Mon','Tue','Wed','Thu','Fri'])
df | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
First thing we can do is drop rows with missing values with the dropna() function. By default, rows are dropped, but you can change this to columns as well. | df.dropna()
df.dropna(axis = 0)
df.dropna(axis = 1) | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
While this can be helpful in some ways, if your dataset is small, you are losing a significant portion of your data.
For example, if 100 rows out of 1 million rows have missing data, that's negligible, and can potentially be thrown away. What if you have 10 out of 85 rows with incorrect, unusable or missing data? | df2 = df.copy()
df2
df2.mean()
# Are these really the means though?
df
mean = df2['SF'].mean()
mean | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Imputation
Using the fillna function, we can replace missing values. | df = pd.DataFrame({'NYC':[3,np.nan,7,9,6],
'SF':[4,3,8,7,15],
'CHI':[4,np.nan,np.nan,14,6],
'MIA':[3, 9,12,8,9]}, index = ['Mon','Tue','Wed','Thu','Fri'])
df
df.mean()
df.fillna(value = df.mean(), inplace = True)
df
df = pd.DataFrame({'NYC':[3,np.nan,7,9,6],
... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
But sometimes, the data isn't part of the table. Consider the scenario below. We know that the below tables contains names of female babies. But it's missing in our dataset. | baby_names = {
'id': ['101', '102', '103', '104', '105'],
'first_name': ['Emma', 'Madison', 'Hannah', 'Grace', 'Emily']
}
df_baby = pd.DataFrame(baby_names, columns = ['id', 'first_name'])
df_baby
df_baby.columns
df_baby["gender"] = "F"
df_baby
df_baby['gender'] = 0
df_baby | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Interpolation
Read up more on the interpolate function here and here | df = pd.read_csv("data/cafe_sales2015.csv")
df
df["Date"].head()
df["Date"] = pd.to_datetime(df["Date"])
df.set_index(["Date"], inplace = True)
df.head()
df.tail()
df.head(3)
df.describe()
%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,5)
df.plot(kind="line")
df["Water... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Keep in mind though, that these are at best approximations.
A Quick Detour into some Data Viz
Install Vincent by running the following line in your command line:
Python 2.x: pip install vincent <br>
Python 3.x: pip3 install vincent | import vincent
vincent.core.initialize_notebook()
line = vincent.Line(df)
line.axis_titles(x='Date', y='Amount')
line = vincent.Line(df[["Latte", "Water"]])
line.axis_titles(x='Date', y='Amount')
stacked = vincent.StackedArea(df)
stacked.axis_titles(x='Date', y='Amount')
stacked.legend(title='Cafe Sales')
stacked.co... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Read about using the Vincent package here.
The latest update to Matplotlib, V 2.0.0 has really improved the quality of the graphics, but it's still not quite production ready, while on the positive side, it is stable and has a large community of people who use it. Niche packages like Vincent can produce some amazing g... | customers = {
'customer_id': ['101', '102', '103', '104', '105'],
'first_name': ['Tony', 'Silvio', 'Paulie', 'Corrado', 'Christopher'],
'last_name': ['Soprano', 'Dante', 'Gualtieri', 'Soprano', 'Moltisanti']}
df_1 = pd.DataFrame(customers, columns = ['customer_id', 'first_name', 'last_name'])
d... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Join | customers = {
'customer_id': ['101', '102', '103', '104', '105'],
'first_name': ['Tony', 'Silvio', 'Paulie', 'Corrado', 'Christopher'],
'last_name': ['Soprano', 'Dante', 'Gualtieri', 'Soprano', 'Moltisanti']}
customers
orders = {
'customer_id': ['101', '104', '105', '108', '111'],
... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Concatenate | customers = {
'customer_id': ['101', '102', '103', '104', '105'],
'first_name': ['Tony', 'Silvio', 'Paulie', 'Corrado', 'Christopher'],
'last_name': ['Soprano', 'Dante', 'Gualtieri', 'Soprano', 'Moltisanti']}
df_1 = pd.DataFrame(customers, columns = ['customer_id', 'first_name', 'last_name'])
d... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
One final resource on why you would want to perform these operations in Pandas - and evidence on how fast it really is! http://wesmckinney.com/blog/high-performance-database-joins-with-pandas-dataframe-more-benchmarks/
Grouping, a.k.a. split-apply-combine
While analysing data, a Data Scientist has to very often perform... | import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = (15,7)
paintball = {'Team': ['Super Ducks','Super Ducks', 'Super Ducks', 'Super Ducks', 'Super Ducks', 'Bobcats', 'Bobcats', 'Bobcats', 'Bobcats', 'Tigers', 'Tigers', 'Tiger... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Cool graph, but can we improve it, visually speaking? Yes of course we can! Let's look at some of the styles available within Matplotlib. | plt.style.available | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Personally I am quite partial to ggplot and seaborn, but not so much to fivethirtyeight. Let's try these. | plt.style.use('ggplot')
plt.rcParams["figure.figsize"] = (15,7)
Team_Before.join(Team_After).plot(kind="Bar") | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
What about fivethirtyeight? | plt.style.use('fivethirtyeight')
plt.rcParams["figure.figsize"] = (15,7)
Team_Before.join(Team_After).plot(kind="Bar") | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
And seaborn. Note that seaborn is a visualisation library that works with Matplotlib. You can mimic the style without actually using it. | plt.style.use('seaborn')
plt.rcParams["figure.figsize"] = (15,7)
Team_Before.join(Team_After).plot(kind="Bar")
plt.rcParams.update(plt.rcParamsDefault)
plt.style.use('seaborn-poster')
plt.rcParams["figure.figsize"] = (15,7)
Team_Before.join(Team_After).plot(kind="Bar")
pd.crosstab(df["Team"], df["Kills"], margins =... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Apply
We can use the apply function to perform an operation over an axis in a dataframe. | import pandas as pd
import numpy as np
df = pd.read_csv("data/cafe_sales2015.csv")
df.head()
df["Date"] = pd.to_datetime(df["Date"])
df.set_index(["Date"], inplace = True)
df.interpolate(method = "linear", inplace = True)
df.head()
#print(df.apply(np.cumsum))
df.apply(np.average)
df.apply(lambda x: x.max() - x.min... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Map
The map function iterates over each element of a series. | import pandas as pd
import numpy as np
df = pd.read_csv("data/cafe_sales2015.csv")
df.head()
df["Latte"] = df["Latte"].map(lambda x: x+2)
df.head()
df.interpolate(method = "linear", inplace = True)
df["Water"] = df["Water"].map(lambda x: x-1 if (x>0) else 0)
df.head() | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
ApplyMap | import pandas as pd
import numpy as np
df = pd.read_csv("data/cafe_sales2015.csv")
df.head()
def to_int(x):
if type(x) is float:
x = int(x)
return x
else:
return x
df.interpolate(method = "linear", inplace = True)
df.applymap(to_int).head() | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Further Reading<br>
Wes McKinney's amazing book covers this issue. Refer to Page 132.
Pivot Tables
Pivot tables are summarisation tables that help the user sort, count, total or average the data available in a dataset. If you have used Excel, you will be very familiar with them. If not, let's look at it from a fresh Pa... | import pandas as pd
import numpy as np
# The 'xlrd' module gets imported automatically, if not, install it with 'pip install xlrd'
df = pd.read_excel("Data/bev-sales.xlsx")
df.head()
df.tail()
df.describe()
help(pd.pivot_table)
df.head()
pd.pivot_table(df,index=["Sales Exec"],values=["Revenue"],aggfunc="sum")
%m... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Tips | df = pd.read_csv("Data/tips.csv")
df.head()
df["tip_pc"] = df["tip"] / df["total_bill"]
df.head()
pd.pivot_table(df,index=["sex"], values = ["tip_pc"], aggfunc="mean")
pd.pivot_table(df, index = ["smoker", "sex"], values = ["tip_pc"], aggfunc = "mean")
pd.pivot_table(df,index=["sex"], values = ["total_bill","tip"]... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Bada Bing! | import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
df = pd.read_excel("Data/Sopranos/sopranos-killings.xlsx")
df.head()
pd.pivot_table(df,index=["Cause of Death"],values = ["Season"], aggfunc="first")
pd.pivot_table(df,index=["Cause of Death"],values = ["Season"], aggfunc="coun... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
Basic Statistical Operations/Explorations | import pandas as pd
import numpy as np
df = pd.read_csv("data/cafe_sales2015.csv")
df["Date"] = pd.to_datetime(df["Date"])
df.set_index(["Date"], inplace = True)
df.interpolate(method = "linear", inplace = True)
df.head()
df.tail()
df.describe()
print("Mean\n", df.mean())
print("\n\nMedian\n", df.median())
print("... | 12.Introduction_to_Pandas.ipynb | prasants/pyds | mit |
The backbone of the decision tree algorithms is a criterion (e.g. entropy, Gini, error) with which we can choose the best (in a greedy sense) attribute to add to the tree. ID3 and C4.5 use information gain (entropy) and normalized information gain, respectively. | def weighted_entropy(data, col_num):
entropies = []
n_s = []
entropy_of_attribute = entropy(data[:,col_num])
for value in columns[col_num]:
candidate_child = data[data[:,col_num] == value]
n_s.append(len(candidate_child))
entropies.append(entropy(candidate_child[:,6]))
n_s = ... | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
To store our tree, we wll use dictionaries. Each node of the tree is a Python dict. | def build_node(data, entropy, label, depth, class_="TBD", parent=None):
new_node = dict()
new_node['data'] = data
new_node['entropy'] = entropy
new_node['label'] = label
new_node['depth'] = depth
new_node['class'] = class_
new_node['parent'] = parent
new_node['children'] = []
return ... | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
Functions that helps us build our tree and classify its leaves. find_best_split acts on a node, and returns the attribute that leads to the best (possibly normalized) information gain. | def find_best_split(node, c45 = False):
data = node['data']
entropy = node['entropy']
gains = []
for col_num in range(len(columns) - 1):
new_entropy, entropy_of_attribute = weighted_entropy(data, col_num)
if c45:
if entropy_of_attribute==0:
gains.append(0)
... | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
This function is recursive and will construct a decision tree out of a root node that contains your training data. | def build_tree(node, c45 = False, max_depth = 999, noisy=False):
next_split_attribute = find_best_split(node, c45)
if next_split_attribute == -1 or node['depth'] == max_depth:
node['class'] = classify(node['data'])
#this if statement just handles some printing of the tree (rudimentary visualizat... | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
Lastly, before building the tree, we need a function to check the tree's accuracy. | def correct(decision_tree):
if not decision_tree['children']:
return np.sum(classify(decision_tree['data'])==decision_tree['data'][:,6])
else:
n_correct = 0
for child in decision_tree['children']:
n_correct += correct(child)
return n_correct
correct(root)/1728 | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
Let's make a tree!
But first, a quick look at the class distribution after splitting on safety, an important attribute according to our algorithm | for safety in columns[5]:
plt.hist(data[data[:,5]==safety, 6])
plt.title(safety + " safety")
plt.show()
root = build_node(data, entropy(data[:,6]), "all data", 0)
build_tree(root, max_depth=1, noisy=True)
print("\nTree Accuracy: {}".format(correct(root)/1728))
root = build_node(data, entropy(data[:,6]), ... | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
On this dataset, C4.5 and ID3 get similar accuracies... | print("Training Accuracy Comparison")
print("---------")
print(" ID3 C4.5")
for depth in range(7):
root = build_node(data, entropy(data[:,6]), "all data", 0)
build_tree(root, max_depth=depth, c45=False)
id3=correct(root)/1728
root = build_node(data, entropy(data[:,6]), "all data", 0)
build_tree(r... | decision trees/Decision Trees.ipynb | bbartoldson/examples | mit |
စိတ်ထဲမှာ ပေါ်လာတာကို ကောက်ရေးပြီးတော့ syllable segmentation လုပ်ခိုင်းလိုက်တာပါ။ :)
နောက်ထပ် ဥပမာအနေနဲ့ Wikipedia Myanmar မှာရေးထားတဲ့ အာခီမီးဒီးစ် ရဲ့ အတ္ထုပ္ပတ္တိအကျဉ်း ထဲမှာရေးထားတဲ့
စာကြောင်းတွေကို sylbreak နဲ့ ဖြတ်ကြည့်ရအောင်။ | sylbreak("""အာခီမီးဒီးစ်ကို ဘီစီ ၂၈၇ ခန့်က ရှေးဟောင်း မဂ္ဂနာဂရေစီယာပြည်လက်အောက်ခံ စစ္စလီပြည် ဆိုင်ရာကျူးစ် မြို့ တွင် မွေးဖွားခဲ့သည်။ ဘိုင်ဇန်တိုင်းဂရိခေတ် က သမိုင်းပညာရှင် ဂျွန်ဇီဇီ ၏ မှတ်တမ်းအရ အာခီမီးဒီးစ်သည် အသက် ၇၅ နှစ်အထိ နေထိုင်သွားရကြောင်း သိရသည်။ အာခီမီးဒီးစ်သည် သူ၏ တီထွင်မှု တစ်ခုဖြစ်သော သဲနာရီ နှင့် ပတ်သက်၍ ... | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
Typing order
မြန်မာစာနဲ့ ပတ်သက်တဲ့ NLP (Natural Language Processing) အလုပ် တစ်ခုခု လုပ်ဖို့အတွက် syllable segmentation လုပ်ကြမယ်ဆိုရင် တကယ်တမ်းက မလုပ်ခင်မှာ၊ မြန်မာစာ စာကြောင်းတွေရဲ့ typing order အပါအဝင် တခြား ဖြစ်တတ်တဲ့ အမှားတွေကိုလည်း cleaning လုပ်ရပါတယ်။ အဲဒီလိုမလုပ်ရင် sylbreak က ကျွန်တော် အကြမ်းမျဉ်းသတ်မှတ်ထားတဲ့ ... | sylbreak("ဘီစီ ၂၈၇ ခန့်") | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
တကယ်တန်း မှန်ကန်တဲ့ "ခန့်" ရဲ့ typing order က "ခ န ် ့" (ခခွေး နငယ် အသတ် အောက်မြစ်) ပါ။
အမြင်အားဖြင့်ကတော့ မခွဲနိုင်ပေမဲ့၊ မှန်ကန်တဲ့ typing order နဲ့ ရိုက်ထားရင်တော့ "ခန့်" ဆိုပြီး syllable တစ်ခုအနေနဲ့ ရိုက်ထုတ်ပြပေးပါလိမ့်မယ်။ | sylbreak("ဘီစီ ၂၈၇ ခန့်") | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
နောက်ထပ် typing order အမှားတစ်ခုကို ကြည့်ကြရအောင်။ | sylbreak("ထည့်သွင်းထားသည့်ရုပ်တု") | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
"ညကြီး အောက်မြစ် အသတ်" ဆိုတဲ့ မှားနေတဲ့ အစီအစဉ်ကို "ညကြီး အသတ် အောက်မြစ်" ဆိုပြီး
ပြောင်းရိုက်ပြီးတော့ sylbreak လုပ်ကြည့်ရင်တော့ အောက်ပါအတိုင်း "ထ" နဲ့ "ည့်", "သ" နဲ့ "ည့်" တွေက ကွဲမနေတော့ပဲ မှန်မှန်ကန်ကန်ဖြတ်ပေးပါလိမ့်မယ်။ | sylbreak("ထည့်သွင်းထားသည့်ရုပ်တု") | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
တချို့အမှားတွေကတော့ ဂရုစိုက်ရင် မျက်စိနဲ့ မြင်နိုင်ပါတယ်။
ဥပမာ "ဥ" (အက္ခရာ ဥ) နဲ့ "ဉ" (ညကလေး) ကိုမှားရိုက်တဲ့ကိစ္စပါ။
သို့သော် ကျွန်တော်မြန်မာစာကြောင်းတွေအများကြီးကို ကိုင်တွယ်အလုပ်လုပ်တဲ့အခါတိုင်းမှာ ဒီလိုအမှားက အမြဲတမ်းကို ပါတတ်ပါတယ်။
ဖောင့် (font) မှာလည်း မှန်မှန်ကန်ကန်ခွဲထားမယ်ဆိုရင်၊ အမှန်က ညကလေးဆိုရင် အမြီးက ရှည်... | sylbreak("ကာရီသည်ဒီနှစ်၏ပါရမီရှင်တစ်ဉီးနှင့်ထိုက်တန်သောအမျိုးသမီးအဆိုရှင်ဖြစ်သည်။") | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
ဝီကီပီးဒီးယားက မှားနေတဲ့ "ညကလေး" ကို "အက္ခရာ ဥ" နဲ့ပြန်ပြင်ရိုက်ထားတဲ့ စာကြောင်းနဲ့ နောက်တစ်ခေါက် syllable ဖြတ်ထားတာက အောက်ပါအတိုင်းဖြစ်ပါတယ်။ "ညကလေး" နဲ့ "အက္ခရာ ဥ" အမှားကိစ္စမှာတော့ syllable segmentation ဖြတ်တဲ့အပိုင်းမှာတော့ ထူးထူးခြားခြား အပြောင်းအလဲ မရှိပါဘူး။ | sylbreak("ကာရီသည်ဒီနှစ်၏ပါရမီရှင်တစ်ဦးနှင့်ထိုက်တန်သောအမျိုးသမီးအဆိုရှင်ဖြစ်သည်။") | jupyter-notebook/using-sylbreak-in-jupyter-notebook.ipynb | ye-kyaw-thu/sylbreak | apache-2.0 |
Neural Network
<img style="float: left" src="images/neural_network.png"/>
For the neural network, we'll test on a 3 layer neural network with ReLU activations and an Adam optimizer. The lessons you learn apply to other neural networks, including different activations and optimizers. | # Save the shapes of weights for each layer
print(mnist.train.images.shape[1])
layer_1_weight_shape = (mnist.train.images.shape[1], 256)
layer_2_weight_shape = (256, 128)
layer_3_weight_shape = (128, mnist.train.labels.shape[1]) | tutorials/weight-initialization/weight_initialization.ipynb | liumengjun/cn-deep-learning | mit |
Small scale example | def func(a, b, c):
res = tf.einsum('ijk,ja,kb->iab', a, b, c) + 1
res = tf.einsum('iab,kb->iak', res, c)
return res
a = tf.random_normal((10, 11, 12))
b = tf.random_normal((11, 13))
c = tf.random_normal((12, 14))
# res = func(a, b, c)
orders, optimized_func = tf_einsum_opt.optimizer(func, sess, a, b, c)
re... | example.ipynb | Bihaqo/tf_einsum_opt | mit |
Example with more savings, but slower to optimize | def func(a, b, c, d):
res = tf.einsum('si,sj,sk,ij->s', a, b, d, c)
res += tf.einsum('s,si->s', res, a)
return res
a = tf.random_normal((100, 101))
b = tf.random_normal((100, 102))
c = tf.random_normal((101, 102))
d = tf.random_normal((100, 30))
orders, optimized_func = tf_einsum_opt.optimizer(func, sess, a... | example.ipynb | Bihaqo/tf_einsum_opt | mit |
Look at the recommendations: | orders | example.ipynb | Bihaqo/tf_einsum_opt | mit |
Original Voce-Chaboche model
First we will use RESSPyLab to generate a formatted table of parameters including the relative error metric, $\bar{\varphi}$.
The inputs to this function are:
1. Information about the name of the data set and the load protocols used in the optimization.
2. The file containing the history o... | # Identify the material
material_def = {'material_id': ['Example 1'], 'load_protocols': ['1,5']}
# Set the path to the x log file
x_log_file_1 = './output/x_log.txt'
x_logs_all = [x_log_file_1]
# Load the data
data_files_1 = ['example_1.csv']
data_1 = rpl.load_data_set(data_files_1)
data_all = [data_1]
# Make the tabl... | examples/Post_Processing_Example_1.ipynb | AlbanoCastroSousa/RESSPyLab | mit |
Tables can be easily generated following a standard format for several data sets by appending additional entries to the lists of values in material_def and to x_logs_all and data_all.
Now we will generate the consistency metric, $\xi_2$.
The input arguments are:
1. The parameters of the base case.
2. The parameters of ... | # Load the base parameters, we want the last entry in the file
x_base = np.loadtxt(x_log_file_1, delimiter=' ')
x_base = x_base[-1]
# Load (or set) the sample parameters
x_sample = np.array([179750., 318.47, 100.72, 8.00, 11608.17, 145.22, 1026.33, 4.68])
# Calculate the metric
consistency_metric = rpl.vc_consistency_... | examples/Post_Processing_Example_1.ipynb | AlbanoCastroSousa/RESSPyLab | mit |
The value of $\xi_2 = 65$ %, indicating that the two sets of parameters are inconsistent for this data set.
Updated Voce-Chaboche model
The inputs to generate the tables are the same as for the original model, however the input parameters have to come from optimization using the updated model. | # Identify the material
material_def = {'material_id': ['Example 1'], 'load_protocols': ['1']}
# Set the path to the x log file
x_log_file_2 = './output/x_log_upd.txt'
x_logs_all = [x_log_file_2]
# Load the data
data_files_2 = ['example_1.csv']
data_2 = rpl.load_data_set(data_files_2)
data_all = [data_2]
# Make the ta... | examples/Post_Processing_Example_1.ipynb | AlbanoCastroSousa/RESSPyLab | mit |
I. Loading Labeling Matricies
First we'll load our label matrices from notebook 2 | from snorkel.annotations import LabelAnnotator
labeler = LabelAnnotator()
L_train = labeler.load_matrix(session, split=0)
L_dev = labeler.load_matrix(session, split=1) | tutorials/workshop/Workshop_3_Generative_Model_Training.ipynb | jasontlam/snorkel | apache-2.0 |
Now we set up and run the hyperparameter search, training our model with different hyperparamters and picking the best model configuration to keep. We'll set the random seed to maintain reproducibility.
Note that we are fitting our model's parameters to the training set generated by our labeling functions, while we are... | from lib.scoring import *
majority_vote_score(L_dev, L_gold_dev) | tutorials/workshop/Workshop_3_Generative_Model_Training.ipynb | jasontlam/snorkel | apache-2.0 |
B. Generative Model
In data programming, we use a more sophisitcated model to unify our labeling functions. We know that these labeling functions will not be perfect, and some may be quite low-quality, so we will model their accuracies with a generative model, which Snorkel will help us easily apply.
This will ultimate... | from snorkel.learning import GenerativeModel
from snorkel.learning import RandomSearch, ListParameter, RangeParameter
# use grid search to optimize the generative model
step_size_param = ListParameter('step_size', [0.1 / L_train.shape[0], 1e-5])
decay_param = ListParameter('decay', [0.9, 0.95])
epochs_para... | tutorials/workshop/Workshop_3_Generative_Model_Training.ipynb | jasontlam/snorkel | apache-2.0 |
2. Model Accuracies
These are the weights learned for each LF | L_dev.lf_stats(session, L_gold_dev, gen_model.learned_lf_stats()['Accuracy'])
train_marginals = gen_model.marginals(L_train) | tutorials/workshop/Workshop_3_Generative_Model_Training.ipynb | jasontlam/snorkel | apache-2.0 |
III. Advanced Generative Model Features
A. Structure Learning
We may also want to include the dependencies between our LFs when training the generative model. Snorkel makes it easy to do this! DependencySelector runs a fast structure learning algorithm over the matrix of LF outputs to identify a set of likely dependenc... | from snorkel.learning.structure import DependencySelector
MAX_DEPS = 5
ds = DependencySelector()
deps = ds.select(L_train, threshold=0.1)
deps = set(list(deps)[0:min(len(deps), MAX_DEPS)])
print "Using {} dependencies".format(len(deps)) | tutorials/workshop/Workshop_3_Generative_Model_Training.ipynb | jasontlam/snorkel | apache-2.0 |
Initializing eyDNA object with free_dna.h5 file
eyDNA object is initialized by using the total number of base-pairs and HDF5 file.
This class contains all the required functions to calculate the elastic properties and deformation free energy. | eyDNA = dnaMD.dnaEY(27, 'BST', filename='elasticity_DNA/free_dna.h5') | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Determining modulus matrix - bending, stretching and twisting
Modulus matrix for all three major motions (bending, stretching and twisting) can be obtained with getStrecthTwistBend method.
In the following example, matrix is calculated for all frames and first 5000 frames, respectively. | # All frames
avg, mod_matrix = eyDNA.getStretchTwistBendModulus([4,20], paxis='X')
print('Average values for all frames: ', avg)
print('Modulus matrix for all frames: \n', mod_matrix )
print(' ')
# Elastic matrix
avg, mod_matrix = eyDNA.getStretchTwistBendModulus([4,20], paxis='X', matrix=True)
print('Average values f... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
The elastic matrix is in this form:
$$\text{Elastic matrix} = \begin{bmatrix}
K_{Bx} & K_{Bx,By} & K_{Bx,S} & K_{Bx,T} \
K_{Bx,By} & K_{By} & K_{By,S} & K_{By,T} \
K_{Bx,S} & K_{By,S} & K_{S} & K_{S,T} \
K_{Bx,T} & K_{Bx,T} & K_{S,T} & K_{T}
\end{bmatrix}
$$
Where:
$Bx$ - Bend... | time, modulus = eyDNA.getModulusByTime([4,20], frameGap=500, masked=True)
print('Keys in returned dictionary:\n', '\n'.join(list(modulus.keys())), '\n-----------')
# Stretching modulus
plt.plot(time, modulus['stretch'])
plt.scatter(time, modulus['stretch'])
plt.xlabel('Time (ps)')
plt.ylabel(r'Stretching Modulus (pN)'... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Deformation free energy of bound DNA
Deformation energy of a probe DNA (bound DNA) can be calculated with reference to the DNA present in the current object.
The deformation free energy is calculated using elastic matrix as follows
$$G = \frac{1}{2L_0}\mathbf{xKx^T}$$
$$\mathbf{x} = \begin{bmatrix}
(\t... | # Load parameters of bound DNA
boundDNA = dnaMD.DNA(27, filename='elasticity_DNA/bound_dna.h5') | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Deformation free energy can be calculated for the following motions that can be used with which option.
'full' : Use entire elastic matrix -- all motions with their coupling
'diag' : Use diagonal of elastic matrix -- all motions but no coupling
'b1' : Only bending-1 motion
'b2' : Only bending-2 motion
'stretch' : Only... | # Deformation free energy of bound DNA and calculate all above listed terms
time, energy = eyDNA.getGlobalDeformationEnergy([4,20], boundDNA, paxis='X', which='all', masked=True)
energyTerms=list(energy.keys())
print('Keys in returned dictionary:\n', '\n'.join(energyTerms), '\n-----------')
# Plot two energy terms
fig... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Local elastic properties or stiffness
Local elastic properties can be caluclated using either local base-step parameters or local helical base-step parameters.
In case of base-step parameters: Shift ($Dx$), Slide ($Dy$), Rise ($Dz$), Tilt ($\tau$), Roll ($\rho$) and Twist ($\omega$), following elastic matrix is calcula... | # base-step
avg, matrix = eyDNA.calculateLocalElasticity([10,13], helical=False)
# Print matrix in nice format
out = ''
mean_out = ''
for i in range(matrix.shape[0]):
for j in range(matrix.shape[0]):
if j != matrix.shape[0]-1:
out += '{0:>10.5f} '.format(matrix[i][j])
else:
... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Local deformation energy of a local small segment
Using the above elastic matrix, deformation energy of this base-step in bound DNA can be calucalted. | # Here calculate energy for one base-step
time, energy = eyDNA.getLocalDeformationEnergy([10,13], boundDNA, helical=False, which='all')
energyTerms=list(energy.keys())
print('Keys in returned dictionary:\n', '\n'.join(energyTerms), '\n-----------')
# Plot two energy terms
fig = plt.figure(figsize=(8,8))
fig.subplots_a... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Deformation energy of the consecutive overlapped DNA segments
Above method gives energy of a small local segment of the DNA. However, we mostly interested in large segment of the DNA. This large segment can be further divided into smaller local segments. For these smaller segments local deformation energy can be calcul... | # First calculation for local base-step parameters
segments, energies, error = eyDNA.getLocalDeformationEnergySegments([4,20], boundDNA, span=4,
helical=False, which='all',
err_type='... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Same as the above but energy is calculated using helical base-step parameters | # Secind calculation for local base-step parameters
segments, energies, error = eyDNA.getLocalDeformationEnergySegments([4,20], boundDNA, span=4,
helical=True, which='all',
err_type='... | docs/notebooks/calculate_elasticity_tutorial.ipynb | rjdkmr/do_x3dna | gpl-3.0 |
Creating the training set
In order to learn the relationship between ACSFs and the energy of the system, we need a database of ACSFs for several atomic configurations, and the corresponding energy.
The sample configurations consist of the dimer, stretched and compressed. In reality the energy is calculated with quantum... | # array of meaningful distances
dists = numpy.arange(1.95, Rcut, Rcut/30)
# LJ energy at those distances
energy = numpy.power(dists/2,-12)-numpy.power(dists/2,-6) - 2
plt.plot(dists, energy,'.' )
plt.xlabel('Pair distance')
plt.ylabel('Energy')
plt.show() | ACSF-Dimer.ipynb | fullmetalfelix/ML-CSC-tutorial | gpl-3.0 |
Then we calculate the ACSFs for each dimer configuration. The results are formatted as a matrix: one row for each configuration, one column for each ACSF. | # ACSFs G1 parameter pairs: this is a list of eta/Rs values
params = [(0.4, 0.2),(0.4, 0.5)]
# initialise a matrix that will store the ACSFs of the first atom in all dimer configurations
nConfs = dists.shape[0]
acsf = numpy.zeros((nConfs, 1+len(params)))
print("Number of configurations: " + str(nConfs))
print("Number... | ACSF-Dimer.ipynb | fullmetalfelix/ML-CSC-tutorial | gpl-3.0 |
OPTIONAL TRICK
We can center the ACSFs around their mean and rescale them so that their standard deviation is 1. This is a common trick in ML with neural networks, to make the learning easier. | acsf_mean = numpy.mean(acsf, axis=0)
for a in range(acsf.shape[1]):
acsf[:,a] -= acsf_mean[a]
acsf_std = numpy.std(acsf, axis=0)
for a in range(acsf.shape[1]):
acsf[:,a] /= acsf_std[a]
# plot the Gs as a function of distance
for a in range(acsf.shape[1]):
plt.plot(dists, acsf[:,a])
plt.xlabel('Pair distanc... | ACSF-Dimer.ipynb | fullmetalfelix/ML-CSC-tutorial | gpl-3.0 |
Training
We create a neural network and train it on the ACSF database we just constructed. | # setup the neural network
# the network uses tanh function on all hidden neurons
nn = MLPRegressor(hidden_layer_sizes=(5,), activation='tanh') | ACSF-Dimer.ipynb | fullmetalfelix/ML-CSC-tutorial | gpl-3.0 |
The fitting may not be trivial since our database is small... the next instruction can be executed multiple times let the NN train more and hopefully improve. | # change some training parameters
nn.set_params(solver='lbfgs', alpha=0.001, tol=1.0e-10, learning_rate='constant', learning_rate_init=0.01)
# do some training steps
nn.fit(acsf, energy);
# evaluate the training error
energyML = nn.predict(acsf)
print ("Mean Abs Error (training) : ", (numpy.abs(energyML-energy)).mea... | ACSF-Dimer.ipynb | fullmetalfelix/ML-CSC-tutorial | gpl-3.0 |
Remarks
Do not be fooled! Real systems are much more difficult to model, requiring more ACSFs, larger NNs, and much larger datasets for training.
Exercises
1. Create a vaidation set and test the NN performance
For simplicity we just checked the error on training data, but it is better to check performance on a validati... | # atomic positions as matrix
molxyz = numpy.load("./data/molecule.coords.npy")
# atom types
moltyp = numpy.load("./data/molecule.types.npy")
atoms_sys = Atoms(positions=molxyz, numbers=moltyp)
view(atoms_sys)
from dscribe.descriptors import ACSF
# Setting up the ACSF descriptor
acsf = ACSF(
species=["H", "C", "... | ACSF-Dimer.ipynb | fullmetalfelix/ML-CSC-tutorial | gpl-3.0 |
Scikit-Learn
Scikit-Learn (http://scikit-learn.org) is a python package that uses NumPy & SciPy to enable the application of popular machine learning algorithms up on small to medium datasets.
Referring back to the machine learning models, every model in scikit is a python class with a uniform interface. Every instance... | %matplotlib inline
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import numpy as np
X, y = make_blobs(n_samples=200,n_features=2,centers=6,cluster_std=0.8, shuffle=True,random_state=0)
plt.scatter(X[:,0],X[:,1]) | session-3/HPC-2016-Session-III-Supervised-Unsupervised-Learning.ipynb | stanfordhpccenter/datatutorial | mit |
Steps in the K-means algorithm:
Choose k centroids from the sample points as initial cluster centers.
Assign each data point to the nearest centroid (based on Euclidean distance).
Update the centroid locations to the mean of the samples that were assigned to it.
Repeat steps 2 and 3 till the cluster assignments do not... | #import Kmeans class for the cluster module
from sklearn.cluster import KMeans
#instantiate the model
km = KMeans(n_clusters=3, init='random', n_init=10, max_iter=300, tol=1e-04, random_state=0) | session-3/HPC-2016-Session-III-Supervised-Unsupervised-Learning.ipynb | stanfordhpccenter/datatutorial | mit |
The arguments to the algorithm:
* n_clusters: The number of groups to be divided in.
* n_init: The number of different initial random centroids to be run.
* max_iter: The maximum number of iterations for each single run.
* tol: Cut-off for the changes in the within-cluster sum-squared-error. | #fitting the model to the data
y_km = km.fit_predict(X)
plt.scatter(X[y_km==0,0], X[y_km ==0,1], s=50, c='lightgreen', marker='o', label='Group A')
plt.scatter(X[y_km ==1,0], X[y_km ==1,1], s=50, c='orange', marker='o', label='Group B')
plt.scatter(X[y_km ==2,0], X[y_km ==2,1], s=50, c='white', marker='o', label='Gro... | session-3/HPC-2016-Session-III-Supervised-Unsupervised-Learning.ipynb | stanfordhpccenter/datatutorial | mit |
Exercise 2
Clustering the iris dataset based on sepal and petal lengths and widths. | display(Image(filename='1.png'))
from sklearn.datasets import load_iris
iris = load_iris()
n_samples, n_features = iris.data.shape
X, y = iris.data, iris.target
f, axarr = plt.subplots(2, 2)
axarr[0, 0].scatter(iris.data[:, 0], iris.data[:, 1],c=iris.target, cmap=plt.cm.get_cmap('RdYlBu', 3))
axarr[0, 0].set_title('S... | session-3/HPC-2016-Session-III-Supervised-Unsupervised-Learning.ipynb | stanfordhpccenter/datatutorial | mit |
Regression | x=np.arange(100)
eps=50*np.random.randn(100)
y=2*x+eps
plt.scatter(x,y)
plt.xlabel("X")
plt.ylabel("Y")
from sklearn.linear_model import LinearRegression
model=LinearRegression(normalize=True)
X=x[:,np.newaxis]
model.fit(X,y)
X_fit=x[:,np.newaxis]
y_pred=model.predict(X_fit)
plt.scatter(x,y)
plt.plot(X_fit,y_pred,l... | session-3/HPC-2016-Session-III-Supervised-Unsupervised-Learning.ipynb | stanfordhpccenter/datatutorial | mit |
Exercise 3
Linear Regression over a multi-dimensional data set. The data exhibits the advertising expenditure over TV, radio and the print media, versus the change in sales of the product. | import pandas as pd
data=pd.read_csv('addata.csv', index_col=0)
data.head(5)
#from sklearn.linear_model import LinearRegression
from sklearn import linear_model
clf=linear_model.LinearRegression()
feature_cols=["TV","Radio","Newspaper"]
X=data[feature_cols]
y=data["Sales"]
from sklearn.cross_validation import train... | session-3/HPC-2016-Session-III-Supervised-Unsupervised-Learning.ipynb | stanfordhpccenter/datatutorial | mit |
Define backend (here are implemented: caffe and torch) | backend = 'caffe' | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
Load a caffe model | if backend == 'caffe':
# make sure pycaffe is in your system path
caffe_root = os.getenv("HOME") + '/caffe/'
sys.path.insert(0, caffe_root + 'python')
# Load CaffeAdapter class
from emu.caffe import CaffeAdapter
# Define the path to .caffemodel, deploy.prototxt and mean.npy
# Here... | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
Load a torch model | if backend == 'torch':
# Load TorchAdapter class
from emu.torch import TorchAdapter
# Define the path to the model file where the file can be a torch7 or pytorch model.
# Torch7 models are supported but not well tested.
model_fp = 'models/resnet-18.t7'
# Alternatively, we can use pret... | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
Load available layers and their types | layer_types = adapter.get_layers()
for lname, ltype in layer_types.items():
print('%s:\t%s' % (lname, ltype)) | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
Select convolutional layers | conv_layers = [lname for lname, ltype in layer_types.items() if 'conv' in ltype.lower()] | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
2. Forward images through network
Define path to a directory containing images and run them through the network | images_dp = 'images/'
files = os.listdir(images_dp)
# Filter for jpeg extension
image_files = [os.path.join(images_dp, f) for f in files if f.endswith('.jpg')]
# Run in batched fashion
batch_size = 32
# As we run in batch mode, we have to store the intermediate layer outputs
layer_outputs = OrderedDict()
for layer i... | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
3. Calculate summary statistics
Estimate mean and standard deviation per layer | means = [output.mean() for output in layer_outputs.values()]
stds = [output.std() for output in layer_outputs.values()]
plt.plot(means)
plt.xticks(range(len(conv_layers)), conv_layers, rotation=45.0)
plt.title('Convolution output mean over network depth');
plt.xlabel('Layer');
plt.plot(stds)
plt.xticks(range(len(con... | examples/summary_statistics.ipynb | mlosch/nnadapter | mit |
Create an example dataframe | raw_data = {'geo': ['40.0024, -105.4102', '40.0068, -105.266', '39.9318, -105.2813', np.nan]}
df = pd.DataFrame(raw_data, columns = ['geo'])
df | python/pandas_split_lat_and_long_into_variables.ipynb | tpin3694/tpin3694.github.io | mit |
Split the geo variable into seperate lat and lon variables | # Create two lists for the loop results to be placed
lat = []
lon = []
# For each row in a varible,
for row in df['geo']:
# Try to,
try:
# Split the row by comma and append
# everything before the comma to lat
lat.append(row.split(',')[0])
# Split the row by comma and append
... | python/pandas_split_lat_and_long_into_variables.ipynb | tpin3694/tpin3694.github.io | mit |
View the dataframe | df | python/pandas_split_lat_and_long_into_variables.ipynb | tpin3694/tpin3694.github.io | mit |
3 DOF System
<img src="bending.svg" style="width:100%">
In the figure above
<ol type='a'>
<li> the system under investigation, with the two supported masses and
the dynamical degrees of freedom that describe the system deformation
(top left);
<li> the three diagrams of bending moment (in red positive bend... | bm = [[p(( 1, 0)), p(( 1, 1)), p(( 1, 2)), p(( 3, 0)), p(( 0, 0))],
[p(( 0, 0)), p(( 0, 0)), p(( 1, 0)), p(( 1, 0)), p(( 0, 0))],
[p(( 0, 0)), p(( 0,-1)), p(( 0,-1)), p((-1, 0)), p((-1, 0))]] | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
To compute the flexibilities we sum the integrals of the products of bending moments on each of the five spans of unit length that we are using and place the results in a 2D data structure that is eventually converted to a matrix by np.mat. | F = np.mat([[sum(polyint(bm0[i]*bm1[i])(1) for i in range(5))
for bm1 in bm] for bm0 in bm])
print('F = 1/6 * L^3/EJ *')
print(F*6) | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
we invert the flexibility matrix to obtain the stiffness matrix | K = F.I
print('K = 3/136 * EJ/L^3 *')
print(K*136/3) | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
and eventually we define the mass matrix | M = np.mat(np.eye(3)) ; M[2,2]=2
print('M = m *')
print (M)
evals, evecs = eigh(K,M)
print("Eigenvalues, w_0^2 *", evals)
for i in range(3):
if evecs[0,i]<0: evecs[:,i]*=-1
print("Matrix of mass normalized eigenvectors,")
print(evecs) | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
The Load
The load is $F_0\,\boldsymbol{r}\,f(t)$ with $F_0 = \delta EJ/L^3$, $\boldsymbol{r}=\begin{Bmatrix}1&0&0\end{Bmatrix}^T$ and
$f(t) = 2\sin^2(\omega_0t/2)=1-\cos(\omega_0t)$ for $0\le \omega_0 t\le 2\pi$ while $f(t)=0$ otherwise. | pi = np.pi
t1 = np.linspace(0,2*pi,601)
plt.plot(t1,1-np.cos(t1))
plt.xlabel(r'$\omega_0t$', size=20)
plt.ylabel(r'$p(t)\,\frac{L^3}{\delta\,EJ}$', size=20)
plt.xlim((0,2*pi))
plt.ylim((-0.05,2.05))
plt.xticks((0,pi/2,pi,pi*1.5,2*pi),
(r'$0$', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$'), fontsize=20)
... | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
The Particular Integrals
For our load, each modal equation of motion can be written as
\begin{align}
m \ddot q_i + m \Lambda_i^2\omega_0^2 q_i &=
\delta\frac{EJ}{L^3}\boldsymbol\psi_i^T\boldsymbol{r}\,
(1-\cos(\omega_0t))\Rightarrow\
\ddot q_i + \Lambda_i^2\omega_0^2 q_i &= G_i \delta\omega_0^2 \,
(1-... | r = np.array((1,0,0))
w = np.sqrt(evals)
C = np.dot(evecs.T,r)/evals
D = np.dot(evecs.T,r)/(1-evals)
display(Latex(r'\begin{align}' +
r'\\'.join(r"""
\frac{\xi_%d(t)}\delta &= %+g %+g \cos(\omega_0 t),
&& \text{for } 0 \le \omega_0 t \le 2\pi.
""" % (i+1,C[i],D[i]... | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
Modal Responses
With respect to the forced phase, the modal responses have the generic expression
\begin{align}
q_i(t) & = A_i\cos(\Lambda_i\omega_0t)
+ B_i\sin(\Lambda_i\omega_0t) + C_i + D_i\cos(\omega_0t),\
\dot q_i(t) & = \Lambda_i\omega_0 \left(
B_i\cos(\Lambda_i\omega_0t) - A_i\sin(\Lambd... | A = -C - D
L = np.sqrt(evals)
t1 = np.linspace(0,2*pi,601)
q1 = [A[i]*np.cos(L[i]*t1) + C[i] + D[i]*np.cos(t1) for i in (0,1,2)]
display(Latex(r'\begin{align}' +
r'\\'.join(r"""
\frac{q_%d(t)}\delta &= %+g %+g \cos(\omega_0 t) %+g \cos(%g\omega_0t), &&
\text{for } 0 \le \omega_0 t \le 2\pi.
... | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
With respect to the free response phase, $2\pi \le \omega_0t$, writing
$$
q^_i(t) = A^_i \cos(\Lambda_i\omega_0t) + B^*_i \sin(\Lambda_i\omega_0t)
$$
imposing the continuity of modal displacements and modal velocities we have
\begin{align}
q_i(t_1) &= A^_i \cos(\Lambda_i\omega_0t_1) + B^_i \sin(\Lambda_i\omega_0t... | ct1 = np.cos(L*2*pi)
st1 = np.sin(L*2*pi)
q0t1 = C + D*np.cos(2*pi) + A*ct1
q1t1 = - D*np.sin(2*pi) - A*st1*L
print(q0t1, q1t1)
As = (q0t1*L*ct1 - q1t1*st1)/L
Bs = (q0t1*L*st1 + q1t1*ct1)/L
print(As*ct1+Bs*st1, L*(Bs*ct1-As*st1))
t2 = np.linspace(2*pi, 4*pi, 601)
q2 = [As[i]*np.cos(L[i]*t2) + Bs[i]*np.sin(L[i]*t2)... | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
Plotting the modal responses
Let's plot the modal responses, first one by one, to appreciate the details of the single modal response | for i in (0,1,2):
plt.plot(t1/pi,q1[i], color=l_colors[i],
label='$q_{%d}(t)$'%(i+1))
plt.plot(t2/pi,q2[i], color=l_colors[i])
plt.xlabel(r'$\omega_0t/\pi$', fontsize=18)
plt.ylabel(r'$q/\delta$', fontsize=18)
plt.legend(loc=0, fontsize=18)
plt.show() | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
then all of them in a single plot, to appreciate the relative magnutudes of the different modal responses | for i in (0,1,2):
plt.plot(t1/pi,q1[i], color=l_colors[i],
label='$q_{%d}(t)$'%(i+1))
plt.plot(t2/pi,q2[i], color=l_colors[i])
plt.xlabel(r'$\omega_0t/\pi$', fontsize=18)
plt.ylabel(r'$q/\delta$', fontsize=18)
plt.legend(loc=0, fontsize=18)
plt.show() | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
System Response in Natural Coordinates
We stack together the times and the modal responses for the forced and the free phases in two single vectors, then we compute the nodal response by premultiplying the modal response by the eigenvectors matrix | t = np.hstack((t1, t2))
q = np.hstack((q1, q2))
x = np.dot(evecs, q) | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
Plotting of the natural coordinate responses
All of them in a single plot, as they have the same order of magnitude | for i in (0,1,2): plt.plot(t/pi,x[i],
label='$x_{%d}(t)$'%(i+1))
plt.xlabel(r'$\omega_0t/\pi$', fontsize=18)
plt.ylabel(r'$x/\delta$', fontsize=18)
plt.legend(loc=0, fontsize=18)
plt.show() | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
Final Displacements and Final Velocities
Say that $t_2=4\pi/\omega_0$, we compute the vectors of sines and cosines with different frequencies at $t_2$, then we compute the modal displacements and velocities (note that the dimensional velocities are these adimensional velocities multiplied by $\omega_0\,\delta$) and eve... | ct2 = np.cos(L*4*pi)
st2 = np.sin(L*4*pi)
q0t2 = As*ct2+Bs*st2 ; q1t2 = L*(Bs*ct2-As*st2)
display(Latex(r"$\boldsymbol x(t_2) = \{"+
",".join("%10.6f"%x for x in np.dot(evecs,q0t2))+
"\}\,\delta$"))
display(Latex(r"$\boldsymbol v(t_2) = \{"+
",".join("%10.6f"%x for x in np.do... | dati_2015/ha03/06_3_DOF_System.ipynb | boffi/boffi.github.io | mit |
Mission Fire Exploration
At the time that this was created, there is a lot of press going on right now about Mission district fires, and gossip that maybe it's the cause of landlords or some arsonist trying to get more money for older properties. This notebook captures some
initial thoughts about this.
This exploration... | query_url = 'https://data.sfgov.org/resource/wbb6-uh78.json?$order=close_dttm%20DESC&$offset={}&$limit={}'
# query_url = "https://data.sfgov.org/resource/wbb6-uh78.json?$where=alarm_dttm>='2013-02-12 04:52:17'&$order=close_dttm%20DESC"
# query_url = "https://data.sfgov.org/resource/wbb6-uh78.json?$where=alarm_dttm>='20... | notebooks/exploratory/0.8-mission-fire-exploration-revisited.ipynb | mikezawitkowski/fireRiskSF | mit |
Ch-Ch-Ch-Changes
Data which can be modified in place is called mutable, while data which cannot be modified is called immutable. Strings and numbers are immutable. This does not mean that variables with string or number values are constants, but when we want to change the value of a string or number variable, we can on... | odds.append(11)
print('odds after adding a value:', odds)
del odds[0]
print('odds after removing the first element:', odds)
odds.reverse()
print('odds after reversing:', odds) | 02-Python1/02-Python-1-Lists_Instructor.ipynb | OpenAstronomy/workshop_sunpy_astropy | mit |
This is because python stores a list in memory, and then can use multiple names to refer to the same list. If all we want to do is copy a (simple) list, we can use the list() command, so we do not modify a list we did not mean to: | odds = [1, 3, 5, 7]
primes = list(odds)
primes += [2]
print('primes:', primes)
print('odds:', odds) | 02-Python1/02-Python-1-Lists_Instructor.ipynb | OpenAstronomy/workshop_sunpy_astropy | mit |
First, create a set of views to limit the individual indicators to one record per county. The Ambry SQL parser is
ver simplistic, and can't handle anything mroe then very simple joins. | w = b.warehouse('hci_counties')
w.clean()
print w.dsn
w.query("""
-- Get only counties in California
CREATE VIEW geo AS SELECT gvid, name AS county_name, geometry FROM census.gov-tiger-2015-counties
WHERE statefp = 6;
-- Get only records for all race/ethinicities
CREATE VIEW hf_total AS SELECT gvid, mrfei FROM cdph.... | test/bundle_tests/build.example.com/classification/Using SQL JOINS.ipynb | CivicKnowledge/ambry | bsd-2-clause |
Now we can run a query to join the indicators. | sql="""
SELECT county_name, mrfei, pm25_concentration, percent as percent_poverty FROM geo as counties
JOIN hf_total ON hf_total.gvid = counties.gvid
JOIN aq_total ON aq_total.gvid = counties.gvid
JOIN pr_total ON pr_total.gvid = counties.gvid;
"""
df = w.dataframe(sql)
df.head()
df.corr() | test/bundle_tests/build.example.com/classification/Using SQL JOINS.ipynb | CivicKnowledge/ambry | bsd-2-clause |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.