Skip to content

Instantly share code, notes, and snippets.

@gto76
Created March 18, 2023 16:03
Show Gist options
  • Save gto76/9ed16511a046d9746d38c55a14ec09a9 to your computer and use it in GitHub Desktop.
Save gto76/9ed16511a046d9746d38c55a14ec09a9 to your computer and use it in GitHub Desktop.

This document was generated by ChatGPT after it was provided with the Dictionary section of the Comprehensive Python Cheatsheet and grilled about the formatting of the generated test sections with approximately 10 queries. The rest were generated using the '<title>?' query. Descriptions of the libraries were generated by asking for one sentence description of each. Unlike the previous test where it was asked to generate already existing sections from the cheatsheet, here it was asked to generate sections on libraries that are missing in the original.

Machine Learning

SciPy

A library for scientific computing and technical computing.

import scipy
<ndarray> = scipy.array(<iterable>)                    # Converts iterable to a numpy array.
<ndarray> = scipy.zeros(<shape>, dtype=<dtype>)        # Returns an array of given shape and type filled with 0s.
<ndarray> = scipy.ones(<shape>, dtype=<dtype>)         # Returns an array of given shape and type filled with 1s.
<ndarray> = scipy.empty(<shape>, dtype=<dtype>)        # Returns an uninitialized array of given shape and type.
<ndarray> = scipy.linspace(<start>, <stop>, <num>)     # Returns an array of evenly spaced values over a range.
<ndarray> = scipy.arange(<start>, <stop>, <step>)      # Returns an array with values in a given range and step size.
<ndarray> = scipy.eye(<N>, M=None, k=0, dtype=<dtype>) # Returns a 2D array with ones on the diagonal and zeros elsewhere.
<ndarray> = scipy.fft(<array>)                         # Computes the discrete Fourier Transform of the input array.
<ndarray> = scipy.ifft(<array>)                        # Computes the inverse of the Fourier Transform.
<ndarray> = scipy.fftfreq(<n>, d=1.0)                  # Computes the Discrete Fourier Transform sample frequencies.
<ndarray> = scipy.integrate.cumtrapz(<y>, x=None, dx=1.0, initial=None) # Cumulatively integrate y using trapezoidal rule.
<ndarray> = scipy.optimize.minimize(<fun>, x0, args=(), method=None) # Minimizes a function using various methods.
<ndarray> = scipy.optimize.curve_fit(<func>, xdata, ydata, p0=None)   # Fits a function to data using least squares.

Scikit-learn

A machine learning library for Python, built on top of SciPy.

<model>.fit(X, y)                   # Trains the model on input X and output y.
<y_pred> = <model>.predict(X)       # Predicts the output based on input X.
<y_pred> = <model>.transform(X)     # Transforms the input X into the output y.
<y_pred> = <model>.fit_transform(X) # Fits the model to X and transforms X into y.
<score>  = <model>.score(X, y)      # Returns the accuracy score of the model.
<params> = <model>.get_params()     # Returns the hyperparameters of the model.
<model>  = <estimator>.fit(X, y)    # Fits an estimator to X and y.
<y_pred> = <estimator>.predict(X)   # Predicts the output based on input X.
<score>  = <metrics>.score(y_true, y_pred)  # Computes the score based on y_true and y_pred.
<X_train>, <X_test>, <y_train>, <y_test> = <model_selection>.train_test_split(X, y, test_size=0.2) # Splits X and y into train and test sets.
<grid_search> = <model_selection>.GridSearchCV(<estimator>, <param_grid>) # Searches for the best hyperparameters using a grid search.
<cluster_labels> = <cluster>.fit_predict(X) # Assigns each element of X to a cluster.
<transformed> = <preprocessing>.StandardScaler().fit_transform(X) # Scales X to have mean 0 and variance 1.

Keras

A high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.

<model> = keras.models.Sequential()                # A linear stack of layers.
<model>.add(keras.layers.Layer())                  # Adds a layer to the model.
<layer_output> = <layer>(<input>)                   # Applies the layer to an input tensor.
<model>.compile()                                  # Configures the model for training.
<model>.fit(<x_train>, <y_train>)                   # Trains the model on training data.
<model>.evaluate(<x_test>, <y_test>)                # Evaluates the model on test data.
<model>.predict(<x>)                               # Generates predictions for new data.
<layer> = keras.layers.Layer()                      # Base class for Keras layers.
<layer> = keras.layers.Dense(<units>, activation)   # Fully connected layer.
<layer> = keras.layers.Conv2D(<filters>, <kernel>)  # 2D convolutional layer.
<layer> = keras.layers.LSTM(<units>)                # Long short-term memory layer.
<activation> = keras.activations.<name>(<x>)        # Applies an activation function to input.
<regularizer> = keras.regularizers.<name>()         # Regularizer for kernel or layer activity.
<optimizer> = keras.optimizers.<name>()             # Optimizer for model training.
<loss_fn> = keras.losses.<name>()                   # Loss function for model training.
<metric_fn> = keras.metrics.<name>()                # Metric to evaluate model performance.
<callback> = keras.callbacks.<name>(<args>)         # Callback for Keras model training.
<tensorboard> = keras.callbacks.TensorBoard(<dir>)  # TensorBoard visualizations.
<checkpoint> = keras.callbacks.ModelCheckpoint(<path>) # Saves model checkpoints.
<early_stopping> = keras.callbacks.EarlyStopping()  # Stops training when monitored quantity stops improving.
<dataset> = keras.utils.<name>(<args>)              # Generates a Keras dataset.
<one_hot> = keras.utils.to_categorical(<y>, <num_classes>) # Converts a class vector to binary class matrix.

Scraping

Requests

A Python library for sending HTTP requests and working with APIs.

<requests.Response> = requests.get(url, params=None, **kwargs)   # Sends GET request to the given URL.
<requests.Response> = requests.post(url, data=None, json=None, **kwargs)  # Sends POST request to the given URL.
<requests.Response> = requests.put(url, data=None, **kwargs)    # Sends PUT request to the given URL.
<requests.Response> = requests.patch(url, data=None, **kwargs)  # Sends PATCH request to the given URL.
<requests.Response> = requests.delete(url, **kwargs)            # Sends DELETE request to the given URL.
<requests.Response> = requests.head(url, **kwargs)              # Sends HEAD request to the given URL.
<requests.Response> = requests.options(url, **kwargs)           # Sends OPTIONS request to the given URL.
<requests.Response>.status_code                       # Returns the status code of the response.
<requests.Response>.text                              # Returns the response content as a string.
<requests.Response>.content                           # Returns the response content in bytes.
<requests.Response>.json()                            # Returns the JSON-encoded content of the response.
<requests.Response>.headers                           # Returns a dictionary of the response headers.
<requests.Response>.url                               # Returns the URL of the response.
<requests.Response>.request.method                   # Returns the HTTP method of the request.
<requests.Response>.request.url                      # Returns the URL of the request.
<requests.Response>.request.headers                  # Returns a dictionary of the request headers.
<requests.Response>.request.body                     # Returns the body of the request.
<requests.Response>.raise_for_status()                # Raises an HTTPError if status code is not ok.

BeautifulSoup

A Python library for parsing HTML and XML documents.

<soup> = BeautifulSoup(<html> [, <parser>])         # Parses HTML and returns a BeautifulSoup obj.
<el>   = <soup>.find(<tag> [, <attrs>])             # Returns the first <tag> element that matches <attrs>.
<iter> = <soup>.find_all(<tag> [, <attrs>])         # Returns a generator of all <tag> elements that match <attrs>.
<el>   = <soup>.select_one(<selector>)               # Returns the first element that matches <selector>.
<iter> = <soup>.select(<selector>)                   # Returns a generator of all elements that match <selector>.
<el>   = <soup>.new_tag(<tag>)                       # Creates a new element <tag>.
<el>   = <soup>.new_string(<string> [, <markup>])    # Creates a new NavigableString object containing <string>.
<el>   = <soup>.new_comment(<string> [, <markup>])   # Creates a new Comment object containing <string>.
<el>   = <soup>.new_tag('a', href='/url', id='link') # Creates a new <a> element with href and id attributes.
<el>   = <soup>.insert(<index>, <el>)                # Inserts <el> at <index> in the BeautifulSoup obj.
<el>   = <soup>.append(<el>)                         # Adds <el> as a child of the BeautifulSoup obj.
<el>   = <soup>.prepend(<el>)                        # Adds <el> as the first child of the BeautifulSoup obj.
<el>   = <soup>.extract()                            # Removes the BeautifulSoup obj from its parent.
  • Note that '<tag>' and '<attrs>' in this case represent a string, a function, a regular expression or a dictionary. '<selector>' is a string that uses CSS selectors to find elements.

Selenium

A browser automation tool, primarily used for testing web applications.

<webdriver> = webdriver.<browser>()   # Returns a new webdriver instance using the specified browser.
<webdriver>.get(<url>)                # Loads a web page in the current browser session.
<webdriver>.find_element_by_<method>(<selector>)   # Returns the first element matching the specified selector.
<webdriver>.find_elements_by_<method>(<selector>)  # Returns a list of elements matching the specified selector.
<element>.send_keys(<text>)           # Types the specified text into the element.
<element>.click()                     # Clicks the element.
<element>.text                       # Returns the text content of the element.
<webdriver>.switch_to.<frame/window/alert>(<identifier>)  # Switches to the specified frame, window or alert.
<webdriver>.execute_script(<script>)  # Executes the specified JavaScript code in the context of the current page.
<webdriver>.back()                    # Navigates the browser back to the previous page.
<webdriver>.forward()                 # Navigates the browser forward to the next page.
<webdriver>.refresh()                 # Refreshes the current page.
  • Note: '<browser>' and '<method>' are placeholders for the actual names of the browser and method, respectively. '<selector>' and '<identifier>' are placeholders for the selector or identifier used to locate elements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment