markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Again, the cabin doesn't guarantee whether the passengers are save. Cabin B has 3 times ratio but so other cabins. Cabin C not even a magnitude higher. So there's some limitations that we have.
At least whether the passenger is in the upper class can be one of the factor of whether the passengers is survived. Let's te... | compute_freq_chi2(df.Survived,df.Pclass) | p2-introds/titanic/titanic.ipynb | napjon/ds-nd | mit |
Since we already validated our dataset earlier (also from the frequency table, we can see that each cell has at least 5 expected cases), we can directly perform the statistical test.
So indeed, the test statistic result really high and again p-value is practically zero. The data provide convincing evidence that economi... | df.groupby('Embarked').Survived.mean() | p2-introds/titanic/titanic.ipynb | napjon/ds-nd | mit |
Okay, we see that (C)herbourg has huge survivability rate compared to other two! How is this possible? My first thought would be Cherbourg could be the first/last embarkation point. So they will put around the corner of the ship. When googling schedule of embarkation, I found this link that stated the embarkation is in... | compute_freq_chi2(df.Survived,df.Pclass) | p2-introds/titanic/titanic.ipynb | napjon/ds-nd | mit |
Again the dataset has been validated for statistical test. The other thing is to observe the expected cases of each cell is 5, from the frequency table we know that to be true. Using ChiSquare Indepence test, our p-value is also practically zero. Thus the data also provide convincing evidence that Embarkation Point and... | df.groupby('Embarked').Age.describe().unstack(level=0) | p2-introds/titanic/titanic.ipynb | napjon/ds-nd | mit |
Looking at this statistics, I know that all of the embarkation point has pretty normal distribution since the median and mean are similar. Let's use the violin plot again to confirm this and comparing age distribution by embarkation point side by side. | p = sns.violinplot(data = df, x = 'Embarked', y = 'Age',order=["S","C","Q"])
p.set(title = 'Age Distribution by Embarkation Point',
xlabel = 'Location',
ylabel = 'Age Distribution',
xticklabels = ['Southampton','Cherbourg', 'Queenstown']); | p2-introds/titanic/titanic.ipynb | napjon/ds-nd | mit |
I can see from the violin plot little different age distribution betwen each location. Queenstown has wider distribution among the other two. Cherbourg in the middle, and Southampton has thinner distribution and highest peak. This wide distribution can also be explained by standard deviation in the earlier statistics. ... | df.apply(lambda x: x.isnull().value_counts()) | p2-introds/titanic/titanic.ipynb | napjon/ds-nd | mit |
Unzipping files with house sales data
Dataset is from house sales in King County, the region where the city of Seattle, WA is located. | # Put files in current direction into a list
files_list = [f for f in os.listdir('.') if os.path.isfile(f)]
# Filenames of unzipped files
unzip_files = ['kc_house_data.csv','wk3_kc_house_set_1_data.csv', 'wk3_kc_house_set_2_data.csv',
'wk3_kc_house_set_3_data.csv', 'wk3_kc_house_set_4_data.csv', 'wk3_kc... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Polynomial regression, revisited
We build on the material from Week 3, where we wrote the function to produce an SFrame with columns containing the powers of a given input. Copy and paste the function polynomial_sframe from Week 3: | def polynomial_dataframe(feature, degree): # feature is pandas.Series type
# assume that degree >= 1
# initialize the dataframe:
poly_dataframe = pd.DataFrame()
# and set poly_dataframe['power_1'] equal to the passed feature
poly_dataframe['power_1'] = feature
# first check if degree > 1
... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Let's use matplotlib to visualize what a polynomial regression looks like on the house data. | # Dictionary with the correct dtypes for the DataFrame columns
dtype_dict = {'bathrooms':float, 'waterfront':int, 'sqft_above':int, 'sqft_living15':float,
'grade':int, 'yr_renovated':int, 'price':float, 'bedrooms':float,
'zipcode':str, 'long':float, 'sqft_lot15':float, 'sqft_living':float,... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Plotting the data we are working with | plt.figure(figsize=(8,6))
plt.plot(sales['sqft_living'], sales['price'],'.')
plt.xlabel('Living Area (ft^2)', fontsize=16)
plt.ylabel('House Price ($)', fontsize=16)
plt.title('King County, Seattle House Price Data', fontsize=18)
plt.axis([0.0, 14000.0, 0.0, 8000000.0])
plt.show() | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Let us revisit the 15th-order polynomial model using the 'sqft_living' input. Generate polynomial features up to degree 15 using polynomial_dataframe() and fit a model with these features. When fitting the model, use an L2 penalty of 1e-5: | # Bulding dataframe with 15 polynomial features
poly15_data = polynomial_dataframe(sales['sqft_living'], 15)
l2_small_penalty = 1e-5
model = linear_model.Ridge(alpha=l2_small_penalty, normalize=True)
model.fit(poly15_data, sales['price']) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Note: When we have so many features and so few data points, the solution can become highly numerically unstable, which can sometimes lead to strange unpredictable results. Thus, rather than using no regularization, we will introduce a tiny amount of regularization (l2_penalty=1e-5) to make the solution numerically sta... | print 'Weight for power_1 feature is: %.2f' % (model.coef_[0]) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Observe overfitting
Recall from Week 3 that the polynomial fit of degree 15 changed wildly whenever the data changed. In particular, when we split the sales data into four subsets and fit the model of degree 15, the result came out to be very different for each subset. The model had a high variance. We will see in a mo... | set_1 = pd.read_csv('wk3_kc_house_set_1_data.csv', dtype=dtype_dict)
set_2 = pd.read_csv('wk3_kc_house_set_2_data.csv', dtype=dtype_dict)
set_3 = pd.read_csv('wk3_kc_house_set_3_data.csv', dtype=dtype_dict)
set_4 = pd.read_csv('wk3_kc_house_set_4_data.csv', dtype=dtype_dict) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Next, fit a 15th degree polynomial on set_1, set_2, set_3, and set_4, using 'sqft_living' to predict prices. Print the weights and make a plot of the resulting model. | # Putting data and keys in a list for looping
data_list = [set_1, set_2, set_3, set_4]
key_list = ['set_1', 'set_2', 'set_3', 'set_4']
# model_poly_deg is a dict which holds all the regression models for the ith polynomial fit
poly_15_dframe_dict = {}
models_poly_15_dict = {}
# First, learn models with a really small... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Plotting the data and the 4 different 15 degree polynomials we learned from the data. | plt.figure(figsize=(8,6))
plt.plot(sales['sqft_living'], sales['price'],'.', label= 'House Price Data')
plt.hold(True)
#
for i, key in enumerate(key_list):
leg_label = 'Model ' + str(i+1)
plt.plot( poly_15_dframe_dict[key]['power_1'], models_poly_15_dict[key].predict(poly_15_dframe_dict[key]), '-', label = leg_... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
The four curves should differ from one another a lot, as should the coefficients you learned.
QUIZ QUESTION: For the models learned in each of these training sets, what are the smallest and largest values you learned for the coefficient of feature power_1? (For the purpose of answering this question, negative numbers... | power_l_coeff_list = []
for key in key_list:
power_l_coeff_list.append( models_poly_15_dict[key].coef_[0] )
print 'Smallest power_1 weight with small L2 penalty is: %.2f' %( min(power_l_coeff_list))
print 'Largest power_1 weight with small L2 penalty is: %.2f' %( max(power_l_coeff_list)) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Ridge regression comes to rescue
Generally, whenever we see weights change so much in response to change in data, we believe the variance of our estimate to be large. Ridge regression aims to address this issue by penalizing "large" weights. (Weights of model15 looked quite small, but they are not that small because 's... | # model_poly_deg is a dict which holds all the regression models for the ith polynomial fit
poly_15_dframe_dict = {}
models_poly_15_dict = {}
# Re-learn models with a large L2 penalty
l2_large_penalty=1.23e2
# Looping over polynomial features from 1-15
for key, dframe in zip(key_list, data_list):
# Entering ... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
These curves should vary a lot less, now that you applied a high degree of regularization.
QUIZ QUESTION: For the models learned with the high level of regularization in each of these training sets, what are the smallest and largest values you learned for the coefficient of feature power_1? (For the purpose of answeri... | power_l_coeff_list = []
for key in key_list:
power_l_coeff_list.append( models_poly_15_dict[key].coef_[0] )
print 'Smallest power_1 weight with large L2 penalty is: %.2f' %( min(power_l_coeff_list))
print 'Largest power_1 weight with large L2 penalty is: %.2f' %( max(power_l_coeff_list)) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Selecting an L2 penalty via cross-validation
Just like the polynomial degree, the L2 penalty is a "magic" parameter we need to select. We could use the validation set approach as we did in the last module, but that approach has a major disadvantage: it leaves fewer observations available for training. Cross-validation ... | train_valid_shuffled = pd.read_csv('wk3_kc_house_train_valid_shuffled.csv', dtype=dtype_dict)
test = pd.read_csv('wk3_kc_house_test_data.csv', dtype=dtype_dict) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Let us familiarize ourselves with array slicing with DataFrames. To extract a continuous slice from a DataFrame, use colon in square brackets. For instance, the following cell extracts rows 0 to 9 of train_valid_shuffled. Notice that the first index (0) is included in the slice but the last index (10) is omitted. | train_valid_shuffled[0:10] # rows 0 to 9 | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Now let us extract individual segments with array slicing. Consider the scenario where we group the houses in the train_valid_shuffled dataframe into k=10 segments of roughly equal size, with starting and ending indices computed as above.
Extract the fourth segment (segment 3) and assign it to a variable called validat... | i = 3
start_ind = (n*3)/k
end_ind = (n*(3+1))/k-1
validation4 = train_valid_shuffled[ start_ind : end_ind + 1] | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Extract the remainder of the data after excluding fourth segment (segment 3) and assign the subset to train4. | i = 3
k = 10
n = len(train_valid_shuffled)
start_ind = (n*3)/k
end_ind = (n*(3+1))/k-1
train4 = train_valid_shuffled[0:start].append(train_valid_shuffled[end+1:n]) | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Now we are ready to implement k-fold cross-validation. Write a function that computes k validation errors by designating each of the k segments as the validation set. It accepts as parameters (i) k, (ii) l2_penalty, (iii) dataframe, (iv) name of output column (e.g. price) and (v) list of feature names. The function ret... | def k_fold_cross_validation(k, l2_penalty, data, output_vals):
# Defining n as the number of observations and an empty list to store the k cross_validation errors
n = len(data)
cv_error_list = []
# Looping to compute k slices. Computing the array index to get the kth_slice.
for i in range(... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Once we have a function to compute the average validation error for a model, we can write a loop to find the model that minimizes the average validation error. Write a loop that does the following:
* We will again be aiming to fit a 15th-order polynomial model using the sqft_living input
* For l2_penalty in [10^1, 10^1... | l2_penalty_list = np.logspace(3, 9, num=26)
poly_15_dframe = polynomial_dataframe(train_valid_shuffled['sqft_living'], 15)
output_values = train_valid_shuffled['price']
l2_RSS_list = []
for l2_pen in l2_penalty_list:
RSS_error = k_fold_cross_validation(10, l2_pen, poly_15_dframe, output_values)
l2_RSS_li... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
QUIZ QUESTIONS: What is the best value for the L2 penalty according to 10-fold validation? | print 'Minimum value for RSS error is : %.2e' %min(l2_RSS_list)[0]
print 'L2 penalty for this RSS error is: %.2e' %min(l2_RSS_list)[1] | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
You may find it useful to plot the k-fold cross-validation errors you have obtained to better understand the behavior of the method. | # Putting all L2 penalties and RSS errors for plotting
L2_plot_list = []
RSS_plot_list = []
for entry in l2_RSS_list:
L2_plot_list.append(entry[1])
RSS_plot_list.append(entry[0])
# Plot the l2_penalty values in the x axis and the cross-validation error in the y axis.
# Using plt.xscale('log') will make you... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
Once you found the best value for the L2 penalty using cross-validation, it is important to retrain a final model on all of the training data using this value of l2_penalty. This way, your final model will be trained on the entire dataset. | min_L2_pen = min(l2_RSS_list)[1] | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
QUIZ QUESTION: Using the best L2 penalty found above, train a model using all training data. What is the RSS on the TEST data of the model you learn with this L2 penalty? | # Loading the training set data and defining the dataframe w/ 15 polynomial features
train_data = pd.read_csv('wk3_kc_house_train_data.csv', dtype = dtype_dict)
train_data = train_data.sort_values(['sqft_living', 'price'])
poly_15_train_data = polynomial_dataframe(train_data['sqft_living'], 15)
# Training a linear reg... | Week_4_Ridge_Regression/assign_1_ridge-regression.ipynb | Santana9937/Regression_ML_Specialization | mit |
First , we introduce vector compression by Product Quantization (PQ) [Jegou, TPAMI 11]. The first task is to train an encoder. Let us assume that there are 1000 six-dimensional vectors for training; $X_1 \in \mathbb{R}^{1000\times6}$ | X1 = numpy.random.random((1000, 6))
print("X1.shape:\n{}\n".format(X1.shape))
print("X1:\n{}".format(X1)) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Then we can train a PQEncoder using $X_1$. | encoder = pqkmeans.encoder.PQEncoder(num_subdim=2, Ks=256)
encoder.fit(X1) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
The encoder takes two parameters: $num_subdim$ and $Ks$. In the training step, each vector is splitted into $num_subdim$ sub-vectors, and quantized with $Ks$ codewords. The $num_subdim$ decides the bit length of PQ-code, and typically set as 4, 8, etc. The $Ks$ is usually set as 256 so as to represent each sub-code by ... | print("codewords.shape:\n{}".format(encoder.codewords.shape)) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Note that you can train the encoder preliminary using training data, and write/read the encoder via pickle. | # pickle.dump(encoder, open('encoder.pkl', 'wb')) # Write
# encoder = pickle.load(open('encoder.pkl', 'rb')) # Read | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Next, let us consider database vectors (2000 six-dimensional vectors, $X_2$) that we'd like to compress. | X2 = numpy.random.random((2000, 6))
print("X2.shape:\n{}\n".format(X2.shape))
print("X2:\n{}\n".format(X2))
print("Data type of each element:\n{}\n".format(type(X2[0][0])))
print("Memory usage:\n{} byte".format(X2.nbytes)) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
We can compress these vectors by the trained PQ-encoder. | X2_pqcode = encoder.transform(X2)
print("X2_pqcode.shape:\n{}\n".format(X2_pqcode.shape))
print("X2_pqcode:\n{}\n".format(X2_pqcode))
print("Data type of each element:\n{}\n".format(type(X2_pqcode[0][0])))
print("Memory usage:\n{} byte".format(X2_pqcode.nbytes)) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Each vector is splitted into $num_subdim(=2)$ sub-vectors, and the nearest codeword is searched for each sub-vector. The id of the nearest codeword is recorded, i.e., two integers in this case. This representation is called PQ-code.
PQ-code is a memory efficient data representation. The original 6D vector requies $6 * ... | X2_reconstructed = encoder.inverse_transform(X2_pqcode)
print("original X2:\n{}\n".format(X2))
print("reconstructed X2:\n{}".format(X2_reconstructed)) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
As can be seen, the reconstructed vectors are similar to the original one.
In a large-scale data processing scenario where all data cannot be stored on memory, you can compress input vectors to PQ-codes, and store the PQ-codes only (X2_pqcode). | # numpy.save('pqcode.npy', X2_pqcode) # You can store the PQ-codes only | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
2. Clustering by PQk-means
Let us run the clustering over the PQ-codes. The clustering object is instanciated with the trained encoder. Here, we set the number of cluster as $k=10$. | kmeans = pqkmeans.clustering.PQKMeans(encoder=encoder, k=10) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Let's run the PQk-means over X2_pqcode. | clustered = kmeans.fit_predict(X2_pqcode)
print(clustered[:100]) # Just show the 100 results | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
The resulting vector (clustered) contains the id of assigned codeword for each input PQ-code. | print("The id of assigned codeword for the 1st PQ-code is {}".format(clustered[0]))
print("The id of assigned codeword for the 2nd PQ-code is {}".format(clustered[1]))
print("The id of assigned codeword for the 3rd PQ-code is {}".format(clustered[2])) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
You can fetch the center of the clustering by: | print("clustering centers:{}\n".format(kmeans.cluster_centers_)) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
The centers are also PQ-codes. They can be reconstructed by the PQ-encoder. | clustering_centers_numpy = numpy.array(kmeans.cluster_centers_, dtype=encoder.code_dtype) # Convert to np.array with the proper dtype
clustering_centers_reconstructd = encoder.inverse_transform(clustering_centers_numpy) # From PQ-code to 6D vectors
print("reconstructed clustering centers:\n{}".format(clustering_center... | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Let's summalize the result: | print("13th input vector:\n{}\n".format(X2[12]))
print("13th PQ code:\n{}\n".format(X2_pqcode[12]))
print("reconstructed 13th PQ code:\n{}\n".format(X2_reconstructed[12]))
print("ID of the assigned center:\n{}\n".format(clustered[12]))
print("Assigned center (PQ-code):\n{}\n".format(kmeans.cluster_centers_[clustered[12... | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Note that you can pickle the kmeans instace. The instance can be reused later as a vector quantizer for new input vectors. | # pickle.dump(kmeans, open('kmeans.pkl', 'wb')) # Write
# kmeans = pickle.load(open('kmeans.pkl', 'rb')) # Read | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
3. Comparison to other clustering methods
Let us compare PQk-means and the traditional k-means using high-dimensional data. | from sklearn.cluster import KMeans
X3 = numpy.random.random((1000, 1024)) # 1K 1024-dim vectors, for training
X4 = numpy.random.random((10000, 1024)) # 10K 1024-dim vectors, for database
K = 100
# Train the encoder
encoder_large = pqkmeans.encoder.PQEncoder(num_subdim=4, Ks=256)
encoder_large.fit(X3)
# Encode the ... | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Let's run the PQ-kmeans, and see the computational cost | %time clustered_pqkmeans = pqkmeans.clustering.PQKMeans(encoder=encoder_large, k=K).fit_predict(X4_pqcode) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Then, run the traditional k-means clustering | %time clustered_kmeans = KMeans(n_clusters=K, n_jobs=-1).fit_predict(X4) | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
PQk-means would be tens to hundreds of times faster than k-means depending on your machine. Then let's see the accuracy. Since the result of PQk-means is the approximation of that of k-means, k-means achieved the lower error: | _, pqkmeans_micro_average_error, _ = pqkmeans.evaluation.calc_error(clustered_pqkmeans, X4, K)
_, kmeans_micro_average_error, _ = pqkmeans.evaluation.calc_error(clustered_kmeans, X4, K)
print("PQk-means, micro avg error: {}".format(pqkmeans_micro_average_error))
print("k-means, micro avg error: {}".format(kmeans_micro... | tutorial/1_pqkmeans.ipynb | DwangoMediaVillage/pqkmeans | mit |
Making a GrainDist object from scratch | MRN = graindist.sizedist.Powerlaw(amax=AMAX)
ECUT = graindist.sizedist.ExpCutoff(acut=AMAX)
SIL = graindist.composition.CmSilicate(rho=RHO)
gd_mrn = graindist.GrainDist(MRN, SIL, md=MD, custom=True)
gd_ecut = graindist.GrainDist(ECUT, SIL, md=MD, custom=True)
ax = plt.subplot(111)
gd_mrn.plot(ax, color='k', lw=2, ... | examples/plot_graindist.ipynb | eblur/newdust | bsd-2-clause |
Making a GrainDist object with helper function
This is a shortcut for making some common grain distributions
You can change AMAX and RHO from here, too
For the different grain size distributions, amax acts in a different way:
graindist.sizedist.Grain: amax sets the singular grain size
graindist.sizedist.Powerlaw: a... | gd_mrn2 = graindist.GrainDist('Powerlaw', 'Silicate', amax=AMAX, rho=RHO, md=MD)
gd_ecut2 = graindist.GrainDist('ExpCutoff', 'Silicate', amax=AMAX, rho=RHO, md=MD)
ax = plt.subplot(111)
gd_mrn.plot(ax, color='k', lw=2, label='')
gd_mrn2.plot(ax, color='r', lw=2, ls='--', label='Powerlaw')
gd_ecut.plot(ax, color='k', ... | examples/plot_graindist.ipynb | eblur/newdust | bsd-2-clause |
Note that the values I've used in this example are different from the defaults
Silicate has a default grain material density of 3.8 g cm^-3 | gd_mrn3 = graindist.GrainDist('Powerlaw', 'Silicate', md=MD)
gd_ecut3 = graindist.GrainDist('ExpCutoff', 'Silicate', md=MD)
ax = plt.subplot(111)
gd_mrn.plot(ax, color='k', lw=2, label='')
gd_mrn3.plot(ax, color='r', lw=2, ls='--', label='Powerlaw')
gd_ecut.plot(ax, color='k', lw=2, label='')
gd_ecut3.plot(ax, color=... | examples/plot_graindist.ipynb | eblur/newdust | bsd-2-clause |
You can also get the mass density of dust grains
But the GrainDist.plot function only does number density | plt.plot(gd_mrn.a, gd_mrn.mdens, label='Powerlaw')
plt.plot(gd_ecut.a, gd_ecut.mdens, label='ExpCutoff')
plt.loglog()
plt.xlabel('Radius (um)')
plt.ylabel('Mass density (g cm$^{-2}$ um$^{-1}$)')
plt.legend(loc='upper right', frameon=False) | examples/plot_graindist.ipynb | eblur/newdust | bsd-2-clause |
You can't plot single grain sizes
It prints the number density instead | gd_single = graindist.GrainDist('Grain', 'Silicate', amax=AMAX)
gd_single.plot() | examples/plot_graindist.ipynb | eblur/newdust | bsd-2-clause |
Imports and configuration
We set the path to the config.cfg file using the environment variable 'PYPMJ_CONFIG_FILE'. If you do not have a configuration file yet, please look into the Setting up a configuration file example. | import os
os.environ['PYPMJ_CONFIG_FILE'] = '/path/to/your/config.cfg' | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
On import, we receive information on the configured logging and JCMsuite version, if the logging level is appropriate. We can get additional info on the version and the license. | jpy.jcm_license_info() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The versions of JCMsuite and pypmj can be accessed by the module attributes __jcm_version__ and __version__ | print 'Version of JCMsuite:', jpy.__jcm_version__
print 'Version of pypmj:', jpy.__version__ | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Extensions
You can get a list of available extensions to pypmj using the extensions module attribute. | jpy.extensions | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Extensions may have additional dependencies or may need data that is not shipped with pypmj. You can load an extension using the load_extension function. | jpy.load_extension('materials') | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Simulation
Preparing and configuring the simulation set
We start by creating a JCMProject-instance describing the project we'd like to run. The mie2D-project is located in a subdirectory of our project catalog, which is configured in the section Data under key projects in the configuration file. We could also specify ... | wdir = os.path.abspath('working_dir')
project = jpy.JCMProject('scattering/mie/mie2D', working_dir=wdir) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The JCMProject-instance automatically detected the name of the project file: | project.project_file_name | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
If it fails to find a proper file or if it finds multiple project files, it raises an Exception. You can specify the project file name manually using the parameter project_file_name on initialization.
To run simulations using this project, we create a SimulationSet (this could also be a single simulation). The keys tha... | mie_keys = {'constants' :{},
'parameters': {},
'geometry': {'radius':np.linspace(0.3, 0.5, 40)}} | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Now, the SimulationSet can be initialized. For now, we also set the storage_base and storage_folder attributes. This will ignore the storage base folder of your configuration and use the folder 'tmp_storage_folder' in the current working directory. And instead of a folder named by the current date, the subfolder will b... | simuset = jpy.SimulationSet(project, mie_keys,
storage_folder='mie2D_test',
storage_base=os.path.abspath('tmp_storage_folder')) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We are now informed about the directory in which our data is stored as configured in the configuration file and by the duplicate_path_levels parameter. The path is also stored in the attribute storage_dir. It now contains an .h5 database file: | os.listdir(simuset.storage_dir) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We can now make a schedule for the simulations that we want to perform. This includes that
all parameter combinations are determinded,
the simulations are sorted in a way to have minimal calls of JCMgeo (which can be expensive) and
the database is checked for matching simulations which already have been done.
In our ... | simuset.make_simulation_schedule() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The store should still be empty at this time: | simuset.is_store_empty() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Depending on the configured servers, there might be multiple workstations or queues which can be used by the JCMdaemon to run the simulations. For this 2D project, we can restrict the resources to be only the local computer, i.e. 'localhost', and use only 2 workers. | simuset.use_only_resources('localhost')
simuset.resource_manager.resources.set_m_n_for_all(2,1) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
You can get ResourceDict of the currently configured resources that will be used by the SimulationSet using the get_current_resources-method or simply using the resources attribute of the resource_manager. | simuset.get_current_resources() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Computing geometries only
Computing a geometry (i.e. running jcm.geo) for a specific simulation of our SimulationSet is an easy task now. We only need to call the compute_geometry method with the index of the simulation (or the Simulation instance itself). We can pass additional keyword arguments to jcm.geo, such as a ... | sim = simuset.simulations[0]
print sim
print 'keys:', sim.keys | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Now we run jcm.geo for it (we also could have written simuset.compute_geometry(0)) | simuset.compute_geometry(0) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The project's working directory now contains a grid file: | os.listdir(simuset.get_project_wdir()) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Running a single simulation
Before demonstrating how to solve the complete set of simulations, we show how to solve a single simulation using JCMsolve. This can be very useful if the simulation is still being developed or if something needs to be recomputed later. We can solve a specific simulation using the solve_sing... | print 'Status before solving:', sim.status
results, logs = simuset.solve_single_simulation(sim)
print 'Status after solving:', sim.status | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
All the results and logs are also stored as attributes of the Simulation-instance as logs, error_message, exit_code and jcm_results, e.g. | print sim.logs['Out'] | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The fieldbag file path is also set as an attribute: | sim.fieldbag_file | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Developing and using a processing function
So far we only have solved the simulation by (1) computing the geometry and (2) running JCMsolve. This is fine and it may be that no further step is desired. However, yet nothing is set up to be saved to the HDF5 store, so nothing but the input parameters will appear in it. As... | sim.process_results() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
If the logging-level is set to 'DEBUG', you see the message 'No result processing was done.' to inform you that only the computational costs have been read out. The results that will be stored are now described by the (hidden) attribute _results_dict: | sim._results_dict | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The status of the simulation is now updated. | sim.status | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
To execute further processings, we can use the processing_func-argument. From the docs:
The `processing_func` must be a function of one or two input arguments.
A list of all results returned by post processes in JCMsolve are passed
as the first argument to this function. If a second input argument is
present, it must... | sim.jcm_results[1:] | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We'd like to store the real part of the ElectromagneticFieldEnergyFlux of the zeroth source, as this is the scattering cross section (SCS) of our Mie-scatterer. The function processing_func will simply look like this: | def read_scs(pp):
results = {} #must be a dict
results['SCS'] = pp[0]['ElectromagneticFieldEnergyFlux'][0][0].real
return results | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We can try it out by running process_results results again, this time passing our custom function. We need to set overwrite to True to force the processing to be executed again. | sim.process_results(processing_func=read_scs, overwrite=True) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We now have an additional key called 'SCS' in our _results_dict. It will be stored in the HDF5 store. | sim._results_dict['SCS'] | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
You can get the complete data of the simulation including input parameters and results as a pandas DataFrame: | sim._get_DataFrame() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Running all simulations
Finally, we can run all our simulations and process them using our custom processing_func. This is done in parallel using all the resources that we have added. The results will be appended to the HDF5 store. | simuset.run(N=10, processing_func=read_scs)
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('notebook')
data = simuset.get_store_data().sort_values(by='radius')
data.plot(x='radius', y='SCS', title='Results of the simulation') | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Adding data in a later run
Let's assume we want to extend our store in a later session. The automatic data comparison will detect that some of the data is already known and will only execute the missing simulations. The same mechanism helps to continue the SimulationSet on the point were it stopped, e.g. due to a keybo... | extended_radii = np.append(np.linspace(0.5, 0.6, 40)[1:], np.linspace(0.3, 0.5, 40))
mie_keys_extended = {'constants' :{},
'parameters': {},
'geometry': {'radius':extended_radii}} | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We close the store, delete the SimulationSet instance and start all over. | simuset.close_store()
del simuset
simuset = jpy.SimulationSet(project, mie_keys_extended)
simuset.make_simulation_schedule() | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We are now informed that matches have been found in the HDF5 store. The residual simulations can be executed as before. This time, we set the wdir_mode to 'zip' to demonstrate the automatic zipping and deletion of the working directories. If the directories are no longer needed, you can set wdir_mode to 'delete'. Also,... | jpy.utils.run_simusets_in_save_mode(simuset, N=10, processing_func=read_scs, wdir_mode='zip') | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
The storage folder now only contains the HDF5 store and a zip-archive with all the working directories. | os.listdir(simuset.storage_dir) | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We can now plot our extended results. | simuset.get_store_data().sort_values(by='radius').plot(x='radius', y='SCS', title='Results of the simulation') | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
We can also write our data to a CSV or an Excel file using the write_store_data_to_file method: | simuset.write_store_data_to_file() # default is results.csv in the storage folder
simuset.write_store_data_to_file(os.path.join(simuset.storage_dir, 'results_excel.xls'), mode='Excel') | examples/Using jcmpython - the mie2D-project.ipynb | cbpygit/pypmj | gpl-3.0 |
Observations:
- The cost starts very high. This is because with large random-valued weights, the last activation (sigmoid) outputs results that are very close to 0 or 1 for some examples, and when it gets that example wrong it incurs a very high loss for that example. Indeed, when $\log(a^{[3]}) = \log(0)$, the loss go... | # GRADED FUNCTION: initialize_parameters_he
def initialize_parameters_he(layers_dims):
"""
Arguments:
layer_dims -- python array (list) containing the size of each layer.
Returns:
parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
W1 -- ... | Course 2/Initialization.ipynb | ShubhamDebnath/Coursera-Machine-Learning | mit |
Building on logistic regression with no L2 penalty assignment
Let us now build on Module 3 assignment. Recall from lecture that the link function for logistic regression can be defined as:
$$
P(y_i = +1 | \mathbf{x}_i,\mathbf{w}) = \frac{1}{1 + \exp(-\mathbf{w}^T h(\mathbf{x}_i))},
$$
where the feature vector $h(\mathb... | '''
produces probablistic estimate for P(y_i = +1 | x_i, w).
estimate ranges between 0 and 1.
'''
def predict_probability(feature_matrix, coefficients):
# Take dot product of feature_matrix and coefficients
## YOUR CODE HERE
...
# Compute P(y_i = +1 | x_i, w) using the link function
## YOUR C... | ml-classification/blank/module-4-linear-classifier-regularization-assignment-blank.ipynb | dnc1994/MachineLearning-UW | mit |
required software
This analysis tool requires the seaborn module for the heatmap plotting. toyplot also has a matrix function for plotting heatmaps, but I found that it grinds on assemblies with many taxa. | # conda isntall -c conda-forge seaborn
import ipyrad
import ipyrad.analysis as ipa
from ipyrad.analysis.sharing import Sharing
# the path to your VCF or HDF5 formatted snps file
data = "/home/isaac/ipyrad/test-data/pedicularis/analysis-ipyrad/ped_outfiles/ped.snps.hdf5"
imap = {
"prz": ["32082_przewalskii_SRR175... | newdocs/API-analysis/cookbook-sharing.ipynb | dereneaton/ipyrad | gpl-3.0 |
Removing samples from the sharing matrix is as simple as removing them from the imap
This can be a convenience for speeding up the pairwise calculations if you have lots of samples and only want to examine a few of them. | imap = {
"prz": ["32082_przewalskii_SRR1754729", "33588_przewalskii_SRR1754727"],
"cys": ["41478_cyathophylloides_SRR1754722", "41954_cyathophylloides_SRR1754721"],
"cya": ["30686_cyathophylla_SRR1754730"],
"sup": ["29154_superba_SRR1754715"],
"cup": ["33413_thamno_SRR1754728"],
"tha": ["30556_t... | newdocs/API-analysis/cookbook-sharing.ipynb | dereneaton/ipyrad | gpl-3.0 |
The order of samples in the figure can be reconfigured with the keep_order argument
This allows for flexibly reordering samples in the figure without recalculating the sharing values. This parameter will accept a list or a dictionary, and will only plot the specified samples in list order. | imap = {
"prz": ["32082_przewalskii_SRR1754729", "33588_przewalskii_SRR1754727"],
"cys": ["41478_cyathophylloides_SRR1754722", "41954_cyathophylloides_SRR1754721"],
"cya": ["30686_cyathophylla_SRR1754730"],
"sup": ["29154_superba_SRR1754715"],
"cup": ["33413_thamno_SRR1754728"],
"tha": ["30556_t... | newdocs/API-analysis/cookbook-sharing.ipynb | dereneaton/ipyrad | gpl-3.0 |
An example of using keep_order to plot a subset of the imap samples | imap2 = {
"prz": ["32082_przewalskii_SRR1754729", "33588_przewalskii_SRR1754727"],
"cys": ["41478_cyathophylloides_SRR1754722", "41954_cyathophylloides_SRR1754721"],
"cya": ["30686_cyathophylla_SRR1754730"],
"sup": ["29154_superba_SRR1754715"],
"cup": ["33413_thamno_SRR1754728"],
"tha": ["30556_... | newdocs/API-analysis/cookbook-sharing.ipynb | dereneaton/ipyrad | gpl-3.0 |
The matrices can also be sorted either by shared "loci" or shared "missing"
This will sort the rows/columns by mean locus sharing or mean missingness. Notice, no need to rerun the pairwise calculations, this is just a manipulation of the data. The sort argument will be superceded by order. | fig, ax = share.draw(sort="loci")
imap
[item for sublist in imap.values() for item in sublist] | newdocs/API-analysis/cookbook-sharing.ipynb | dereneaton/ipyrad | gpl-3.0 |
Show most negative reviews for giraffe | giraffe_reviews[-1]['review']
giraffe_reviews[-2]['review']
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
def sent_word_count(word_counts):
if 'hate' in word_counts:
return word_counts['hate']
else:
return 0
produ... | dato/recommendations/Analyzing product sentiment.ipynb | jrrembert/cybernetic-organism | gpl-2.0 |
Reading the data
The astropy package has an I/O package to simplify reading and writing a number of popular formats common in astronomy. | from astropy.io import ascii | notebooks/Haiti2016/spectrum.ipynb | astroumd/GradMap | gpl-3.0 |
We obtained a very simple ascii table with two columns, wavelength and intensity, of a spectrum at a particular point in the sky. The astropy module has some classes to deal with such ascii tables. Although this is not a very reliable way to store data, it is very simple and portable. Almost any software will be able t... | data = ascii.read('../data/extract.dat')
print(data)
x = data['col1']
y = data['col2'] | notebooks/Haiti2016/spectrum.ipynb | astroumd/GradMap | gpl-3.0 |
Plotting the basics | import matplotlib.pyplot as plt
plt.plot(x,y) | notebooks/Haiti2016/spectrum.ipynb | astroumd/GradMap | gpl-3.0 |
There is a very strong line around 4865 Angstrom dominating this plot, and many weaker lines. If we rescale this plot and look what is near the baseline, we get to see these weaker lines much better: | plt.plot(x,y)
z = y*0.0
plt.plot(x,z)
plt.ylim(-0.5,2.0) | notebooks/Haiti2016/spectrum.ipynb | astroumd/GradMap | gpl-3.0 |
<h3>Beschreibende Statistik</h3> | # Ausgabe df.shape
print(dataset.shape)
# Ausgabe df.dtypes
# Spalte enthält die Classifikation R oder M
set_option('display.max_rows', 500)
print(dataset.dtypes)
# Ausgabe df.head mit vergösserter display width
set_option('display.width', 100)
print(dataset.head(20))
# Ausgabe df.describe() mit 4 Nachkomma Stellen
... | 18-05-14-ml-workcamp/sonar-daten/Projekt-Sonardaten-Workcamp-ML.ipynb | mediagit2016/workcamp-maschinelles-lernen-grundlagen | gpl-3.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.