markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Initialize service code data structures
Service code / service name map
Service code histogram | h_file = open("./serviceCodesCount.tsv","r")
code_name_map = {}
code_histogram = {}
patternobj = re.compile('^([0-9a-z]+)\s\|\s([0-9a-z\s]+)$')
for fields in csv.reader(h_file, delimiter="\t"):
matchobj = patternobj.match(fields[0])
cur_code = matchobj.group(1)
code_name_map[cur_... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Plot Cincinnati 311 Service Code Statistics
References
Descending Array Sort
Change Plot Font Size | total_count_fraction = code_histogram.values()
total_count_fraction.sort()
total_count_fraction = total_count_fraction[::-1]
total_count_fraction /= np.sum(total_count_fraction)
total_count_fraction = np.cumsum(total_count_fraction)
sns.set(font_scale=2)
f,h_ax = plt.subplots(1,2,figsize=(12,6))
h_ax[0].bar(range(0,l... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Cluster service code names
Compute Term Frequency Inverse Document Frequency (TF-IDF) feature vectors
Apply the K-means algorithm to cluster service code names based on their TF-IDF feature vector
References:
Rose, B. "Document Clustering in Python"
Text pre-processing to reduce dictionary size | from nltk.stem.snowball import SnowballStemmer
def tokenize(text):
""" Extracts unigrams (i.e. words) from a string that contains
a service code name.
Args:
text: String that stores a service code name
Returns:
filtered_tokens: List of words contained in a service code name"""... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Apply a word tokenizer to the service names and construct a TF-IDF feature matrix | params = {'maxdocumentfrequency': 0.25,
'mindocumentcount': 10}
(tfidf_matrix,
tfidf_vectorizer) = compute_tfidf_features(code_name_map,
tokenize,
params)
print "# of terms: %d" % (tfidf_matrix.shape[1])
print tfidf_vec... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Apply the K-means algorithm to cluster the Cincinnati 311 service names based on their TF-IDF feature vector | num_clusters = 20
kmeans_seed = 3806933558
(clusterid_code_map,
clusterid_name_map) = cluster_311_services(tfidf_matrix,
num_clusters,
kmeans_seed)
clusterid_total_count =\
compute_clusterid_totalcounts(clusterid_code_map,
... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Plot the service code histogram for the maximum size cluster | eval_maxcount_clusterid(clusterid_code_map,
clusterid_total_count,
code_histogram) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Apply a word tokenizer (with stemming) to the service names and construct a TF-IDF feature matrix | params = {'maxdocumentfrequency': 0.25,
'mindocumentcount': 10}
(tfidf_matrix,
tfidf_vectorizer) = compute_tfidf_features(code_name_map,
tokenize_and_stem,
params)
print "# of terms: %d" % (tfidf_matrix.shape[1])
print ... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Apply the K-means algorithm to cluster the Cincinnati 311 service names based on their TF-IDF feature vector | num_clusters = 20
kmeans_seed = 3806933558
(clusterid_code_map,
clusterid_name_map) = cluster_311_services(tfidf_matrix,
num_clusters,
kmeans_seed)
clusterid_total_count =\
compute_clusterid_totalcounts(clusterid_code_map,
... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Create a separate service name(s) cluster for the 'mtlfrn' service code | add_new_cluster(1,
'mtlfrn',
clusterid_total_count,
clusterid_code_map,
clusterid_name_map) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Evaluate the service name(s) cluster statistics | clusterid_total_count =\
compute_clusterid_totalcounts(clusterid_code_map,
code_histogram)
print_cluster_stats(clusterid_name_map,
clusterid_total_count) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Create a separate service name(s) cluster for the 'ydwstaj' service code | add_new_cluster(1,
'ydwstaj',
clusterid_total_count,
clusterid_code_map,
clusterid_name_map) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Create a separate service name(s) cluster for the 'grfiti' service code | add_new_cluster(1,
'grfiti',
clusterid_total_count,
clusterid_code_map,
clusterid_name_map) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Create a separate service name(s) cluster for the 'dapub1' service code | add_new_cluster(1,
'dapub1',
clusterid_total_count,
clusterid_code_map,
clusterid_name_map) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Evaluate the service name(s) cluster statistics | clusterid_total_count =\
compute_clusterid_totalcounts(clusterid_code_map,
code_histogram)
print_cluster_stats(clusterid_name_map,
clusterid_total_count)
plot_cluster_stats(clusterid_code_map,
clusterid_total_count) | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Label each service name(s) cluster | cur_clusterid = 0
clusterid_category_map = {}
clusterid_category_map[cur_clusterid] = 'streetmaintenance'
print_clustered_servicenames(cur_clusterid,
clusterid_name_map)
cur_clusterid += 1
clusterid_category_map[cur_clusterid] = 'miscellaneous'
print_clustered_servicenames(cur_clusterid... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Plot Cincinnati 311 Service Name Categories | import pandas as pd
category_totalcountdf =\
pd.DataFrame({'totalcount': clusterid_total_count.values()},
index=clusterid_category_map.values())
sns.set(font_scale=1.5)
category_totalcountdf.plot(kind='barh') | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Write service code / category map to disk
Storing Python Dictionaries | servicecode_category_map = {}
for clusterid in clusterid_name_map.keys():
cur_category = clusterid_category_map[clusterid]
for servicecode in clusterid_code_map[clusterid]:
servicecode_category_map[servicecode] = cur_category
with open('serviceCodeCategory.txt', 'w') as fp:
num_names... | ClusterServiceCodes.ipynb | mspcvsp/cincinnati311Data | gpl-3.0 |
Tabular data | df = pd.read_csv("data/titanic.csv")
df.head() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Starting from reading this dataset, to answering questions about this data in a few lines of code:
What is the age distribution of the passengers? | df['Age'].hist() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
How does the survival rate of the passengers differ between sexes? | df.groupby('Sex')[['Survived']].aggregate(lambda x: x.sum() / len(x)) | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Or how does it differ between the different classes? | df.groupby('Pclass')['Survived'].aggregate(lambda x: x.sum() / len(x)).plot(kind='bar') | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Are young people more likely to survive? | df['Survived'].sum() / df['Survived'].count()
df25 = df[df['Age'] <= 25]
df25['Survived'].sum() / len(df25['Survived']) | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
All the needed functionality for the above examples will be explained throughout this tutorial.
Data structures
Pandas provides two fundamental data objects, for 1D (Series) and 2D data (DataFrame).
Series
A Series is a basic holder for one-dimensional labeled data. It can be created much as a NumPy array is created: | s = pd.Series([0.1, 0.2, 0.3, 0.4])
s | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Attributes of a Series: index and values
The series has a built-in concept of an index, which by default is the numbers 0 through N - 1 | s.index | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
You can access the underlying numpy array representation with the .values attribute: | s.values | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
We can access series values via the index, just like for NumPy arrays: | s[0] | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Unlike the NumPy array, though, this index can be something other than integers: | s2 = pd.Series(np.arange(4), index=['a', 'b', 'c', 'd'])
s2
s2['c'] | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
In this way, a Series object can be thought of as similar to an ordered dictionary mapping one typed value to another typed value.
In fact, it's possible to construct a series directly from a Python dictionary: | pop_dict = {'Germany': 81.3,
'Belgium': 11.3,
'France': 64.3,
'United Kingdom': 64.9,
'Netherlands': 16.9}
population = pd.Series(pop_dict)
population | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
We can index the populations like a dict as expected: | population['France'] | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
but with the power of numpy arrays: | population * 1000 | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
DataFrames: Multi-dimensional Data
A DataFrame is a tablular data structure (multi-dimensional object to hold labeled data) comprised of rows and columns, akin to a spreadsheet, database table, or R's data.frame object. You can think of it as multiple Series object which share the same index.
<img src="img/dataframe.pn... | data = {'country': ['Belgium', 'France', 'Germany', 'Netherlands', 'United Kingdom'],
'population': [11.3, 64.3, 81.3, 16.9, 64.9],
'area': [30510, 671308, 357050, 41526, 244820],
'capital': ['Brussels', 'Paris', 'Berlin', 'Amsterdam', 'London']}
countries = pd.DataFrame(data)
countries | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Attributes of the DataFrame
A DataFrame has besides a index attribute, also a columns attribute: | countries.index
countries.columns | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
To check the data types of the different columns: | countries.dtypes | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
An overview of that information can be given with the info() method: | countries.info() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Also a DataFrame has a values attribute, but attention: when you have heterogeneous data, all values will be upcasted: | countries.values | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
If we don't like what the index looks like, we can reset it and set one of our columns: | countries = countries.set_index('country')
countries | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
To access a Series representing a column in the data, use typical indexing syntax: | countries['area'] | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Basic operations on Series/Dataframes
As you play around with DataFrames, you'll notice that many operations which work on NumPy arrays will also work on dataframes. | # redefining the example objects
population = pd.Series({'Germany': 81.3, 'Belgium': 11.3, 'France': 64.3,
'United Kingdom': 64.9, 'Netherlands': 16.9})
countries = pd.DataFrame({'country': ['Belgium', 'France', 'Germany', 'Netherlands', 'United Kingdom'],
'population': [11.3, 64.3, 8... | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Elementwise-operations (like numpy)
Just like with numpy arrays, many operations are element-wise: | population / 100
countries['population'] / countries['area'] | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Alignment! (unlike numpy)
Only, pay attention to alignment: operations between series will align on the index: | s1 = population[['Belgium', 'France']]
s2 = population[['France', 'Germany']]
s1
s2
s1 + s2 | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Reductions (like numpy)
The average population number: | population.mean() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
The minimum area: | countries['area'].min() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
For dataframes, often only the numeric columns are included in the result: | countries.median() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
<div class="alert alert-success">
<b>EXERCISE</b>: Calculate the population numbers relative to Belgium
</div> | population / population['Belgium'].mean() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
<div class="alert alert-success">
<b>EXERCISE</b>: Calculate the population density for each country and add this as a new column to the dataframe.
</div> | countries['population']*1000000 / countries['area']
countries['density'] = countries['population']*1000000 / countries['area']
countries | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Some other useful methods
Sorting the rows of the DataFrame according to the values in a column: | countries.sort_values('density', ascending=False) | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
One useful method to use is the describe method, which computes summary statistics for each column: | countries.describe() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
The plot method can be used to quickly visualize the data in different ways: | countries.plot() | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
However, for this dataset, it does not say that much: | countries['population'].plot(kind='bar') | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
You can play with the kind keyword: 'line', 'bar', 'hist', 'density', 'area', 'pie', 'scatter', 'hexbin'
Importing and exporting data
A wide range of input/output formats are natively supported by pandas:
CSV, text
SQL database
Excel
HDF5
json
html
pickle
... | pd.read
states.to | solved - 02 - Data structures.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Exercise 1: Finding Correlations of Non-Linear Relationships
a. Traditional (Pearson) Correlation
Find the correlation coefficient for the relationship between x and y. | n = 100
x = np.linspace(1, n, n)
y = x**5
#Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
b. Spearman Rank Correlation
Find the Spearman rank correlation coefficient for the relationship between x and y using the stats.rankdata function and the formula
$$r_S = 1 - \frac{6 \sum_{i=1}^n d_i^2}{n(n^2 - 1)}$$
where $d_i$ is the difference in rank of the ith pair of x and y values. | #Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
Check your results against scipy's Spearman rank function. stats.spearmanr | # Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
Exercise 2: Limitations of Spearman Rank Correlation
a. Lagged Relationships
First, create a series b that is identical to a but lagged one step (b[i] = a[i-1]). Then, find the Spearman rank correlation coefficient of the relationship between a and b. | n = 100
a = np.random.normal(0, 1, n)
#Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
b. Non-Monotonic Relationships
First, create a series d using the relationship $d=10c^2 - c + 2$. Then, find the Spearman rank rorrelation coefficient of the relationship between c and d. | n = 100
c = np.random.normal(0, 2, n)
#Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
Exercise 3: Real World Example
a. Factor and Forward Returns
Here we'll define a simple momentum factor (model). To evaluate it we'd need to look at how its predictions correlate with future returns over many days. We'll start by just evaluating the Spearman rank correlation between our factor values and forward return... | #Pipeline Setup
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor, Returns, RollingLinearRegressionOfReturns
from quantopian.pipeline.classifiers.morningstar import Secto... | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
b. Rolling Spearman Rank Correlation
Repeat the above correlation for the first 60 days in the dataframe as opposed to just a single day. You should get a time series of Spearman rank correlations. From this we can start getting a better sense of how the factor correlates with forward returns.
What we're driving toward... | rolling_corr = pd.Series(index=None, data=None)
#Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
b. Rolling Spearman Rank Correlation
Plot out the rolling correlation as a time series, and compute the mean and standard deviation. | # Your code goes here | notebooks/lectures/Spearman_Rank_Correlation/questions/notebook.ipynb | quantopian/research_public | apache-2.0 |
Loading Data
First, we load the data. NOTE, targets with the starting name of PSR are radio scans of known pulsars PSR_B0355+54_0013. But, files with HIP65960 cataloged targets that shouldn't have pulsar characteristics. If you wish to learn more about the data check out https://ui.adsabs.harvard.edu/abs/2019PASP..131... | from blimpy import Waterfall
import pylab as plt
import numpy as np
import math
from scipy import stats, interpolate
from copy import deepcopy
%matplotlib inline
obs = Waterfall('/content/spliced_blc40414243444546o7o0515253545556o7o061626364656667_guppi_58837_86186_PSR_B0355+54_0013.gpuspec.8.0001.fil',
... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
Band Pass Removal
The goal of this process is to clean the data of its artifacts created by combining multiple bands. Our data is created by taking sliding windows of the raw voltage data and computing an FFT of that sliding window. With these FFTs (each containing frequency information about a timestamp) for each coa... | average_power = np.zeros((data.shape[2]))
shifted_power = np.zeros((int(data.shape[2]/8)))
x=[]
spl_order = 2
print("Fitting Spline")
data_adjust = np.zeros(data.shape)
average_power = data.mean(axis=0)
# Note the value 8 is the COARSE CHANNEL WIDTH
# We adjust each coarse channel to correct the bandpass artifacts
fo... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
Dedispersion
When pulses reach Earth they reach the observer at different times due to dispersion. This dispersion is the result of the interstellar medium causing time delays. This creates a "swooping curve" on the radio spectrogram instead of plane waves. If we are going to fold the pulses to increase the SNR then we... | def delay_from_DM(DM, freq_emitted):
if (type(freq_emitted) == type(0.0)):
if (freq_emitted > 0.0):
return DM / (0.000241 * freq_emitted * freq_emitted)
else:
return 0.0
else:
return Num.where(freq_emitted > 0.0,
DM / (0.000241 * freq_emit... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
Dedispersion Trials
The computer now checks multiple DM values and adjust each frequency channel where it records its SNR. We increment the trial DM by a tunable parameter sens. After the trials, we take the largest SNR created by adjusting the time delays. We use that data to perform the FFT's and record the folded pr... | small_data = data_adjust[:,0,:]
data_base = data_adjust[:,0,:]
sens =0.05
DM_base = 6.4
candidates = 50
fchan = obs.header['fch1']
width = obs.header['foff']
tsamp = obs.header['tsamp']
fchan = fchan+ width*small_data.shape[1]
snrs = DM_can(small_data, data_base, sens, DM_base, candidates, fchan, abs(width),tsamp)
p... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
Detecting Pulses - Fourier Transforms and Folding
Next, we apply the discrete Fourier transform on the data to detect periodic pulses. To do so, we look for the greatest magnitude of the Fourier transform. This indicates potential periods within the data. Then we check for consistency by folding the data by the period ... | # Preforming the fourier transform.
%matplotlib inline
import scipy.fftpack
from scipy.fft import fft
N = 1000
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = abs(data_adjust[:,0,:].mean(axis=1))
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
# Magintude of the fourier transform
# Between 0.00035 and 3.5 seco... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
Folding Algorithm
The idea of the folding algorithm is to see if the signal forms a consistent profile as you fold/integrate the values together. If the profile appears consistent/stable then you're looking at an accurate reading of the pulsar's period. This confirms the implications drawn from the Fourier transform. T... | # Lets take an example of such a period!
# The 0th candidate is the top ranked candidate by the FFT
period = 895
fold = np.zeros((period, data.shape[2]))
multiples = int(data.data.shape[0]/period)
results = np.zeros((period))
for i in range(multiples-1):
fold[:,:]=data_adjust[i*period:(i+1)*period,0,:]+ fold
res... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
What Happens If The Data Doesn't Contain Pulses?
Below we will show you that this algorithm detects pulses and excludes targets that do not include this feature. We will do so by loading a target that isn't known to be a pulsar. HIP65960 is a target that doesn't contain repeating signals.
Below we will repeat and apply... | !wget http://blpd13.ssl.berkeley.edu/dl/GBT_58402_66282_HIP65960_time.h5
from blimpy import Waterfall
import pylab as plt
import numpy as np
import math
from scipy import stats, interpolate
%matplotlib inline
obs = Waterfall('/content/GBT_58402_66282_HIP65960_time.h5',
f_start=0,f_stop= 361408,max_lo... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
NOTICE
Notice how the signal to noise ratio is a lot smaller, It's smaller by 2 orders of magnitude (100x) than the original pulsar fold. Typically with a SNR of 1, it isn't considered a signal of interest as it's most likely just noise. | # Lets take an example of such a period!
# The 0th candidate is the top ranked candidate by the FFT
can_snr =[]
for k in range(len(candidates)):
period = candidates[k]
fold = np.zeros((period, data.shape[2]))
multiples = int(data.data.shape[0]/period)
results = np.zeros((period))
for i in range(multiples-... | GBT/pulsar_searches/Pulsar_Search/Pulsar_DedisperseV3.ipynb | UCBerkeleySETI/breakthrough | gpl-3.0 |
Introduction
Ce notebook est la traduction française du cours sur SymPy disponible entre autre sur Wakari avec quelques modifications et compléments notamment pour la résolution d'équations différentielles. Il a pour but de permettre aux étudiants de différents niveaux d'expérimenter des notions mathématiques en leur f... | from sympy import * | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Pour obtenir des sorties mathématiques formatées $\LaTeX$ : | from sympy import init_printing
init_printing(use_latex=True) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Variables symboliques
Dans SymPy on a besoin de créer des symboles pour les variables qu'on souhaite employer. Pour cela on utilise la class Symbol: | x = Symbol('x')
(pi + x)**2
# manière alternative de définir plusieurs symboles en une seule instruction
a, b, c = symbols("a, b, c") | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
On peut ajouter des contraintes sur les symboles lors de leur création : | x = Symbol('x', real=True)
x.is_imaginary
x = Symbol('x', positive=True)
x > 0 | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Nombres complexes
L'unité imaginaire est notée I dans Sympy. | 1+1*I
I**2
(1 + x * I)**2 | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Nombres rationnels
Il y a trois types numériques différents dans SymPy : Real (réel), Rational (rationnel), Integer (entier) : | r1 = Rational(4,5)
r2 = Rational(5,4)
r1
r1+r2
r1/r2 | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Evaluation numérique
SymPy permet une précision arbitraire des évaluations numériques et fournit des expressions pour quelques constantes comme : pi, E, oo pour l'infini.
Pour évaluer numériquement une expression nous utilisons la fonction evalf (ou N). Elle prend un argument n qui spécifie le nombre de chiffres signif... | pi.evalf(n=50)
E.evalf(n=4)
y = (x + pi)**2
N(y, 5) # raccourci pour evalf | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Quand on évalue des expressions algébriques on souhaite souvent substituer un symbole par une valeur numérique. Dans SymPy cela s'effectue par la fonction subs : | y.subs(x, 1.5)
N(y.subs(x, 1.5)) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
La fonction subs permet de substituer aussi des symboles et des expressions : | y.subs(x, a+pi) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
On peut aussi combiner l'évolution d'expressions avec les tableaux de NumPy (pour tracer une fonction par ex) : | import numpy
x_vec = numpy.arange(0, 10, 0.1)
y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec])
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x_vec, y_vec); | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Manipulations algébriques
Une des principales utilisations d'un système de calcul symbolique est d'effectuer des manipulations algébriques d'expression. Il est possible de développer un produit ou bien de factoriser une expression. Les fonctions pour réaliser ces opérations de bases figurent dans les exemples des secti... | (x+1)*(x+2)*(x+3)
expand((x+1)*(x+2)*(x+3)) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
La fonction expand (développer) prend des mots clés en arguments pour indiquer le type de développement à réaliser. Par exemple pour développer une expression trigonomètrique on utilise l'argument trig=True : | sin(a+b)
expand(sin(a+b), trig=True)
sin(a+b)**3
expand(sin(a+b)**3, trig=True) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Lancer help(expand) pour une explication détaillée des différents types de développements disponibles.
L'opération opposée au développement est bien sur la factorisation qui s'effectue grâce à la fonction factor : | factor(x**3 + 6 * x**2 + 11*x + 6)
x1, x2 = symbols("x1, x2")
factor(x1**2*x2 + 3*x1*x2 + x1*x2**2) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Simplify
The simplify tries to simplify an expression into a nice looking expression, using various techniques. More specific alternatives to the simplify functions also exists: trigsimp, powsimp, logcombine, etc.
The basic usages of these functions are as follows: | # simplify expands a product
simplify((x+1)*(x+2)*(x+3))
# simplify uses trigonometric identities
simplify(sin(a)**2 + cos(a)**2)
simplify(cos(x)/sin(x)) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
simplify permet aussi de tester l'égalité d'expressions : | exp1 = sin(a+b)**3
exp2 = sin(a)**3*cos(b)**3 + 3*sin(a)**2*sin(b)*cos(a)*cos(b)**2 + 3*sin(a)*sin(b)**2*cos(a)**2*cos(b) + sin(b)**3*cos(a)**3
simplify(exp1 - exp2)
if simplify(exp1 - exp2) == 0:
print "{0} = {1}".format(exp1, exp2)
else:
print "exp1 et exp2 sont différentes" | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
apart and together
Pour manipuler des expressions numériques de fractions on dispose des fonctions apart and together : | f1 = 1/((a+1)*(a+2))
f1
apart(f1)
f2 = 1/(a+2) + 1/(a+3)
f2
together(f2) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Simplify combine les fractions mais ne factorise pas : | simplify(f2) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Calcul
En plus des manipulations algébriques, l'autre grande utilisation d'un système de calcul symbolique et d'effectuer des calculs comme des dérivées et intégrales d'expressions algébriques.
Dérivation
La dérivation est habituellement simple. On utilise la fonction diff avec pour premier argument l'expression à dér... | y | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Dérivée première | diff(y**2, x) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Pour des dérivées d'ordre supérieur : | diff(y**2, x, x) # dérivée seconde
diff(y**2, x, 2) # dérivée seconde avec une autre syntaxe | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Pour calculer la dérivée d'une expression à plusieurs variables : | x, y, z = symbols("x,y,z")
f = sin(x*y) + cos(y*z) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
$\frac{d^3f}{dxdy^2}$ | diff(f, x, 1, y, 2) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Integration
L'intégration est réalisée de manière similaire : | f
integrate(f, x) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
En fournissant des limites pour la variable d'intégration on peut évaluer des intégrales définies : | integrate(f, (x, -1, 1)) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
et aussi des intégrales impropres pour lesquelles on ne connait pas de primitive | x_i = numpy.arange(-5, 5, 0.1)
y_i = numpy.array([N((exp(-x**2)).subs(x, xx)) for xx in x_i])
fig2, ax2 = plt.subplots()
ax2.plot(x_i, y_i)
ax2.set_title("$e^{-x^2}$")
integrate(exp(-x**2), (x, -oo, oo)) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Rappel, oo est la notation SymPy pour l'infini.
Sommes et produits
On peut évaluer les sommes et produits d'expression avec les fonctions Sum et Product : | n = Symbol("n")
Sum(1/n**2, (n, 1, 10))
Sum(1/n**2, (n,1, 10)).evalf()
Sum(1/n**2, (n, 1, oo)).evalf()
N(pi**2/6) # fonction zeta(2) de Riemann | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Les produits sont calculés de manière très semblables : | Product(n, (n, 1, 10)) # 10! | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Limites
Les limites sont évaluées par la fonction limit. Par exemple : | limit(sin(x)/x, x, 0) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
On peut changer la direction d'approche du point limite par l'argument du mot clé dir : | limit(1/x, x, 0, dir="+")
limit(1/x, x, 0, dir="-") | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Séries
Le développement en série est une autre fonctionnalité très utile d'un système de calcul symbolique. Dans SymPy on réalise les développements en série grâce à la fonction series : | series(exp(x), x) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Par défaut le développement de l'expression s'effectue au voisinage de $x=0$, mais on peut développer la série au voisinage de toute autre valeur de $x$ en incluant explicitement cette valeur lors de l'appel à la fonction : | series(exp(x), x, 1) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Et on peut explicitement définir jusqu'à quel ordre le développement doit être mené : | series(exp(x), x, 1, 10) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Le développement en séries inclue l'ordre d'approximation. Ceci permet de gérer l'ordre du résultat de calculs utilisant des développements en séries d'ordres différents : | s1 = cos(x).series(x, 0, 5)
s1
s2 = sin(x).series(x, 0, 2)
s2
expand(s1 * s2) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Si on ne souhaite pas afficher l'ordre on utilise la méthode removeO : | expand(s1.removeO() * s2.removeO()) | Calcul symbolique.ipynb | regisDe/compagnons | gpl-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.