markdown
stringlengths
0
1.02M
code
stringlengths
0
832k
output
stringlengths
0
1.02M
license
stringlengths
3
36
path
stringlengths
6
265
repo_name
stringlengths
6
127
Visualising the clusters - On the first two columns
plt.figure(figsize=(14,10)) plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = 'tab:orange', label = 'Iris-setosa') plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = 'tab:blue', label = 'Iris-versicolour') plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], ...
_____no_output_____
Apache-2.0
Task_2_Clustering.ipynb
BakkeshAS/GRIP_Task_2_Predict_Optimum_Clusters
Welcome to an example Binder Test markup This notebook uses a Python environment with a few libraries, including `dask`, all of which were specificied using a `conda` [environment.yml](../edit/environment.yml) file. To demo the environment, we'll show a simplified example of using `dask` to analyze time series data, ...
%matplotlib inline
_____no_output_____
BSD-3-Clause
index.ipynb
trailmarkerlib/dataExplore
Turn on a global progress bar
from dask.diagnostics import ProgressBar progress_bar = ProgressBar() progress_bar.register()
_____no_output_____
BSD-3-Clause
index.ipynb
trailmarkerlib/dataExplore
Generate fake data
import dask.dataframe as dd df = dd.demo.make_timeseries(start='2000', end='2015', dtypes={'A': float, 'B': int}, freq='5s', partition_freq='3M', seed=1234)
_____no_output_____
BSD-3-Clause
index.ipynb
trailmarkerlib/dataExplore
Compute and plot a cumulative sum
df.A.cumsum().resample('1w').mean().compute().plot();
[########################################] | 100% Completed | 16.5s
BSD-3-Clause
index.ipynb
trailmarkerlib/dataExplore
> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python. 3.7. Processing webcam images in real-time from the notebook In this recipe, we show how to communicate data in both directions from the...
from IPython.html.widgets import DOMWidget from IPython.utils.traitlets import Unicode, Bytes, Instance from IPython.display import display from skimage import io, filter, color import urllib import base64 from PIL import Image import StringIO import numpy as np from numpy import array, ndarray import matplotlib.pyplo...
_____no_output_____
BSD-2-Clause
notebooks/chapter03_notebook/07_webcam_py2.ipynb
khanparwaz/PythonProjects
2. We define two functions to convert images from and to base64 strings. This conversion is a common way to pass binary data between processes (here, the browser and Python).
def to_b64(img): imgdata = StringIO.StringIO() pil = Image.fromarray(img) pil.save(imgdata, format='PNG') imgdata.seek(0) return base64.b64encode(imgdata.getvalue()) def from_b64(b64): im = Image.open(StringIO.StringIO(base64.b64decode(b64))) return array(im)
_____no_output_____
BSD-2-Clause
notebooks/chapter03_notebook/07_webcam_py2.ipynb
khanparwaz/PythonProjects
3. We define a Python function that will process the webcam image in real time. It accepts and returns a NumPy array. This function applies an edge detector with the `roberts()` function in scikit-image.
def process_image(image): img = filter.roberts(image[:,:,0]/255.) return (255-img*255).astype(np.uint8)
_____no_output_____
BSD-2-Clause
notebooks/chapter03_notebook/07_webcam_py2.ipynb
khanparwaz/PythonProjects
4. Now, we create a custom widget to handle the bidirectional communication of the video flow from the browser to Python and reciprocally.
class Camera(DOMWidget): _view_name = Unicode('CameraView', sync=True) # This string contains the base64-encoded raw # webcam image (browser -> Python). imageurl = Unicode('', sync=True) # This string contains the base64-encoded processed # webcam image(Python -> browser). imageur...
_____no_output_____
BSD-2-Clause
notebooks/chapter03_notebook/07_webcam_py2.ipynb
khanparwaz/PythonProjects
5. The next step is to write the Javascript code for the widget.
%%javascript var video = $('<video>')[0]; var canvas = $('<canvas>')[0]; var canvas2 = $('<img>')[0]; var width = 320; var height = 0; require(["widgets/js/widget"], function(WidgetManager){ var CameraView = IPython.DOMWidgetView.extend({ render: function(){ var that = this;...
_____no_output_____
BSD-2-Clause
notebooks/chapter03_notebook/07_webcam_py2.ipynb
khanparwaz/PythonProjects
6. Finally, we create and display the widget.
c = Camera() display(c)
_____no_output_____
BSD-2-Clause
notebooks/chapter03_notebook/07_webcam_py2.ipynb
khanparwaz/PythonProjects
raytracer-imagingFor PET image reconstruction
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.axes3d import Axes3D import math import numpy as np from numba import cuda,types, from_dtype import raytracer.cudaOptions from raytracer.rotation.quaternion import * from raytracer.raytracer.voxel import * from raytracer.raytracer.raytrace import * data_rays = 0...
_____no_output_____
MIT
examples/raytracer_data.ipynb
akhilsadam/raytracer-imaging
2.1
df = pd.read_csv('week2.csv') df = df.sort_values(by=['Date']) df = df.drop(['Unnamed: 2', 'Month.1', 'Year.1'], axis = 1) df.head() df.dtypes df['Date'] = pd.to_datetime(df['Date']) df.dtypes df.set_index("Date", inplace = True) df.head() df.reset_index().plot(x='Date', y='Close_Price');
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.2
plt.stem(df.index.values,df.Day_Perc_Change, bottom=0);
C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:1: UserWarning: In Matplotlib 3.3 individual lines on a stem plot will be added as a LineCollection instead of individual lines. This significantly improves the performance of a stem plot. To remove this warning and switch to the new beha...
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.3
df.reset_index().plot(x='Date', y='Total_Traded_Quantity'); sns.set(style="darkgrid") scaledvolume = df["Total_Traded_Quantity"] - df["Total_Traded_Quantity"].min() scaledvolume = scaledvolume/scaledvolume.max() * df.Day_Perc_Change.max() fig, ax = plt.subplots(figsize=(12, 6)) ax.stem(df.index, df.Day_Perc_Change ...
C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:8: UserWarning: In Matplotlib 3.3 individual lines on a stem plot will be added as a LineCollection instead of individual lines. This significantly improves the performance of a stem plot. To remove this warning and switch to the new beha...
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.4
df = df.reset_index() df.head() df.Trend.groupby(df.Trend).count().plot(kind='pie', autopct='%.1f%%') plt.axis('equal') plt.show() avg = df.groupby(df['Trend'])['Total_Traded_Quantity'].mean() med = df.groupby(df['Trend'])['Total_Traded_Quantity'].median() plt.subplot(2, 1, 1) avg.plot.bar(color='Blue', label='mean').l...
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.5
df['Day_Perc_Change'].plot.hist(bins=20);
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.6
jublfood = pd.read_csv('JUBLFOOD.csv') jublfood = jublfood.drop(jublfood[jublfood.Series != 'EQ'].index) jublfood.reset_index(inplace=True) print(jublfood.shape) jublfood.head() godrejind = pd.read_csv('GODREJIND.csv') godrejind = godrejind.drop(godrejind[godrejind.Series != 'EQ'].index) godrejind.reset_index(inplace=T...
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.7
perc_change = perc_change.drop([0]) perc_change.reset_index(inplace=True) perc_change.head() print(perc_change[['GODREJIND', 'JUBLFOOD', 'MARUTI', 'PVR', 'TCS']].rolling(7, min_periods=1).std(ddof=0).head()) plt.plot(perc_change[['GODREJIND', 'JUBLFOOD', 'MARUTI', 'PVR', 'TCS']].rolling(7, min_periods=1).std(ddof=0)) p...
GODREJIND JUBLFOOD MARUTI PVR TCS 0 0.000000 0.000000 0.000000 0.000000 0.000000 1 0.215246 1.304872 0.922343 0.743322 0.814782 2 1.539099 2.159120 1.524084 0.821539 0.936910 3 1.378038 1.869992 1.348622 0.713198 1.721081 4 1.484981 1.758872 1.297517 1.012500 1.553279
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.8
nifty = pd.read_csv('Nifty50.csv') nifty['PCT'] = nifty['Close'].pct_change()*100 nifty = nifty.drop([0]) nifty.reset_index(inplace=True) nifty.head() print(nifty[['PCT']].rolling(7, min_periods=1).std(ddof=0).head()) plt.plot(nifty[['PCT']].rolling(7, min_periods=1).std(ddof=0), color='Black'); plt.legend(["Nifty"]); ...
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.9
short_window = 21 long_window = 34 tcs_roll_21 = combined['TCS'].rolling(short_window, min_periods=1).mean() tcs_roll_34 = combined['TCS'].rolling(long_window, min_periods=1).mean() date = pd.to_datetime(tcs['Date']) signals = pd.DataFrame(index=date) signals['signal'] = 0 signals['signal'][short_window:] = np.where(tc...
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
2.10
tcs_mean_14 = combined.TCS.rolling(14, min_periods=1).mean() tcs_std_14 = combined.TCS.rolling(14, min_periods=1).std() upper = tcs_mean_14 + 2*tcs_std_14 lower = tcs_mean_14 - 2*tcs_std_14 plt.figure(figsize=(16,8)) plt.plot(date, tcs_mean_14, color='black', label='TCS') plt.plot(date, tcs['Average Price'], color='red...
_____no_output_____
MIT
Module 2.ipynb
parth2608/Stock-Analysis
Beam finite element matricesBased on: \[1] Neto, M. A., Amaro, A., Roseiro, L., Cirne, J., & Leal, R. (2015). Finite Element Method for Beams. In Engineering Computation of Structures: The Finite Element Method (pp. 115–156). Springer International Publishing. http://doi.org/10.1007/978-3-319-17710-6_4The beam finite ...
from sympy import * init_printing() def symb(x, y): return symbols('{0}_{1}'.format(x, y), type = float) E, A, L, G, I_1, I_2, I_3, rho = symbols('E A L G I_1 I_2 I_3 rho', type = float)
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
The finite element matrices should have order 12, accounting for each of the element's DOFs, shown below:
u = Matrix(12, 1, [symb('u', v + 1) for v in range(12)]) transpose(u)
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Axial deformation along $x_1$In terms of generic coordinates $v_i$:
v_a = Matrix(2, 1, [symb('v', v + 1) for v in range(2)]) transpose(v_a)
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
which are equivalent to the $u_i$ coordinates in the following way:$$ \mathbf{v} = \mathbf{R} \mathbf{u},$$where:$$ v_1 = u_1, \\ v_2 = u_7,$$with the following coordinate transformation matrix:
R = zeros(12) R[1 - 1, 1 - 1] = 1 R[2 - 1, 7 - 1] = 1 R[:2, :]
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Stiffness matrixEq. (3.15) of [1]:
K_a = (E * A / L) * Matrix([[1, -1], [-1, 1]]) K_a
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Inertia matrixEq. (3.16) of [1]:
M_a = (rho * A * L / 6) * Matrix([[2, 1], [1, 2]]) M_a
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Torsional deformation around $x_1$According to [1], one can obtain the matrices for the torsional case from the axial case by replacing the elasticity modulus $E$ and the cross-sectional area $A$ by the shear modulus $G$ and the polar area moment of inertia $I_1$.In terms of generic coordinates $v_i$:
v_t = Matrix(2, 1, [symb('v', v + 3) for v in range(2)]) transpose(v_t)
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
which are equivalent to the $u_i$ coordinates in the following way:$$ v_3 = u_4, \\ v_4 = u_{10},$$with the following coordinate transformation matrix:
R[3 - 1, 4 - 1] = 1 R[4 - 1, 10 - 1] = 1 R[0:4, :]
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Stiffness matrix
K_t = K_a.subs([(E, G), (A, I_1)]) K_t
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Inertia matrix
M_t = M_a.subs([(E, G), (A, I_1)]) M_t
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Bending on the plane $x_1-x_3$In this case the bending torsion occurs around the $x_2$ axis. In terms of generic coordinates $v_i$:
v_b13 = Matrix(4, 1, [symb('v', v + 9) for v in range(4)]) transpose(v_b13)
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
which are equivalent to the $u_i$ coordinates in the following way:$$ v_9 = u_3, \\ v_{10} = u_5, \\ v_{11} = u_9, \\ v_{12} = u_{11},$$with the following coordinate transformation matrix:
R[9 - 1, 3 - 1] = 1 R[10 - 1, 5 - 1] = 1 R[11 - 1, 9 - 1] = 1 R[12 - 1, 11 - 1] = 1 R
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Stiffness matrix
K_b13 = (E * I_2 / L**3) * Matrix([[ 12 , 6 * L , -12 , 6 * L ], [ 6 * L, 4 * L**2, - 6 * L, 2 * L**2], [-12 , -6 * L , 12 , -6 * L ], [ 6 * L, 2 * L**2, - 6 * L, 4 * L**2]]) if (not K_...
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Inertia matrix
M_b13 = (rho * L * A / 420) * Matrix([[ 156 , 22 * L , 54 , -13 * L ], [ 22 * L, 4 * L**2, 13 * L, - 3 * L**2], [ 54 , 13 * L , 156 , -22 * L ], [- 13 * L, - 3 * L**2, - 22 * ...
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Bending on the plane $x_1-x_2$In this case the bending torsion occurs around the $x_3$ axis, but in the opposite direction. The matrices are similar to the case of bending on the $x_1-x_3$ plane, needing proper coordinate transformation and replacing the index of the area moment of inertia from 2 to 3.Written in terms...
v_b12 = Matrix(4, 1, [symb('v', v + 5) for v in range(4)]) transpose(v_b12)
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
which are equivalent to the $u_i$ coordinates in the following way:$$ v_5 = u_2, \\ v_6 = -u_6, \\ v_7 = u_8, \\ v_8 = -u_{12},$$with the following coordinate transformation matrix:
R[5 - 1, 2 - 1] = 1 R[6 - 1, 6 - 1] = -1 R[7 - 1, 8 - 1] = 1 R[8 - 1, 12 - 1] = -1 R
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Stiffness matrix
K_b12 = K_b13.subs(I_2, I_3) K_b12
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Inertia matrix
M_b12 = M_b13 if (not M_b12.is_symmetric()): print('Error in M_b12.') M_b12
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Assembly of the full matricesAccounting for axial loads, torques and bending in both planes.
RAR = lambda A: transpose(R)*A*R transpose(R**-1*u) K_f = diag(K_a, K_t, K_b12, K_b13) K = RAR(K_f) if (not K.is_symmetric()): print('Error in K.') K M_f = diag(M_a, M_t, M_b12, M_b13) M = RAR(M_f) if (not M.is_symmetric()): print('Error in M.') M
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
Dynamic matrices for Lin and ParkerSee:Lin, J., & Parker, R. G. (1999). Analytical characterization of the unique properties of planetary gear free vibration. Journal of Vibration and Acoustics, Transactions of the ASME, 121(3), 316–321. http://doi.org/10.1115/1.2893982Considering translation on directions $x_2$ and $...
id = [2,3, 4, 8, 9, 10] u_LP = [symb('u', i) for i in id] Matrix(u_LP) T = zeros(12,6) T[2 - 1, 1 - 1] = 1 T[3 - 1, 2 - 1] = 1 T[4 - 1, 3 - 1] = 1 T[8 - 1, 4 - 1] = 1 T[9 - 1, 5 - 1] = 1 T[10 - 1, 6 - 1] = 1 (T.T) * K * T
_____no_output_____
MIT
notes/FEM_beam.ipynb
gfsReboucas/Drivetrain-python
🚜 Predicting the Sale Price of Bulldozers using Machine LearningIn this notebook, we're going to go through an example machine learning project with the goal of predicting the sale price of bulldozers. 1. Problem defition> How well can we predict the future sale price of a bulldozer, given its characteristics and pre...
import numpy as np import pandas as pd import matplotlib.pyplot as plt import sklearn # Import training and validation sets df = pd.read_csv("data/bluebook-for-bulldozers/TrainAndValid.csv", low_memory=False) df.info() df.isna().sum() df.columns fig, ax = plt.subplots() ax.scatter(df["saledate"][:1000]...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Parsing datesWhen we work with time series data, we want to enrich the time & date component as much as possible.We can do that by telling pandas which of our columns has dates in it using the `parse_dates` parameter.
# Import data again but this time parse dates df = pd.read_csv("data/bluebook-for-bulldozers/TrainAndValid.csv", low_memory=False, parse_dates=["saledate"]) df.saledate.dtype df.saledate[:1000] fig, ax = plt.subplots() ax.scatter(df["saledate"][:1000], df["SalePrice"][:1000]) df.head()...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Sort DataFrame by saledateWhen working with time series data, it's a good idea to sort it by date.
# Sort DataFrame in date order df.sort_values(by=["saledate"], inplace=True, ascending=True) df.saledate.head(20)
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Make a copy of the original DataFrameWe make a copy of the original dataframe so when we manipulate the copy, we've still got our original data.
# Make a copy of the original DataFrame to perform edits on df_tmp = df.copy()
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Add datetime parameters for `saledate` column
df_tmp["saleYear"] = df_tmp.saledate.dt.year df_tmp["saleMonth"] = df_tmp.saledate.dt.month df_tmp["saleDay"] = df_tmp.saledate.dt.day df_tmp["saleDayOfWeek"] = df_tmp.saledate.dt.dayofweek df_tmp["saleDayOfYear"] = df_tmp.saledate.dt.dayofyear df_tmp.head().T # Now we've enriched our DataFrame with date time features...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
5. Modelling We've done enough EDA (we could always do more) but let's start to do some model-driven EDA.
# Let's build a machine learning model from sklearn.ensemble import RandomForestRegressor model = RandomForestRegressor(n_jobs=-1, random_state=42) model.fit(df_tmp.drop("SalePrice", axis=1), df_tmp["SalePrice"]) df_tmp.info() df_tmp["UsageBand"].dtype df_tmp.isna().sum()
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Convert string to categoriesOne way we can turn all of our data into numbers is by converting them into pandas catgories.We can check the different datatypes compatible with pandas here: https://pandas.pydata.org/pandas-docs/stable/reference/general_utility_functions.htmldata-types-related-functionality
df_tmp.head().T pd.api.types.is_string_dtype(df_tmp["UsageBand"]) # Find the columns which contain strings for label, content in df_tmp.items(): if pd.api.types.is_string_dtype(content): print(label) # If you're wondering what df.items() does, here's an example random_dict = {"key1": "hello", ...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Thanks to pandas Categories we now have a way to access all of our data in the form of numbers.But we still have a bunch of missing data...
# Check missing data df_tmp.isnull().sum()/len(df_tmp)
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Save preprocessed data
# Export current tmp dataframe df_tmp.to_csv("data/bluebook-for-bulldozers/train_tmp.csv", index=False) # Import preprocessed data df_tmp = pd.read_csv("data/bluebook-for-bulldozers/train_tmp.csv", low_memory=False) df_tmp.head().T df_tmp.isna().sum()
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Fill missing values Fill numerical missing values first
for label, content in df_tmp.items(): if pd.api.types.is_numeric_dtype(content): print(label) df_tmp.ModelID # Check for which numeric columns have null values for label, content in df_tmp.items(): if pd.api.types.is_numeric_dtype(content): if pd.isnull(content).sum(): print(label) #...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Filling and turning categorical variables into numbers
# Check for columns which aren't numeric for label, content in df_tmp.items(): if not pd.api.types.is_numeric_dtype(content): print(label) # Turn categorical variables into numbers and fill missing for label, content in df_tmp.items(): if not pd.api.types.is_numeric_dtype(content): # Add binary ...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Now that all of data is numeric as well as our dataframe has no missing values, we should be able to build a machine learning model.
df_tmp.head() len(df_tmp) %%time # Instantiate model model = RandomForestRegressor(n_jobs=-1, random_state=42) # Fit the model model.fit(df_tmp.drop("SalePrice", axis=1), df_tmp["SalePrice"]) # Score the model model.score(df_tmp.drop("SalePrice", axis=1), df_tmp["SalePrice"])
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
**Question:** Why doesn't the above metric hold water? (why isn't the metric reliable) Splitting data into train/validation sets
df_tmp.saleYear df_tmp.saleYear.value_counts() # Split data into training and validation df_val = df_tmp[df_tmp.saleYear == 2012] df_train = df_tmp[df_tmp.saleYear != 2012] len(df_val), len(df_train) # Split data into X & y X_train, y_train = df_train.drop("SalePrice", axis=1), df_train.SalePrice X_valid, y_valid = df...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Building an evaluation function
# Create evaluation function (the competition uses RMSLE) from sklearn.metrics import mean_squared_log_error, mean_absolute_error, r2_score def rmsle(y_test, y_preds): """ Caculates root mean squared log error between predictions and true labels. """ return np.sqrt(mean_squared_log_error(y_test, y_...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Testing our model on a subset (to tune the hyperparameters)
# # This takes far too long... for experimenting # %%time # model = RandomForestRegressor(n_jobs=-1, # random_state=42) # model.fit(X_train, y_train) len(X_train) # Change max_samples value model = RandomForestRegressor(n_jobs=-1, random_state=42, ...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Hyerparameter tuning with RandomizedSearchCV
%%time from sklearn.model_selection import RandomizedSearchCV # Different RandomForestRegressor hyperparameters rf_grid = {"n_estimators": np.arange(10, 100, 10), "max_depth": [None, 3, 5, 10], "min_samples_split": np.arange(2, 20, 2), "min_samples_leaf": np.arange(1, 20, 2), ...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Train a model with the best hyperparamters**Note:** These were found after 100 iterations of `RandomizedSearchCV`.
%%time # Most ideal hyperparamters ideal_model = RandomForestRegressor(n_estimators=40, min_samples_leaf=1, min_samples_split=14, max_features=0.5, n_jobs=-1, ...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Make predictions on test data
# Import the test data df_test = pd.read_csv("data/bluebook-for-bulldozers/Test.csv", low_memory=False, parse_dates=["saledate"]) df_test.head() # Make predictions on the test dataset test_preds = ideal_model.predict(df_test)
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Preprocessing the data (getting the test dataset in the same format as our training dataset)
def preprocess_data(df): """ Performs transformations on df and returns transformed df. """ df["saleYear"] = df.saledate.dt.year df["saleMonth"] = df.saledate.dt.month df["saleDay"] = df.saledate.dt.day df["saleDayOfWeek"] = df.saledate.dt.dayofweek df["saleDayOfYear"] = df.saledate.dt.d...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Finally now our test dataframe has the same features as our training dataframe, we can make predictions!
# Make predictions on the test data test_preds = ideal_model.predict(df_test) test_preds
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
We've made some predictions but they're not in the same format Kaggle is asking for: https://www.kaggle.com/c/bluebook-for-bulldozers/overview/evaluation
# Format predictions into the same format Kaggle is after df_preds = pd.DataFrame() df_preds["SalesID"] = df_test["SalesID"] df_preds["SalesPrice"] = test_preds df_preds # Export prediction data df_preds.to_csv("data/bluebook-for-bulldozers/test_predictions.csv", index=False)
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
Feature ImportanceFeature importance seeks to figure out which different attributes of the data were most importance when it comes to predicting the **target variable** (SalePrice).
# Find feature importance of our best model ideal_model.feature_importances_ # Helper function for plotting feature importance def plot_features(columns, importances, n=20): df = (pd.DataFrame({"features": columns, "feature_importances": importances}) .sort_values("feature_importan...
_____no_output_____
MIT
workbench materials/end-to-end-bluebook-bulldozer-price-regression-video.ipynb
Mikolaj-Myszka/DataScience-bulldozer-price-prediction
BHSA specifics
A = use("bhsa:clone", checkout="clone", hoist=globals()) A.reuse() A.showContext()
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
A slot with standard features
A.displayShow("standardFeatures") w = 2 p = L.u(w, otype="phrase")[0] tree = A.unravel(w, explain=True) tree = A.unravel(p, explain=True) A.pretty(w, standardFeatures=True) A.pretty(p, standardFeatures=True)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Base types
p = 675477 highlights = {p} A.pretty(p, highlights=highlights) A.pretty(p, highlights=highlights, baseTypes="phrase")
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
A tricky verse
v = T.nodeFromSection(("Genesis", 7, 14)) v A.plain(v, explain=False)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Halfverses are hidden. This verse is divided (at top level) in 3 spans: one for each clause chunk. The first and last chunksbelong to clause 1, and the middle chunk is clause 2.Look what happens if we set `hideTypes=True`:
A.plain(v, hideTypes=False, explain=False)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
When you make the browser window narrower, the line breaks are different.Because now the verse is divided in 2 spans: one for each half verse, and the separation betweenthe half verses is within the third clause chunk.See it in pretty view:
A.pretty(v, explain=False)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
We can selectively unhide the half verse and leave everything else hidden:
A.pretty( v, hideTypes=True, hiddenTypes="subphrase phrase_atom clause_atom sentence_atom", explain=False, )
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
This shows the reason of the split.We can also print the full structure (although that's a bit over the top):
A.pretty(v, hideTypes=False)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Alignment in tables
A.table( ((2, 213294, 426583), (3, 213295, 426582), (4, 213296, 426581)), withPassage={1, 2} )
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
SubphrasesSubphrases with equal slots:
w = 3461 sps = L.u(w, otype="subphrase") for sp in sps[0:-1]: print(E.oslots.s(sp)) A.pretty(sp, standardFeatures=True, extraFeatures="rela", withNodes=True)
array('I', [3461, 3462, 3463])
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Sentence spanning two verses
A.pretty(v, withNodes=True, explain=False)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Base types
cl = 427612 words = L.d(cl, otype="word") phrases = L.d(cl, otype="phrase") highlights = {phrases[1]: "lightsalmon", words[2]: "lightblue"} A.pretty(cl, baseTypes="phrase", withNodes=True, highlights=highlights, explain=True)
<0> TOP <1> clause 427612 {322-326} <2> phrase* 651725 {322-323} <3> word 322 {322} <3> word 323 {323} <2> phrase* 651726 {324-326} <3> word 324 {324} <3> word 325 {325} <3> word 326 {326}
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Gaps
c = 427931 s = L.u(c, otype="sentence")[0] v = L.u(c, otype="verse")[0] highlights = {c: "khaki", c + 1: "lightblue"} A.webLink(s) A.plain(s, withNodes=True, highlights=highlights) A.plain(s) T.formats A.plain(c, fmt="text-phono-full", withNodes=True, explain=False) A.plain(c, withNodes=False, explain=False) A.pretty(c...
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Atom types
p = F.otype.s("phrase")[0] pa = F.otype.s("phrase_atom")[0]
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Plain
A.plain(p, highlights={p, pa}) A.plain(p, highlights={p, pa}, hideTypes=False) A.plain(p, highlights={p}) A.plain(p, highlights={p}, hideTypes=False) A.plain(p, highlights={pa}) A.plain(p, highlights={pa}, hideTypes=False) A.plain(pa, highlights={p, pa}) A.plain(pa, highlights={p, pa}, hideTypes=False) A.plain(pa, high...
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Pretty
A.pretty(p, highlights={p, pa}) A.pretty(p, highlights={p, pa}, hideTypes=False) A.pretty(p, highlights={p}) A.pretty(p, highlights={p}, hideTypes=False) A.pretty(p, highlights={pa}) A.pretty(p, highlights={pa}, hideTypes=False) A.pretty(pa, highlights={p, pa}) A.pretty(pa, highlights={p, pa}, hideTypes=False) A.pretty...
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Highlights
cl = 435509 ph = 675481 w = 38625 highlights = {ph, w} A.pretty(cl, highlights=highlights, withNodes=True) A.pretty(cl, highlights=highlights, baseTypes={"phrase"}, withNodes=True) A.plain(ph, highlights=highlights) A.plain(ph, highlights=highlights, baseTypes={"phrase"}, withNodes=True) typeShow(A)
_____no_output_____
MIT
zz_test/030-bhsa.ipynb
sethbam9/tutorials
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.
def main(): n, m = map(int, input().split()) xs = [0] * (n + 2) for _ in range(m): a, b, k = map(int, input().split()) xs[a] += k xs[b + 1] -= k answer = 0 current = 0 for x in xs: current += x answer = max(answer, current) print(answer) if __nam...
_____no_output_____
MIT
Interview Preparation Kit/1. arrays/4. array manipulation.ipynb
faisalsanto007/Hakcerrank-problem-solving
Forward Kinematics for Wheeled Robots; Dead Reckoning
# Preparation import numpy as np np.set_printoptions(precision=4, suppress=True) import matplotlib.pyplot as plt import ipywidgets
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
Let's first setup useful functions (see transforms2d notebook)
def mktr(x, y): return np.array([[1, 0, x], [0, 1, y], [0, 0, 1]]) def mkrot(theta): return np.array([[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]]) def drawf(f, ...
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
A function to draw a robot at a given pose `f`
def drawrobot(f, l, ax=None, alpha=0.5): """ Draw robot at f, with wheel distance from center l, on axis ax (if provided) or on plt.gca() otherwise. if l is None, no wheels are drawn""" if(not ax): ax = plt.gca() robot = ([[-1, 2, -1, -1], # x [-1, 0, 1, -1]]) # y robo...
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
Note how the frame is centered in the middle point between the two wheels, and the robot points towards the $x$ axis.
drawf(np.eye(3)) drawrobot(np.eye(3), 0.1) drawrobot(mktr(0.5, 0.3) @ mkrot(np.pi/4), 0.1) plt.gca().axis("equal")
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
The kinematic model of a differential-drive robot![image.png](attachment:image.png)For a given differential drive robot, we have the following (fixed) parameter:* $l$: distance from the robot frame to each wheel. The distance between the wheel is therefore $2 \cdot l$We control the angular speed of each wheel $\phi_L...
def ddtr(vl, vr, l, dt): """ returns the pose transform for a motion with duration dt of a differential drive robot with wheel speeds vl and vr and wheelbase l """ if(np.isclose(vl, vr)): # we are moving straight, R is at the infinity and we handle this case separately return mktr((vr + vl)/2*dt, ...
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
Let's test our function. Try special cases and try for each to predict where the ICR will be.* $v_R = -v_L$* $v_R = 0$, $v_L > 0$* $v_R = 0.5 \cdot v_L$
l = 0.1 initial_frame = np.eye(3) # try changing this @ipywidgets.interact(vl=ipywidgets.FloatSlider(min=-2, max=+2), vr=ipywidgets.FloatSlider(min=-2, max=+2)) def f(vl, vr): drawf(initial_frame) # Initial frame f = ddtr(vl, vr, l, 1) drawf(f) drawrobot(f, l) plt.axis("equa...
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
This approach tells you how to move from the pose at $t$ to the pose at $t+\delta t$. Then you can concatenate multiple transformations.
dt = 1.0 Ts = [mktr(1, 1) @ mkrot(np.pi/4)] vl, vr = 0.10, 0.05 l = 0.1 for i in range(10): Ts.append(Ts[-1] @ ddtr(vl, vr, l, dt)) drawf(np.eye(3)) for T in Ts: drawrobot(T, l) plt.axis("equal")
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
Question: in the example above, would you get the same result if you estimated the final position in a single step? Try that, and make sure you understand the result.
@ipywidgets.interact( vl=ipywidgets.FloatSlider(min=-0.5, max=0.5, value=0, step=0.02), vr=ipywidgets.FloatSlider(min=-0.5, max=0.5, value=0, step=0.02), l= ipywidgets.FloatSlider(min=0.05, max=0.15, value=0.10, step=0.01)) def f(vl, vr, l): Ts = [np.eye(3)] for i in range(10): Ts.a...
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
ExerciseImplement the same approach, using the Euler method for integration. Compare the results with the method above using different values for $\delta t$. Dead ReckoningWe now have all the necessary info in order to predict the trajectory when the wheel speeds change over time. We define the initial and final sp...
l = 0.05 @ipywidgets.interact( vl0=ipywidgets.FloatSlider(min=-0.5, max=0.5, value=0, step=0.02), vr0=ipywidgets.FloatSlider(min=-0.5, max=0.5, value=0, step=0.02), vl1=ipywidgets.FloatSlider(min=-0.5, max=0.5, value=0, step=0.02), vr1=ipywidgets.FloatSlider(min=-0.5, max=0.5, value=0, step=0.02), ...
_____no_output_____
MIT
Robotics Concepts/03 wheeled.ipynb
gyani91/Robotics
Sorting Lists of Lists Sort the following list of lists by the grades in descending order. The desired output should be: [['Kaylee', 99], ['Simon', 99], ['Zoe', 85], ['Malcolm', 80], ['Wash', 79]] Hints: https://wiki.python.org/moin/HowTo/Sorting
grades = [["Malcolm", 80], ["Zoe", 85], ["Kaylee", 99], ["Simon", 99], ["Wash", 79]]
_____no_output_____
ADSL
Python-Drills/01-Sort_List_of_Lists/Sort_List_of_Lists.ipynb
SVEENASHARMA/web-design-challenge
YOUR CODE HERE
sorted_list = sorted(grades, key = lambda x:(-x[1], x[0])) print(sorted_list)
[['Kaylee', 99], ['Simon', 99], ['Zoe', 85], ['Malcolm', 80], ['Wash', 79]]
ADSL
Python-Drills/01-Sort_List_of_Lists/Sort_List_of_Lists.ipynb
SVEENASHARMA/web-design-challenge
In this challenge we jump directly into building neural networks. We won't get into much theory or generality, but by the end of this exercise you'll build a very simple example of one, and in the mean time gain some intuition for how they work. First, we import numpy as usual.
# LAMBDA SCHOOL # # MACHINE LEARNING # # MIT LICENSE import numpy as np
_____no_output_____
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience
Next, look at the Wikipedia article for the XOR function (https://en.wikipedia.org/wiki/XOR_gate). Basically, it's a function that takes in two truth values (aka booleans) x and y and spits out a third truth value f(x,y) according to the following rule: f(x,y) is true when x is true OR y is true, but not both. If we us...
def xorFunction(x, y): if x == y: return False else: return True
_____no_output_____
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience
Great. Now, define a function sigma(x) that acts the way that the sigmoid function does. If you don't remember exactly how this works, check Wikipedia or ask one of your classmates.
def sigma(x): return 1 / (1 + np.exp(-x))
_____no_output_____
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience
Most machine learning algorithms have free parameters that we tweak to get the behavior we want, and this is no exception. Introduce two variables a and b and assign them both to the value 10 (for now).
a = 10 b = 10
_____no_output_____
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience
Finally, here's our first neural network. Just like linear and logistic regression, it's nothing more than a function that takes in our inputs (x and y) and returns an output according to some prescribed rule. Today our rule consists of the following steps:Step 1: Take x and y and calculate ax + by.Step 2: Plug the res...
def NN(x,y): linear = a*x + b*y out = sigma(linear) return np.round(out)
_____no_output_____
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience
See what happens when you plug the values (0,0), (0,1), (1,0), and (1,1) into NN. The last (and possible trickiest) part of this assignment is to try and find values of a and b such that NN and XOR give the same outputs on each of those inputs. If you find a solution, share it. If you can't, talk with your classmates a...
print([NN(*args) for args in [(0, 0), (0, 1), (1, 0), (1, 1)]])
[0.0, 1.0, 1.0, 1.0]
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience
The XOR function cannot be learned by a single unit, which has a linear decision boundary.See: https://www.youtube.com/watch?v=kNPGXgzxoHw
def NN2(x, y): h1 = np.round(sigma(w11*x + w11*y + b11)) h2 = np.round(sigma(w12*x + w12*y + b12)) out = np.round(sigma(w21*h1 + w22*h2 + b2)) return out w11 = 20 w12 = -20 b11 = -10 b12 = 30 w21 = 20 w22 = 20 b2 = -30 print([NN2(*args) for args in [(0, 0), (0, 1), (1, 0), (1, 1)]])
[0.0, 1.0, 1.0, 0.0]
MIT
Week 09 Neural Networks/Code Challenges/Day 1 XOR.ipynb
jraval/LambdaSchoolDataScience