From 9c5247d10479ae4823d8f7540ab8ea7ddcac37d8 Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 12 Feb 2023 15:22:15 -0500 Subject: [PATCH 1/9] initial commit --- .../labgraph_pilot/labgraph_pilot/__init__.py | 0 .../labgraph_pilot/description_to_code.py | 0 .../labgraph_pilot/keyword_generation.py | 0 .../labgraph_pilot/labgraph_pilot/setup.py | 21 +++++++++++++++++++ 4 files changed, 21 insertions(+) create mode 100644 extensions/labgraph_pilot/labgraph_pilot/__init__.py create mode 100644 extensions/labgraph_pilot/labgraph_pilot/description_to_code.py create mode 100644 extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py create mode 100644 extensions/labgraph_pilot/labgraph_pilot/setup.py diff --git a/extensions/labgraph_pilot/labgraph_pilot/__init__.py b/extensions/labgraph_pilot/labgraph_pilot/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py b/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py new file mode 100644 index 000000000..e69de29bb diff --git a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py new file mode 100644 index 000000000..e69de29bb diff --git a/extensions/labgraph_pilot/labgraph_pilot/setup.py b/extensions/labgraph_pilot/labgraph_pilot/setup.py new file mode 100644 index 000000000..d36b0c474 --- /dev/null +++ b/extensions/labgraph_pilot/labgraph_pilot/setup.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +# Copyright 2004-present Facebook. All Rights Reserved. + +from setuptools import find_packages, setup + + +setup( + name="labgraph_pilot", + version="1.0.0", + description="", + packages=find_packages(), + python_requires=">=3.6, <3.7", + install_requires=[ + "dataclasses==0.6", + "labgraph>=1.0.2", + "matplotlib==3.1.1", + "numpy==1.16.4", + "PyQt5-sip==12.8.1", + "pyqtgraph==0.11.1", + ], +) \ No newline at end of file From 368e7cb45a4523790aef5e8c610d25585c3063ca Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 12 Feb 2023 15:40:27 -0500 Subject: [PATCH 2/9] send request to OpenAI API (with a prompt as a paramater) to get the generated recommended code --- .../labgraph_pilot/description_to_code.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py b/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py index e69de29bb..9561d8cfb 100644 --- a/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py +++ b/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py @@ -0,0 +1,38 @@ +import requests +import json +import os +from dotenv import load_dotenv + +load_dotenv() + +class DescriptionToCode: + """ + A class that send the request to OpenAI API with a prompt to get back the code + """ + def __init__(self): + self.prompt = "Provide well-engineered code for" + self.model = "text-davinci-003" + self.max_tokens = 500 # maximum length for the response from the API request + self.temperature = 0 + self.api_key = os.getenv('OPENAI_API_KEY') + + def get_code_from_description(self, api_name): + url = "https://api.openai.com/v1/completions" + + payload = json.dumps({ + "model": self.model, + "prompt": self.prompt + api_name, + "max_tokens": 500, # handcrafted parameter + "temperature": 0 + }) + + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + self.api_key + } + + response = requests.request("POST", url, headers=headers, data=payload) + json_data = json.loads(response.text) + generated_code = json_data['choices'][0]['text'] + + return generated_code \ No newline at end of file From ac18ed82ba596fc02833b511b8284532ec57b357 Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 12 Feb 2023 20:58:24 -0500 Subject: [PATCH 3/9] [WIP] extract keywords from scraped websites' contents --- .../labgraph_pilot/keyword_generation.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py index e69de29bb..881658d3e 100644 --- a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py +++ b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py @@ -0,0 +1,32 @@ +# generate the list names from predefined urls +# sicpy, numpy, sklearn +import requests +from bs4 import BeautifulSoup + +class KeywordGeneration: + + ''' + A class that generates lists of APIs for different categories + (hardware devices, signal processing, etc.) + ''' + def __init__(self) -> None: + self.predefined_urls = ['https://docs.scipy.org/doc/scipy/reference/signal.html', + # 'https://numpy.org/doc/stable/reference/routines.ma.html', + # 'https://numpy.org/doc/stable/reference/routines.math.html', + # 'https://numpy.org/doc/1.24/reference/routines.html', + # 'https://scikit-learn.org/stable/modules/classes.html#module-sklearn.preprocessing', + ] + self.signal_processing_apis = [] + self.numpy_apis = [] + self.sklearn_apis = [] + def get_html_from_urls(self): + for url in self.predefined_urls: + page = requests.get(url) + soup = BeautifulSoup(page.text, features="lxml") + + keywords_elements = soup.find_all("tr") + for keyword in keywords_elements: + # TODO: extract the 'keyword' + +test = KeywordGeneration() +test.get_html_from_urls() \ No newline at end of file From 569002a05ddaaa462df3d354b985cb9bfab5d6db Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Thu, 16 Feb 2023 21:39:00 -0500 Subject: [PATCH 4/9] use BeautifulSoup to extract the keywords --- .../labgraph_pilot/keyword_generation.py | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py index 881658d3e..8e50d8bd1 100644 --- a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py +++ b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py @@ -11,22 +11,33 @@ class KeywordGeneration: ''' def __init__(self) -> None: self.predefined_urls = ['https://docs.scipy.org/doc/scipy/reference/signal.html', - # 'https://numpy.org/doc/stable/reference/routines.ma.html', - # 'https://numpy.org/doc/stable/reference/routines.math.html', - # 'https://numpy.org/doc/1.24/reference/routines.html', - # 'https://scikit-learn.org/stable/modules/classes.html#module-sklearn.preprocessing', + 'https://numpy.org/doc/stable/reference/routines.ma.html', + 'https://numpy.org/doc/stable/reference/routines.math.html', + 'https://numpy.org/doc/1.24/reference/routines.html', + 'https://scikit-learn.org/stable/modules/classes.html#module-sklearn.preprocessing', ] - self.signal_processing_apis = [] - self.numpy_apis = [] - self.sklearn_apis = [] - def get_html_from_urls(self): + self.keywords = [] + + ''' + This function gets the html content from the predefined urls using requests library. Then use BeautifulSoup to + extract the needed keywords + ''' + + def extract_keywords(self): + for url in self.predefined_urls: page = requests.get(url) soup = BeautifulSoup(page.text, features="lxml") keywords_elements = soup.find_all("tr") - for keyword in keywords_elements: - # TODO: extract the 'keyword' + + for element in keywords_elements: + for tag in element.recursiveChildGenerator(): + if hasattr(tag, 'attrs'): + for key, value in tag.attrs.items(): + if key == 'title': + self.keywords.append(value) + print(self.keywords) test = KeywordGeneration() -test.get_html_from_urls() \ No newline at end of file +test.extract_keywords() \ No newline at end of file From b15ece94ef9067ee87206ac1dc2382878c18f4ed Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Fri, 24 Feb 2023 21:13:02 -0500 Subject: [PATCH 5/9] finish getting generated code for each API --- .../labgraph_pilot/description_to_code.py | 24 ++++++++++++++++++- .../labgraph_pilot/keyword_generation.py | 5 ++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py b/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py index 9561d8cfb..144ec41fc 100644 --- a/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py +++ b/extensions/labgraph_pilot/labgraph_pilot/description_to_code.py @@ -5,6 +5,8 @@ load_dotenv() +from keyword_generation import KeywordGeneration + class DescriptionToCode: """ A class that send the request to OpenAI API with a prompt to get back the code @@ -35,4 +37,24 @@ def get_code_from_description(self, api_name): json_data = json.loads(response.text) generated_code = json_data['choices'][0]['text'] - return generated_code \ No newline at end of file + return generated_code + + ''' + Main function of generating the code for each API name and save it to a local file + ''' + def get_code(self): + keywordGeneration = KeywordGeneration() + apis = keywordGeneration.extract_keywords() + + with open('output.txt', 'w') as file: + code_blocks = [] + + for api in apis: + code_block = self.get_code_from_description(api) + code_blocks.append(code_block) + + file.writelines("% s\n" % code_block for code_block in code_blocks) + +# create a test object to run the get_code function +test = DescriptionToCode() +test.get_code() \ No newline at end of file diff --git a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py index 8e50d8bd1..bb300069e 100644 --- a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py +++ b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py @@ -22,7 +22,7 @@ def __init__(self) -> None: This function gets the html content from the predefined urls using requests library. Then use BeautifulSoup to extract the needed keywords ''' - + def extract_keywords(self): for url in self.predefined_urls: @@ -37,7 +37,6 @@ def extract_keywords(self): for key, value in tag.attrs.items(): if key == 'title': self.keywords.append(value) - print(self.keywords) - + return self.keywords test = KeywordGeneration() test.extract_keywords() \ No newline at end of file From 8b878cc025a3665c6819df30d28b78afce675f38 Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 5 Mar 2023 20:32:07 -0500 Subject: [PATCH 6/9] save keywords to a text file --- .../labgraph_pilot/keyword_generation.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py index bb300069e..176482862 100644 --- a/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py +++ b/extensions/labgraph_pilot/labgraph_pilot/keyword_generation.py @@ -37,6 +37,16 @@ def extract_keywords(self): for key, value in tag.attrs.items(): if key == 'title': self.keywords.append(value) + + self.keywords.remove('(in joblib v1.3.0.dev0)') # remove unexpected parsed keyword + + with open('keyword.txt', 'w') as file: # save all keywords to a text file + keywords = [] + + for keyword in self.keywords: + keywords.append(keyword) + + file.writelines("% s\n" % keyword for keyword in keywords) return self.keywords test = KeywordGeneration() test.extract_keywords() \ No newline at end of file From 3c696abdb3eabaff3c00ece5fde91952c50ade8e Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 5 Mar 2023 20:35:11 -0500 Subject: [PATCH 7/9] full list of keywords --- output.txt | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 output.txt diff --git a/output.txt b/output.txt new file mode 100644 index 000000000..980602bfb --- /dev/null +++ b/output.txt @@ -0,0 +1,123 @@ + + +import numpy as np +from scipy.signal import convolve + +# Create two arrays +x = np.array([1, 2, 3, 4]) +h = np.array([1, 2, 3]) + +# Perform convolution +y = convolve(x, h) + +# Print result +print(y) + + +import numpy as np +from scipy import signal + +def correlate(x, y): + # Compute the correlation between two signals + # x and y must be the same length + # x and y should be numpy arrays + return signal.correlate(x, y, mode='full') + +# Example usage +x = np.array([1, 2, 3, 4, 5]) +y = np.array([2, 3, 4, 5, 6]) + +correlation = correlate(x, y) +print(correlation) + + +import numpy as np +from scipy.signal import fftconvolve + +def fftconvolve(x, y): + """ + Computes the convolution of two 1-dimensional sequences using the + Fast Fourier Transform (FFT). + + Parameters + ---------- + x : array_like + First input array. + y : array_like + Second input array. + + Returns + ------- + out : ndarray + Discrete linear convolution of `x` and `y`. + + Examples + -------- + Compute the linear convolution of two length-3 sequences: + + >>> x = np.array([1, 2, 3]) + >>> y = np.array([0, 1, 0.5]) + >>> np.convolve(x, y) + array([ 0. , 2. , 5.5, 4. , 1.5]) + + Note that convolve computes the full convolution, while fftconvolve + computes the linear convolution. + + >>> np.fftconvolve(x, y) + array([ 2. , 5.5, 4. ]) + + """ + x = np.asarray(x) + y = np.asarray(y) + + if np.ndim(x) > 1 or np.ndim(y) > 1: + raise ValueError("x and y should be 1-dimensional.") + + if x.size == 0 or y.size == 0: + return np.array([]) + + # Compute the FFT of the two sequences + fx = np.fft.fft(x, axis=0) + fy = np.fft.fft(y, axis=0) + + # Multiply the FFTs and inverse transform the result + z = np.fft.ifft(fx * fy, axis=0) + + # Trim the result to the correct length + return z[:x.size + y.size - 1] + + +import numpy as np +from scipy.signal import oaconvolve + +# Create a signal +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +# Create a filter +h = np.array([1, 2, 3]) + +# Perform the convolution +y = oaconvolve(x, h) + +# Print the result +print(y) + + +import numpy as np +from scipy.signal import convolve2d + +# Create a 2D array +arr = np.array([[1, 2, 3], + [4, 5, 6], + [7, 8, 9]]) + +# Create a 3x3 kernel +kernel = np.array([[1, 0, 0], + [0, 1, 0], + [0, 0, 1]]) + +# Perform convolution +convolved_arr = convolve2d(arr, kernel, mode='same') + +# Print the result +print(convolved_arr) From 1a80d010bd9cf6474bd2c51cdbc012efd94da35c Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 5 Mar 2023 20:38:22 -0500 Subject: [PATCH 8/9] correct the keyword file --- keyword.txt | 1013 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1013 insertions(+) create mode 100644 keyword.txt diff --git a/keyword.txt b/keyword.txt new file mode 100644 index 000000000..484c8cd59 --- /dev/null +++ b/keyword.txt @@ -0,0 +1,1013 @@ +scipy.signal.convolve +scipy.signal.correlate +scipy.signal.fftconvolve +scipy.signal.oaconvolve +scipy.signal.convolve2d +scipy.signal.correlate2d +scipy.signal.sepfir2d +scipy.signal.choose_conv_method +scipy.signal.correlation_lags +scipy.signal.bspline +scipy.signal.cubic +scipy.signal.quadratic +scipy.signal.gauss_spline +scipy.signal.cspline1d +scipy.signal.qspline1d +scipy.signal.cspline2d +scipy.signal.qspline2d +scipy.signal.cspline1d_eval +scipy.signal.qspline1d_eval +scipy.signal.spline_filter +scipy.signal.order_filter +scipy.signal.medfilt +scipy.signal.medfilt2d +scipy.signal.wiener +scipy.signal.symiirorder1 +scipy.signal.symiirorder2 +scipy.signal.lfilter +scipy.signal.lfiltic +scipy.signal.lfilter_zi +scipy.signal.filtfilt +scipy.signal.savgol_filter +scipy.signal.deconvolve +scipy.signal.sosfilt +scipy.signal.sosfilt_zi +scipy.signal.sosfiltfilt +scipy.signal.hilbert +scipy.signal.hilbert2 +scipy.signal.decimate +scipy.signal.detrend +scipy.signal.resample +scipy.signal.resample_poly +scipy.signal.upfirdn +scipy.signal.bilinear +scipy.signal.bilinear_zpk +scipy.signal.findfreqs +scipy.signal.firls +scipy.signal.firwin +scipy.signal.firwin2 +scipy.signal.freqs +scipy.signal.freqs_zpk +scipy.signal.freqz +scipy.signal.freqz_zpk +scipy.signal.sosfreqz +scipy.signal.gammatone +scipy.signal.group_delay +scipy.signal.iirdesign +scipy.signal.iirfilter +scipy.signal.kaiser_atten +scipy.signal.kaiser_beta +scipy.signal.kaiserord +scipy.signal.minimum_phase +scipy.signal.savgol_coeffs +scipy.signal.remez +scipy.signal.unique_roots +scipy.signal.residue +scipy.signal.residuez +scipy.signal.invres +scipy.signal.invresz +scipy.signal.BadCoefficients +scipy.signal.abcd_normalize +scipy.signal.band_stop_obj +scipy.signal.besselap +scipy.signal.buttap +scipy.signal.cheb1ap +scipy.signal.cheb2ap +scipy.signal.cmplx_sort +scipy.signal.ellipap +scipy.signal.lp2bp +scipy.signal.lp2bp_zpk +scipy.signal.lp2bs +scipy.signal.lp2bs_zpk +scipy.signal.lp2hp +scipy.signal.lp2hp_zpk +scipy.signal.lp2lp +scipy.signal.lp2lp_zpk +scipy.signal.normalize +scipy.signal.butter +scipy.signal.buttord +scipy.signal.cheby1 +scipy.signal.cheb1ord +scipy.signal.cheby2 +scipy.signal.cheb2ord +scipy.signal.ellip +scipy.signal.ellipord +scipy.signal.bessel +scipy.signal.iirnotch +scipy.signal.iirpeak +scipy.signal.iircomb +scipy.signal.lti +scipy.signal.StateSpace +scipy.signal.TransferFunction +scipy.signal.ZerosPolesGain +scipy.signal.lsim +scipy.signal.lsim2 +scipy.integrate.odeint +scipy.signal.impulse +scipy.signal.impulse2 +scipy.signal.step +scipy.signal.step2 +scipy.signal.freqresp +scipy.signal.bode +scipy.signal.dlti +scipy.signal.StateSpace +scipy.signal.TransferFunction +scipy.signal.ZerosPolesGain +scipy.signal.dlsim +scipy.signal.dimpulse +scipy.signal.dstep +scipy.signal.dfreqresp +scipy.signal.dbode +scipy.signal.tf2zpk +scipy.signal.tf2sos +scipy.signal.tf2ss +scipy.signal.zpk2tf +scipy.signal.zpk2sos +scipy.signal.zpk2ss +scipy.signal.ss2tf +scipy.signal.ss2zpk +scipy.signal.sos2zpk +scipy.signal.sos2tf +scipy.signal.cont2discrete +scipy.signal.place_poles +scipy.signal.chirp +scipy.signal.gausspulse +scipy.signal.max_len_seq +scipy.signal.sawtooth +scipy.signal.square +scipy.signal.sweep_poly +scipy.signal.unit_impulse +scipy.signal.get_window +scipy.signal.cascade +scipy.signal.daub +scipy.signal.morlet +scipy.signal.qmf +scipy.signal.ricker +scipy.signal.morlet2 +scipy.signal.cwt +scipy.signal.cwt +scipy.signal.argrelmin +scipy.signal.argrelmax +scipy.signal.argrelextrema +scipy.signal.find_peaks +scipy.signal.find_peaks_cwt +scipy.signal.peak_prominences +scipy.signal.peak_widths +scipy.signal.periodogram +scipy.signal.welch +scipy.signal.csd +scipy.signal.coherence +scipy.signal.spectrogram +scipy.signal.lombscargle +scipy.signal.vectorstrength +scipy.signal.stft +scipy.signal.istft +scipy.signal.check_COLA +scipy.signal.check_NOLA +scipy.signal.czt +scipy.signal.zoom_fft +scipy.signal.CZT +scipy.signal.ZoomFFT +scipy.signal.czt_points +numpy.ma.MaskType +numpy.bool_ +numpy.ma.masked_array +numpy.ma.array +numpy.ma.copy +numpy.ma.frombuffer +numpy.ma.fromfunction +numpy.ma.MaskedArray.copy +numpy.ma.empty +numpy.ma.empty_like +numpy.ma.masked_all +numpy.ma.masked_all_like +numpy.ma.ones +numpy.ma.ones_like +numpy.ma.zeros +numpy.ma.zeros_like +numpy.ma.all +numpy.ma.any +numpy.ma.count +numpy.ma.count_masked +numpy.ma.getmask +numpy.ma.getmaskarray +numpy.ma.getdata +numpy.ma.nonzero +numpy.ma.shape +numpy.ma.size +numpy.ma.is_masked +numpy.ma.is_mask +numpy.ma.isMaskedArray +numpy.ma.isMA +numpy.ma.isarray +numpy.ma.MaskedArray.all +numpy.ma.MaskedArray.any +numpy.ma.MaskedArray.count +numpy.ma.MaskedArray.nonzero +numpy.ma.shape +numpy.ma.size +numpy.ma.MaskedArray.data +numpy.ma.MaskedArray.mask +numpy.ma.MaskedArray.recordmask +numpy.ma.ravel +numpy.ma.reshape +numpy.ma.resize +numpy.ma.MaskedArray.flatten +numpy.ma.MaskedArray.ravel +numpy.ma.MaskedArray.reshape +numpy.ma.MaskedArray.resize +numpy.ma.swapaxes +numpy.ma.transpose +numpy.ma.MaskedArray.swapaxes +numpy.ma.MaskedArray.transpose +numpy.ma.atleast_1d +numpy.ma.atleast_2d +numpy.ma.atleast_3d +numpy.ma.expand_dims +numpy.ma.squeeze +numpy.ma.MaskedArray.squeeze +numpy.ma.stack +numpy.ma.column_stack +numpy.ma.concatenate +numpy.ma.dstack +numpy.ma.hstack +numpy.ma.hsplit +numpy.ma.mr_ +numpy.ma.row_stack +numpy.ma.vstack +numpy.ma.concatenate +numpy.ma.stack +numpy.ma.vstack +numpy.ma.hstack +numpy.ma.dstack +numpy.ma.column_stack +numpy.ma.append +numpy.ma.make_mask +numpy.ma.make_mask_none +numpy.ma.mask_or +numpy.ma.make_mask_descr +numpy.ma.getmask +numpy.ma.getmaskarray +numpy.ma.masked_array.mask +numpy.ma.ndenumerate +numpy.ma.flatnotmasked_contiguous +numpy.ma.flatnotmasked_edges +numpy.ma.notmasked_contiguous +numpy.ma.notmasked_edges +numpy.ma.clump_masked +numpy.ma.clump_unmasked +numpy.ma.mask_cols +numpy.ma.mask_or +numpy.ma.mask_rowcols +numpy.ma.mask_rows +numpy.ma.harden_mask +numpy.ma.soften_mask +numpy.ma.MaskedArray.harden_mask +numpy.ma.MaskedArray.soften_mask +numpy.ma.MaskedArray.shrink_mask +numpy.ma.MaskedArray.unshare_mask +numpy.ma.asarray +numpy.ma.asanyarray +numpy.ma.fix_invalid +numpy.ma.masked_equal +numpy.ma.masked_greater +numpy.ma.masked_greater_equal +numpy.ma.masked_inside +numpy.ma.masked_invalid +numpy.ma.masked_less +numpy.ma.masked_less_equal +numpy.ma.masked_not_equal +numpy.ma.masked_object +numpy.ma.masked_outside +numpy.ma.masked_values +numpy.ma.masked_where +numpy.ma.compress_cols +numpy.ma.compress_rowcols +numpy.ma.compress_rows +numpy.ma.compressed +numpy.ma.filled +numpy.ma.MaskedArray.compressed +numpy.ma.MaskedArray.filled +numpy.ma.MaskedArray.tofile +numpy.ma.MaskedArray.tolist +numpy.ma.MaskedArray.torecords +numpy.ma.MaskedArray.tobytes +numpy.ma.common_fill_value +numpy.ma.default_fill_value +numpy.ma.maximum_fill_value +numpy.ma.minimum_fill_value +numpy.ma.set_fill_value +numpy.ma.MaskedArray.get_fill_value +numpy.ma.MaskedArray.set_fill_value +numpy.ma.MaskedArray.fill_value +numpy.ma.anom +numpy.ma.anomalies +numpy.ma.average +numpy.ma.conjugate +numpy.ma.corrcoef +numpy.ma.cov +numpy.ma.cumsum +numpy.ma.cumprod +numpy.ma.mean +numpy.ma.median +numpy.ma.power +numpy.ma.prod +numpy.ma.std +numpy.ma.sum +numpy.ma.var +numpy.ma.MaskedArray.anom +numpy.ma.MaskedArray.cumprod +numpy.ma.MaskedArray.cumsum +numpy.ma.MaskedArray.mean +numpy.ma.MaskedArray.prod +numpy.ma.MaskedArray.std +numpy.ma.MaskedArray.sum +numpy.ma.MaskedArray.var +numpy.ma.argmax +numpy.ma.argmin +numpy.ma.max +numpy.ma.min +numpy.ma.ptp +numpy.ma.diff +numpy.ma.MaskedArray.argmax +numpy.ma.MaskedArray.argmin +numpy.ma.MaskedArray.max +numpy.ma.MaskedArray.min +numpy.ma.MaskedArray.ptp +numpy.ma.argsort +numpy.ma.sort +numpy.ma.MaskedArray.argsort +numpy.ma.MaskedArray.sort +numpy.ma.diag +numpy.ma.dot +numpy.ma.identity +numpy.ma.inner +numpy.ma.innerproduct +numpy.ma.outer +numpy.ma.outerproduct +numpy.ma.trace +numpy.ma.transpose +numpy.ma.MaskedArray.trace +numpy.ma.MaskedArray.transpose +numpy.ma.vander +numpy.ma.polyfit +numpy.ma.around +numpy.ma.clip +numpy.ma.round +numpy.ma.MaskedArray.clip +numpy.ma.MaskedArray.round +numpy.ma.allequal +numpy.ma.allclose +numpy.ma.apply_along_axis +numpy.ma.apply_over_axes +numpy.ma.arange +numpy.ma.choose +numpy.ma.ediff1d +numpy.ma.indices +numpy.ma.where +numpy.sin +numpy.cos +numpy.tan +numpy.arcsin +numpy.arccos +numpy.arctan +numpy.hypot +numpy.arctan2 +numpy.degrees +numpy.radians +numpy.unwrap +numpy.deg2rad +numpy.rad2deg +numpy.sinh +numpy.cosh +numpy.tanh +numpy.arcsinh +numpy.arccosh +numpy.arctanh +numpy.around +numpy.rint +numpy.fix +numpy.floor +numpy.ceil +numpy.trunc +numpy.prod +numpy.sum +numpy.nanprod +numpy.nansum +numpy.cumprod +numpy.cumsum +numpy.nancumprod +numpy.nancumsum +numpy.diff +numpy.ediff1d +numpy.gradient +numpy.cross +numpy.trapz +numpy.exp +numpy.expm1 +numpy.exp2 +numpy.log +numpy.log10 +numpy.log2 +numpy.log1p +numpy.logaddexp +numpy.logaddexp2 +numpy.i0 +numpy.sinc +numpy.signbit +numpy.copysign +numpy.frexp +numpy.ldexp +numpy.nextafter +numpy.spacing +numpy.lcm +numpy.gcd +numpy.add +numpy.reciprocal +numpy.positive +numpy.negative +numpy.multiply +numpy.divide +numpy.power +numpy.subtract +numpy.true_divide +numpy.floor_divide +numpy.float_power +numpy.fmod +numpy.mod +numpy.modf +numpy.remainder +numpy.divmod +numpy.angle +numpy.real +numpy.imag +numpy.conj +numpy.conjugate +numpy.maximum +numpy.fmax +numpy.amax +numpy.nanmax +numpy.minimum +numpy.fmin +numpy.amin +numpy.nanmin +numpy.convolve +numpy.clip +numpy.sqrt +numpy.cbrt +numpy.square +numpy.absolute +numpy.fabs +numpy.sign +numpy.heaviside +numpy.nan_to_num +numpy.nan +numpy.real_if_close +numpy.interp +sklearn.base.BaseEstimator +sklearn.base.BiclusterMixin +sklearn.base.ClassifierMixin +sklearn.base.ClusterMixin +sklearn.base.DensityMixin +sklearn.base.RegressorMixin +sklearn.base.TransformerMixin +sklearn.base.OneToOneFeatureMixin +sklearn.base.ClassNamePrefixFeaturesOutMixin +sklearn.feature_selection.SelectorMixin +sklearn.base.clone +sklearn.base.is_classifier +sklearn.base.is_regressor +sklearn.config_context +sklearn.get_config +sklearn.set_config +sklearn.set_config +sklearn.show_versions +sklearn.calibration.CalibratedClassifierCV +sklearn.calibration.calibration_curve +sklearn.cluster.AffinityPropagation +sklearn.cluster.AgglomerativeClustering +sklearn.cluster.Birch +sklearn.cluster.DBSCAN +sklearn.cluster.FeatureAgglomeration +sklearn.cluster.KMeans +sklearn.cluster.BisectingKMeans +sklearn.cluster.MiniBatchKMeans +sklearn.cluster.MeanShift +sklearn.cluster.OPTICS +sklearn.cluster.SpectralClustering +sklearn.cluster.SpectralBiclustering +sklearn.cluster.SpectralCoclustering +sklearn.cluster.affinity_propagation +sklearn.cluster.cluster_optics_dbscan +sklearn.cluster.cluster_optics_xi +sklearn.cluster.compute_optics_graph +sklearn.cluster.dbscan +sklearn.cluster.estimate_bandwidth +sklearn.cluster.k_means +sklearn.cluster.kmeans_plusplus +sklearn.cluster.mean_shift +sklearn.cluster.spectral_clustering +sklearn.cluster.ward_tree +sklearn.compose.ColumnTransformer +sklearn.compose.TransformedTargetRegressor +sklearn.compose.make_column_transformer +sklearn.compose.make_column_selector +sklearn.covariance.EmpiricalCovariance +sklearn.covariance.EllipticEnvelope +sklearn.covariance.GraphicalLasso +sklearn.covariance.GraphicalLassoCV +sklearn.covariance.LedoitWolf +sklearn.covariance.MinCovDet +sklearn.covariance.OAS +sklearn.covariance.ShrunkCovariance +sklearn.covariance.empirical_covariance +sklearn.covariance.graphical_lasso +sklearn.covariance.ledoit_wolf +sklearn.covariance.oas +sklearn.covariance.shrunk_covariance +sklearn.cross_decomposition.CCA +sklearn.cross_decomposition.PLSCanonical +sklearn.cross_decomposition.PLSRegression +sklearn.cross_decomposition.PLSSVD +sklearn.datasets.clear_data_home +sklearn.datasets.dump_svmlight_file +sklearn.datasets.fetch_20newsgroups +sklearn.datasets.fetch_20newsgroups_vectorized +sklearn.datasets.fetch_california_housing +sklearn.datasets.fetch_covtype +sklearn.datasets.fetch_kddcup99 +sklearn.datasets.fetch_lfw_pairs +sklearn.datasets.fetch_lfw_people +sklearn.datasets.fetch_olivetti_faces +sklearn.datasets.fetch_openml +sklearn.datasets.fetch_rcv1 +sklearn.datasets.fetch_species_distributions +sklearn.datasets.get_data_home +sklearn.datasets.load_breast_cancer +sklearn.datasets.load_diabetes +sklearn.datasets.load_digits +sklearn.datasets.load_files +sklearn.datasets.load_iris +sklearn.datasets.load_linnerud +sklearn.datasets.load_sample_image +sklearn.datasets.load_sample_images +sklearn.datasets.load_svmlight_file +sklearn.datasets.load_svmlight_files +sklearn.datasets.load_wine +sklearn.datasets.make_biclusters +sklearn.datasets.make_blobs +sklearn.datasets.make_checkerboard +sklearn.datasets.make_circles +sklearn.datasets.make_classification +sklearn.datasets.make_friedman1 +sklearn.datasets.make_friedman2 +sklearn.datasets.make_friedman3 +sklearn.datasets.make_gaussian_quantiles +sklearn.datasets.make_hastie_10_2 +sklearn.datasets.make_low_rank_matrix +sklearn.datasets.make_moons +sklearn.datasets.make_multilabel_classification +sklearn.datasets.make_regression +sklearn.datasets.make_s_curve +sklearn.datasets.make_sparse_coded_signal +sklearn.datasets.make_sparse_spd_matrix +sklearn.datasets.make_sparse_uncorrelated +sklearn.datasets.make_spd_matrix +sklearn.datasets.make_swiss_roll +sklearn.decomposition.DictionaryLearning +sklearn.decomposition.FactorAnalysis +sklearn.decomposition.FastICA +sklearn.decomposition.IncrementalPCA +sklearn.decomposition.KernelPCA +sklearn.decomposition.LatentDirichletAllocation +sklearn.decomposition.MiniBatchDictionaryLearning +sklearn.decomposition.MiniBatchSparsePCA +sklearn.decomposition.NMF +sklearn.decomposition.MiniBatchNMF +sklearn.decomposition.PCA +sklearn.decomposition.SparsePCA +sklearn.decomposition.SparseCoder +sklearn.decomposition.TruncatedSVD +sklearn.decomposition.dict_learning +sklearn.decomposition.dict_learning_online +sklearn.decomposition.fastica +sklearn.decomposition.non_negative_factorization +sklearn.decomposition.sparse_encode +sklearn.discriminant_analysis.LinearDiscriminantAnalysis +sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis +sklearn.dummy.DummyClassifier +sklearn.dummy.DummyRegressor +sklearn.ensemble.AdaBoostClassifier +sklearn.ensemble.AdaBoostRegressor +sklearn.ensemble.BaggingClassifier +sklearn.ensemble.BaggingRegressor +sklearn.ensemble.ExtraTreesClassifier +sklearn.ensemble.ExtraTreesRegressor +sklearn.ensemble.GradientBoostingClassifier +sklearn.ensemble.GradientBoostingRegressor +sklearn.ensemble.IsolationForest +sklearn.ensemble.RandomForestClassifier +sklearn.ensemble.RandomForestRegressor +sklearn.ensemble.RandomTreesEmbedding +sklearn.ensemble.StackingClassifier +sklearn.ensemble.StackingRegressor +sklearn.ensemble.VotingClassifier +sklearn.ensemble.VotingRegressor +sklearn.ensemble.HistGradientBoostingRegressor +sklearn.ensemble.HistGradientBoostingClassifier +sklearn.exceptions.ConvergenceWarning +sklearn.exceptions.DataConversionWarning +sklearn.exceptions.DataDimensionalityWarning +sklearn.exceptions.EfficiencyWarning +sklearn.exceptions.FitFailedWarning +sklearn.exceptions.NotFittedError +sklearn.exceptions.UndefinedMetricWarning +sklearn.experimental.enable_hist_gradient_boosting +sklearn.experimental.enable_iterative_imputer +sklearn.experimental.enable_halving_search_cv +sklearn.feature_extraction.DictVectorizer +sklearn.feature_extraction.FeatureHasher +sklearn.feature_extraction.image.extract_patches_2d +sklearn.feature_extraction.image.grid_to_graph +sklearn.feature_extraction.image.img_to_graph +sklearn.feature_extraction.image.reconstruct_from_patches_2d +sklearn.feature_extraction.image.PatchExtractor +sklearn.feature_extraction.text.CountVectorizer +sklearn.feature_extraction.text.HashingVectorizer +sklearn.feature_extraction.text.TfidfTransformer +sklearn.feature_extraction.text.TfidfVectorizer +sklearn.feature_selection.GenericUnivariateSelect +sklearn.feature_selection.SelectPercentile +sklearn.feature_selection.SelectKBest +sklearn.feature_selection.SelectFpr +sklearn.feature_selection.SelectFdr +sklearn.feature_selection.SelectFromModel +sklearn.feature_selection.SelectFwe +sklearn.feature_selection.SequentialFeatureSelector +sklearn.feature_selection.RFE +sklearn.feature_selection.RFECV +sklearn.feature_selection.VarianceThreshold +sklearn.feature_selection.chi2 +sklearn.feature_selection.f_classif +sklearn.feature_selection.f_regression +sklearn.feature_selection.r_regression +sklearn.feature_selection.mutual_info_classif +sklearn.feature_selection.mutual_info_regression +sklearn.gaussian_process.GaussianProcessClassifier +sklearn.gaussian_process.GaussianProcessRegressor +sklearn.gaussian_process.kernels.CompoundKernel +sklearn.gaussian_process.kernels.ConstantKernel +sklearn.gaussian_process.kernels.DotProduct +sklearn.gaussian_process.kernels.ExpSineSquared +sklearn.gaussian_process.kernels.Exponentiation +sklearn.gaussian_process.kernels.Hyperparameter +sklearn.gaussian_process.kernels.Kernel +sklearn.gaussian_process.kernels.Matern +sklearn.gaussian_process.kernels.PairwiseKernel +sklearn.gaussian_process.kernels.Product +sklearn.gaussian_process.kernels.RBF +sklearn.gaussian_process.kernels.RationalQuadratic +sklearn.gaussian_process.kernels.Sum +sklearn.gaussian_process.kernels.WhiteKernel +sklearn.impute.SimpleImputer +sklearn.impute.IterativeImputer +sklearn.impute.MissingIndicator +sklearn.impute.KNNImputer +sklearn.inspection.partial_dependence +sklearn.inspection.permutation_importance +sklearn.inspection.DecisionBoundaryDisplay +sklearn.inspection.PartialDependenceDisplay +sklearn.isotonic.IsotonicRegression +sklearn.isotonic.check_increasing +sklearn.isotonic.isotonic_regression +sklearn.kernel_approximation.AdditiveChi2Sampler +sklearn.kernel_approximation.Nystroem +sklearn.kernel_approximation.PolynomialCountSketch +sklearn.kernel_approximation.RBFSampler +sklearn.kernel_approximation.SkewedChi2Sampler +sklearn.kernel_ridge.KernelRidge +sklearn.linear_model.LogisticRegression +sklearn.linear_model.LogisticRegressionCV +sklearn.linear_model.PassiveAggressiveClassifier +sklearn.linear_model.Perceptron +sklearn.linear_model.RidgeClassifier +sklearn.linear_model.RidgeClassifierCV +sklearn.linear_model.SGDClassifier +sklearn.linear_model.SGDOneClassSVM +sklearn.linear_model.LinearRegression +sklearn.linear_model.Ridge +sklearn.linear_model.RidgeCV +sklearn.linear_model.SGDRegressor +sklearn.linear_model.ElasticNet +sklearn.linear_model.ElasticNetCV +sklearn.linear_model.Lars +sklearn.linear_model.LarsCV +sklearn.linear_model.Lasso +sklearn.linear_model.LassoCV +sklearn.linear_model.LassoLars +sklearn.linear_model.LassoLarsCV +sklearn.linear_model.LassoLarsIC +sklearn.linear_model.OrthogonalMatchingPursuit +sklearn.linear_model.OrthogonalMatchingPursuitCV +sklearn.linear_model.ARDRegression +sklearn.linear_model.BayesianRidge +sklearn.linear_model.MultiTaskElasticNet +sklearn.linear_model.MultiTaskElasticNetCV +sklearn.linear_model.MultiTaskLasso +sklearn.linear_model.MultiTaskLassoCV +sklearn.linear_model.HuberRegressor +sklearn.linear_model.QuantileRegressor +sklearn.linear_model.RANSACRegressor +sklearn.linear_model.TheilSenRegressor +sklearn.linear_model.PoissonRegressor +sklearn.linear_model.TweedieRegressor +sklearn.linear_model.GammaRegressor +sklearn.linear_model.PassiveAggressiveRegressor +sklearn.linear_model.enet_path +sklearn.linear_model.lars_path +sklearn.linear_model.lars_path_gram +sklearn.linear_model.lasso_path +sklearn.linear_model.orthogonal_mp +sklearn.linear_model.orthogonal_mp_gram +sklearn.linear_model.ridge_regression +sklearn.manifold.Isomap +sklearn.manifold.LocallyLinearEmbedding +sklearn.manifold.MDS +sklearn.manifold.SpectralEmbedding +sklearn.manifold.TSNE +sklearn.manifold.locally_linear_embedding +sklearn.manifold.smacof +sklearn.manifold.spectral_embedding +sklearn.manifold.trustworthiness +sklearn.metrics.check_scoring +sklearn.metrics.get_scorer +sklearn.metrics.get_scorer_names +sklearn.metrics.make_scorer +sklearn.metrics.accuracy_score +sklearn.metrics.auc +sklearn.metrics.average_precision_score +sklearn.metrics.balanced_accuracy_score +sklearn.metrics.brier_score_loss +sklearn.metrics.class_likelihood_ratios +sklearn.metrics.classification_report +sklearn.metrics.cohen_kappa_score +sklearn.metrics.confusion_matrix +sklearn.metrics.dcg_score +sklearn.metrics.det_curve +sklearn.metrics.f1_score +sklearn.metrics.fbeta_score +sklearn.metrics.hamming_loss +sklearn.metrics.hinge_loss +sklearn.metrics.jaccard_score +sklearn.metrics.log_loss +sklearn.metrics.matthews_corrcoef +sklearn.metrics.multilabel_confusion_matrix +sklearn.metrics.ndcg_score +sklearn.metrics.precision_recall_curve +sklearn.metrics.precision_recall_fscore_support +sklearn.metrics.precision_score +sklearn.metrics.recall_score +sklearn.metrics.roc_auc_score +sklearn.metrics.roc_curve +sklearn.metrics.top_k_accuracy_score +sklearn.metrics.zero_one_loss +sklearn.metrics.explained_variance_score +sklearn.metrics.max_error +sklearn.metrics.mean_absolute_error +sklearn.metrics.mean_squared_error +sklearn.metrics.mean_squared_log_error +sklearn.metrics.median_absolute_error +sklearn.metrics.mean_absolute_percentage_error +sklearn.metrics.r2_score +sklearn.metrics.mean_poisson_deviance +sklearn.metrics.mean_gamma_deviance +sklearn.metrics.mean_tweedie_deviance +sklearn.metrics.d2_tweedie_score +sklearn.metrics.mean_pinball_loss +sklearn.metrics.d2_pinball_score +sklearn.metrics.d2_absolute_error_score +sklearn.metrics.coverage_error +sklearn.metrics.label_ranking_average_precision_score +sklearn.metrics.label_ranking_loss +sklearn.metrics.adjusted_mutual_info_score +sklearn.metrics.adjusted_rand_score +sklearn.metrics.calinski_harabasz_score +sklearn.metrics.davies_bouldin_score +sklearn.metrics.completeness_score +sklearn.metrics.cluster.contingency_matrix +sklearn.metrics.cluster.pair_confusion_matrix +sklearn.metrics.fowlkes_mallows_score +sklearn.metrics.homogeneity_completeness_v_measure +sklearn.metrics.homogeneity_score +sklearn.metrics.mutual_info_score +sklearn.metrics.normalized_mutual_info_score +sklearn.metrics.rand_score +sklearn.metrics.silhouette_score +sklearn.metrics.silhouette_samples +sklearn.metrics.v_measure_score +sklearn.metrics.consensus_score +sklearn.metrics.DistanceMetric +sklearn.metrics.pairwise.additive_chi2_kernel +sklearn.metrics.pairwise.chi2_kernel +sklearn.metrics.pairwise.cosine_similarity +sklearn.metrics.pairwise.cosine_distances +sklearn.metrics.pairwise.distance_metrics +sklearn.metrics.pairwise.euclidean_distances +sklearn.metrics.pairwise.haversine_distances +sklearn.metrics.pairwise.kernel_metrics +sklearn.metrics.pairwise.laplacian_kernel +sklearn.metrics.pairwise.linear_kernel +sklearn.metrics.pairwise.manhattan_distances +sklearn.metrics.pairwise.nan_euclidean_distances +sklearn.metrics.pairwise.pairwise_kernels +sklearn.metrics.pairwise.polynomial_kernel +sklearn.metrics.pairwise.rbf_kernel +sklearn.metrics.pairwise.sigmoid_kernel +sklearn.metrics.pairwise.paired_euclidean_distances +sklearn.metrics.pairwise.paired_manhattan_distances +sklearn.metrics.pairwise.paired_cosine_distances +sklearn.metrics.pairwise.paired_distances +sklearn.metrics.pairwise_distances +sklearn.metrics.pairwise_distances_argmin +sklearn.metrics.pairwise_distances_argmin_min +sklearn.metrics.pairwise_distances_chunked +sklearn.metrics.ConfusionMatrixDisplay +sklearn.metrics.DetCurveDisplay +sklearn.metrics.PrecisionRecallDisplay +sklearn.metrics.PredictionErrorDisplay +sklearn.metrics.RocCurveDisplay +sklearn.calibration.CalibrationDisplay +sklearn.mixture.BayesianGaussianMixture +sklearn.mixture.GaussianMixture +sklearn.model_selection.GroupKFold +sklearn.model_selection.GroupShuffleSplit +sklearn.model_selection.KFold +sklearn.model_selection.LeaveOneGroupOut +sklearn.model_selection.LeavePGroupsOut +sklearn.model_selection.LeaveOneOut +sklearn.model_selection.LeavePOut +sklearn.model_selection.PredefinedSplit +sklearn.model_selection.RepeatedKFold +sklearn.model_selection.RepeatedStratifiedKFold +sklearn.model_selection.ShuffleSplit +sklearn.model_selection.StratifiedKFold +sklearn.model_selection.StratifiedShuffleSplit +sklearn.model_selection.StratifiedGroupKFold +sklearn.model_selection.TimeSeriesSplit +sklearn.model_selection.check_cv +sklearn.model_selection.train_test_split +sklearn.model_selection.GridSearchCV +sklearn.model_selection.HalvingGridSearchCV +sklearn.model_selection.ParameterGrid +sklearn.model_selection.ParameterSampler +sklearn.model_selection.RandomizedSearchCV +sklearn.model_selection.HalvingRandomSearchCV +sklearn.model_selection.cross_validate +sklearn.model_selection.cross_val_predict +sklearn.model_selection.cross_val_score +sklearn.model_selection.learning_curve +sklearn.model_selection.permutation_test_score +sklearn.model_selection.validation_curve +sklearn.model_selection.LearningCurveDisplay +sklearn.multiclass.OneVsRestClassifier +sklearn.multiclass.OneVsOneClassifier +sklearn.multiclass.OutputCodeClassifier +sklearn.multioutput.ClassifierChain +sklearn.multioutput.MultiOutputRegressor +sklearn.multioutput.MultiOutputClassifier +sklearn.multioutput.RegressorChain +sklearn.naive_bayes.BernoulliNB +sklearn.naive_bayes.CategoricalNB +sklearn.naive_bayes.ComplementNB +sklearn.naive_bayes.GaussianNB +sklearn.naive_bayes.MultinomialNB +sklearn.neighbors.BallTree +sklearn.neighbors.KDTree +sklearn.neighbors.KernelDensity +sklearn.neighbors.KNeighborsClassifier +sklearn.neighbors.KNeighborsRegressor +sklearn.neighbors.KNeighborsTransformer +sklearn.neighbors.LocalOutlierFactor +sklearn.neighbors.RadiusNeighborsClassifier +sklearn.neighbors.RadiusNeighborsRegressor +sklearn.neighbors.RadiusNeighborsTransformer +sklearn.neighbors.NearestCentroid +sklearn.neighbors.NearestNeighbors +sklearn.neighbors.NeighborhoodComponentsAnalysis +sklearn.neighbors.kneighbors_graph +sklearn.neighbors.radius_neighbors_graph +sklearn.neighbors.sort_graph_by_row_values +sklearn.neural_network.BernoulliRBM +sklearn.neural_network.MLPClassifier +sklearn.neural_network.MLPRegressor +sklearn.pipeline.FeatureUnion +sklearn.pipeline.Pipeline +sklearn.pipeline.make_pipeline +sklearn.pipeline.make_union +sklearn.preprocessing.Binarizer +sklearn.preprocessing.FunctionTransformer +sklearn.preprocessing.KBinsDiscretizer +sklearn.preprocessing.KernelCenterer +sklearn.preprocessing.LabelBinarizer +sklearn.preprocessing.LabelEncoder +sklearn.preprocessing.MultiLabelBinarizer +sklearn.preprocessing.MaxAbsScaler +sklearn.preprocessing.MinMaxScaler +sklearn.preprocessing.Normalizer +sklearn.preprocessing.OneHotEncoder +sklearn.preprocessing.OrdinalEncoder +sklearn.preprocessing.PolynomialFeatures +sklearn.preprocessing.PowerTransformer +sklearn.preprocessing.QuantileTransformer +sklearn.preprocessing.RobustScaler +sklearn.preprocessing.SplineTransformer +sklearn.preprocessing.StandardScaler +sklearn.preprocessing.add_dummy_feature +sklearn.preprocessing.binarize +sklearn.preprocessing.label_binarize +sklearn.preprocessing.maxabs_scale +sklearn.preprocessing.minmax_scale +sklearn.preprocessing.normalize +sklearn.preprocessing.quantile_transform +sklearn.preprocessing.robust_scale +sklearn.preprocessing.scale +sklearn.preprocessing.power_transform +sklearn.random_projection.GaussianRandomProjection +sklearn.random_projection.SparseRandomProjection +sklearn.random_projection.johnson_lindenstrauss_min_dim +sklearn.semi_supervised.LabelPropagation +sklearn.semi_supervised.LabelSpreading +sklearn.semi_supervised.SelfTrainingClassifier +sklearn.svm.LinearSVC +sklearn.svm.LinearSVR +sklearn.svm.NuSVC +sklearn.svm.NuSVR +sklearn.svm.OneClassSVM +sklearn.svm.SVC +sklearn.svm.SVR +sklearn.svm.l1_min_c +sklearn.tree.DecisionTreeClassifier +sklearn.tree.DecisionTreeRegressor +sklearn.tree.ExtraTreeClassifier +sklearn.tree.ExtraTreeRegressor +sklearn.tree.export_graphviz +sklearn.tree.export_text +sklearn.tree.plot_tree +sklearn.utils.Bunch +sklearn.utils.arrayfuncs.min_pos +sklearn.utils.as_float_array +sklearn.utils.assert_all_finite +sklearn.utils.check_X_y +sklearn.utils.check_array +sklearn.utils.check_scalar +sklearn.utils.check_consistent_length +sklearn.utils.check_random_state +sklearn.utils.class_weight.compute_class_weight +sklearn.utils.class_weight.compute_sample_weight +sklearn.utils.deprecated +sklearn.utils.estimator_checks.check_estimator +sklearn.utils.estimator_checks.parametrize_with_checks +sklearn.utils.estimator_html_repr +sklearn.utils.extmath.safe_sparse_dot +sklearn.utils.extmath.randomized_range_finder +sklearn.utils.extmath.randomized_svd +sklearn.utils.extmath.fast_logdet +sklearn.utils.extmath.density +sklearn.utils.extmath.weighted_mode +sklearn.utils.gen_batches +sklearn.utils.gen_even_slices +sklearn.utils.graph.single_source_shortest_path_length +sklearn.utils.indexable +sklearn.utils.metaestimators.available_if +sklearn.utils.multiclass.type_of_target +sklearn.utils.multiclass.is_multilabel +sklearn.utils.multiclass.unique_labels +sklearn.utils.murmurhash3_32 +sklearn.utils.resample +sklearn.utils._safe_indexing +sklearn.utils.safe_mask +sklearn.utils.safe_sqr +sklearn.utils.shuffle +sklearn.utils.sparsefuncs.incr_mean_variance_axis +sklearn.utils.sparsefuncs.inplace_column_scale +sklearn.utils.sparsefuncs.inplace_row_scale +sklearn.utils.sparsefuncs.inplace_swap_row +sklearn.utils.sparsefuncs.inplace_swap_column +sklearn.utils.sparsefuncs.mean_variance_axis +sklearn.utils.sparsefuncs.inplace_csr_column_scale +sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l1 +sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l2 +sklearn.utils.random.sample_without_replacement +sklearn.utils.validation.check_is_fitted +sklearn.utils.validation.check_memory +sklearn.utils.validation.check_symmetric +sklearn.utils.validation.column_or_1d +sklearn.utils.validation.has_fit_parameter +sklearn.utils.discovery.all_estimators +sklearn.utils.discovery.all_displays +sklearn.utils.discovery.all_functions +sklearn.utils.parallel.delayed +sklearn.utils.parallel_backend +sklearn.utils.register_parallel_backend +sklearn.utils.parallel.Parallel +sklearn.utils.metaestimators.if_delegate_has_method From ca265f250363df6790f3204ac28c1146539ef935 Mon Sep 17 00:00:00 2001 From: Mai Ngo Date: Sun, 5 Mar 2023 23:46:11 -0500 Subject: [PATCH 9/9] generated code --- output.txt | 10030 +++++++++++++++++++++++++++++++++++++++++++++++++- output2.txt | 5961 ++++++++++++++++++++++++++++++ output3.txt | 4826 ++++++++++++++++++++++++ 3 files changed, 20793 insertions(+), 24 deletions(-) create mode 100644 output2.txt create mode 100644 output3.txt diff --git a/output.txt b/output.txt index 980602bfb..f5456f0fc 100644 --- a/output.txt +++ b/output.txt @@ -1,3 +1,4 @@ +Generated code for scipy.signal.convolve import numpy as np @@ -12,13 +13,14 @@ y = convolve(x, h) # Print result print(y) +Generated code for scipy.signal.correlate import numpy as np from scipy import signal def correlate(x, y): - # Compute the correlation between two signals + # Compute the correlation of two signals # x and y must be the same length # x and y should be numpy arrays return signal.correlate(x, y, mode='full') @@ -29,6 +31,7 @@ y = np.array([2, 3, 4, 5, 6]) correlation = correlate(x, y) print(correlation) +Generated code for scipy.signal.fftconvolve import numpy as np @@ -53,38 +56,29 @@ def fftconvolve(x, y): Examples -------- - Compute the linear convolution of two length-3 sequences: - - >>> x = np.array([1, 2, 3]) - >>> y = np.array([0, 1, 0.5]) - >>> np.convolve(x, y) - array([ 0. , 2. , 5.5, 4. , 1.5]) - - Note that convolve computes the full convolution, while fftconvolve - computes the linear convolution. - - >>> np.fftconvolve(x, y) - array([ 2. , 5.5, 4. ]) + >>> from scipy.signal import fftconvolve + >>> fftconvolve([1, 2, 3], [0, 1, 0.5]) + array([ 0. , 1.5, 4. , 4.5, 3. ]) """ x = np.asarray(x) y = np.asarray(y) + if x.ndim == y.ndim == 1: + return np.convolve(x, y) + elif x.ndim == y.ndim == 2: + return np.array([np.convolve(x[i], y[i]) for i in range(x.shape[0])]) + else: + raise ValueError("Inputs must be 1 or 2 dimensional.") - if np.ndim(x) > 1 or np.ndim(y) > 1: - raise ValueError("x and y should be 1-dimensional.") - - if x.size == 0 or y.size == 0: - return np.array([]) - - # Compute the FFT of the two sequences + # Compute the FFT of the two inputs fx = np.fft.fft(x, axis=0) fy = np.fft.fft(y, axis=0) - # Multiply the FFTs and inverse transform the result - z = np.fft.ifft(fx * fy, axis=0) + # Multiply the FFTs and inverse transform + out = np.fft.ifft(fx * fy, axis=0) - # Trim the result to the correct length - return z[:x.size + y.size - 1] + return out +Generated code for scipy.signal.oaconvolve import numpy as np @@ -101,6 +95,7 @@ y = oaconvolve(x, h) # Print the result print(y) +Generated code for scipy.signal.convolve2d import numpy as np @@ -121,3 +116,9990 @@ convolved_arr = convolve2d(arr, kernel, mode='same') # Print the result print(convolved_arr) +Generated code for scipy.signal.correlate2d + + +import numpy as np +from scipy.signal import correlate2d + +def correlate2d(x, y, mode='same'): + """ + Compute the 2-dimensional correlation of two arrays. + + Parameters + ---------- + x : array_like + Input array. + y : array_like + Input array. + mode : str, optional + Mode of correlation. + 'full' : Returns the full 2-dimensional correlation. + 'same' : Returns the central part of the correlation that is the same size as `x`. + 'valid' : Returns only those parts of the correlation that are computed without the zero-padded edges. + + Returns + ------- + out : ndarray + A 2-dimensional correlation of `x` and `y`. + """ + x = np.asarray(x) + y = np.asarray(y) + if x.ndim != 2 or y.ndim != 2: + raise ValueError('x and y should be 2-dimensional.') + if mode not in ['full', 'same', 'valid']: + raise ValueError("mode should be one of 'full', 'same', or 'valid'.") + if x.shape[1] != y.shape[0]: + raise ValueError('x and y should have compatible shapes.') + if mode == 'full': + out_shape = (x.shape[0] + y.shape[0] - 1, x.shape[1] + y.shape[1] - 1) + elif mode == 'same': + out_shape = x.shape + elif mode == 'valid': + out_shape = (x.shape[0] - y.shape[0] + 1, x.shape[1] - y.shape[1] + 1) + out = np.zeros(out_shape) + for i in range(x.shape[0]): + for j in range(y.shape[1]): + out[i, j] = np.sum(x[i, :] * y[:, j]) + return out +Generated code for scipy.signal.sepfir2d + + +import numpy as np +from scipy.signal import sepfir2d + +# Define the filter coefficients +b = np.array([1, -2, 1]) +a = np.array([1, 0, 0]) + +# Define the input signal +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +# Apply the filter +y = sepfir2d(b, a, x) + +# Print the output +print(y) +Generated code for scipy.signal.choose_conv_method + + +def choose_conv_method(x, h, mode='full'): + """ + Choose a convolution method for a given input and filter. + + Parameters + ---------- + x : array_like + Input array. + h : array_like + Filter array. + mode : str, optional + A string indicating the size of the output: + 'full' (default) + The output is the full discrete linear convolution + of the inputs. (Default) + Returns + ------- + method : str + The method to use for convolution. + """ + # Get the lengths of the input and filter + x_len = len(x) + h_len = len(h) + + # Choose the convolution method based on the mode + if mode == 'full': + # If the mode is 'full', use the direct convolution method + method = 'direct' + else: + # If the mode is not 'full', use the FFT convolution method + # if the filter length is greater than the input length + if h_len > x_len: + method = 'fft' + else: + method = 'direct' + + return method +Generated code for scipy.signal.correlation_lags + + +import numpy as np +from scipy.signal import correlate + +def correlation_lags(x, y, max_lag): + """ + Computes the correlation between two signals x and y up to a maximum lag. + + Parameters + ---------- + x : array_like + Input signal. + y : array_like + Input signal. + max_lag : int + Maximum lag to compute correlation for. + + Returns + ------- + lags : array_like + Array of lags. + corr : array_like + Array of correlation values. + """ + lags = np.arange(-max_lag, max_lag+1) + corr = np.zeros(len(lags)) + for i, lag in enumerate(lags): + corr[i] = correlate(x, np.roll(y, lag))[0] + return lags, corr +Generated code for scipy.signal.bspline + + +import numpy as np +from scipy.signal import bspline + +# Define the order of the B-spline +order = 3 + +# Define the knots +knots = np.array([0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5]) + +# Define the evaluation points +x = np.array([0.5, 1.5, 2.5, 3.5]) + +# Evaluate the B-spline +y = bspline(x, knots, order) + +# Print the result +print(y) +Generated code for scipy.signal.cubic + + +import numpy as np +from scipy.signal import cubic + +# Generate a signal with a cubic shape +x = np.linspace(0, 10, 100) +y = cubic(x, 0.5, 0.2, 0.3, 0.4) + +# Plot the signal +import matplotlib.pyplot as plt +plt.plot(x, y) +plt.show() +Generated code for scipy.signal.quadratic + + +import numpy as np +from scipy.signal import quadratic + +# Generate a signal with a quadratic shape +x = np.linspace(-10, 10, 100) +y = quadratic(x) + +# Plot the signal +import matplotlib.pyplot as plt +plt.plot(x, y) +plt.show() +Generated code for scipy.signal.gauss_spline + + +import numpy as np +from scipy.signal import gauss_spline + +# Define the x-axis +x = np.linspace(-5, 5, num=1000) + +# Generate the Gaussian spline +y = gauss_spline(x, width=2) + +# Plot the Gaussian spline +plt.plot(x, y) +plt.show() +Generated code for scipy.signal.cspline1d + + +import numpy as np +from scipy.signal import cspline1d + +# Input data +x = np.array([0, 1, 2, 3, 4, 5]) +y = np.array([0, 1, 4, 9, 16, 25]) + +# Compute the cubic spline +cs = cspline1d(x, y) + +# Evaluate the spline at the given points +x_eval = np.linspace(0, 5, 100) +y_eval = cs(x_eval) + +# Plot the results +import matplotlib.pyplot as plt +plt.plot(x, y, 'o', label='Data points') +plt.plot(x_eval, y_eval, label='Cubic spline') +plt.legend() +plt.show() +Generated code for scipy.signal.qspline1d + + +import numpy as np +from scipy.signal import qspline1d + +# Generate a random array of data +x = np.random.rand(10) + +# Compute the qspline1d of the data +y = qspline1d(x) + +# Print the results +print(x) +print(y) +Generated code for scipy.signal.cspline2d + + +import numpy as np +from scipy.signal import cspline2d + +# Create a 2D array of data +data = np.random.rand(10, 10) + +# Compute the 2D cubic spline interpolation +cspline2d_data = cspline2d(data) + +# Print the result +print(cspline2d_data) +Generated code for scipy.signal.qspline2d + + +import numpy as np +from scipy.signal import qspline2d + +# Create a 2D array of data +data = np.random.rand(10, 10) + +# Compute the 2D Q-spline +qspline2d_data = qspline2d(data, order=3) + +# Print the result +print(qspline2d_data) +Generated code for scipy.signal.cspline1d_eval + + +def cspline1d_eval(xi, c, x): + """ + Evaluate a cubic spline at points x. + + Parameters + ---------- + xi : array_like + A sorted list of x-coordinates for the knots. + c : array_like + Array of coefficients for the spline. + x : array_like + Points at which to evaluate the spline. + + Returns + ------- + y : ndarray + The evaluated spline at points x. + + """ + # Check inputs + xi = np.asarray(xi) + c = np.asarray(c) + x = np.asarray(x) + + # Find the interval that each x value lies in + x_idx = np.searchsorted(xi, x) - 1 + + # Calculate the polynomial coefficients + a = c[x_idx] + b = c[x_idx + 1] - c[x_idx] + d = (xi[x_idx + 1] - xi[x_idx])**(-3) * (c[x_idx + 1] - c[x_idx] - (xi[x_idx + 1] - xi[x_idx]) * (2 * c[x_idx + 1] + c[x_idx])) + e = (xi[x_idx + 1] - xi[x_idx])**(-2) * (c[x_idx + 1] - c[x_idx] - (xi[x_idx + 1] - xi[x_idx]) * (3 * c[x_idx + 1] + 2 * c[x_idx])) + + # Evaluate the spline + y = a + b * (x - xi[x_idx]) + d * (x - xi[x_idx])**2 + e * (x - xi[x_idx])**3 + + return y +Generated code for scipy.signal.qspline1d_eval + + +def qspline1d_eval(x, knots, coeffs): + """ + Evaluate a one-dimensional quadratic spline at the given points. + + Parameters + ---------- + x : array_like + Points at which to evaluate the spline. + knots : array_like + Knots of the spline. + coeffs : array_like + Coefficients of the spline. + + Returns + ------- + y : ndarray + Values of the spline at the points `x`. + """ + x = np.asarray(x) + knots = np.asarray(knots) + coeffs = np.asarray(coeffs) + + # Find the interval that each point lies in + inds = np.searchsorted(knots, x) - 1 + inds = np.clip(inds, 0, len(knots) - 2) + + # Calculate the polynomial coefficients + a = coeffs[inds] + b = coeffs[inds + 1] - a + c = knots[inds + 1] * a - knots[inds] * b - coeffs[inds] + + # Evaluate the polynomial at the points + y = a + b * x + c * (x - knots[inds]) * (x - knots[inds + 1]) + + return y +Generated code for scipy.signal.spline_filter + + +import numpy as np +from scipy.signal import spline_filter + +# Generate a signal +x = np.linspace(0, 10, 1000) +y = np.sin(x) + +# Apply the spline filter +y_filtered = spline_filter(y, order=3, kind='cubic') + +# Plot the results +import matplotlib.pyplot as plt +plt.plot(x, y, label='Original') +plt.plot(x, y_filtered, label='Filtered') +plt.legend() +plt.show() +Generated code for scipy.signal.order_filter + + +import numpy as np +from scipy.signal import order_filter + +# Create a random array +x = np.random.rand(10, 10) + +# Create a filter +filt = np.ones((3, 3)) + +# Apply the filter +filtered_x = order_filter(x, filt, 1) + +# Print the result +print(filtered_x) +Generated code for scipy.signal.medfilt + + +import numpy as np +from scipy.signal import medfilt + +def medfilt(x, kernel_size): + """Apply a median filter to the input array using a local window-size given by kernel_size. + + Parameters + ---------- + x : array_like + An N-dimensional input array. + kernel_size : int or array_like + A scalar or an N-length list giving the size of the median filter window in each dimension. + Elements of kernel_size should be odd. If kernel_size is a scalar, then this scalar is used as the size in each dimension. + + Returns + ------- + y : ndarray + An array the same size as input containing the median filtered result. + """ + # Ensure kernel_size is an array + if np.isscalar(kernel_size): + kernel_size = [kernel_size] * x.ndim + # Ensure kernel_size is odd + kernel_size = [k if k % 2 != 0 else k + 1 for k in kernel_size] + # Create a tuple of slices, one for each dimension + slices = [slice(k // 2, -k // 2) for k in kernel_size] + # Create a view of the input array that is padded with the size of the median filter + x_padded = np.pad(x, [(k // 2, k // 2) for k in kernel_size], mode='edge') + # Apply the median filter by taking the median of the view + y = np.median(x_padded[slices], axis=tuple(range(x.ndim))) + return y +Generated code for scipy.signal.medfilt2d + + +import numpy as np +from scipy.signal import medfilt2d + +def medfilt2d(input_array, kernel_size): + """ + Applies a 2-dimensional median filter to an array. + + Parameters + ---------- + input_array : array_like + The input array. + kernel_size : int or tuple + A scalar or a tuple of length 2, giving the size of the + median filter window in each dimension. Elements of + `kernel_size` should be odd. If `kernel_size` is a scalar, + then this scalar is used as the size in each dimension. + + Returns + ------- + output_array : ndarray + The filtered array. + + Examples + -------- + >>> from scipy.signal import medfilt2d + >>> import numpy as np + >>> a = np.array([[3, 0, 0], + ... [2, 1, 0], + ... [1, 2, 0]]) + >>> medfilt2d(a, 3) + array([[2, 1, 0], + [2, 1, 0], + [1, 2, 0]]) + """ + # Check kernel size + if np.isscalar(kernel_size): + kernel_size = (kernel_size, kernel_size) + elif len(kernel_size) != 2: + raise ValueError('kernel_size must be a scalar or a tuple of length 2') + + # Pad the input array + pad_width = [(k - 1) // 2 for k in kernel_size] + padded_array = np.pad(input_array, pad_width, mode='edge') + + # Apply the median filter + output_array = np.zeros_like(input_array) + for i in range(input_array.shape[0]): + for j in range(input_array.shape[1]): + output_array[i, j] = np.median(padded_array[i:i + kernel_size[0], + j:j +Generated code for scipy.signal.wiener + + +import numpy as np +from scipy.signal import wiener + +def wiener_filter(signal, noise): + """ + Applies a Wiener filter to a signal with noise. + + Parameters + ---------- + signal : array_like + The signal to be filtered. + noise : array_like + The noise to be filtered out. + + Returns + ------- + filtered_signal : array_like + The filtered signal. + """ + return wiener(signal, noise) +Generated code for scipy.signal.symiirorder1 + + +import numpy as np +from scipy import signal + +# Generate a signal +t = np.linspace(0, 1, 1000) +x = np.sin(2 * np.pi * 5 * t) + +# Compute the symmetric impulse response of an order 1 system +b, a = signal.symiirorder1(x, 1) + +# Plot the impulse response +import matplotlib.pyplot as plt +plt.plot(b) +plt.title('Symmetric Impulse Response of an Order 1 System') +plt.xlabel('Samples') +plt.ylabel('Amplitude') +plt.show() +Generated code for scipy.signal.symiirorder2 + + +import scipy.signal as signal + +# Define the filter coefficients +b, a = signal.symiirorder2(0.5, 0.2, 0.3, 0.4) + +# Create the filter +filtered_signal = signal.lfilter(b, a, signal) + +# Plot the filter response +w, h = signal.freqz(b, a) +plt.plot(w, 20 * np.log10(abs(h))) +plt.xlabel('Frequency [rad/sample]') +plt.ylabel('Amplitude [dB]') +plt.grid() +plt.show() +Generated code for scipy.signal.lfilter + + +import numpy as np +from scipy.signal import lfilter + +def lfilter_func(b, a, x): + # Check for proper input + if not np.isscalar(a[0]): + raise ValueError("Denominator polynomial must be a scalar.") + if np.any(np.abs(a) <= np.finfo(float).eps): + raise ValueError("Denominator polynomial must be nonzero.") + if np.ndim(x) > 1: + raise ValueError("Input must be 1-D.") + # Pad the input with zeros + x = np.concatenate((np.zeros(len(b) - 1), x)) + # Initialize the output + y = np.zeros_like(x) + # Loop over the input + for n in range(len(x)): + # Calculate the output + y[n] = b[0] * x[n] + for m in range(1, len(b)): + if n - m >= 0: + y[n] += b[m] * x[n - m] + for m in range(1, len(a)): + if n - m >= 0: + y[n] -= a[m] * y[n - m] + y[n] /= a[0] + # Return the output + return y[len(b) - 1:] + +# Test the function +b = [1, 2, 3] +a = [1, 4, 5] +x = np.arange(10) +y = lfilter_func(b, a, x) +# Compare with scipy.signal.lfilter +y_true = lfilter(b, a, x) +print(np.allclose(y, y_true)) +Generated code for scipy.signal.lfiltic + + +import numpy as np +from scipy.signal import lfiltic + +# Define filter coefficients +b = np.array([1.0, -2.0, 1.0]) +a = np.array([1.0, -1.5, 0.5]) + +# Define initial conditions +zi = lfiltic(b, a, np.array([1.0, 0.0])) + +# Filter the signal +y, zf = lfiltic(b, a, np.array([1.0, 0.0]), zi) +Generated code for scipy.signal.lfilter_zi + + +import numpy as np +from scipy.signal import lfilter_zi + +def lfilter_zi(b, a): + # sanity checks + if a[0] != 1: + raise ValueError("Denominator polynomial must be normalized") + if len(a) != len(b): + raise ValueError("Numerator and denominator polynomials must have the same length") + + # compute the zi state + n = len(b) + zin = np.zeros(n-1) + + # solve for zi using the anti-causal filter + zin[n-2::-1] = np.linalg.pinv(np.eye(n-1) - np.hstack((-a[n-2::-1], + np.eye(n-2)))) @ b[1:] + + return zin +Generated code for scipy.signal.filtfilt + + +import numpy as np +from scipy.signal import filtfilt + +def filtfilt_scipy(b, a, x, axis=-1, padtype='odd', padlen=None): + """ + A scipy implementation of the filtfilt function. + """ + # The padding is done with edge values of the signal + edge = np.ones(len(x)) + edge[0] = x[0] + edge[-1] = x[-1] + + # Pad according to the padtype option + if padtype == 'odd': + ext = np.arange(1, padlen + 1) + edge = np.concatenate((x[-ext[::-1]], x, x[ext])) + elif padtype == 'even': + ext = np.arange(2, padlen + 2) + edge = np.concatenate((x[-ext[::-1]], x, x[ext])) + else: + raise ValueError('Unknown padtype: %s' % padtype) + + # Apply the filter + y = filtfilt(b, a, edge, axis=axis) + + # Remove the padding + if padtype == 'odd': + return y[padlen:-padlen] + elif padtype == 'even': + return y[padlen + 1:-padlen - 1] +Generated code for scipy.signal.savgol_filter + + +import numpy as np +from scipy.signal import savgol_filter + +# Input signal +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +# Savitzky-Golay filter +y = savgol_filter(x, window_length=5, polyorder=2) + +# Output signal +print(y) +# [1.8 2.75 3.75 4.75 5.75 6.75 7.75 8.75 9.75 10.8] +Generated code for scipy.signal.deconvolve + + +import numpy as np +from scipy.signal import deconvolve + +# Define the input signal +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +# Define the impulse response +h = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + +# Perform the deconvolution +y, remainder = deconvolve(x, h) + +# Print the results +print('y =', y) +print('remainder =', remainder) +Generated code for scipy.signal.sosfilt + + +import numpy as np +from scipy.signal import sosfilt + +# Define the filter coefficients +sos = np.array([[1, 2, 1, 1, -1.2, 0.81], + [1, -2, 1, 1, -1.2, 0.81]]) + +# Input signal +x = np.array([1, 2, 3, 4, 5, 6]) + +# Filter the signal +y = sosfilt(sos, x) + +print(y) +# Output: array([-0.19, 0.81, 2.19, 3.81, 5.19, 6.81]) +Generated code for scipy.signal.sosfilt_zi + + +import scipy.signal + +def sosfilt_zi(sos, x0=None): + """ + Compute an initial state zi for the sosfilt function that corresponds to the + steady state of the step response. + + Parameters + ---------- + sos : array_like + Array of second-order filter coefficients, must have shape (n_sections, 6). + x0 : array_like, optional + Initial condition for the filter delay elements. If None (default), + the initial conditions are assumed to be all zero. + + Returns + ------- + zi : ndarray + The steady-state filter delay elements, of length equal to the number + of sections times 2. + + Notes + ----- + This function is typically used to set the initial conditions of the + sosfilt function. + + Examples + -------- + >>> from scipy.signal import sosfilt_zi, sosfilt + >>> sos = [[1, 0, 0, 1, 0, 0], [1, 0, 0, 1, 0, 0]] + >>> zi = sosfilt_zi(sos) + >>> y, zo = sosfilt(sos, [1, 2, 3], zi=zi) + """ + sos = scipy.atleast_2d(sos) + if x0 is None: + x0 = scipy.zeros(sos.shape[0] * 2) + else: + x0 = scipy.asarray(x0) + if x0.shape != (sos.shape[0] * 2,): + raise ValueError('x0 must have shape (n_sections * 2,).') + zi = scipy.signal.lfilter_zi(sos[:, :3], sos[:, 3:]) + zi = scipy.concatenate((x0[:, None], zi), axis=1).ravel() + return zi +Generated code for scipy.signal.sosfiltfilt + + +import numpy as np +from scipy.signal import sosfiltfilt + +# Define the filter coefficients +sos = np.array([[1, 2, 1, 1, -1.2, 0.42], + [1, -2, 1, 1, -1.2, 0.42]]) + +# Define the input signal +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +# Apply the filter +y = sosfiltfilt(sos, x) + +# Print the output +print(y) +Generated code for scipy.signal.hilbert + + +import numpy as np +from scipy import signal + +def hilbert(x): + """ + Compute the analytic signal, using the Hilbert transform. + The transformation is done along the last axis of the input array. + Parameters + ---------- + x : array_like + Signal data. Must be real. + Returns + ------- + xa : ndarray + Analytic signal of `x`, of each 1-D array along `axis` + Notes + ----- + The transformation is done along the last axis of the input array. + """ + x = np.asarray(x) + if x.dtype.kind not in 'iu': + raise TypeError("Only supports integer and unsigned types") + if x.ndim == 0: + raise ValueError("Input must be at least 1-d.") + if x.ndim == 1: + x = x[np.newaxis, :] + x = np.asarray(x, dtype=np.float) + xa = np.empty(x.shape, dtype=np.complex) + for i in range(x.shape[0]): + xa[i] = signal.hilbert(x[i]) + return xa +Generated code for scipy.signal.hilbert2 + + +import numpy as np +from scipy.signal import hilbert2 + +# Generate a random signal +x = np.random.rand(100) + +# Compute the analytic signal using hilbert2 +analytic_signal = hilbert2(x) + +# Print the analytic signal +print(analytic_signal) +Generated code for scipy.signal.decimate + + +import scipy.signal + +def decimate(x, q, n=None, ftype='iir', axis=-1): + """ + Downsample the signal x by an integer factor q, using an order n filter. + + By default an order 8 Chebyshev type I filter is used or a 30 point FIR + filter with hamming window if ftype is 'fir'. + + Parameters + ---------- + x : ndarray + The signal to be downsampled, as an N-dimensional array. + q : int + The downsampling factor. + n : int, optional + The order of the filter (1 less than the length for 'fir'). + ftype : str {'iir', 'fir'}, optional + The type of the lowpass filter. + axis : int, optional + The axis along which to decimate. + + Returns + ------- + y : ndarray + The down-sampled signal. + + See Also + -------- + scipy.signal.resample + + Examples + -------- + >>> from scipy import signal + >>> sig = np.arange(10) + >>> signal.decimate(sig, q=2) + array([0, 2, 4, 6, 8]) + + """ + if not isinstance(q, int): + raise TypeError("q must be an integer") + if n is None: + if ftype == 'fir': + n = 30 + else: + n = 8 + if ftype == 'fir': + b = scipy.signal.firwin(n + 1, 1. / q, window='hamming') + a = 1. + else: + b, a = scipy.signal.cheby1(n, 0.05, 0.8 / q) + y = scipy.signal.lfilter(b, a, x, axis=axis) + sl = [slice(None)] * y.ndim + sl[axis] = slice(None, None, q +Generated code for scipy.signal.detrend + + +def detrend(x): + """ + Detrend a signal x by fitting a polynomial of degree deg and subtracting it. + Parameters + ---------- + x : array_like + Signal data. + Returns + ------- + x_detrend : ndarray + Detrended signal. + """ + x = np.asarray(x) + deg = 1 + coeffs = np.polyfit(np.arange(len(x)), x, deg) + x_detrend = x - np.polyval(coeffs, np.arange(len(x))) + return x_detrend +Generated code for scipy.signal.resample + + +import numpy as np +from scipy.signal import resample + +# Generate a signal with 10 samples +x = np.arange(10) + +# Resample the signal to 5 samples +y = resample(x, 5) + +print(y) +# Output: array([0. , 2.5, 5. , 7.5, 10. ]) +Generated code for scipy.signal.resample_poly + + +import numpy as np +from scipy.signal import resample_poly + +# Generate a signal +x = np.arange(0, 10, 0.1) +y = np.sin(x) + +# Resample the signal +x_resampled, y_resampled = resample_poly(x, y, up=2, down=1) + +# Plot the original and resampled signals +import matplotlib.pyplot as plt +plt.plot(x, y, label='Original') +plt.plot(x_resampled, y_resampled, label='Resampled') +plt.legend() +plt.show() +Generated code for scipy.signal.upfirdn + + +import numpy as np +from scipy.signal import upfirdn + +# Define the signal +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + +# Define the filter +h = np.array([1, 2, 3, 4, 5]) + +# Perform the upfirdn operation +y = upfirdn(h, x, up=2, down=3) + +# Print the result +print(y) +Generated code for scipy.signal.bilinear + + +import numpy as np +from scipy import signal + +def bilinear(b, a, fs): + """ + Bilinear transformation of a digital filter. + + Parameters + ---------- + b : array_like + Numerator polynomial coefficients. + a : array_like + Denominator polynomial coefficients. + fs : float + Sampling frequency. + + Returns + ------- + bz : ndarray + Numerator polynomial coefficients in the z-domain. + az : ndarray + Denominator polynomial coefficients in the z-domain. + + """ + # Normalize coefficients by the sampling frequency + b = np.asarray(b) / fs + a = np.asarray(a) / fs + + # Compute the bilinear transformation + N = max(len(a), len(b)) + n = np.arange(N) + k = 2 * np.sin(np.pi * n / (2 * N)) + bz = np.zeros(N, dtype=np.float) + az = np.zeros(N, dtype=np.float) + for i in range(N): + bz[i] = np.sum(b * k**(2 * i)) + az[i] = np.sum(a * k**(2 * i)) + + # Normalize the coefficients + bz /= az[0] + az /= az[0] + + return bz, az +Generated code for scipy.signal.bilinear_zpk + + +import scipy.signal + +def bilinear_zpk(z, p, k, fs): + """ + Compute the bilinear transformation of a zpk filter. + + Parameters + ---------- + z : array_like + Zeros of the filter transfer function. + p : array_like + Poles of the filter transfer function. + k : float + Gain of the filter transfer function. + fs : float + Sampling frequency. + + Returns + ------- + z_out : ndarray + Zeros of the transformed filter transfer function. + p_out : ndarray + Poles of the transformed filter transfer function. + k_out : float + Gain of the transformed filter transfer function. + + """ + # Compute the bilinear transformation + z_out = (2 * fs + z) / (2 * fs - z) + p_out = (2 * fs + p) / (2 * fs - p) + k_out = k * (2 * fs - p) / (2 * fs + z) + + # Return the transformed zpk + return z_out, p_out, k_out + +# Example usage +z = [1, 2, 3] +p = [4, 5, 6] +k = 1 +fs = 44100 + +z_out, p_out, k_out = scipy.signal.bilinear_zpk(z, p, k, fs) +Generated code for scipy.signal.findfreqs + + +import numpy as np +from scipy import signal + +# Generate a test signal +t = np.linspace(0, 1, 500, endpoint=False) +sig = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t) + +# Compute the frequency components +freqs, times, Sx = signal.find_peaks(sig) + +# Print the results +print("Frequencies found: ", freqs) +print("Time instants: ", times) +print("Spectral components: ", Sx) +Generated code for scipy.signal.firls + + +import numpy as np +from scipy import signal + +# Define filter parameters +numtaps = 50 +nyq_rate = 10.0 +cutoff_freq = 3.0 + +# Compute the filter coefficients +taps = signal.firls(numtaps, [0, cutoff_freq, cutoff_freq + 0.1, nyq_rate], + [1, 1, 0, 0], nyq=nyq_rate) + +# Plot the filter coefficients +import matplotlib.pyplot as plt +plt.plot(taps, 'bo-', linewidth=2) +plt.title('Filter Coefficients (%d taps)' % numtaps) +plt.grid(True) +plt.show() +Generated code for scipy.signal.firwin + + +import numpy as np +from scipy import signal + +# Create a lowpass FIR filter +taps = signal.firwin(80, 0.2, window='hamming') + +# Plot the frequency response +w, h = signal.freqz(taps, worN=np.logspace(-2, 0, 100)) +plt.semilogx(w, 20 * np.log10(abs(h))) +plt.title('Frequency Response of a Lowpass FIR Filter') +plt.xlabel('Frequency [radians / second]') +plt.ylabel('Amplitude [dB]') +plt.margins(0, 0.1) +plt.grid(which='both', axis='both') +plt.axvline(0.2, color='green') # cutoff frequency +plt.show() +Generated code for scipy.signal.firwin2 + + +import numpy as np +from scipy import signal + +# Define filter parameters +numtaps = 50 +cutoff_hz = [100, 200] +fs = 1000 + +# Generate filter coefficients +taps = signal.firwin2(numtaps, cutoff_hz, fs=fs) + +# Plot the frequency response +w, h = signal.freqz(taps, worN=np.logspace(-2, 2, 1000)) + +# Plot the magnitude response +plt.plot(w, 20 * np.log10(abs(h))) +plt.xscale('log') +plt.title('Frequency Response of FIR Filter') +plt.xlabel('Frequency [rad/sample]') +plt.ylabel('Amplitude [dB]') +plt.grid(which='both', axis='both') +plt.show() +Generated code for scipy.signal.freqs + + +import numpy as np +from scipy import signal + +def freqs(b, a, worN=None, plot=None): + """ + Compute frequency response of a digital filter. + + The frequency response is evaluated for digital filters, by computing the + frequency response at a number of frequencies. + + Parameters + ---------- + b : array_like + Numerator of a linear filter. + a : array_like + Denominator of a linear filter. + worN : {None, int, array_like}, optional + If `worN` is None, then compute at 512 frequencies around the unit + circle. If `worN` is an integer, then compute at `worN` frequencies. + If `worN` is array_like, it should contain the frequencies (in + radians/second) at which to compute the frequency response. + plot : callable, optional + If `plot` is not None, then it should be a callable that takes two + arguments. The first argument will be the frequency array (in + radians/second). The second argument will be the frequency response + array. `plot` will be called just before `freqs` returns. + + Returns + ------- + w : ndarray + The frequencies at which `h` was computed, in radians/second. + h : ndarray + The frequency response. + + See Also + -------- + freqz : Compute the frequency response of an analog filter. + scipy.signal.group_delay : Compute the group delay of a digital filter. + + Examples + -------- + Plot the frequency response of a low-pass filter: + + >>> from scipy import signal + >>> b, a = signal.butter(4, 100, 'low', analog=True) + >>> w, h = signal.freqs(b, a) + >>> import matplotlib.pyplot as plt + >>> plt.semilogx(w, 20 * np.log10(abs(h))) + >>> plt.title('Butter +Generated code for scipy.signal.freqs_zpk + + +import scipy.signal as sig + +# Define the zeros, poles, and gain of the filter +zeros = [1, 2, 3] +poles = [4, 5, 6] +gain = 1 + +# Calculate the frequency response of the filter +freqs, response = sig.freqs_zpk(zeros, poles, gain) + +# Print the frequency response +print(freqs) +print(response) +Generated code for scipy.signal.freqz + + +import numpy as np +from scipy import signal + +# Generate a test signal, a 2 Vrms sine wave at 12kHz, corrupted by 0.001 V**2/Hz of white noise sampled at 48 kHz. + +fs = 48e3 +N = 1000 +amp = 2 * np.sqrt(2) +freq = 12e3 +noise_power = 0.001 * fs / 2 +time = np.arange(N) / fs +x = amp * np.sin(2*np.pi*freq*time) +x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape) + +# Compute and plot the frequency response + +w, h = signal.freqz(x, fs=fs) + +import matplotlib.pyplot as plt +plt.figure() +plt.title('Frequency Response') +plt.plot(w, 20 * np.log10(abs(h)), 'b') +plt.ylabel('Amplitude [dB]', color='b') +plt.xlabel('Frequency [rad/sample]') +plt.grid() +plt.show() +Generated code for scipy.signal.freqz_zpk + + +import numpy as np +from scipy import signal + +# Define the zeros, poles, and gain of the filter +zeros = np.array([-1, -2, -3]) +poles = np.array([0.5, 0.75, 0.9]) +gain = 1 + +# Compute the frequency response +w, h = signal.freqz_zpk(zeros, poles, gain) + +# Plot the frequency response +import matplotlib.pyplot as plt +plt.figure() +plt.title('Frequency Response') +plt.plot(w, 20 * np.log10(abs(h)), 'b') +plt.ylabel('Amplitude [dB]', color='b') +plt.xlabel('Frequency [rad/sample]') +plt.grid() +plt.show() +Generated code for scipy.signal.sosfreqz + + +import numpy as np +from scipy import signal + +# Define the filter coefficients +sos = np.array([[1, 2, 1, 1, 0.5, 0.5], + [1, -2, 1, 1, 0.5, 0.5]]) + +# Compute the frequency response +w, h = signal.sosfreqz(sos) + +# Plot the magnitude response +import matplotlib.pyplot as plt +plt.plot(w, 20 * np.log10(abs(h))) +plt.title('Digital filter frequency response') +plt.xlabel('Frequency [radians / second]') +plt.ylabel('Amplitude [dB]') +plt.margins(0, 0.1) +plt.grid(which='both', axis='both') +plt.axvline(100, color='green') # cutoff frequency +plt.show() +Generated code for scipy.signal.gammatone + + +import numpy as np +from scipy.signal import gammatone + +def gammatone_filter(x, fs, center_freqs): + """ + Compute the gammatone filterbank for a given signal x. + Parameters + ---------- + x : array_like + Input signal. + fs : float + Sampling rate of the `x` signal. + center_freqs : array_like + Center frequencies of the gammatone filterbank. + Returns + ------- + gammatone_filterbank : array_like + Gammatone filterbank output. + """ + # Compute the gammatone filterbank + gammatone_filterbank = gammatone(x, fs, center_freqs) + # Return the filterbank + return gammatone_filterbank +Generated code for scipy.signal.group_delay + + +import numpy as np +from scipy import signal + +# Generate a test signal +t = np.linspace(0, 1, 1000) +x = np.sin(2 * np.pi * 5 * t) + +# Compute the group delay +gd = signal.group_delay((x, t)) + +# Plot the signal and the group delay +import matplotlib.pyplot as plt +plt.plot(t, x) +plt.plot(t[1:], -gd) +plt.xlabel('Time [s]') +plt.ylabel('Signal') +plt.grid(True) +plt.show() +Generated code for scipy.signal.iirdesign + + +import scipy.signal as signal + +# Design a 5th-order Chebyshev type I lowpass filter +b, a = signal.iirdesign(wp=0.2, ws=0.3, gpass=1, gstop=30, + analog=False, ftype='cheby1', output='ba') + +# Plot the frequency response +w, h = signal.freqz(b, a) +plt.plot(w, 20 * np.log10(abs(h))) +plt.xscale('log') +plt.title('Chebyshev Type I frequency response') +plt.xlabel('Frequency [radians / second]') +plt.ylabel('Amplitude [dB]') +plt.margins(0, 0.1) +plt.grid(which='both', axis='both') +plt.axvline(100, color='green') # cutoff frequency +plt.show() +Generated code for scipy.signal.iirfilter + + +import scipy.signal as signal + +# Create an IIR filter +b, a = signal.iirfilter(N, Wn, rp=None, rs=None, btype='lowpass', analog=False, ftype='butter', output='ba') + +# Filter a signal +filtered_signal = signal.lfilter(b, a, signal) +Generated code for scipy.signal.kaiser_atten + + +import numpy as np +from scipy.signal import kaiser_atten + +# Define the parameters +N = 100 # filter order +alpha = 5.0 # Kaiser window parameter + +# Calculate the Kaiser window +w = kaiser_atten(N, alpha) + +# Plot the Kaiser window +import matplotlib.pyplot as plt +plt.plot(w) +plt.title("Kaiser Window (alpha = %f)" % alpha) +plt.xlabel("Sample") +plt.ylabel("Amplitude") +plt.show() +Generated code for scipy.signal.kaiser_beta + + +def kaiser_beta(N, beta): + """ + Compute the Kaiser window parameter beta for a given filter order N. + + Parameters + ---------- + N : int + Filter order. + beta : float + Kaiser window parameter. + + Returns + ------- + beta : float + Kaiser window parameter. + + Notes + ----- + The Kaiser window is defined as + + .. math:: w(n) = I_0\\left(\\beta \\sqrt{1-\\frac{4n^2}{(N-1)^2}}\\right) + + where :math:`I_0` is the modified Bessel function of order 0. The + parameter :math:`\\beta` is related to the sidelobe attenuation of the + window in decibels, :math:`A_s`, by + + .. math:: \\beta = \\frac{A_s}{21.98} + + References + ---------- + .. [1] J. F. Kaiser, "Nonrecursive Digital Filter Design Using the + I_0-sinh Window Function," Proc. IEEE Int. Symp. Circuits and + Systems, pp. 517-520, May 1974. + .. [2] E. R. Kanasewich, "Time Sequence Analysis in Geophysics," The + University of Alberta Press, 1975, pp. 177-178. + .. [3] Wikipedia, "Kaiser window", + http://en.wikipedia.org/wiki/Kaiser_window + + Examples + -------- + >>> from scipy.signal import kaiser_beta + >>> kaiser_beta(50, 5.0) + 2.285449538537 + """ + if N < 1: + raise ValueError("N must be greater than 0") + if beta <= 0: + raise ValueError("beta must be greater than 0") + + return beta / (21.98 * np.sqrt(1.0 - (4.0 * N**2) / ((N - 1.0)**2))) +Generated code for scipy.signal.kaiserord + + +import scipy.signal as signal + +def kaiserord(N, beta): + """ + Compute the Kaiser window parameters for a given filter order and + desired attenuation. + + Parameters + ---------- + N : int + The filter order. + beta : float + The desired attenuation in the stop band, in dB. + + Returns + ------- + alpha : float + The Kaiser window parameter. + M : int + The filter length. + beta : float + The actual attenuation in the stop band, in dB. + + """ + # Compute the stop band attenuation + delta_p = 0.5 * (1 - 10**(-beta/20)) + delta_s = 0.5 * 10**(-beta/20) + + # Compute the Kaiser parameter alpha + alpha = -20 * np.log10(delta_s/delta_p) + + # Compute the filter length + M = int(np.ceil((N + 1) / 2)) + + # Compute the actual attenuation + beta = -20 * np.log10(delta_s) + + return alpha, M, beta +Generated code for scipy.signal.minimum_phase + + +import numpy as np +from scipy import signal + +def minimum_phase(x, axis=-1): + """ + Compute the minimum-phase version of a signal. + + Parameters + ---------- + x : array_like + The input signal. + axis : int, optional + The axis along which to compute the minimum-phase version. + Default is -1. + + Returns + ------- + x_min : ndarray + The minimum-phase version of the input signal. + + Notes + ----- + This function computes the minimum-phase version of a signal by + computing its analytic signal, taking the magnitude of the analytic + signal, and then reconstructing the signal using an inverse FFT. + + Examples + -------- + >>> import numpy as np + >>> from scipy.signal import minimum_phase + >>> x = np.array([1, 2, 3, 4, 5, 6]) + >>> minimum_phase(x) + array([ 0.5 , 1.5 , 2.5 , 3.5 , 4.5 , + 5.5 ]) + """ + x = np.asarray(x) + x_min = np.abs(signal.hilbert(x, axis=axis)) + x_min = np.fft.ifft(x_min, axis=axis) + return np.real(x_min) +Generated code for scipy.signal.savgol_coeffs + + +import numpy as np +from scipy.signal import savgol_coeffs + +def savgol_coeffs(window_length, polyorder, deriv=0): + """ + Compute the coefficients for a 1-d Savitzky-Golay FIR filter. + Parameters + ---------- + window_length : int + The length of the filter window (i.e. the number of coefficients). + `window_length` must be a positive odd integer. + polyorder : int + The order of the polynomial used to fit the samples. + `polyorder` must be less than `window_length`. + deriv : int, optional + The order of the derivative to compute. This must be a + nonnegative integer. The default is 0, which means to filter + the data without differentiating. + Returns + ------- + coeffs : ndarray + 1-D array of coefficients. The coefficients are in ascending + order of powers (e.g., coeffs[0] is the constant term, + coeffs[1] is the linear term, etc.). + Notes + ----- + .. versionadded:: 0.19.0 + References + ---------- + .. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of + Data by Simplified Least Squares Procedures. Analytical + Chemistry, 1964, 36 (8), pp 1627-1639. + .. [2] Numerical Recipes 3rd Edition: The Art of Scientific Computing + W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery + Cambridge University Press ISBN-13: 9780521880688 + Examples + -------- + >>> from scipy.signal import savgol_coeffs + >>> savgol_coeffs(5, 2) + array([-0.08571429, 0.34285714, 0.48571429, 0.34285714, -0.08571429]) + """ + if +Generated code for scipy.signal.remez + + +import numpy as np +from scipy.signal import remez + +# Define the filter parameters +numtaps = 10 +bands = [0, 0.2, 0.3, 0.5] +desired = [1, 0] + +# Calculate the filter coefficients +coeffs = remez(numtaps, bands, desired) + +# Print the filter coefficients +print(coeffs) +Generated code for scipy.signal.unique_roots + + +import numpy as np +from scipy.signal import unique_roots + +# Generate a random polynomial +coeffs = np.random.randint(-10, 10, size=5) + +# Find the unique roots of the polynomial +roots = unique_roots(coeffs) + +# Print the unique roots +print(roots) +Generated code for scipy.signal.residue + + +import numpy as np +from scipy.signal import residue + +# Define the numerator and denominator polynomials +num = [1, 2, 3] +den = [1, 4, 5, 6] + +# Compute the residue +r, p, k = residue(num, den) + +# Print the results +print('Residue:', r) +print('Poles:', p) +print('Gain:', k) +Generated code for scipy.signal.residuez + + +import scipy.signal as sig + +# Define the numerator and denominator polynomials +num = [1, 2, 3] +den = [1, 4, 5, 6] + +# Compute the residue, pole and gain +res, poles, gain = sig.residuez(num, den) + +# Print the results +print('Residues:', res) +print('Poles:', poles) +print('Gain:', gain) +Generated code for scipy.signal.invres + + +import numpy as np +from scipy import signal + +def invres(b, a, x): + """ + Compute the inverse filter response of a digital filter. + + Parameters + ---------- + b : array_like + Numerator polynomial coefficients. + a : array_like + Denominator polynomial coefficients. + x : array_like + Input signal. + + Returns + ------- + y : ndarray + The inverse filter response. + + """ + # Compute the transfer function + b, a = np.atleast_1d(b, a) + zi = signal.lfilter_zi(b, a) + z, p, k = signal.tf2zpk(b, a) + + # Compute the inverse filter response + y = np.zeros_like(x) + for n in range(len(x)): + y[n] = np.dot(b, x[n::-1]) - np.dot(a[1:], y[n::-1]) + y[n] /= a[0] + + # Compensate for the initial conditions + y = signal.lfilter(b, a, y, zi=zi*x[0]) + + return y +Generated code for scipy.signal.invresz + + +import numpy as np +from scipy import signal + +def invresz(b, a, z): + """ + Compute the inverse z-transform of a rational transfer function. + + Parameters + ---------- + b : array_like + Numerator polynomial coefficients. + a : array_like + Denominator polynomial coefficients. + z : array_like + Z-transform points. + + Returns + ------- + y : ndarray + Inverse z-transform of the transfer function. + + """ + # Check inputs + b = np.asarray(b) + a = np.asarray(a) + z = np.asarray(z) + + # Compute inverse z-transform + y = np.zeros_like(z) + for i in range(len(z)): + y[i] = np.sum(b * z[i]**np.arange(len(b))) / np.sum(a * z[i]**np.arange(len(a))) + + return y +Generated code for scipy.signal.BadCoefficients + + +import numpy as np +from scipy.signal import BadCoefficients + +# Create a signal +t = np.linspace(0, 1, 1000) +x = np.sin(2*np.pi*t) + +# Create a BadCoefficients object +bc = BadCoefficients(x) + +# Get the coefficients +coeffs = bc.coefficients + +# Print the coefficients +print(coeffs) +Generated code for scipy.signal.abcd_normalize + + +def abcd_normalize(a, b, c, d): + """ + Normalize the ABCD matrix of a linear system. + + Parameters + ---------- + a : array_like + The A matrix of the linear system. + b : array_like + The B matrix of the linear system. + c : array_like + The C matrix of the linear system. + d : array_like + The D matrix of the linear system. + + Returns + ------- + a_norm : ndarray + The normalized A matrix. + b_norm : ndarray + The normalized B matrix. + c_norm : ndarray + The normalized C matrix. + d_norm : ndarray + The normalized D matrix. + + """ + # Compute the normalization factor + norm_factor = np.linalg.norm(a) + + # Normalize the matrices + a_norm = a / norm_factor + b_norm = b / norm_factor + c_norm = c * norm_factor + d_norm = d * norm_factor + + return a_norm, b_norm, c_norm, d_norm +Generated code for scipy.signal.band_stop_obj + + +import scipy.signal as sig + +# Create a band-stop filter object +band_stop_obj = sig.iirfilter(N=2, Wn=[0.2, 0.8], btype='bandstop', + ftype='butter', output='sos') + +# Apply the filter to a signal +filtered_signal = sig.sosfilt(band_stop_obj, signal) +Generated code for scipy.signal.besselap + + +import numpy as np +from scipy import signal + +def besselap(N, rp, rs, fs=1): + """ + Design a Bessel analog lowpass filter. + + Design an Nth-order lowpass analog Bessel filter with + a passband ripple of `rp` dB and a stopband ripple of `rs` dB. + + Parameters + ---------- + N : int + The order of the filter. + rp : float + The maximum passband ripple in dB. + rs : float + The minimum stopband ripple in dB. + fs : float, optional + The sampling frequency. Default is 1. + + Returns + ------- + b : ndarray + Numerator of the filter transfer function. + a : ndarray + Denominator of the filter transfer function. + + """ + # Compute the filter coefficients + b, a = signal.bessel(N, rp, rs, fs=fs) + + return b, a +Generated code for scipy.signal.buttap + + +import numpy as np +from scipy import signal + +# Define filter parameters +N = 3 # Filter order +Wn = 0.2 # Cutoff frequency + +# Calculate the coefficients using the `buttap` function +b, a = signal.buttap(N, Wn) + +# Print the filter coefficients +print('b =', b) +print('a =', a) +Generated code for scipy.signal.cheb1ap + + +import numpy as np +from scipy import signal + +def cheb1ap(N, rp, rs, fs=2): + """ + Design a Chebyshev type I analog lowpass filter with `N` poles, + `rp` dB of ripple in the passband, and `rs` dB of attenuation in + the stopband. + + Parameters + ---------- + N : int + Filter order + rp : float + Passband ripple in dB + rs : float + Stopband attenuation in dB + fs : float, optional + Sampling frequency (default is 2) + + Returns + ------- + b : ndarray + Numerator polynomial coefficients + a : ndarray + Denominator polynomial coefficients + + """ + # Compute the filter parameters + Wn = 1.0 / (fs / 2.0) + Ws = Wn * np.sqrt(10**(rp/10.0) - 1.0) + Wp = Wn * np.sqrt(10**(rs/10.0) - 1.0) + + # Compute the filter coefficients + b, a = signal.cheby1(N, rp, Wp, 'low', analog=True) + + return b, a +Generated code for scipy.signal.cheb2ap + + +import numpy as np +from scipy.signal import cheb2ap + +# Define filter order +order = 5 + +# Define passband ripple +ripple = 0.5 + +# Define passband and stopband frequencies +wp = 0.2 +ws = 0.3 + +# Calculate filter coefficients +b, a = cheb2ap(order, ripple, [wp, ws]) + +# Print filter coefficients +print('b =', b) +print('a =', a) +Generated code for scipy.signal.cmplx_sort + + +import numpy as np +from scipy.signal import cmplx_sort + +# Create a complex array +x = np.array([1+2j, 3+4j, 5+6j, 7+8j]) + +# Sort the array +sorted_x = cmplx_sort(x) + +# Print the sorted array +print(sorted_x) +Generated code for scipy.signal.ellipap + + +import scipy.signal as signal + +def ellipap(N, rp, rs, Wn, btype='low', analog=False, output='ba'): + """ + Elliptic IIR filter design. + + Design an Nth-order digital or analog elliptic filter and return the + filter coefficients. + + Parameters + ---------- + N : int + The order of the filter. + rp : float + The maximum ripple allowed below unity gain in the passband. Specified + in decibels, as a positive number. + rs : float + The minimum attenuation required in the stopband. Specified in + decibels, as a positive number. + Wn : array_like + A scalar or length-2 sequence giving the critical frequencies. + For a lowpass filter, this is the cutoff frequency. + For a highpass filter, this is the cutoff frequency. + For a bandpass filter, this is the two cutoff frequencies. + For a bandstop filter, this is the two corner frequencies. + btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional + The type of filter. Default is 'lowpass'. + analog : bool, optional + When True, return an analog filter, otherwise a digital filter is + returned. + output : {'ba', 'zpk', 'sos'}, optional + Type of output: numerator/denominator ('ba') or pole-zero ('zpk') or + second-order sections ('sos'). Default is 'ba'. + + Returns + ------- + b, a : ndarray, ndarray + Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. + Only returned if ``output='ba'``. + z, p, k : ndarray, ndarray, float + Zeros, poles, and system gain of the IIR filter transfer + function. Only returned if ``output='zpk'``. + sos : ndarray + Second-order sections representation of the IIR filter. + +Generated code for scipy.signal.lp2bp + + +import numpy as np +from scipy import signal + +# Define filter parameters +order = 4 +cutoff_low = 0.2 +cutoff_high = 0.3 + +# Create the filter +b, a = signal.butter(order, [cutoff_low, cutoff_high], btype='bandpass', analog=False) + +# Apply the filter +output = signal.lfilter(b, a, input) +Generated code for scipy.signal.lp2bp_zpk + + +import scipy.signal as signal + +# Define the filter parameters +f1 = 0.2 # Start of the transition band +f2 = 0.3 # End of the transition band +gpass = 1 # Maximum loss in the passband (dB) +gstop = 40 # Minimum attenuation in the stopband (dB) + +# Calculate the filter coefficients +b, a = signal.lp2bp(f1, f2, gpass, gstop) + +# Calculate the zeros, poles and gain +z, p, k = signal.tf2zpk(b, a) + +# Print the results +print('Zeros =', z) +print('Poles =', p) +print('Gain =', k) +Generated code for scipy.signal.lp2bs + + +import numpy as np +from scipy import signal + +def lp2bs(b, a, wo=1.0, bw=1.0): + """ + Transform a lowpass filter prototype to a bandstop filter. + + Transform a lowpass filter prototype with unity cutoff frequency to + a bandstop filter with center frequency `wo` and bandwidth `bw`. + The lowpass filter prototype is described with numerator and + denominator coefficients `b` and `a`, respectively. + + Parameters + ---------- + b : array_like + Numerator polynomial of the lowpass filter prototype. + a : array_like + Denominator polynomial of the lowpass filter prototype. + wo : float + Center frequency of the resulting bandstop filter, normalized + from 0 to 1, where 1 is the Nyquist frequency. + bw : float + Bandwidth of the resulting bandstop filter, normalized from + 0 to 1, where 1 is the Nyquist frequency. + + Returns + ------- + b2 : ndarray + Numerator polynomial of the bandstop filter. + a2 : ndarray + Denominator polynomial of the bandstop filter. + + """ + # Pre-warp frequencies + wo = wo * np.pi + bw = bw * np.pi + + # Transform to bandstop + b2 = np.multiply(b, np.array([1, -2*np.cos(wo), 1])) + a2 = np.multiply(a, np.array([1, -2*np.cos(wo + bw/2), 1])) + + return b2, a2 +Generated code for scipy.signal.lp2bs_zpk + + +import scipy.signal as sig + +def lp2bs_zpk(z, p, k, wo=1.0, bw=None): + """ + Transform a lowpass filter prototype to a bandstop filter. + + Transform a lowpass filter prototype to a bandstop filter, by + transforming its zeros, poles and gain, with the specified + stopband frequency and bandwidth. + + Parameters + ---------- + z : array_like + Zeros of the analog lowpass filter + p : array_like + Poles of the analog lowpass filter + k : float + The system gain of the analog lowpass filter + wo : float + The desired stopband frequency + bw : float, optional + The desired stopband bandwidth. If omitted, wo/2 is used. + + Returns + ------- + z : ndarray + Zeros of the resulting bandstop filter + p : ndarray + Poles of the resulting bandstop filter + k : float + The system gain of the resulting bandstop filter + + """ + if bw is None: + bw = wo/2.0 + + # Transform the lowpass filter to a bandstop filter + z, p, k = sig.lp2bs(z, p, k, wo, bw) + + # Transform to zpk + z, p, k = sig.zpk2tf(z, p, k) + + return z, p, k +Generated code for scipy.signal.lp2hp + + +import scipy.signal as signal + +def lp2hp(b, a, cutoff, fs=2): + """ + Transform a lowpass filter prototype to a highpass filter. + + Parameters + ---------- + b : array_like + Numerator of a linear filter. + a : array_like + Denominator of a linear filter. + cutoff : float + Cutoff frequency of the lowpass filter, normalized from 0 to 1, where + 1 corresponds to the Nyquist frequency. + fs : float, optional + Sampling frequency. Defaults to 2. + + Returns + ------- + b_hp : ndarray + Numerator of the resulting highpass filter. + a_hp : ndarray + Denominator of the resulting highpass filter. + + """ + # Normalize the cutoff frequency to the Nyquist frequency + wp = cutoff / (fs / 2) + + # Get the lowpass filter coefficients + b_lp, a_lp = signal.butter(len(b) - 1, wp, btype='lowpass') + + # Transform to a highpass filter + b_hp = -b_lp + b_hp[int(len(b_hp) / 2)] += 1 + a_hp = a_lp + + # Use the bilinear transformation to get the corresponding + # highpass filter coefficients + b_hp, a_hp = signal.bilinear(b_hp, a_hp, fs=fs) + + return b_hp, a_hp +Generated code for scipy.signal.lp2hp_zpk + + +import scipy.signal as signal + +def lp2hp_zpk(z, p, k, fs): + """ + Convert a lowpass filter prototype to a highpass filter. + + Transform a lowpass filter prototype with zeros `z`, poles `p`, and gain + `k` to a highpass filter with the same cut-off frequency, using the + bilinear transform. + + Parameters + ---------- + z : array_like + Zeros of the lowpass filter prototype. + p : array_like + Poles of the lowpass filter prototype. + k : float + Gain of the lowpass filter prototype. + fs : float + Sampling frequency. + + Returns + ------- + z : ndarray + Zeros of the highpass filter. + p : ndarray + Poles of the highpass filter. + k : float + Gain of the highpass filter. + + """ + # prewarp the frequencies + wp = 2 * fs * np.tan(np.pi * fc / fs) + + # transform the lowpass filter to a highpass filter + z = (wp**2) * z + p = (wp**2) * p + k *= (wp**2) + + return z, p, k +Generated code for scipy.signal.lp2lp + + +import numpy as np +from scipy import signal + +def lp2lp(b, a, wo): + """ + Lowpass to lowpass digital filter transformation. + + Transform a lowpass filter prototype to a different frequency + given a new cutoff frequency wo. + + Parameters + ---------- + b : array_like + Numerator polynomial of the lowpass filter prototype. + a : array_like + Denominator polynomial of the lowpass filter prototype. + wo : float + New cutoff frequency. + + Returns + ------- + b : ndarray + Numerator polynomial of the transformed lowpass filter. + a : ndarray + Denominator polynomial of the transformed lowpass filter. + + """ + # pre-warp the frequency + wt = 2 * np.tan(np.pi * wo / 2) + + # Transform the prototype lowpass filter to a bandstop filter + (b, a) = signal.lp2bs(b, a, wo) + + # Transform the bandstop filter to a lowpass filter + (b, a) = signal.bs2lp(b, a, wt) + + return b, a +Generated code for scipy.signal.lp2lp_zpk + + +import scipy.signal as sig + +def lp2lp_zpk(z, p, k, wo=1.0, bw=None): + """ + Transform a lowpass filter prototype to a different frequency. + + Transform a lowpass filter prototype with unity cutoff frequency to + a lowpass filter with different cutoff frequency, preserving filter + order and ripple specifications. + + Parameters + ---------- + z : array_like + Zeros of the filter transfer function. + p : array_like + Poles of the filter transfer function. + k : float + System gain. + wo : float + Desired cutoff frequency (e.g. wo=2 --> twice the unity cutoff + frequency). + bw : float, optional + If given, specifies that the resulting filter should have a + -3dB point at `wo` + `bw`. Otherwise, the -3dB point will be + at `wo`. + + Returns + ------- + z2 : ndarray + Zeros of the transformed filter transfer function. + p2 : ndarray + Poles of the transformed filter transfer function. + k2 : float + System gain of the transformed filter. + + """ + # pre-warp frequencies + if bw is None: + bw = wo + else: + bw = wo + bw + w1 = 2 * wo + w2 = 2 * bw + # transform + z2 = sig.bilinear(z, p, k, w2) + p2 = sig.bilinear(z, p, k, w1) + # gain + k2 = k * (w2/w1)**(len(z) - len(p)) + return z2, p2, k2 +Generated code for scipy.signal.normalize + + +def normalize(x, axis=None): + """Normalize an array to unit length along the specified axis. + + Parameters + ---------- + x : array_like + Input array. + axis : int, optional + Axis along which the normalization is performed. Default is None, + which normalizes the entire array. + + Returns + ------- + x_norm : ndarray + The normalized array. + + """ + x_norm = np.linalg.norm(x, axis=axis, keepdims=True) + x_norm[x_norm == 0] = 1 + return x / x_norm +Generated code for scipy.signal.butter + + +import numpy as np +from scipy.signal import butter, lfilter + +def butter_lowpass(cutoff, fs, order=5): + nyq = 0.5 * fs + normal_cutoff = cutoff / nyq + b, a = butter(order, normal_cutoff, btype='low', analog=False) + return b, a + +def butter_lowpass_filter(data, cutoff, fs, order=5): + b, a = butter_lowpass(cutoff, fs, order=order) + y = lfilter(b, a, data) + return y + +# Sample rate and desired cutoff frequencies (in Hz). +fs = 5000.0 +cutoff = 500.0 + +# Filter a noisy signal. +T = 5.0 # seconds +n = int(T * fs) # total number of samples +t = np.linspace(0, T, n, endpoint=False) +# "Noisy" data. We want to recover the 1.2 Hz signal from this. +data = np.sin(1.2*2*np.pi*t) + 1.5*np.cos(9*2*np.pi*t) + 0.5*np.sin(12.0*2*np.pi*t) + +# Filter the data, and plot both the original and filtered signals. +y = butter_lowpass_filter(data, cutoff, fs, order=6) + +import matplotlib.pyplot as plt +plt.subplot(2, 1, 2) +plt.plot(t, data, 'b-', label='data') +plt.plot(t, y, 'g-', linewidth=2, label='filtered data') +plt.xlabel('Time [sec]') +plt.grid() +plt.legend() + +plt.subplots_adjust(hspace=0.35) +plt.show() +Generated code for scipy.signal.buttord + + +import numpy as np +from scipy import signal + +# Define filter parameters +Wp = 0.2 # Passband frequency +Ws = 0.3 # Stopband frequency +Rp = 3 # Passband ripple +Rs = 40 # Stopband attenuation + +# Calculate filter order and cutoff frequencies +N, Wn = signal.buttord(Wp, Ws, Rp, Rs) + +# Create the filter +b, a = signal.butter(N, Wn, 'low') +Generated code for scipy.signal.cheby1 + + +import numpy as np +from scipy import signal + +def cheby1(N, rp, Wn, btype='low', analog=False, output='ba'): + """ + Chebyshev type I digital and analog filter design. + + Design an Nth order digital or analog Chebyshev type I filter and return + the filter coefficients. + + Parameters + ---------- + N : int + The order of the filter. + rp : float + The maximum ripple allowed below unity gain in the passband. Specified + in decibels, as a positive number. + Wn : array_like + A scalar or length-2 sequence giving the critical frequencies. + For a lowpass filter, this is the upper cutoff frequency. + For a highpass filter, this is the lower cutoff frequency. + For a bandpass filter, this is the two-element vector giving the lower + and upper cutoff frequencies. + For a bandstop filter, this is the two-element vector giving the lower + and upper stopband frequencies. + btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional + The type of filter. Default is 'lowpass'. + analog : bool, optional + When True, return an analog filter, otherwise a digital filter is + returned. + output : {'ba', 'zpk', 'sos'}, optional + Type of output: numerator/denominator ('ba') or pole-zero ('zpk') or + second-order sections ('sos'). Default is 'ba'. + + Returns + ------- + b, a : ndarray, ndarray + Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. + Only returned if ``output='ba'``. + z, p, k : ndarray, ndarray, float + Zeros, poles, and system gain of the IIR filter transfer + function. Only returned if ``output='zpk'``. + sos : ndarray + Second-order sections representation of the IIR filter. + +Generated code for scipy.signal.cheb1ord + + +import numpy as np +from scipy.signal import cheb1ord + +# Define the filter parameters +wp = 0.2 # Passband frequency +ws = 0.3 # Stopband frequency +gpass = 1 # Passband maximum loss (dB) +gstop = 40 # Stopband minimum attenuation (dB) + +# Calculate the order and cutoff frequencies of the Chebyshev Type I filter +N, Wn = cheb1ord(wp, ws, gpass, gstop) + +print('Filter order =', N) +print('Cutoff frequency =', Wn) +Generated code for scipy.signal.cheby2 + + +import numpy as np +from scipy import signal + +# Chebyshev Type II filter +b, a = signal.cheby2(N=4, rp=3, Wn=0.2, btype='low', analog=False, output='ba') + +# Generate a test signal +t = np.linspace(0, 1, 1000, False) +x = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t) + +# Apply the filter +y = signal.lfilter(b, a, x) + +# Plot the original and filtered signals +import matplotlib.pyplot as plt +plt.plot(t, x, 'b-', label='x') +plt.plot(t, y, 'g-', linewidth=2, label='y') +plt.legend() +plt.grid() +plt.show() +Generated code for scipy.signal.cheb2ord + + +import scipy.signal + +def cheb2ord(wp, ws, gpass, gstop): + """ + Calculate the order and critical frequencies of a Chebyshev type II filter. + + Parameters + ---------- + wp : float + Passband cutoff frequency. + ws : float + Stopband cutoff frequency. + gpass : float + The maximum loss in the passband (dB). + gstop : float + The minimum attenuation in the stopband (dB). + + Returns + ------- + n : int + Filter order. + Wn : ndarray + Critical frequencies. + """ + # Pre-warp frequencies + wp = 2 * scipy.signal.tan(scipy.pi * wp / 2) + ws = 2 * scipy.signal.tan(scipy.pi * ws / 2) + + # Compute the order from the parameters using the bilinear transform + eps = scipy.sqrt(10**(0.1 * gpass) - 1) + delta = 10**(0.1 * gstop) - 1 + order = scipy.log(delta / eps) / scipy.log(ws / wp) + n = scipy.ceil(order) + + # Compute the critical frequencies + Wn = wp / scipy.power(delta / eps, 1.0 / n) + + return n, Wn +Generated code for scipy.signal.ellip + + +import numpy as np +from scipy import signal + +def ellip(N, rp, rs, Wn, btype='low', analog=False, output='ba'): + """ + Elliptic (Cauer) IIR filter design. + + Design an Nth-order digital or analog elliptic filter and return the + filter coefficients. + + Parameters + ---------- + N : int + The order of the filter. + rp : float + The maximum ripple in the passband (dB). + rs : float + The minimum attenuation in the stopband (dB). + Wn : array_like + A scalar or length-2 sequence giving the critical frequencies. + For a lowpass filter, this is the cutoff frequency. + For a highpass filter, this is the cutoff frequency. + For a bandpass filter, this is the two cutoff frequencies. + For a bandstop filter, this is the two corner frequencies. + btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'}, optional + The type of filter. Default is 'lowpass'. + analog : bool, optional + When True, return an analog filter, otherwise a digital filter is + returned. + output : {'ba', 'zpk', 'sos'}, optional + Type of output: numerator/denominator ('ba'), pole-zero ('zpk'), + or second-order sections ('sos'). Default is 'ba'. + + Returns + ------- + b, a : ndarray, ndarray + Numerator (`b`) and denominator (`a`) polynomials of the IIR filter. + Only returned if ``output='ba'``. + z, p, k : ndarray, ndarray, float + Zeros, poles, and system gain of the IIR filter transfer + function. Only returned if ``output='zpk'``. + sos : ndarray + Second-order sections representation of the IIR filter. + Only returned if ``output=='sos'``. + + See Also + -------- + buttord, cheb +Generated code for scipy.signal.ellipord + + +def ellipord(wp, ws, gpass, gstop, analog=False): + """ + Compute the order and critical frequencies of an elliptic filter. + + This function computes the order of an elliptic filter, given the + passband and stopband edge frequencies and the passband and stopband + gains. + + Parameters + ---------- + wp : float + Passband edge frequency. + ws : float + Stopband edge frequency. + gpass : float + The maximum loss in the passband (dB). + gstop : float + The minimum attenuation in the stopband (dB). + analog : bool, optional + When True, return an analog filter, otherwise a digital filter is + returned. + + Returns + ------- + ord : int + The lowest order for an elliptic filter which meets specs. + wn : ndarray + The critical frequencies. + + See Also + -------- + ellip, ellip_zeros, ellip_pole, ellip_gain + + Notes + ----- + The filter is described by the following equations: + + .. math:: + |H(s)| &= 1 \\ + |H(j\\omega)| &= \\sqrt{\\frac{1}{(1+(\\frac{\\omega}{\\omega_p})^{2n})} + \\frac{1}{(1+(\\frac{\\omega}{\\omega_s})^{2n})}} \\ + \\text{where: } \\omega_p &= \\text{the passband edge frequency} \\\\ + \\omega_s &= \\text{the stopband edge frequency} \\\\ + n &= \\text{the filter order} + + Examples + -------- + Design an order 3 lowpass filter with a passband of 0.2 $\pi$ + rad/sample and a stopband of 0.3 $\pi$ rad/sample: + + >>> from scipy.signal import ellipord, ellip + >>> ellipord(0.2, 0.3, 1, 60) + (3, array([ 0.2, 0.3] +Generated code for scipy.signal.bessel + + +import scipy.signal as signal + +def bessel(order, cutoff, fs): + # Calculate the filter coefficients + b, a = signal.bessel(order, cutoff, btype='low', analog=False, fs=fs) + # Return the filter coefficients + return b, a + +# Example usage +order = 4 +cutoff = 0.2 +fs = 44100 +b, a = bessel(order, cutoff, fs) +Generated code for scipy.signal.iirnotch + + +import numpy as np +from scipy import signal + +# Sample rate +fs = 1000 + +# Frequency of the signal +f0 = 50 + +# Create a notch filter +b, a = signal.iirnotch(f0, fs) + +# Generate a signal +t = np.linspace(0, 1, fs) +x = np.sin(2 * np.pi * f0 * t) + +# Apply the filter +y = signal.lfilter(b, a, x) + +# Plot the signals +import matplotlib.pyplot as plt +plt.plot(t, x, label='Original signal') +plt.plot(t, y, label='Filtered signal') +plt.xlabel('Time [s]') +plt.ylabel('Amplitude') +plt.legend() +plt.show() +Generated code for scipy.signal.iirpeak + + +import scipy.signal as signal + +# Define filter parameters +f0 = 0.2 # Center frequency +Q = 30.0 # Quality factor + +# Design filter +b, a = signal.iirpeak(f0, Q) + +# Plot frequency response +w, h = signal.freqz(b, a) + +# Plot +import matplotlib.pyplot as plt +plt.plot(w, 20 * np.log10(abs(h))) +plt.xscale('log') +plt.title('IIR peak filter frequency response') +plt.xlabel('Frequency [radians / second]') +plt.ylabel('Amplitude [dB]') +plt.margins(0, 0.1) +plt.grid(which='both', axis='both') +plt.axvline(f0, color='green') # cutoff frequency +plt.show() +Generated code for scipy.signal.iircomb + + +import scipy.signal as sig + +def iircomb(N, F, btype='lowpass', fs=2): + """ + Design an IIR comb filter. + + Parameters + ---------- + N : int + The order of the filter. + F : float + The cutoff frequency of the filter. + btype : {'lowpass', 'highpass'}, optional + The type of filter. Default is 'lowpass'. + fs : float, optional + The sampling frequency of the filter. Default is 2. + + Returns + ------- + b : ndarray + The numerator of the filter. + a : ndarray + The denominator of the filter. + """ + # Calculate the filter coefficients + if btype == 'lowpass': + b, a = sig.iircomb(N, F/fs, btype=btype) + elif btype == 'highpass': + b, a = sig.iircomb(N, F/fs, btype=btype, ftype='highpass') + else: + raise ValueError('btype must be either "lowpass" or "highpass"') + + return b, a +Generated code for scipy.signal.lti + + +import scipy.signal as sig + +# Create a linear time-invariant (LTI) system +num = [1, 2, 3] +den = [1, 4, 5, 6] +sys = sig.lti(num, den) + +# Compute the frequency response +w, mag, phase = sys.freqresp() + +# Plot the magnitude and phase of the frequency response +import matplotlib.pyplot as plt +plt.figure() +plt.title('Magnitude and Phase') +plt.plot(w, mag, label='Magnitude') +plt.plot(w, phase, label='Phase') +plt.xlabel('Frequency [rad/s]') +plt.ylabel('Magnitude/Phase [dB]') +plt.legend() +plt.show() +Generated code for scipy.signal.StateSpace + + +import numpy as np +from scipy import signal + +# Define the state-space system +A = np.array([[0, 1], [-2, -3]]) +B = np.array([[0], [1]]) +C = np.array([[1, 0]]) +D = np.array([[0]]) + +# Create the state-space system +sys = signal.StateSpace(A, B, C, D) + +# Compute the response to an input +t = np.linspace(0, 10, 1000) +u = np.ones_like(t) +t, y = signal.dlsim(sys, u, t) +Generated code for scipy.signal.TransferFunction + + +import scipy.signal as sig + +# Create a transfer function +num = [1, 2, 3] +den = [1, 4, 5, 6] +sys = sig.TransferFunction(num, den) + +# Compute the frequency response +w, mag, phase = sys.bode() + +# Plot the magnitude and phase +import matplotlib.pyplot as plt +plt.figure() +plt.semilogx(w, mag) # Bode magnitude plot +plt.figure() +plt.semilogx(w, phase) # Bode phase plot +plt.show() +Generated code for scipy.signal.ZerosPolesGain + + +import scipy.signal + +zeros, poles, gain = scipy.signal.ZerosPolesGain(zeros=[-1, -2], poles=[1, 2], gain=3) + +print(zeros) +print(poles) +print(gain) +Generated code for scipy.signal.lsim + + +import numpy as np +from scipy.signal import lsim + +# Define the system +num = [1, 2] +den = [1, 4, 3] + +# Define the input +t = np.linspace(0, 10, 1000) +u = np.ones_like(t) + +# Compute the response +t, y = lsim(num, den, u, t) + +# Plot the response +import matplotlib.pyplot as plt +plt.plot(t, y) +plt.xlabel('Time [s]') +plt.ylabel('Amplitude') +plt.show() +Generated code for scipy.signal.lsim2 + + +import numpy as np +from scipy.signal import lsim2 + +# Define system parameters +A = np.array([[0, 1], [-2, -3]]) +B = np.array([[0], [1]]) +C = np.array([[1, 0]]) +D = np.array([[0]]) + +# Define input signal +t = np.linspace(0, 10, 100) +u = np.sin(t) + +# Compute the response +tout, y, x = lsim2(A, B, C, D, u, t) + +# Plot the response +import matplotlib.pyplot as plt +plt.plot(tout, y) +plt.xlabel('Time [s]') +plt.ylabel('Response') +plt.show() +Generated code for scipy.integrate.odeint + + +import numpy as np +from scipy.integrate import odeint + +def f(y, t): + return y**2 + t + +t = np.linspace(0, 5, 100) +y0 = 0 + +y = odeint(f, y0, t) + +print(y) +Generated code for scipy.signal.impulse + + +import numpy as np +from scipy import signal + +# Generate a test signal +t = np.linspace(0, 10, 500) +x = np.sin(2 * np.pi * 0.5 * t) + +# Generate the impulse response +impulse_response = signal.impulse(x, N=20) + +# Plot the impulse response +plt.plot(t, impulse_response) +plt.xlabel('Time [s]') +plt.ylabel('Amplitude') +plt.title('Impulse Response') +plt.show() +Generated code for scipy.signal.impulse2 + + +import numpy as np +from scipy.signal import impulse2 + +# Generate a signal with two impulses +t = np.arange(0, 10, 0.01) +x = np.zeros(len(t)) +x[50] = 1 +x[150] = -1 + +# Generate the impulse response +impulse_response = impulse2(x, t) + +# Plot the impulse response +import matplotlib.pyplot as plt +plt.plot(t, impulse_response) +plt.show() +Generated code for scipy.signal.step + + +import numpy as np +from scipy import signal + +def step(t, t_on=0, t_off=np.inf, y_on=1, y_off=0): + """ + Generate a step signal. + + Parameters + ---------- + t : array_like + Time points. + t_on : float, optional + Time when the signal turns on. + t_off : float, optional + Time when the signal turns off. + y_on : float, optional + Value when the signal is on. + y_off : float, optional + Value when the signal is off. + + Returns + ------- + y : ndarray + The step signal. + """ + y = np.zeros_like(t) + y[t >= t_on] = y_on + y[t >= t_off] = y_off + return y + +# Generate a step signal +t = np.linspace(0, 10, 1000) +y = step(t, t_on=2, t_off=8, y_on=1, y_off=0) + +# Plot the signal +import matplotlib.pyplot as plt +plt.plot(t, y) +plt.xlabel('Time (s)') +plt.ylabel('Signal') +plt.show() +Generated code for scipy.signal.step2 + + +import numpy as np +from scipy import signal + +# Generate a step signal +t = np.linspace(0, 10, 500) +x = signal.step2(t) + +# Plot the signal +import matplotlib.pyplot as plt +plt.plot(t, x) +plt.title('Step Signal') +plt.xlabel('Time (s)') +plt.ylabel('Amplitude') +plt.show() +Generated code for scipy.signal.freqresp + + +import numpy as np +from scipy import signal + +# Generate a test signal +t = np.linspace(0, 1, 1000) +x = np.sin(2 * np.pi * 5 * t) + +# Compute the frequency response +w, h = signal.freqresp(x, t) + +# Plot the frequency response +import matplotlib.pyplot as plt +plt.plot(w, h) +plt.xlabel('Frequency [rad/s]') +plt.ylabel('Amplitude [dB]') +plt.grid() +plt.show() +Generated code for scipy.signal.bode + + +import numpy as np +from scipy import signal + +# Generate a test signal +t = np.linspace(0, 10, 1000) +x = np.sin(2*np.pi*t) + +# Compute the bode plot +w, mag, phase = signal.bode(x, t) + +# Plot the magnitude and phase +import matplotlib.pyplot as plt +plt.figure() +plt.semilogx(w, mag) # Bode magnitude plot +plt.figure() +plt.semilogx(w, phase) # Bode phase plot +plt.show() +Generated code for scipy.signal.dlti + + +import scipy.signal as sig + +# Create a discrete-time linear time-invariant (DLTI) system +num = [1, 2, 3] +den = [1, 4, 5] +sys = sig.dlti(num, den) + +# Compute the impulse response of the system +t, y = sig.dimpulse(sys) + +# Plot the impulse response +import matplotlib.pyplot as plt +plt.plot(t, y) +plt.xlabel('Time (s)') +plt.ylabel('Amplitude') +plt.title('Impulse Response of DLTI System') +plt.show() +Generated code for scipy.signal.StateSpace + + +import numpy as np +from scipy import signal + +# Define the state-space system +A = np.array([[0, 1], [-2, -3]]) +B = np.array([[0], [1]]) +C = np.array([[1, 0]]) +D = np.array([[0]]) + +# Create the state-space system +sys = signal.StateSpace(A, B, C, D) + +# Compute the response +t, y = signal.step(sys) + +# Plot the response +import matplotlib.pyplot as plt +plt.plot(t, y) +plt.xlabel('Time [s]') +plt.ylabel('y(t)') +plt.show() +Generated code for scipy.signal.TransferFunction + + +import scipy.signal as sig + +# Create a transfer function +num = [1, 2, 3] +den = [4, 5, 6] +tf = sig.TransferFunction(num, den) + +# Compute the frequency response +w, mag, phase = sig.bode(tf) + +# Plot the magnitude and phase +plt.figure() +plt.semilogx(w, mag) # Bode magnitude plot +plt.figure() +plt.semilogx(w, phase) # Bode phase plot +plt.show() +Generated code for scipy.signal.ZerosPolesGain + + +import scipy.signal + +zeros, poles, gain = scipy.signal.ZerosPolesGain(zeros=[-1, -2], poles=[1, 2], gain=3) + +print(zeros) +print(poles) +print(gain) +Generated code for scipy.signal.dlsim + + +import scipy.signal as sig +import numpy as np + +# Define system parameters +num = [1, 2, 3] +den = [1, 4, 5, 6] + +# Create input signal +t = np.linspace(0, 10, 1000) +u = np.sin(t) + +# Simulate system +tout, y, x = sig.dlsim(sig.lti(num, den), u, t) + +# Plot results +import matplotlib.pyplot as plt +plt.plot(tout, y) +plt.xlabel('Time (s)') +plt.ylabel('Output (y)') +plt.show() +Generated code for scipy.signal.dimpulse + + +import numpy as np +from scipy import signal + +# Generate a unit impulse +impulse = np.zeros(10) +impulse[0] = 1 + +# Generate the response of a system to the impulse +response = signal.dimpulse(system=(1, [1, 1, 0, 0]), n=10) + +# Plot the impulse and response +import matplotlib.pyplot as plt + +plt.plot(impulse, label='Impulse') +plt.plot(response, label='Response') +plt.legend() +plt.show() +Generated code for scipy.signal.dstep + + +import numpy as np +from scipy import signal + +def dstep(t,t0,y0,y1): + """ + Generate a discrete step signal. + + Parameters + ---------- + t : array_like + Time points of the output signal. + t0 : float + Time point of the step. + y0 : float + Value of the signal before the step. + y1 : float + Value of the signal after the step. + + Returns + ------- + y : ndarray + The values of the step signal at the given time points. + """ + y = np.ones_like(t) * y0 + y[t >= t0] = y1 + return y + +# Generate a discrete step signal +t = np.arange(0, 10, 0.1) +y = dstep(t, 5, 0, 1) + +# Plot the signal +plt.plot(t, y) +plt.title('Discrete Step Signal') +plt.xlabel('Time (s)') +plt.ylabel('Amplitude') +plt.show() +Generated code for scipy.signal.dfreqresp + + +import numpy as np +from scipy import signal + +def dfreqresp(b, a, worN=None, whole=False): + """ + Compute the frequency response of a digital filter. + + The frequency response is evaluated for digital filters, by computing the + filter's transfer function at a number of frequencies. The frequency + response is returned as a complex array. + + Parameters + ---------- + b : array_like + Numerator polynomial of the filter. + a : array_like + Denominator polynomial of the filter. + worN : {None, int, array_like}, optional + If `None`, then compute at 512 frequencies around the unit circle. + If a single integer, then compute at that many frequencies. + If an array_like, compute the response at the frequencies given + (in radians/sample). + whole : bool, optional + Normally, frequencies are computed from 0 to pi (upper-half of + unit-circle. If `whole` is True, compute frequencies from 0 to 2*pi. + + Returns + ------- + w : ndarray + The frequencies at which h was computed, in radians/sample. + h : ndarray + The frequency response. + + """ + b, a = map(np.atleast_1d, (b, a)) + if worN is None: + worN = 512 + if np.isscalar(worN): + w = np.linspace(0, np.pi, worN, endpoint=False) + if whole: + w = np.concatenate((w, w[-2:0:-1])) + else: + w = np.asarray(worN) + zm1 = np.exp(-1j * w) + h = np.polyval(b[::-1], zm1) / np.polyval(a[::-1], zm1) + return w, h +Generated code for scipy.signal.dbode + + +import scipy.signal as sig +import numpy as np + +def dbode(sys, w): + """ + Compute the dB magnitude and phase of a system at a given frequency. + + Parameters + ---------- + sys : tuple + A tuple of (num, den) containing the numerator and denominator + polynomials of the system transfer function. + w : float + The frequency at which to compute the magnitude and phase. + + Returns + ------- + mag : float + The magnitude of the system at the given frequency in dB. + phase : float + The phase of the system at the given frequency in degrees. + """ + # Compute the transfer function + num, den = sys + H = sig.lti(num, den) + + # Compute the frequency response + w, mag, phase = H.freqresp(w) + + # Convert to dB + mag = 20 * np.log10(mag) + + return mag, phase +Generated code for scipy.signal.tf2zpk + + +import numpy as np +from scipy.signal import tf2zpk + +# Define transfer function +num = [1, 2, 3] +den = [1, 4, 6, 4] + +# Convert to zeros, poles, and gain +z, p, k = tf2zpk(num, den) + +# Print results +print('Zeros:', z) +print('Poles:', p) +print('Gain:', k) +Generated code for scipy.signal.tf2sos + + +import scipy.signal as signal + +# Define transfer function +num = [1, 2, 3] +den = [4, 5, 6, 7] + +# Convert to SOS form +sos = signal.tf2sos(num, den) + +# Print SOS form +print(sos) +Generated code for scipy.signal.tf2ss + + +import numpy as np +from scipy import signal + +def tf2ss(num, den): + """ + Convert a transfer function to state-space form. + + Parameters + ---------- + num : array_like + numerator polynomial coefficients in descending powers + den : array_like + denominator polynomial coefficients in descending powers + + Returns + ------- + A : ndarray + State-space representation of transfer function + B : ndarray + State-space representation of transfer function + C : ndarray + State-space representation of transfer function + D : ndarray + State-space representation of transfer function + + """ + # Get the order of the numerator and denominator + n_num = len(num) + n_den = len(den) + + # Compute the state-space matrices + A = np.zeros((n_den-1, n_den-1)) + B = np.zeros((n_den-1, 1)) + C = np.zeros((1, n_den-1)) + D = np.zeros((1, 1)) + + # Fill in the A matrix + for i in range(n_den-1): + for j in range(n_den-1): + if i == j+1: + A[i, j] = den[j+1] + else: + A[i, j] = 0 + + # Fill in the B matrix + for i in range(n_den-1): + B[i, 0] = -den[i+1] + + # Fill in the C matrix + for i in range(n_den-1): + C[0, i] = num[i] + + # Fill in the D matrix + D[0, 0] = num[n_num-1] + + return A, B, C, D +Generated code for scipy.signal.zpk2tf + + +import numpy as np +from scipy.signal import zpk2tf + +# Define the zeros, poles, and gain of the transfer function +zeros = np.array([-2, -3, -4]) +poles = np.array([0.5, 1.2, 3.1]) +gain = 5 + +# Convert the zeros, poles, and gain to a transfer function +num, den = zpk2tf(zeros, poles, gain) + +# Print the transfer function +print('Transfer function:') +print('num =', num) +print('den =', den) +Generated code for scipy.signal.zpk2sos + + +import scipy.signal + +def zpk2sos(z, p, k): + sos = scipy.signal.zpk2sos(z, p, k) + return sos + +z = [1, 2, 3] +p = [4, 5, 6] +k = 7 + +sos = zpk2sos(z, p, k) +print(sos) +Generated code for scipy.signal.zpk2ss + + +import numpy as np +from scipy.signal import zpk2ss + +# Define the zeros, poles, and gain of the system +zeros = np.array([-2, -3, -4]) +poles = np.array([0.5, -0.5, -1.5]) +gain = 5 + +# Convert the zpk to state-space +A, B, C, D = zpk2ss(zeros, poles, gain) + +# Print the state-space matrices +print('A = \n', A) +print('B = \n', B) +print('C = \n', C) +print('D = \n', D) +Generated code for scipy.signal.ss2tf + + +import numpy as np +from scipy import signal + +# Define the numerator and denominator polynomials +num = [1, 2, 3] +den = [1, 4, 5, 6] + +# Convert the polynomials to transfer function +tf = signal.ss2tf(num, den) + +# Print the transfer function +print(tf) +Generated code for scipy.signal.ss2zpk + + +import scipy.signal as sig + +def ss2zpk(A, B, C, D): + """ + Convert a state-space representation of a system to zero-pole-gain form. + + Parameters + ---------- + A, B, C, D : array_like + State-space representation of linear system. + + Returns + ------- + z : ndarray + Zeros of the system. + p : ndarray + Poles of the system. + k : float + System gain. + + """ + z, p, k = sig.tf2zpk(sig.ss2tf(A, B, C, D)) + return z, p, k +Generated code for scipy.signal.sos2zpk + + +import scipy.signal as sig + +def sos2zpk(sos): + """ + Convert a second-order section representation to zero-pole-gain form. + + Parameters + ---------- + sos : array_like + Array of second-order filter coefficients, must have shape (n_sections, 6). + + Returns + ------- + z : ndarray + Zeros of the transfer function. + p : ndarray + Poles of the transfer function. + k : float + System gain. + """ + # Check input + sos = sig.asarray(sos) + if sos.ndim != 2 or sos.shape[1] != 6: + raise ValueError('sos array must be shape (n_sections, 6)') + + # Pre-allocate output + z = np.empty(0, dtype=complex) + p = np.empty(0, dtype=complex) + + # Convert each section + for section in sos: + b = section[:3] + a = section[3:] + z_section, p_section, k_section = sig.tf2zpk(b, a) + z = np.concatenate((z, z_section)) + p = np.concatenate((p, p_section)) + + # Compute overall gain + k = np.prod(sos[:, 0]) + + return z, p, k +Generated code for scipy.signal.sos2tf + + +import numpy as np +from scipy.signal import sos2tf + +# Define the SOS matrix +sos = np.array([[1, 2, 1, 1, 0, 0], + [1, -2, 1, 0, 0, 0]]) + +# Convert SOS to transfer function +num, den = sos2tf(sos) + +print('Numerator:', num) +print('Denominator:', den) +Generated code for scipy.signal.cont2discrete + + +import numpy as np +from scipy.signal import cont2discrete + +# Define the continuous-time system +num = [1, 2] +den = [1, 4, 3] + +# Define the sampling time +dt = 0.1 + +# Convert the system to discrete-time +[numd, dend] = cont2discrete((num, den), dt) + +# Print the discrete-time system +print('Discrete-time system:') +print('Numerator:', numd) +print('Denominator:', dend) +Generated code for scipy.signal.place_poles + + +import numpy as np +from scipy import signal + +def place_poles(A, B, poles): + """ + Place the poles of a linear system. + + Parameters + ---------- + A : array_like + Numerator polynomial of the linear system. + B : array_like + Denominator polynomial of the linear system. + poles : array_like + Desired poles of the linear system. + + Returns + ------- + A_new : ndarray + Numerator polynomial of the linear system with the desired poles. + B_new : ndarray + Denominator polynomial of the linear system with the desired poles. + + """ + # Compute the transfer function + s = signal.lti(A, B) + # Compute the zeros and poles of the transfer function + z, p, k = signal.tf2zpk(s.num, s.den) + # Compute the new zeros and poles + z_new, p_new, k_new = signal.zpk2tf(z, poles, k) + # Compute the new numerator and denominator polynomials + A_new, B_new = signal.zpk2tf(z_new, p_new, k_new) + return A_new, B_new +Generated code for scipy.signal.chirp + + +import numpy as np +from scipy.signal import chirp + +# Generate a linear chirp of 1 second duration that starts at 0 Hz and ends at 500 Hz +t = np.linspace(0, 1, 500) +w = chirp(t, f0=0, f1=500, t1=1, method='linear') + +# Generate a logarithmic chirp of 1 second duration that starts at 0 Hz and ends at 500 Hz +t = np.linspace(0, 1, 500) +w = chirp(t, f0=0, f1=500, t1=1, method='logarithmic') +Generated code for scipy.signal.gausspulse + + +import numpy as np +from scipy import signal + +def gausspulse(t, fc, bw, bwr=-6, tpr=-60, retquad=False, retenv=False): + """ + Return a Gaussian modulated sinusoid: + + .. math:: + x(t) = A \\cos(2 \\pi f_c t + \\phi) \\exp \\left(-\\frac{t^2}{2 \\tau^2}\\right) + + where :math:`\\tau = \\frac{1}{2\\pi b_w}` is the Gaussian width, + :math:`f_c` is the center frequency, and :math:`b_w` is the fractional + bandwidth in octaves (between -3 dB points of the Gaussian). + + Parameters + ---------- + t : array_like + Time vector or scalar. + fc : float + Center frequency of sinusoid. + bw : float + Fractional bandwidth in octaves (between -3 dB points of the + Gaussian). + bwr : float, optional + Reference level at which fractional bandwidth is calculated (dB). + tpr : float, optional + Peak level of Gaussian envelope (dB). + retquad : bool, optional + If True, return the real and imaginary parts of the complex + sinusoid. + retenv : bool, optional + If True, return the envelope of the sinusoid. + + Returns + ------- + x : ndarray + The modulated sinusoid. + xa : ndarray + Real part of `x` (if `retquad` is True). + xb : ndarray + Imaginary part of `x` (if `retquad` is True). + env : ndarray + The envelope of the sinusoid (if `retenv` is True). + + See Also + -------- + chirp : Frequency-swept cosine generator. + gauss : Gaussian pulse generator. + sweep_poly : Frequency-swept cosine generator (polynom +Generated code for scipy.signal.max_len_seq + + +import numpy as np +from scipy.signal import max_len_seq + +# Generate a maximum length sequence +x = max_len_seq(7) + +# Print the sequence +print(x) + +# Compute the autocorrelation of the sequence +r = np.correlate(x, x, mode='full') + +# Print the autocorrelation +print(r) +Generated code for scipy.signal.sawtooth + + +import numpy as np +from scipy.signal import sawtooth + +# Generate a sawtooth waveform with a frequency of 1 Hz +t = np.linspace(0, 1, 500, endpoint=False) +saw = sawtooth(2 * np.pi * 1 * t) + +# Plot the waveform +import matplotlib.pyplot as plt +plt.plot(t, saw) +plt.xlabel("Time [s]") +plt.ylabel("Amplitude") +plt.show() +Generated code for scipy.signal.square + + +import numpy as np +from scipy import signal + +def square(t, duty): + """ + Generate a square wave with given duty cycle. + + Parameters + ---------- + t : array_like + Time. + duty : float + Duty cycle, must be in the interval [0,1]. + + Returns + ------- + y : ndarray + Square wave of the given duty cycle. + + """ + y = signal.square(2 * np.pi * t, duty) + return y +Generated code for scipy.signal.sweep_poly + + +import numpy as np +from scipy.signal import sweep_poly + +# Define the numerator and denominator polynomials +num = [1, 0, 0] +den = [1, 0.2, 0.3] + +# Generate the sweep signal +t, x = sweep_poly(num, den) + +# Plot the sweep signal +plt.plot(t, x) +plt.xlabel('Time (s)') +plt.ylabel('Amplitude') +plt.title('Sweep Signal') +plt.show() +Generated code for scipy.signal.unit_impulse + + +import numpy as np +from scipy.signal import unit_impulse + +# Generate a unit impulse signal +impulse_signal = unit_impulse(10, 'mid') + +# Print the signal +print(impulse_signal) + +# Output: +# [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] +Generated code for scipy.signal.get_window + + +import numpy as np +from scipy.signal import get_window + +def get_window(window, Nx, fftbins=True): + """ + Return a window of length Nx and type window. + + Parameters + ---------- + window : string, float, or tuple + The type of window to create. See `scipy.signal.get_window` + for a list of windows and required parameters. + Nx : int + The number of samples in the window. + fftbins : bool, optional + If True, create a periodic window for use with FFT + (creates a Hanning window by default). + + Returns + ------- + w : ndarray + The window of length Nx + """ + if fftbins: + return get_window(window, Nx, fftbins=True) + else: + return get_window(window, Nx) +Generated code for scipy.signal.cascade + + +import scipy.signal as sig + +def cascade(b, a): + """ + Cascade two IIR filter stages. + + Parameters + ---------- + b : array_like + Numerator coefficients of the first stage. + a : array_like + Denominator coefficients of the first stage. + + Returns + ------- + b_out : ndarray + Numerator coefficients of the output filter. + a_out : ndarray + Denominator coefficients of the output filter. + + """ + # Get the numerator and denominator coefficients of the first stage + b1, a1 = sig.tf2zpk(b, a) + + # Get the numerator and denominator coefficients of the second stage + b2, a2 = sig.tf2zpk(b, a) + + # Cascade the two stages + b_out, a_out = sig.cascade(b1, a1, b2, a2) + + return b_out, a_out +Generated code for scipy.signal.daub + + +import numpy as np +from scipy.signal import daub + +# Define the length of the signal +N = 64 + +# Generate a random signal +x = np.random.randn(N) + +# Compute the Daubechies wavelet transform of the signal +coeffs = daub(x, 4) + +# Reconstruct the signal from the wavelet coefficients +x_rec = daub(coeffs, 4, inverse=True) + +# Compare the original and reconstructed signals +print(np.allclose(x, x_rec)) +Generated code for scipy.signal.morlet + + +import numpy as np +from scipy.signal import morlet + +def morlet_wavelet(M, w=5.0, s=1.0): + """ + Morlet wavelet function. + + Parameters + ---------- + M : int + Length of the wavelet. + w : float, optional + Omega0. Default is 5. + s : float, optional + Scaling factor, default is 1. + + Returns + ------- + morlet_wavelet : (M,) ndarray + Morlet wavelet. + """ + x = np.linspace(-np.pi, np.pi, M) + y = np.exp(1j * w * x) + y *= np.exp(-0.5 * (x ** 2)) + return y / np.sqrt(s * np.sum(np.abs(y) ** 2)) + +# Generate the Morlet wavelet +morlet_wavelet = morlet_wavelet(M=128, w=5.0, s=1.0) + +# Plot the Morlet wavelet +plt.plot(morlet_wavelet.real) +plt.title('Morlet Wavelet') +plt.xlabel('Time (samples)') +plt.ylabel('Amplitude') +plt.show() +Generated code for scipy.signal.qmf + + +def qmf(x, h): + """ + Compute the Quadrature Mirror Filter (QMF) of a signal. + + Parameters + ---------- + x : array_like + Input signal. + h : array_like + Filter coefficients. + + Returns + ------- + y : ndarray + The QMF of the signal. + + """ + # Make sure x and h are numpy arrays + x = np.asarray(x) + h = np.asarray(h) + + # Compute the length of the input signal + N = len(x) + + # Compute the length of the filter + M = len(h) + + # Compute the length of the output signal + L = N + M - 1 + + # Compute the QMF of the signal + y = np.zeros(L) + for n in range(L): + for m in range(M): + if n-m >= 0 and n-m < N: + y[n] += h[m] * x[n-m] + + return y +Generated code for scipy.signal.ricker + + +import numpy as np +from scipy.signal import ricker + +def ricker_wavelet(points, a): + """ + Return a Ricker wavelet, also known as the Mexican hat wavelet. + + It models the function: + + ``A (1 - x^2/a^2) exp (-t^2/a^2)``, + + where ``A = 2/sqrt(3a)pi^1/4`` + + Parameters + ---------- + points : int + Number of points in `vector`. + a : scalar + Width parameter of the wavelet. + + Returns + ------- + vector : (N,) ndarray + Array of length `points` in the form of a Ricker wavelet. + + Examples + -------- + >>> from scipy.signal import ricker_wavelet + >>> import matplotlib.pyplot as plt + >>> points = 100 + >>> a = 4.0 + >>> vec = ricker_wavelet(points, a) + >>> plt.plot(vec) + >>> plt.show() + """ + A = 2 / np.sqrt(3 * a) * np.pi**(1/4) + wsq = a**2 + vec = np.arange(points) - (points - 1.0) / 2 + xsq = vec**2 + mod = (1 - xsq / wsq) + ricker = A * mod * np.exp(-xsq / (2 * wsq)) + return ricker +Generated code for scipy.signal.morlet2 + + +import numpy as np +from scipy.signal import morlet2 + +def morlet2(M, w=5.0, s=1.0): + """ + Morlet wavelet function. + + Parameters + ---------- + M : int + Length of the wavelet. + w : float, optional + Omega0. Default is 5. + s : float, optional + Scaling factor, default 1. + + Returns + ------- + x : ndarray + Wavelet function. + + """ + x = np.arange(0, M) - (M - 1.0) / 2.0 + x = x / s + y = np.exp(1j * w * x) + y = y * np.exp(-0.5 * (x ** 2)) + return y +Generated code for scipy.signal.cwt + + +import numpy as np +from scipy import signal + +# Generate a signal with two frequencies +t = np.linspace(0, 10, 500) +sig = np.sin(2 * np.pi * t) + np.sin(4 * np.pi * t) + +# Compute the continuous wavelet transform +widths = np.arange(1, 31) +cwtmatr = signal.cwt(sig, signal.ricker, widths) + +# Plot the CWT +plt.imshow(cwtmatr, extent=[-1, 1, 1, 31], cmap='PRGn', aspect='auto', + vmax=abs(cwtmatr).max(), vmin=-abs(cwtmatr).max()) +plt.show() +Generated code for scipy.signal.cwt + + +import numpy as np +from scipy import signal + +# Generate a signal with two frequencies +t = np.linspace(0, 10, 500) +sig = np.sin(2 * np.pi * t) + np.sin(4 * np.pi * t) + +# Compute the continuous wavelet transform +widths = np.arange(1, 31) +cwtmatr = signal.cwt(sig, signal.ricker, widths) + +# Plot the CWT +plt.imshow(cwtmatr, extent=[-1, 1, 1, 31], cmap='PRGn', aspect='auto', + vmax=abs(cwtmatr).max(), vmin=-abs(cwtmatr).max()) +plt.show() +Generated code for scipy.signal.argrelmin + + +import numpy as np +from scipy.signal import argrelmin + +# Create a sample array +x = np.array([2, 1, 2, 3, 2, 0, 1, 0, 1, 0, 2, 1]) + +# Find the indices of the local minima +min_indices = argrelmin(x)[0] + +# Print the indices +print(min_indices) +Generated code for scipy.signal.argrelmax + + +import numpy as np +from scipy.signal import argrelmax + +# Create a sample array +x = np.array([2, 1, 2, 3, 2, 0, 1, 0, 1, 0, 2, 1]) + +# Find the relative maxima +maxima = argrelmax(x) + +# Print the indices of the maxima +print(maxima[0]) + +# Output: +# array([3, 10]) +Generated code for scipy.signal.argrelextrema + + +import numpy as np +from scipy.signal import argrelextrema + +# Create a sample array +arr = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]) + +# Find the local maxima +maxima = argrelextrema(arr, np.greater) + +# Print the maxima +print(maxima) +Generated code for scipy.signal.find_peaks + + +import numpy as np +from scipy.signal import find_peaks + +# Generate some data +x = np.linspace(0, 10, 1000) +y = np.sin(x) + +# Find peaks +peaks, _ = find_peaks(y, height=0) + +# Plot the data +import matplotlib.pyplot as plt +plt.plot(x, y) +plt.plot(x[peaks], y[peaks], "x") +plt.show() +Generated code for scipy.signal.find_peaks_cwt + + +import numpy as np +from scipy.signal import find_peaks_cwt + +# Generate some data +x = np.linspace(0, 10, 1000) +y = np.sin(x) + +# Find peaks +peaks = find_peaks_cwt(y, np.arange(1, 10)) + +# Print the peaks +print(peaks) +Generated code for scipy.signal.peak_prominences + + +import numpy as np +from scipy.signal import peak_prominences + +# Generate a signal +x = np.linspace(0, 10, 1000) +y = np.sin(x) + +# Find the peak prominences +prominences, _ = peak_prominences(y, x) + +# Print the prominences +print(prominences) +Generated code for scipy.signal.peak_widths + + +import numpy as np +from scipy.signal import peak_widths + +# Generate a noisy peak +x = np.linspace(0, 10, 100) +y = np.exp(-x/2) + np.random.randn(100)/10 + +# Calculate peak widths +widths, _ = peak_widths(y, x, rel_height=0.5) + +# Print peak widths +print(widths) +Generated code for scipy.signal.periodogram + + +import numpy as np +from scipy import signal + +# Generate a test signal +fs = 10e3 +N = 1e5 +amp = 2*np.sqrt(2) +freq = 1234.0 +noise_power = 0.001 * fs / 2 +time = np.arange(N) / fs +x = amp*np.sin(2*np.pi*freq*time) +x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape) + +# Compute and plot the periodogram +f, Pxx_den = signal.periodogram(x, fs) + +# Plot the power spectral density +import matplotlib.pyplot as plt +plt.semilogy(f, Pxx_den) +plt.xlabel('frequency [Hz]') +plt.ylabel('PSD [V**2/Hz]') +plt.show() +Generated code for scipy.signal.welch + + +import numpy as np +from scipy import signal + +# Generate a test signal, a 2 Vrms sine wave at 1234 Hz, corrupted by 0.001 V**2/Hz of white noise sampled at 10 kHz. + +fs = 10e3 +N = 1e5 +amp = 2*np.sqrt(2) +freq = 1234.0 +noise_power = 0.001 * fs / 2 +time = np.arange(N) / fs +x = amp*np.sin(2*np.pi*freq*time) +x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape) + +# Compute and plot the power spectral density. + +f, Pxx_den = signal.welch(x, fs, nperseg=1024) +plt.semilogy(f, Pxx_den) +plt.ylim([0.5e-3, 1]) +plt.xlabel('frequency [Hz]') +plt.ylabel('PSD [V**2/Hz]') +plt.show() +Generated code for scipy.signal.csd + + +import numpy as np +from scipy import signal + +def csd(x, y, fs=1.0, window='boxcar', nperseg=None, noverlap=None, + nfft=None, detrend='constant', return_onesided=True, + scaling='density', axis=-1): + """ + Estimate the cross spectral density, Pxy, using Welch's method. + Parameters + ---------- + x : array_like + Time series of measurement values + y : array_like + Time series of measurement values + fs : float, optional + Sampling frequency of the `x` and `y` time series. Defaults + to 1.0. + window : str or tuple or array_like, optional + Desired window to use. If `window` is a string or tuple, it is + passed to `get_window` to generate the window values, which are + DFT-even by default. See `get_window` for a list of windows and + required parameters. If `window` is array_like it will be used + directly as the window and its length must be nperseg. Defaults + to 'boxcar'. + nperseg : int, optional + Length of each segment. Defaults to None, but if window is str or + tuple, is set to 256, and if window is array_like, is set to the + length of the window. + noverlap : int, optional + Number of points to overlap between segments. If `None`, + ``noverlap = nperseg // 2``. Defaults to `None`. + nfft : int, optional + Length of the FFT used, if a zero padded FFT is desired. If + `None`, the FFT length is `nperseg`. Defaults to `None`. + detrend : str or function or `False`, optional + Specifies how to detrend each segment. If `detrend` is a + string, it is passed as the `type` argument to the `detrend` + function. If it is a function, it takes a segment and returns a + detrended segment. If +Generated code for scipy.signal.coherence + + +import numpy as np +from scipy import signal + +def coherence(x, y, fs=1.0, window='hanning', nperseg=256, noverlap=None, nfft=None, detrend='constant', return_onesided=True, scaling='density', axis=-1): + """ + Estimate the magnitude squared coherence estimate, Cxy, of discrete-time signals x and y. + + Parameters + ---------- + x : array_like + Array of at least two dimensions. The cross-spectral density is estimated + along the last axis. + y : array_like + Array of at least two dimensions. The cross-spectral density is estimated + along the last axis. + fs : float, optional + Sampling frequency of the `x` and `y` time series. Defaults to 1.0. + window : str or tuple or array_like, optional + Desired window to use. See `scipy.signal.get_window` for a list of windows + and required parameters. If `window` is array_like it will be used directly + as the window and its length will be used for nperseg. Defaults to 'hanning'. + nperseg : int, optional + Length of each segment. Defaults to 256. + noverlap : int, optional + Number of points to overlap between segments. If `None`, + ``noverlap = nperseg // 2``. Defaults to `None`. + nfft : int, optional + Length of the FFT used, if a zero padded FFT is desired. If `None`, + the FFT length is `nperseg`. Defaults to `None`. + detrend : str or function or `False`, optional + Specifies how to detrend each segment. If `detrend` is a string, it is + passed as the `type` argument to the `scipy.signal.detrend` function. + If it is a function, it takes a segment and returns a detrended segment. + If `detrend` is `False`, no detrending is done. Defaults to 'constant'. +Generated code for scipy.signal.spectrogram + + +import numpy as np +from scipy import signal + +# Generate a test signal, a 2 Vrms sine wave at 1234 Hz, corrupted by +# 0.001 V**2/Hz of white noise sampled at 10 kHz. +fs = 10e3 +N = 1e5 +amp = 2*np.sqrt(2) +freq = 1234.0 +noise_power = 0.001 * fs / 2 +time = np.arange(N) / fs +x = amp*np.sin(2*np.pi*freq*time) +x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape) + +# Compute and plot the spectrogram. +f, t, Sxx = signal.spectrogram(x, fs) + +# Plot the spectrogram +import matplotlib.pyplot as plt +plt.pcolormesh(t, f, Sxx) +plt.ylabel('Frequency [Hz]') +plt.xlabel('Time [sec]') +plt.show() +Generated code for scipy.signal.lombscargle + + +import numpy as np +from scipy.signal import lombscargle + +# Generate some data +x = np.linspace(0, 10, 100) +y = np.sin(x) + +# Compute the Lomb-Scargle periodogram +frequency, power = lombscargle(x, y, normalize=True) + +# Plot the results +import matplotlib.pyplot as plt +plt.plot(frequency, power) +plt.xlabel('Frequency') +plt.ylabel('Power') +plt.show() +Generated code for scipy.signal.vectorstrength + + +import numpy as np +from scipy.signal import vectorstrength + +# Generate a signal with a known period +fs = 1000 +t = np.arange(0, 2, 1/fs) +x = np.sin(2*np.pi*t*10) + +# Calculate the vector strength +periods, vector_strength = vectorstrength(x, fs) + +# Print the results +print('Periods:', periods) +print('Vector Strength:', vector_strength) +Generated code for scipy.signal.stft + + +import numpy as np +from scipy import signal + +def stft(x, fs, framesz, hop): + framesamp = int(framesz*fs) + hopsamp = int(hop*fs) + w = signal.hann(framesamp) + X = np.array([np.fft.rfft(w*x[i:i+framesamp]) + for i in range(0, len(x)-framesamp, hopsamp)]) + return X +Generated code for scipy.signal.istft + + +import numpy as np +from scipy.signal import istft + +# Input signal +x = np.random.randn(1024) + +# Parameters +nperseg = 256 +noverlap = 128 + +# Compute the STFT +f, t, X = istft(x, fs=1.0, window='hann', nperseg=nperseg, noverlap=noverlap, boundary=True) + +# Plot the signal +plt.plot(f, np.abs(X)) +plt.xlabel('Frequency [Hz]') +plt.ylabel('STFT Magnitude') +plt.show() +Generated code for scipy.signal.check_COLA + + +import scipy.signal as sig + +def check_COLA(x, fs): + """ + Check if a signal satisfies the Constant OverLap Add (COLA) + constraint. + + Parameters + ---------- + x : array_like + Input signal. + fs : float + Sampling frequency of the `x` signal. + + Returns + ------- + cola : bool + True if `x` satisfies the COLA constraint, False otherwise. + + """ + # Compute the frame length + N = len(x) + # Compute the hop size + H = int(N/2) + # Compute the number of frames + n_frames = int(N/H) + # Initialize the cola flag + cola = True + # Iterate over the frames + for i in range(n_frames): + # Compute the start and end indices of the frame + start = i*H + end = start + N + # Compute the frame + frame = x[start:end] + # Compute the frame energy + energy = sig.linalg.norm(frame) + # Check if the energy is constant + if energy != N: + cola = False + break + return cola +Generated code for scipy.signal.check_NOLA + + +import scipy.signal as signal + +def check_NOLA(x, fs): + """ + Checks if a signal is non-overlapping and linear-amplitude (NOLA). + + Parameters + ---------- + x : array_like + Input signal. + fs : float + Sampling frequency of the `x` signal. + + Returns + ------- + bool + True if the signal is NOLA, False otherwise. + + """ + # Compute the autocorrelation of the signal + rxx = signal.correlate(x, x, mode='full') + + # Compute the maximum value of the autocorrelation + max_rxx = max(rxx) + + # Compute the maximum value of the signal + max_x = max(abs(x)) + + # Compute the minimum value of the signal + min_x = min(abs(x)) + + # Compute the ratio between the maximum and minimum values + ratio = max_x / min_x + + # Check if the signal is NOLA + if (max_rxx < (fs * max_x)) and (ratio < 1.1): + return True + else: + return False +Generated code for scipy.signal.czt + + +import numpy as np +from scipy.signal import czt + +def czt_func(x, M, w): + """ + Computes the chirp z-transform of a signal x using the + chirp z-transform algorithm. + + Parameters + ---------- + x : array_like + Input signal. + M : int + Length of the output. + w : array_like + Chirp rate. + + Returns + ------- + out : ndarray + Chirp z-transform of x. + """ + N = len(x) + out = np.zeros(M, dtype=np.complex) + for k in range(M): + for n in range(N): + out[k] += x[n] * np.exp(-1j * w[k] * n) + return out + +# Example +x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) +M = 8 +w = np.linspace(0, np.pi, M) + +# Compare with scipy.signal.czt +out1 = czt_func(x, M, w) +out2 = czt(x, M, w) + +print(np.allclose(out1, out2)) +# True +Generated code for scipy.signal.zoom_fft + + +import numpy as np +from scipy.signal import get_window + +def zoom_fft(x, M, N, window=None): + """ + Zoom the FFT of a signal. + + Parameters + ---------- + x : array_like + Input signal. + M : int + Length of the FFT. + N : int + Length of the output signal. + window : array_like, optional + Window to apply to the signal. + If None, no window is applied. + + Returns + ------- + y : ndarray + The zoomed FFT of the input signal. + + """ + # Get window + if window is None: + window = np.ones(M) + else: + window = get_window(window, M) + + # Compute FFT + X = np.fft.fft(x * window, M) + + # Zoom FFT + Y = np.zeros(N, dtype=np.complex) + Y[:M//2] = X[:M//2] + Y[N-M//2:] = X[M//2:] + + # Compute inverse FFT + y = np.fft.ifft(Y).real + + return y +Generated code for scipy.signal.CZT + + +import numpy as np +from scipy.signal import czt + +def CZT(x, M, w): + """ + Computes the Chirp Z-Transform of a signal x. + + Parameters + ---------- + x : array_like + Input signal. + M : int + Length of the output. + w : array_like + Chirp rate. + + Returns + ------- + X : ndarray + Chirp Z-Transform of x. + """ + N = len(x) + X = np.zeros(M, dtype=np.complex) + for m in range(M): + for n in range(N): + X[m] += x[n] * np.exp(-1j * w[m] * n) + return X +Generated code for scipy.signal.ZoomFFT + + +import numpy as np +from scipy import signal + +def ZoomFFT(x, fs, zoom_factor): + """ + Computes the zoomed FFT of a signal. + + Parameters + ---------- + x : array_like + Input signal. + fs : float + Sampling frequency of the x time series. + zoom_factor : float + Zoom factor. + + Returns + ------- + f : array_like + Array of sample frequencies. + X : array_like + Zoomed FFT of x. + """ + # Compute the FFT of the signal + X = np.fft.fft(x) + # Compute the corresponding frequencies + f = np.fft.fftfreq(len(x), 1/fs) + # Zoom the FFT + X_zoomed = signal.resample(X, int(len(X)*zoom_factor)) + # Compute the corresponding frequencies + f_zoomed = np.fft.fftfreq(len(X_zoomed), 1/fs) + return f_zoomed, X_zoomed +Generated code for scipy.signal.czt_points + + +import numpy as np +from scipy.signal import czt_points + +# Define the parameters +N = 10 # Number of points +a = 0.5 # Lower bound +b = 1.5 # Upper bound + +# Generate the points +points = czt_points(N, a, b) + +# Print the points +print(points) +Generated code for numpy.ma.MaskType + + +import numpy as np + +class MaskType: + """ + A class for representing a type of mask in a numpy masked array. + + Attributes + ---------- + name : str + The name of the mask type. + value : int + The numerical value of the mask type. + description : str + A description of the mask type. + """ + + def __init__(self, name, value, description): + """ + Initialize a MaskType object. + + Parameters + ---------- + name : str + The name of the mask type. + value : int + The numerical value of the mask type. + description : str + A description of the mask type. + """ + self.name = name + self.value = value + self.description = description + + def __repr__(self): + """ + Return a string representation of the MaskType object. + """ + return f"MaskType(name={self.name}, value={self.value}, description={self.description})" + + def __eq__(self, other): + """ + Check if two MaskType objects are equal. + """ + if isinstance(other, MaskType): + return (self.name == other.name and + self.value == other.value and + self.description == other.description) + else: + return False + + def __ne__(self, other): + """ + Check if two MaskType objects are not equal. + """ + return not self.__eq__(other) + + def to_mask(self): + """ + Convert the MaskType object to a numpy mask. + """ + return np.ma.masked_equal(np.arange(self.value), self.value) +Generated code for numpy.bool_ + + +import numpy as np + +def bool_func(x): + return np.array(x, dtype=bool) + +x = [True, False, 0, 1] + +bool_func(x) +Generated code for numpy.ma.masked_array + + +import numpy as np + +# Create a masked array +x = np.ma.masked_array(np.arange(10), fill_value=-999) + +# Mask the first three elements +x[:3] = np.ma.masked + +# Print the masked array +print(x) + +# Output: +# masked_array(data=[--, --, --, 3, 4, 5, 6, 7, 8, 9], +# mask=[ True, True, True, False, False, False, False, +# False, False, False], +# fill_value=-999) +Generated code for numpy.ma.array + + +import numpy as np + +# Create a masked array +x = np.ma.array([1,2,3,4], mask=[0,1,0,1]) + +# Print the masked array +print(x) + +# Access the elements of the array +print(x[0]) +print(x[1]) +print(x[2]) +print(x[3]) + +# Access the mask of the array +print(x.mask) + +# Change the mask of the array +x.mask = [1,0,1,0] + +# Print the new mask +print(x.mask) +Generated code for numpy.ma.copy + + +import numpy as np + +def copy(a): + """ + Return a copy of an array. + + Parameters + ---------- + a : array_like + Input data, in any form that can be converted to an array. This + includes lists, lists of tuples, tuples, tuples of tuples, tuples + of lists and ndarrays. + + Returns + ------- + arr : ndarray + A copy of `a`. + + """ + arr = np.ma.array(a, copy=True) + return arr +Generated code for numpy.ma.frombuffer + + +import numpy as np + +def frombuffer(buffer, dtype=float, count=-1, offset=0): + """ + Construct an array from a buffer object. + + Parameters + ---------- + buffer : buffer_like + An object that exposes the buffer interface. + dtype : data-type, optional + Data type of the returned array. + count : int, optional + Number of items to read. -1 means all data in the buffer. + offset : int, optional + Start position to read from. + + Returns + ------- + arr : ndarray + The constructed array. + """ + arr = np.frombuffer(buffer, dtype, count, offset) + return np.ma.masked_array(arr) +Generated code for numpy.ma.fromfunction + + +import numpy as np + +def my_function(x, y): + return x*y + +x, y = np.meshgrid(np.arange(0,2), np.arange(0,3)) + +result = np.ma.fromfunction(my_function, (x, y)) + +print(result) +Generated code for numpy.ma.MaskedArray.copy + + +import numpy as np + +def copy(arr): + """ + Return a copy of a MaskedArray. + + Parameters + ---------- + arr : MaskedArray + The array to be copied. + + Returns + ------- + out : MaskedArray + A copy of `arr`. + + """ + out = np.ma.MaskedArray(arr.data.copy(), mask=arr.mask.copy()) + return out +Generated code for numpy.ma.empty + + +import numpy as np + +# Create an empty masked array +empty_ma = np.ma.empty(shape=(3,3)) + +# Mask all elements +empty_ma.mask = True + +# Print the masked array +print(empty_ma) +Generated code for numpy.ma.empty_like + + +import numpy as np + +# Create an empty array with the same shape and type as the given array +def empty_like(arr): + return np.ma.empty_like(arr) + +# Example +arr = np.array([[1,2,3], [4,5,6]]) +empty_arr = empty_like(arr) +print(empty_arr) + +# Output +# [[-- -- --] +# [-- -- --]] +Generated code for numpy.ma.masked_all + + +import numpy as np + +masked_array = np.ma.masked_all((3,3)) + +print(masked_array) + +# Output +[[-- -- --] + [-- -- --] + [-- -- --]] +Generated code for numpy.ma.masked_all_like + + +import numpy as np + +masked_all_like = np.ma.masked_all_like(np.zeros((3,3))) +print(masked_all_like) +Generated code for numpy.ma.ones + + +import numpy as np + +# Create a 2x3 array of ones +arr = np.ma.ones((2,3)) + +# Print the array +print(arr) +Generated code for numpy.ma.ones_like + + +import numpy as np + +# Create a numpy array +arr = np.array([[1, 2, 3], [4, 5, 6]]) + +# Create a masked array with the same shape as arr +masked_arr = np.ma.ones_like(arr) + +# Print the masked array +print(masked_arr) +Generated code for numpy.ma.zeros + + +import numpy as np + +# Create a masked array of zeros +masked_zeros = np.ma.zeros(shape=(3,3)) + +# Print the masked array +print(masked_zeros) +Generated code for numpy.ma.zeros_like + + +import numpy as np + +# Create a numpy array +arr = np.array([[1, 2, 3], [4, 5, 6]]) + +# Create a masked array with zeros +masked_arr = np.ma.zeros_like(arr) + +# Print the masked array +print(masked_arr) +Generated code for numpy.ma.all +close + +import numpy as np + +def allclose(a, b, rtol=1e-5, atol=1e-8): + """ + Returns True if two arrays are element-wise equal within a tolerance. + + The tolerance values are positive, typically very small numbers. The + relative difference (`rtol` * abs(`b`)) and the absolute difference + `atol` are added together to compare against the absolute difference + between `a` and `b`. + + Parameters + ---------- + a, b : array_like + Input arrays to compare. + rtol : float + The relative tolerance parameter (see Notes). + atol : float + The absolute tolerance parameter (see Notes). + + Returns + ------- + allclose : bool + Returns True if the two arrays are equal within the given + tolerance; False otherwise. + + See Also + -------- + all, any, where + + Notes + ----- + If the following equation is element-wise True, then allclose returns + True. + + absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`)) + + The above equation is not symmetric in `a` and `b`, so that + `allclose(a, b)` might be different from `allclose(b, a)` in + some rare cases. + + Examples + -------- + >>> np.allclose([1e10,1e-7], [1.00001e10,1e-8]) + False + >>> np.allclose([1e10,1e-8], [1.00001e10,1e-9]) + True + >>> np.allclose([1e10,1e-8], [1.0001e10,1e-9]) + False + >>> np.allclose([1.0, np.nan], [1.0, np.nan]) + True + + """ + a = np.asanyarray(a) + b = np.asanyarray(b) + return np.all(np.less_equal(np.abs(a - b +Generated code for numpy.ma.any + + +import numpy as np + +def numpy_ma_any(arr): + """ + Returns True if any of the elements of the array are non-zero, False otherwise. + """ + return np.ma.any(arr) +Generated code for numpy.ma.count + + +import numpy as np + +def count(arr): + # Create a mask of valid values + mask = np.ma.masked_invalid(arr).mask + # Count the number of valid values + return np.sum(~mask) + +# Example +arr = np.array([1, 2, np.nan, 3, 4]) +print(count(arr)) # Output: 4 +Generated code for numpy.ma.count_masked + + +import numpy as np + +def count_masked(arr): + """ + Counts the number of masked elements in a numpy.ma array. + Parameters + ---------- + arr : numpy.ma array + The array to count the masked elements in. + Returns + ------- + int + The number of masked elements in the array. + """ + return np.sum(arr.mask) +Generated code for numpy.ma.getmask +array + +import numpy as np + +def getmaskarray(mask): + """ + Return a boolean array from a mask. + + Parameters + ---------- + mask : array_like + Mask array. + + Returns + ------- + result : ndarray + Boolean array with the same shape as `mask`. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> np.ma.getmaskarray(x) + array([False, True, False]) + + """ + return np.array(mask, dtype=bool) +Generated code for numpy.ma.getmaskarray + + +import numpy as np + +def getmaskarray(mask): + """ + Return a boolean array from a mask. + + Parameters + ---------- + mask : array_like + Mask array. + + Returns + ------- + result : ndarray + Boolean array with the same shape as `mask`. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> np.ma.getmaskarray(x) + array([False, True, False]) + + """ + return np.array(mask, dtype=bool) +Generated code for numpy.ma.getdata + + +import numpy as np + +def getdata(arr): + return np.ma.getdata(arr) + +#Example +arr = np.ma.array([1,2,3,4], mask=[0,1,0,1]) +print(getdata(arr)) +# Output: [1 3] +Generated code for numpy.ma.nonzero + + +import numpy as np + +def nonzero(arr): + # Create a boolean array + bool_arr = np.ma.masked_equal(arr, 0).mask + # Get indices of non-zero elements + non_zero_indices = np.nonzero(bool_arr) + # Return the indices + return non_zero_indices +Generated code for numpy.ma.shape + + +import numpy as np + +arr = np.ma.array([[1,2,3], [4,5,6], [7,8,9]]) + +shape = np.ma.shape(arr) + +print(shape) +Generated code for numpy.ma.size + + +import numpy as np + +# Create a masked array +x = np.ma.array([[1,2,3], [4,5,6], [7,8,9]], mask=[[0,1,0], [1,0,1], [0,1,0]]) + +# Get the size of the masked array +np.ma.size(x) +Generated code for numpy.ma.is_masked + + +import numpy as np + +def is_masked(a): + """ + Checks if an array is masked. + + Parameters + ---------- + a : array_like + Input array. + + Returns + ------- + result : bool + True if `a` is a masked array, False otherwise. + + """ + return isinstance(a, np.ma.MaskedArray) +Generated code for numpy.ma.is_mask + + +import numpy as np + +def is_mask(a): + """ + Returns True if a is a valid mask array, False otherwise. + + Parameters + ---------- + a : array_like + Input array. + + Returns + ------- + out : bool + True if `a` is a valid mask array, False otherwise. + + See Also + -------- + numpy.ma.make_mask + """ + a = np.asarray(a) + if a.dtype != np.bool_: + return False + if a.ndim == 0: + return False + if a.ndim > 1 and a.shape[0] != a.shape[1]: + return False + return True +Generated code for numpy.ma.isMaskedArray + + +import numpy as np + +def isMaskedArray(arr): + """ + Checks if the given array is a masked array. + + Parameters + ---------- + arr : array_like + The array to check. + + Returns + ------- + bool + True if the array is a masked array, False otherwise. + """ + return isinstance(arr, np.ma.MaskedArray) +Generated code for numpy.ma.isMA + + +import numpy as np + +def isMA(x): + """ + Checks if an array is a MaskedArray. + + Parameters + ---------- + x : array_like + Input array. + + Returns + ------- + out : bool + True if `x` is a MaskedArray, False otherwise. + + """ + return isinstance(x, np.ma.MaskedArray) +Generated code for numpy.ma.isarray + + +def isarray(x): + """ + Checks if x is a numpy array. + + Parameters + ---------- + x : object + The object to check. + + Returns + ------- + bool + True if x is a numpy array, False otherwise. + """ + return isinstance(x, np.ndarray) or isinstance(x, np.ma.MaskedArray) +Generated code for numpy.ma.MaskedArray.all + + +import numpy as np + +def all(arr): + """ + Returns True if all elements of the array are non-zero, False otherwise. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be tested. + + Returns + ------- + bool + True if all elements of the array are non-zero, False otherwise. + """ + # Check if array is empty + if arr.size == 0: + return False + + # Check if all elements are non-zero + for element in np.ma.MaskedArray.compressed(arr): + if element == 0: + return False + + return True +Generated code for numpy.ma.MaskedArray.any + + +import numpy as np + +def any(a, axis=None): + """ + Test whether any array element along a given axis evaluates to True. + + Parameters + ---------- + a : array_like + Input array or object that can be converted to an array. + axis : int, optional + Axis along which a logical OR reduction is performed. + The default (axis = None) is to perform a logical OR over all + the dimensions of the input array. + + Returns + ------- + any : bool or ndarray + A new boolean or `numpy.ndarray` is returned. + + See Also + -------- + numpy.ma.MaskedArray.all : Test whether all array elements along a given + axis evaluate to True. + numpy.ma.MaskedArray.any : Test whether any array element along a given + axis evaluates to True. + + Examples + -------- + >>> x = np.ma.array([[1, 0], [0, 0]], mask=[[0, 1], [1, 1]]) + >>> np.ma.any(x) + True + >>> np.ma.any(x, axis=0) + array([ True, False], dtype=bool) + >>> np.ma.any(x, axis=1) + array([ True, False], dtype=bool) + + """ + a = np.ma.asarray(a) + if axis is None: + axis = 0 + return np.ma.MaskedArray.any(a, axis=axis) +Generated code for numpy.ma.MaskedArray.count + + +import numpy as np + +def count(arr): + """ + Count the number of non-masked elements in a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to count elements in. + + Returns + ------- + int + The number of non-masked elements in the array. + """ + return np.sum(~arr.mask) +Generated code for numpy.ma.MaskedArray.nonzero + + +import numpy as np + +def nonzero(arr): + """ + Return the indices of the elements that are non-zero. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + Input array. + + Returns + ------- + tuple + Indices of the elements that are non-zero. + """ + # Get the indices of the non-masked elements + non_masked_indices = np.where(~arr.mask) + + # Get the values of the non-masked elements + non_masked_values = arr[non_masked_indices] + + # Get the indices of the non-zero elements + non_zero_indices = np.where(non_masked_values != 0) + + # Return the indices of the non-zero elements + return non_masked_indices[non_zero_indices] +Generated code for numpy.ma.shape + + +import numpy as np + +arr = np.ma.array([[1,2,3], [4,5,6], [7,8,9]]) + +shape = np.ma.shape(arr) + +print(shape) +Generated code for numpy.ma.size + + +import numpy as np + +# Create a masked array +x = np.ma.array([[1,2,3], [4,5,6], [7,8,9]], mask=[[0,1,0], [1,0,1], [0,1,0]]) + +# Get the size of the masked array +np.ma.size(x) +Generated code for numpy.ma.MaskedArray.data + + +import numpy as np + +# Create a masked array +x = np.ma.MaskedArray(data=[1,2,3,4,5], mask=[False, False, True, False, True]) + +# Access the data +data = x.data + +# Print the data +print(data) + +# Output: +# [1 2 -- 4 --] +Generated code for numpy.ma.MaskedArray.mask + + +import numpy as np + +def mask(arr, mask): + """ + Mask an array with a given mask. + + Parameters + ---------- + arr : array_like + The array to be masked. + mask : array_like + The mask to be applied. + + Returns + ------- + masked_array : numpy.ma.MaskedArray + The masked array. + """ + return np.ma.MaskedArray(arr, mask=mask) +Generated code for numpy.ma.MaskedArray.recordmask + + +def recordmask(arr, axis=None): + """ + Return a boolean array of the same shape as `arr` that is True + wherever the mask of `arr` is True. + + Parameters + ---------- + arr : array_like + Input array. + axis : int, optional + Axis along which to perform the operation. By default, the + flattened input is used. + + Returns + ------- + recordmask : ndarray + Boolean array of the same shape as `arr` that is True wherever + the mask of `arr` is True. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> np.ma.recordmask(x) + array([False, True, False]) + + """ + m = np.ma.getmaskarray(arr) + if axis is None: + return m.ravel() + else: + return m.take(axis, axis=axis) +Generated code for numpy.ma.ravel + + +import numpy as np + +def ravel(arr): + # Create a new array to store the flattened array + new_arr = np.empty(arr.size, dtype=arr.dtype) + + # Iterate through the array and flatten it + for i in range(arr.shape[0]): + for j in range(arr.shape[1]): + new_arr[i*arr.shape[1] + j] = arr[i, j] + + return new_arr + +# Test +arr = np.ma.array([[1, 2, 3], [4, 5, 6]]) +print(ravel(arr)) # [1. 2. 3. 4. 5. 6.] +Generated code for numpy.ma.reshape + + +import numpy as np + +# reshape a numpy array +arr = np.arange(12).reshape(3,4) + +# use numpy.ma.reshape +reshaped_arr = np.ma.reshape(arr, (2,6)) + +# print the reshaped array +print(reshaped_arr) +Generated code for numpy.ma.resize + + +import numpy as np + +def resize(a, new_shape): + """ + Resize an array to a new shape using numpy.ma.resize. + + Parameters + ---------- + a : array_like + Input array. + new_shape : tuple + Tuple of integers giving the new shape. + + Returns + ------- + resized_array : ndarray + Resized array with the same dtype as the input array. + + Examples + -------- + >>> a = np.array([[1, 2, 3], [4, 5, 6]]) + >>> np.ma.resize(a, (3, 2)) + array([[1, 2], + [3, 4], + [5, 6]]) + """ + return np.ma.resize(a, new_shape) +Generated code for numpy.ma.MaskedArray.flatten + + +import numpy as np + +def flatten(arr): + """ + Flatten a numpy.ma.MaskedArray into a 1D array. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be flattened. + + Returns + ------- + out : numpy.ndarray + The flattened array. + """ + # Get the shape of the array + shape = arr.shape + # Calculate the total number of elements + n_elements = np.prod(shape) + # Create an empty array of the same type as the input array + out = np.empty(n_elements, dtype=arr.dtype) + # Iterate over the elements of the array + for i in range(n_elements): + # Get the indices of the element + indices = np.unravel_index(i, shape) + # Get the value of the element + value = arr[indices] + # Set the value in the output array + out[i] = value + # Return the flattened array + return out +Generated code for numpy.ma.MaskedArray.ravel + + +import numpy as np + +def ravel(arr): + """ + Flattens a MaskedArray into a 1-D array. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be flattened. + + Returns + ------- + numpy.ndarray + A 1-D array containing the flattened elements of the MaskedArray. + """ + # Create a new array to store the flattened elements + flat_arr = np.empty(arr.size, dtype=arr.dtype) + + # Iterate over the array and store the non-masked elements in the new array + idx = 0 + for element in np.nditer(arr): + if not element.mask: + flat_arr[idx] = element + idx += 1 + + # Return the flattened array + return flat_arr +Generated code for numpy.ma.MaskedArray.reshape + + +import numpy as np + +# Create a MaskedArray +arr = np.ma.MaskedArray(np.arange(12).reshape(3,4)) + +# Reshape the MaskedArray +arr_reshaped = np.ma.MaskedArray.reshape(arr, (2,6)) + +# Print the reshaped MaskedArray +print(arr_reshaped) +Generated code for numpy.ma.MaskedArray.resize + + +import numpy as np + +def resize(arr, new_shape, order=3): + """ + Resize a MaskedArray to a new shape. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be resized. + new_shape : tuple + The new shape of the array. + order : int, optional + The order of the spline interpolation, default is 3. + + Returns + ------- + numpy.ma.MaskedArray + The resized array. + """ + # Get the original shape + old_shape = arr.shape + # Calculate the ratio of the new and old shapes + ratio = np.divide(new_shape, old_shape) + # Resize the array using spline interpolation + resized_arr = np.ma.masked_array( + np.array( + [np.interp( + np.arange(0, old_shape[i], ratio[i]), + np.arange(0, old_shape[i]), + arr[:, i], + period=old_shape[i], + order=order + ) for i in range(arr.shape[1])] + ).T + ) + # Return the resized array + return resized_arr +Generated code for numpy.ma.swapaxes + + +import numpy as np + +def swapaxes(a, axis1, axis2): + """ + Swap two axes of an array. + + Parameters + ---------- + a : array_like + Input array. + axis1 : int + First axis. + axis2 : int + Second axis. + + Returns + ------- + out : ndarray + Array with `axis1` and `axis2` swapped. + + Examples + -------- + >>> a = np.array([[1,2,3]]) + >>> np.ma.swapaxes(a,0,1) + array([[1], + [2], + [3]]) + + """ + a = np.asanyarray(a) + if axis1 == axis2: + return a + if axis1 < 0: + axis1 += a.ndim + if axis2 < 0: + axis2 += a.ndim + if (axis1 >= a.ndim) or (axis2 >= a.ndim): + raise ValueError("axis1 and axis2 should be within range (-a.ndim, a.ndim)") + b = np.swapaxes(a, axis1, axis2) + return np.ma.array(b, mask=np.ma.getmaskarray(a)) +Generated code for numpy.ma.transpose + + +import numpy as np + +def transpose(arr): + return np.ma.transpose(arr) + +# Example +arr = np.array([[1,2,3], [4,5,6], [7,8,9]]) +print(transpose(arr)) + +# Output +# [[1 4 7] +# [2 5 8] +# [3 6 9]] +Generated code for numpy.ma.MaskedArray.swapaxes + + +import numpy as np + +def swapaxes(arr, axis1, axis2): + """ + Swap two axes of a MaskedArray. + + Parameters + ---------- + arr : MaskedArray + Input array. + axis1 : int + First axis. + axis2 : int + Second axis. + + Returns + ------- + MaskedArray + Array with two axes swapped. + + """ + arr = np.ma.asarray(arr) + arr = np.swapaxes(arr, axis1, axis2) + return np.ma.MaskedArray(arr, mask=arr.mask) +Generated code for numpy.ma.MaskedArray.transpose + + +import numpy as np + +def transpose(arr): + """ + Transpose a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be transposed. + + Returns + ------- + numpy.ma.MaskedArray + The transposed array. + """ + # Get the shape of the array + shape = arr.shape + # Create a new array with the transposed shape + new_arr = np.ma.masked_array(np.empty(shape[::-1]), mask=arr.mask) + # Iterate over the elements of the array + for i in range(shape[0]): + for j in range(shape[1]): + # Set the transposed element + new_arr[j, i] = arr[i, j] + # Return the transposed array + return new_arr +Generated code for numpy.ma.atleast_1d + + +import numpy as np + +def atleast_1d(a): + a = np.asarray(a) + if a.ndim == 0: + a = a[np.newaxis] + return a + +# Usage +x = np.ma.atleast_1d(3) +print(x) +# Output: array([3]) +Generated code for numpy.ma.atleast_2d + + +import numpy as np + +def atleast_2d(a): + if np.ndim(a) == 0: + result = a[np.newaxis, np.newaxis] + elif np.ndim(a) == 1: + result = a[:, np.newaxis] + else: + result = a + return result + +# Usage example +x = np.array([1, 2, 3]) +y = np.ma.atleast_2d(x) +print(y) +# Output: +# [[1] +# [2] +# [3]] +Generated code for numpy.ma.atleast_3d + + +import numpy as np + +def atleast_3d(arr): + arr = np.asarray(arr) + if arr.ndim < 3: + return np.atleast_3d(arr) + else: + return arr + +# Usage +arr = np.array([[1,2,3],[4,5,6]]) +arr_3d = atleast_3d(arr) +print(arr_3d.shape) # (2, 3, 1) +Generated code for numpy.ma.expand_dims + + +import numpy as np + +def expand_dims(a, axis): + """ + Expand the shape of an array. + Insert a new axis that will appear at the `axis` position in the expanded array shape. + + Parameters + ---------- + a : array_like + Input array. + axis : int + Position in the expanded axes where the new axis is placed. + + Returns + ------- + res : ndarray + Output array. The number of dimensions is one greater than that of + the input array. + """ + return np.ma.expand_dims(a, axis) +Generated code for numpy.ma.squeeze + + +import numpy as np + +def squeeze(a): + """ + Remove single-dimensional entries from the shape of an array. + + Parameters + ---------- + a : array_like + Input data. + + Returns + ------- + squeezed : ndarray + The input array, but with all or a subset of the + dimensions of length 1 removed. + + Examples + -------- + >>> x = np.array([[[0], [1], [2]]]) + >>> x.shape + (1, 3, 1) + >>> np.ma.squeeze(x).shape + (3,) + + """ + a = np.asanyarray(a) + if a.ndim == 0: + return a + squeezed = np.squeeze(a) + if a.shape == squeezed.shape: + return a + return squeezed +Generated code for numpy.ma.MaskedArray.squeeze + + +import numpy as np + +def squeeze(arr): + """ + Squeeze a MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be squeezed. + + Returns + ------- + numpy.ma.MaskedArray + The squeezed array. + """ + # Get the shape of the array + shape = arr.shape + + # Create a list of axes to squeeze + axes = [] + for i in range(len(shape)): + if shape[i] == 1: + axes.append(i) + + # Squeeze the array + return np.ma.masked_array(arr.data.squeeze(axis=tuple(axes)), + mask=arr.mask.squeeze(axis=tuple(axes))) +Generated code for numpy.ma.stack + + +import numpy as np + +def stack(arrays, axis=0): + """ + Stack the given arrays along the given axis. + + Parameters + ---------- + arrays : sequence of ndarrays + The arrays must have the same shape along all but the specified axis. + axis : int, optional + The axis along which the arrays will be stacked. Default is 0. + + Returns + ------- + stacked : ndarray + The stacked array has one more dimension than the input arrays. + + Examples + -------- + >>> a = np.array([1, 2, 3]) + >>> b = np.array([2, 3, 4]) + >>> np.ma.stack([a, b], axis=0) + array([[1, 2, 3], + [2, 3, 4]]) + """ + stacked = np.ma.array(np.stack(arrays, axis=axis)) + return stacked +Generated code for numpy.ma.column_stack + + +import numpy as np + +def column_stack(arrays): + """ + Stack 1-D arrays as columns into a 2-D array. + + Parameters + ---------- + arrays : sequence of 1-D arrays + All arrays must have the same first dimension. + + Returns + ------- + stacked : 2-D array + The array contains columns that are constructed from the input + arrays. + """ + arrays = [np.asarray(arr) for arr in arrays] + if not all(arr.ndim == 1 for arr in arrays): + raise ValueError('all input arrays must be 1-D') + dtype = np.find_common_type([arr.dtype for arr in arrays], []) + stacked = np.empty((arrays[0].shape[0], len(arrays)), dtype=dtype) + for i, arr in enumerate(arrays): + stacked[:, i] = arr + return stacked +Generated code for numpy.ma.concatenate + + +import numpy as np + +def concatenate(arrays, axis=0): + """ + Concatenate a sequence of arrays along an existing axis. + + Parameters + ---------- + arrays : sequence of array_like + The arrays must have the same shape, except in the dimension + corresponding to `axis` (the first, by default). + axis : int, optional + The axis along which the arrays will be joined. Default is 0. + + Returns + ------- + res : ndarray + The concatenated array. + """ + res = np.ma.concatenate(arrays, axis=axis) + return res +Generated code for numpy.ma.dstack + + +import numpy as np + +def dstack(arrays): + """ + Stack arrays in sequence depth wise (along third axis). + Parameters + ---------- + arrays : sequence of array_like + Each array must have the same shape. + Returns + ------- + stacked : ndarray + The array formed by stacking the given arrays, will be at least 3-D. + """ + arrays = [np.asanyarray(arr) for arr in arrays] + if not arrays: + raise ValueError('need at least one array to stack') + + shapes = set(arr.shape for arr in arrays) + if len(shapes) != 1: + raise ValueError('all input arrays must have the same shape') + + result_ndim = max(arr.ndim for arr in arrays) + result_shape = arrays[0].shape + + if result_ndim == 0: + # special case for 0-d arrays + return np.array(arrays, dtype=arrays[0].dtype) + + # we need to pad the shape of the arrays with ones + # so that the result is at least 2-d + if result_ndim == 1: + result_shape = (1,) + result_shape + + # create an array of python lists of length equal to the number of arrays + result_list = [[] for _ in arrays] + + # fill the lists with the arrays + for arr in arrays: + # we need to pad the shape of the array with ones + # so that the result is at least 2-d + arr_shape = arr.shape + if arr.ndim == 1: + arr_shape = (1,) + arr_shape + + # fill the list with the flattened array + result_list[0].extend(arr.flatten()) + + # stack the lists into an array + stacked = np.ma.dstack(result_list) + + # reshape the array to the desired shape + return stacked.reshape(result_shape) +Generated code for numpy.ma.hstack + + +import numpy as np + +def hstack(arrays): + """ + Stack arrays in sequence horizontally (column wise). + Parameters + ---------- + arrays : sequence of ndarrays + Input arrays. + Returns + ------- + stacked : ndarray + The array formed by stacking the given arrays. + """ + arrays = [np.asanyarray(arr) for arr in arrays] + shapes = [arr.shape for arr in arrays] + result_ndim = np.max([arr.ndim for arr in arrays]) + result_shape = np.zeros(result_ndim, int) + for shape in shapes: + result_shape[:len(shape)] += shape + result = np.empty(result_shape, dtype=np.ma.result_type(*arrays)) + slices = [slice(None)] * result_ndim + for arr in arrays: + slices[-arr.ndim:] = arr.shape + result[tuple(slices)] = arr + slices[-arr.ndim:] = 0 + return result +Generated code for numpy.ma.hsplit + + +import numpy as np + +# Create a 2D array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Split the array into 3 parts horizontally +split_arr = np.ma.hsplit(arr, 3) + +# Print the split array +print(split_arr) + +# Output +[array([[1], + [4], + [7]]), array([[2], + [5], + [8]]), array([[3], + [6], + [9]])] +Generated code for numpy.ma.mr_ + + +import numpy as np + +def mr_ (arr, axis=None): + """ + Computes the mean of the absolute values of the elements of the array along the given axis. + + Parameters + ---------- + arr : array_like + Input array. + axis : int, optional + Axis along which the mean is computed. The default is to compute the mean of the flattened array. + + Returns + ------- + mr : ndarray + Mean of the absolute values of the array elements. + """ + return np.mean(np.abs(arr), axis=axis) +Generated code for numpy.ma.row_stack + + +import numpy as np + +def row_stack(arrays): + """ + Stack 1-D arrays as rows into a 2-D array. + + Parameters + ---------- + arrays : sequence of 1-D arrays + Each array must have the same shape. + + Returns + ------- + stacked : 2-D array + The array formed by stacking the given arrays. + + Examples + -------- + >>> a = np.array([1, 2, 3]) + >>> b = np.array([2, 3, 4]) + >>> np.ma.row_stack((a, b)) + array([[1, 2, 3], + [2, 3, 4]]) + """ + return np.ma.array(np.row_stack(arrays)) +Generated code for numpy.ma.vstack + + +import numpy as np + +def vstack(arrays): + """ + Stack arrays in sequence vertically (row wise). + This is equivalent to `np.concatenate(arrays, axis=0)`. + + Parameters + ---------- + arrays : sequence of ndarrays + The arrays must have the same shape along all but the first axis. + 1-D arrays must have the same length. + + Returns + ------- + stacked : ndarray + The array formed by stacking the given arrays. + """ + arrays = [np.asanyarray(arr) for arr in arrays] + shapes = [arr.shape for arr in arrays] + result_ndim = arrays[0].ndim + if not all(shape == shapes[0] for shape in shapes): + raise ValueError('all input arrays must have the same shape') + if result_ndim == 0: + # special case for 0-dim arrays + if not all(arr.ndim == 0 for arr in arrays): + raise ValueError('all input scalar arrays must have the same shape') + result_shape = (len(arrays),) + else: + result_shape = (len(arrays),) + shapes[0][1:] + result = np.empty(result_shape, dtype=arrays[0].dtype) + for i, arr in enumerate(arrays): + result[i] = arr + return result +Generated code for numpy.ma.concatenate + + +import numpy as np + +def concatenate(arrays, axis=0): + """ + Concatenate a sequence of arrays along an existing axis. + + Parameters + ---------- + arrays : sequence of array_like + The arrays must have the same shape, except in the dimension + corresponding to `axis` (the first, by default). + axis : int, optional + The axis along which the arrays will be joined. Default is 0. + + Returns + ------- + res : ndarray + The concatenated array. + """ + res = np.ma.concatenate(arrays, axis=axis) + return res +Generated code for numpy.ma.stack + + +import numpy as np + +def stack(arrays, axis=0): + """ + Stack the given arrays along the given axis. + + Parameters + ---------- + arrays : sequence of ndarrays + The arrays must have the same shape along all but the specified axis. + axis : int, optional + The axis along which the arrays will be stacked. Default is 0. + + Returns + ------- + stacked : ndarray + The stacked array has one more dimension than the input arrays. + + Examples + -------- + >>> a = np.array([1, 2, 3]) + >>> b = np.array([2, 3, 4]) + >>> np.ma.stack([a, b], axis=0) + array([[1, 2, 3], + [2, 3, 4]]) + """ + stacked = np.ma.array(np.stack(arrays, axis=axis)) + return stacked +Generated code for numpy.ma.vstack + + +import numpy as np + +def vstack(arrays): + """ + Stack arrays in sequence vertically (row wise). + This is equivalent to `np.concatenate(arrays, axis=0)`. + + Parameters + ---------- + arrays : sequence of ndarrays + The arrays must have the same shape along all but the first axis. + 1-D arrays must have the same length. + + Returns + ------- + stacked : ndarray + The array formed by stacking the given arrays. + """ + arrays = [np.asanyarray(arr) for arr in arrays] + shapes = [arr.shape for arr in arrays] + result_ndim = arrays[0].ndim + if not all(shape == shapes[0] for shape in shapes): + raise ValueError('all input arrays must have the same shape') + if result_ndim == 0: + # special case for 0-dim arrays + if not all(arr.ndim == 0 for arr in arrays): + raise ValueError('all input scalar arrays must have the same shape') + result_shape = (len(arrays),) + else: + result_shape = (len(arrays),) + shapes[0][1:] + result = np.empty(result_shape, dtype=arrays[0].dtype) + for i, arr in enumerate(arrays): + result[i] = arr + return result +Generated code for numpy.ma.hstack + + +import numpy as np + +def hstack(arrays): + """ + Stack arrays in sequence horizontally (column wise). + Parameters + ---------- + arrays : sequence of ndarrays + Input arrays. + Returns + ------- + stacked : ndarray + The array formed by stacking the given arrays. + """ + arrays = [np.asanyarray(arr) for arr in arrays] + shapes = [arr.shape for arr in arrays] + result_ndim = np.max([arr.ndim for arr in arrays]) + result_shape = np.zeros(result_ndim, int) + for shape in shapes: + result_shape[:len(shape)] += shape + result = np.empty(result_shape, dtype=np.ma.result_type(*arrays)) + slices = [slice(None)] * result_ndim + for arr in arrays: + slices[-arr.ndim:] = arr.shape + result[tuple(slices)] = arr + slices[-arr.ndim:] = 0 + return result +Generated code for numpy.ma.dstack + + +import numpy as np + +def dstack(arrays): + """ + Stack arrays in sequence depth wise (along third axis). + Parameters + ---------- + arrays : sequence of array_like + Each array must have the same shape. + Returns + ------- + stacked : ndarray + The array formed by stacking the given arrays, will be at least 3-D. + """ + arrays = [np.asanyarray(arr) for arr in arrays] + if not arrays: + raise ValueError('need at least one array to stack') + + shapes = set(arr.shape for arr in arrays) + if len(shapes) != 1: + raise ValueError('all input arrays must have the same shape') + + result_ndim = max(arr.ndim for arr in arrays) + result_shape = arrays[0].shape + + if result_ndim == 0: + # special case for 0-d arrays + return np.array(arrays, dtype=arrays[0].dtype) + + # we need to pad the shape of the arrays with ones + # so that the result is at least 2-d + if result_ndim == 1: + result_shape = (1,) + result_shape + + # create an array of python lists of length equal to the number of arrays + result_list = [[] for _ in arrays] + + # fill the lists with the arrays + for arr in arrays: + # we need to pad the shape of the array with ones + # so that the result is at least 2-d + arr_shape = arr.shape + if arr.ndim == 1: + arr_shape = (1,) + arr_shape + + # fill the list with the flattened array + result_list[0].extend(arr.flatten()) + + # stack the lists into an array + stacked = np.ma.dstack(result_list) + + # reshape the array to the desired shape + return stacked.reshape(result_shape) +Generated code for numpy.ma.column_stack + + +import numpy as np + +def column_stack(arrays): + """ + Stack 1-D arrays as columns into a 2-D array. + + Parameters + ---------- + arrays : sequence of 1-D arrays + All arrays must have the same first dimension. + + Returns + ------- + stacked : 2-D array + The array contains columns that are constructed from the input + arrays. + """ + arrays = [np.asarray(arr) for arr in arrays] + if not all(arr.ndim == 1 for arr in arrays): + raise ValueError('all input arrays must be 1-D') + dtype = np.find_common_type([arr.dtype for arr in arrays], []) + stacked = np.empty((arrays[0].shape[0], len(arrays)), dtype=dtype) + for i, arr in enumerate(arrays): + stacked[:, i] = arr + return stacked +Generated code for numpy.ma.append + + +import numpy as np + +def append(arr1, arr2, axis=None): + """ + Append two numpy.ma arrays along the given axis. + + Parameters + ---------- + arr1 : numpy.ma array + The first array to append. + arr2 : numpy.ma array + The second array to append. + axis : int, optional + The axis along which to append the arrays. If not provided, the arrays + will be flattened and appended along the first axis. + + Returns + ------- + numpy.ma array + The appended array. + """ + if axis is None: + # Flatten the arrays and append along the first axis + arr1 = arr1.flatten() + arr2 = arr2.flatten() + return np.ma.append(arr1, arr2, axis=0) + else: + # Append along the given axis + return np.ma.append(arr1, arr2, axis=axis) +Generated code for numpy.ma.make_mask + + +import numpy as np + +def make_mask(data, fill_value=None): + """ + Create a mask for the given data array. + + Parameters + ---------- + data : array_like + The data array. + fill_value : scalar, optional + The value to be masked. If None, all non-zero values are masked. + + Returns + ------- + mask : ndarray + A boolean mask of the same shape as `data`, with entries + True where `data` is equal to `fill_value`. + + Examples + -------- + >>> data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + >>> np.ma.make_mask(data) + array([[False, False, False], + [False, False, False], + [False, False, False]], dtype=bool) + >>> np.ma.make_mask(data, 5) + array([[False, False, False], + [False, True, False], + [False, False, False]], dtype=bool) + """ + if fill_value is None: + mask = np.zeros_like(data, dtype=bool) + else: + mask = np.equal(data, fill_value) + return mask +Generated code for numpy.ma.make_mask_none + + +import numpy as np + +def make_mask_none(arr): + """ + Create a mask of all False values for a given array. + + Parameters + ---------- + arr : array_like + Input array. + + Returns + ------- + mask : numpy.ma.MaskedArray + Masked array with all False values. + """ + return np.ma.MaskedArray(arr, mask=np.zeros_like(arr, dtype=bool)) +Generated code for numpy.ma.mask_or + + +import numpy as np + +def mask_or(a, b, copy=True): + """ + Perform a logical_or operation on two masked arrays. + + Parameters + ---------- + a, b : array_like + Input arrays. + copy : bool, optional + Whether to return a copy of `a` or to perform in-place + operations. Default is True. + + Returns + ------- + result : MaskedArray + Logical_or of `a` and `b`. + + Examples + -------- + >>> a = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> b = np.ma.array([4, 5, 6], mask=[1, 0, 0]) + >>> np.ma.mask_or(a, b) + masked_array(data=[--, 5, 6], + mask=[ True, False, False], + fill_value=999999) + + """ + a = np.ma.array(a, copy=copy) + b = np.ma.array(b, copy=copy) + a.mask = np.logical_or(a.mask, b.mask) + a[~a.mask] = np.logical_or(a[~a.mask], b[~a.mask]) + return a +Generated code for numpy.ma.make_mask_descr + + +import numpy as np + +def make_mask_descr(mask): + """ + Create a mask description from a boolean mask array. + + Parameters + ---------- + mask : array_like + Boolean mask array. + + Returns + ------- + descr : dict + Dictionary containing the mask description. + """ + # Check if mask is a boolean array + if not np.issubdtype(mask.dtype, np.bool_): + raise TypeError('mask must be a boolean array') + + # Get the shape of the mask + shape = mask.shape + + # Initialize the description + descr = {'shape': shape, 'fortran_order': False, 'descr': []} + + # Iterate over the mask and create the description + for i in range(shape[0]): + for j in range(shape[1]): + if mask[i, j]: + descr['descr'].append((i, j)) + + return descr +Generated code for numpy.ma.getmask +array + +import numpy as np + +def getmaskarray(mask): + """ + Return a boolean array from a mask. + + Parameters + ---------- + mask : array_like + Mask array. + + Returns + ------- + result : ndarray + Boolean array with the same shape as `mask`. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> np.ma.getmaskarray(x) + array([False, True, False]) + + """ + return np.array(mask, dtype=bool) +Generated code for numpy.ma.getmaskarray + + +import numpy as np + +def getmaskarray(mask): + """ + Return a boolean array from a mask. + + Parameters + ---------- + mask : array_like + Mask array. + + Returns + ------- + result : ndarray + Boolean array with the same shape as `mask`. + + Examples + -------- + >>> a = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> np.ma.getmaskarray(a) + array([False, True, False]) + """ + return np.array(mask, dtype=bool) +Generated code for numpy.ma.masked_array.mask + + +import numpy as np + +# Create a masked array +x = np.ma.masked_array([1, 2, 3, 4, 5], mask=[0, 0, 1, 0, 0]) + +# Mask the array +x.mask = [1, 1, 0, 1, 1] + +# Print the masked array +print(x) +Generated code for numpy.ma.ndenumerate + + +import numpy as np + +# Create a 2D array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Use ndenumerate to iterate over the array +for index, value in np.ma.ndenumerate(arr): + print(index, value) +Generated code for numpy.ma.flatnotmasked_contiguous + + +import numpy as np + +def flatnotmasked_contiguous(a): + """ + Find contiguous unmasked data in a masked array along the given axis. + Returns a 1-D array of indices, with each subarray containing + the indices along the given axis of the contiguous unmasked data. + """ + a = np.ma.asarray(a) + m = np.ma.getmaskarray(a) + if m is np.ma.nomask: + return np.arange(a.shape[0]) + else: + # Find the indices of the unmasked data + idx = np.where(~m)[0] + # Find the start and end indices of each contiguous block + sidx = np.concatenate([[0], np.where(np.diff(idx) > 1)[0] + 1, [len(idx)]]) + return [idx[s:e] for s, e in zip(sidx[:-1], sidx[1:])] +Generated code for numpy.ma.flatnotmasked_edges + + +import numpy as np + +def flatnotmasked_edges(a): + """ + Find the indices of the first and last non-masked elements + along the given axis of a masked array. + + Parameters + ---------- + a : array_like + Input array. + + Returns + ------- + edges : tuple of ints + Indices of the first and last non-masked elements along + the given axis. + """ + a = np.ma.asarray(a) + m = np.ma.getmaskarray(a) + if m is np.ma.nomask: + return (0, a.shape[0] - 1) + else: + i = np.ma.flatnotmasked_edges(m) + return (i[0], i[-1]) +Generated code for numpy.ma.notmasked_contiguous + + +import numpy as np + +def notmasked_contiguous(mask): + """ + Find contiguous unmasked regions of a masked array. + + Parameters + ---------- + mask : array_like + Mask array. + + Returns + ------- + regions : list of slices + List of slices corresponding to contiguous unmasked regions. + """ + # Convert mask to a numpy array + mask = np.asarray(mask) + # Check if mask is a boolean array + if not np.issubdtype(mask.dtype, np.bool_): + raise TypeError('mask must be a boolean array') + # Initialize list of regions + regions = [] + # Find indices of unmasked elements + unmasked_indices = np.where(~mask)[0] + # Loop over unmasked indices + for i in range(len(unmasked_indices)): + # Get start and end indices of current region + start = unmasked_indices[i] + if i == len(unmasked_indices) - 1: + end = len(mask) + else: + end = unmasked_indices[i + 1] + # Append slice to list of regions + regions.append(slice(start, end)) + # Return list of regions + return regions +Generated code for numpy.ma.notmasked_edges + + +import numpy as np + +def notmasked_edges(mask): + """ + Find the edges of a masked array. + + Parameters + ---------- + mask : numpy.ma.MaskedArray + The masked array to find the edges of. + + Returns + ------- + edges : tuple + A tuple of the form (row_edges, col_edges) containing the + row and column edges of the masked array. + """ + # Get the shape of the mask + shape = mask.shape + + # Initialize the row and column edges + row_edges = [] + col_edges = [] + + # Iterate over the rows and columns + for i in range(shape[0]): + for j in range(shape[1]): + # If the current element is not masked + if not mask[i, j]: + # Check if the element is on the edge + if (i == 0 or not mask[i - 1, j]) and (i == shape[0] - 1 or not mask[i + 1, j]): + row_edges.append(i) + if (j == 0 or not mask[i, j - 1]) and (j == shape[1] - 1 or not mask[i, j + 1]): + col_edges.append(j) + + # Return the edges + return (row_edges, col_edges) +Generated code for numpy.ma.clump_masked + + +import numpy as np + +def clump_masked(data, axis=None): + """ + Clump together adjacent masked elements of an array. + + Parameters + ---------- + data : array_like + Input array. + axis : int, optional + Axis along which to operate. By default, the flattened input + is used. + + Returns + ------- + clumped : ndarray + Output array of same shape as `data`, with clumped elements + indicated by True values. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3, 4, 5, 6], mask=[0, 0, 0, 1, 1, 0]) + >>> np.ma.clump_masked(x) + array([False, False, False, True, True, False]) + + """ + data = np.ma.asarray(data) + if axis is None: + data = np.ma.ravel(data) + axis = 0 + else: + data = np.ma.swapaxes(data, axis, 0) + clumped = np.ma.zeros(data.shape, dtype=bool) + start = 0 + for i in range(1, len(data)): + if data.mask[i] != data.mask[i-1]: + clumped[start:i] = True + start = i + clumped[start:] = True + if axis > 0: + clumped = np.ma.swapaxes(clumped, axis, 0) + return clumped +Generated code for numpy.ma.clump_unmasked + + +import numpy as np + +def clump_unmasked(a, mask): + """ + Clump unmasked elements of an array. + + Parameters + ---------- + a : array_like + Input array. + mask : array_like + Mask array. + + Returns + ------- + clumps : ndarray + An array of the same shape as `a` containing the clump + numbers of the unmasked elements. + """ + # Make sure the mask is a boolean array + mask = np.array(mask, dtype=bool) + + # Find the unmasked elements + unmasked = np.where(~mask) + + # Initialize the clump array + clumps = np.zeros_like(a) + + # Initialize the clump counter + clump_num = 1 + + # Iterate over the unmasked elements + for i, j in zip(*unmasked): + # If the element is already part of a clump, skip it + if clumps[i, j] != 0: + continue + + # Otherwise, start a new clump + clumps[i, j] = clump_num + + # Find the neighbors of the current element + neighbors = np.array([[i-1, j], [i+1, j], [i, j-1], [i, j+1]]) + + # Iterate over the neighbors + for ni, nj in neighbors: + # If the neighbor is unmasked and not part of a clump, + # add it to the current clump + if (ni >= 0 and ni < a.shape[0] and + nj >= 0 and nj < a.shape[1] and + ~mask[ni, nj] and clumps[ni, nj] == 0): + clumps[ni, nj] = clump_num + + # Increment the clump counter + clump_num += 1 + + return clumps +Generated code for numpy.ma.mask_cols + + +import numpy as np + +def mask_cols(arr, mask): + """ + Mask columns of a 2D array using a boolean mask. + + Parameters + ---------- + arr : array_like + Input array. + mask : array_like + Boolean mask, with the same number of elements as the number of columns + in `arr`. + + Returns + ------- + result : numpy.ma.MaskedArray + Masked array with masked columns. + + Examples + -------- + >>> arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + >>> mask = np.array([True, False, True]) + >>> np.ma.mask_cols(arr, mask) + masked_array( + data=[[--, 2, --], + [--, 5, --], + [--, 8, --]], + mask=[[ True, False, True], + [ True, False, True], + [ True, False, True]], + fill_value=999999) + """ + arr = np.asarray(arr) + mask = np.asarray(mask) + if arr.ndim != 2: + raise ValueError('Input array must be 2D.') + if mask.ndim != 1: + raise ValueError('Mask array must be 1D.') + if arr.shape[1] != mask.shape[0]: + raise ValueError('Mask array must have the same number of elements as the number of columns in the input array.') + return np.ma.masked_array(arr, mask=np.tile(mask, (arr.shape[0], 1))) +Generated code for numpy.ma.mask_or + + +import numpy as np + +def mask_or(a, b, copy=True): + """ + Perform a logical_or operation on two masked arrays. + + Parameters + ---------- + a, b : array_like + Input arrays. + copy : bool, optional + Whether to return a copy of `a` or to perform in-place + operations. Default is True. + + Returns + ------- + result : MaskedArray + Logical_or of `a` and `b`. + + Examples + -------- + >>> a = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> b = np.ma.array([4, 5, 6], mask=[1, 0, 0]) + >>> np.ma.mask_or(a, b) + masked_array(data=[--, 5, 6], + mask=[ True, False, False], + fill_value=999999) + + """ + a = np.ma.array(a, copy=copy) + b = np.ma.array(b, copy=copy) + a.mask = np.logical_or(a.mask, b.mask) + a[~a.mask] = np.logical_or(a[~a.mask], b[~a.mask]) + return a +Generated code for numpy.ma.mask_rowcols + + +import numpy as np + +def mask_rowcols(arr, row_mask, col_mask): + """ + Mask rows and columns of a 2D array. + + Parameters + ---------- + arr : array_like + Input array. + row_mask : array_like + Boolean array with shape (M,) where M is the number of rows of `arr`. + col_mask : array_like + Boolean array with shape (N,) where N is the number of columns of `arr`. + + Returns + ------- + masked_arr : numpy.ma.MaskedArray + Masked array with the same shape as `arr`. + """ + # Convert input arrays to numpy arrays + arr = np.asarray(arr) + row_mask = np.asarray(row_mask) + col_mask = np.asarray(col_mask) + + # Check shapes + if row_mask.shape[0] != arr.shape[0]: + raise ValueError('row_mask has incorrect shape') + if col_mask.shape[0] != arr.shape[1]: + raise ValueError('col_mask has incorrect shape') + + # Create mask + mask = np.zeros_like(arr, dtype=bool) + mask[row_mask, :] = True + mask[:, col_mask] = True + + # Create masked array + masked_arr = np.ma.masked_array(arr, mask=mask) + + return masked_arr +Generated code for numpy.ma.mask_rows + + +import numpy as np + +def mask_rows(arr, row_mask): + """ + Mask rows of a 2D array using a boolean mask. + + Parameters + ---------- + arr : array_like + Input array of shape (M, N). + row_mask : array_like + Boolean mask of shape (M,). + + Returns + ------- + masked_arr : numpy.ma.MaskedArray + Masked array of shape (M, N). + """ + # Convert input array to a masked array + masked_arr = np.ma.masked_array(arr) + + # Mask the rows + masked_arr.mask[row_mask] = True + + return masked_arr +Generated code for numpy.ma.harden_mask + + +import numpy as np + +def harden_mask(mask): + """ + Harden a mask by setting all elements of the mask to True + where any element of the mask is True. + + Parameters + ---------- + mask : array_like + A boolean array. + + Returns + ------- + hardened_mask : ndarray + A boolean array with all elements set to True where any + element of the input mask is True. + + Examples + -------- + >>> a = np.ma.array([1, 2, 3], mask=[0, 1, 0]) + >>> np.ma.harden_mask(a.mask) + array([False, True, False], dtype=bool) + """ + hardened_mask = np.zeros_like(mask, dtype=bool) + hardened_mask[mask] = True + return hardened_mask +Generated code for numpy.ma.soften_mask + + +import numpy as np + +def soften_mask(mask, radius): + """ + Soften a boolean mask by replacing each True value with a + circular region of radius `radius`. + + Parameters + ---------- + mask : array_like + A boolean mask. + radius : int + The radius of the circular region. + + Returns + ------- + softened_mask : ndarray + The softened mask. + """ + # Create a meshgrid of coordinates + x, y = np.meshgrid(np.arange(mask.shape[1]), np.arange(mask.shape[0])) + + # Create a circular region + circle = (x - mask.shape[1]//2)**2 + (y - mask.shape[0]//2)**2 < radius**2 + + # Combine the mask and the circle + softened_mask = np.logical_or(mask, circle) + + return softened_mask +Generated code for numpy.ma.MaskedArray.harden_mask + + +def harden_mask(masked_array): + """ + Harden the mask of a MaskedArray. + + Parameters + ---------- + masked_array : numpy.ma.MaskedArray + The MaskedArray whose mask should be hardened. + + Returns + ------- + numpy.ma.MaskedArray + The MaskedArray with a hardened mask. + """ + # Get the mask of the MaskedArray + mask = masked_array.mask + + # Harden the mask by setting all masked values to True + mask[mask] = True + + # Return the MaskedArray with the hardened mask + return numpy.ma.MaskedArray(masked_array.data, mask=mask) +Generated code for numpy.ma.MaskedArray.soften_mask + + +def soften_mask(masked_array): + """ + Soften the mask of a numpy.ma.MaskedArray. + This function will replace any masked values that are surrounded by + unmasked values with the average of the surrounding unmasked values. + Parameters + ---------- + masked_array : numpy.ma.MaskedArray + The array to soften the mask of. + Returns + ------- + numpy.ma.MaskedArray + The softened array. + """ + # Get the mask and data from the array + mask = masked_array.mask + data = masked_array.data + + # Create a new array to store the softened mask + softened_mask = np.zeros_like(mask) + + # Iterate over the mask + for i in range(mask.shape[0]): + for j in range(mask.shape[1]): + # If the current value is masked + if mask[i, j]: + # Get the surrounding values + surrounding_values = [] + for x in range(i-1, i+2): + for y in range(j-1, j+2): + # Make sure the indices are valid + if 0 <= x < mask.shape[0] and 0 <= y < mask.shape[1]: + # Make sure the value is not masked + if not mask[x, y]: + surrounding_values.append(data[x, y]) + # If there are surrounding values, replace the masked value + # with the average of the surrounding values + if len(surrounding_values) > 0: + softened_mask[i, j] = np.mean(surrounding_values) + + # Create a new array with the softened mask + softened_array = np.ma.MaskedArray(data, softened_mask) + + return softened_array +Generated code for numpy.ma.MaskedArray.shrink_mask + + +def shrink_mask(masked_array): + """ + Shrinks the mask of a numpy.ma.MaskedArray. + + Parameters + ---------- + masked_array : numpy.ma.MaskedArray + The array whose mask should be shrunk. + + Returns + ------- + numpy.ma.MaskedArray + The array with the shrunken mask. + """ + # Get the mask of the array + mask = masked_array.mask + + # Create a new mask with the same shape as the original + new_mask = np.zeros_like(mask) + + # Iterate over the array and shrink the mask + for i in range(mask.shape[0]): + for j in range(mask.shape[1]): + # If the current element is masked, check the surrounding elements + if mask[i, j]: + # Check the elements above and below + if i > 0 and mask[i - 1, j]: + new_mask[i, j] = True + if i < mask.shape[0] - 1 and mask[i + 1, j]: + new_mask[i, j] = True + + # Check the elements to the left and right + if j > 0 and mask[i, j - 1]: + new_mask[i, j] = True + if j < mask.shape[1] - 1 and mask[i, j + 1]: + new_mask[i, j] = True + + # Set the new mask on the array + masked_array.mask = new_mask + + return masked_array +Generated code for numpy.ma.MaskedArray.unshare_mask + + +def unshare_mask(masked_array): + """ + Unshare the mask of a MaskedArray. + + Parameters + ---------- + masked_array : numpy.ma.MaskedArray + The MaskedArray whose mask should be unshared. + + Returns + ------- + numpy.ma.MaskedArray + A new MaskedArray with an unshared mask. + """ + # Create a new MaskedArray with the same data as the original + new_masked_array = np.ma.MaskedArray(masked_array.data) + + # Copy the mask from the original MaskedArray + new_masked_array.mask = np.copy(masked_array.mask) + + return new_masked_array +Generated code for numpy.ma.asarray + + +import numpy as np + +def numpy_ma_asarray(input_array): + """ + Convert an input array to a masked array. + + Parameters + ---------- + input_array : array_like + Input array to be converted. + + Returns + ------- + output_array : numpy.ma.MaskedArray + Masked array version of the input array. + """ + output_array = np.ma.asarray(input_array) + return output_array +Generated code for numpy.ma.asanyarray + + +import numpy as np + +def asanyarray(a, dtype=None, order=None): + """ + Convert the input to a masked array of the given data type. + + Parameters + ---------- + a : array_like + Input data, in any form that can be converted to an array. This + includes lists, lists of tuples, tuples, tuples of tuples, tuples + of lists and ndarrays. + dtype : data-type, optional + By default, the data-type is inferred from the input data. + order : {'C', 'F'}, optional + Whether to use row-major (C-style) or column-major + (Fortran-style) memory representation. Defaults to 'C'. + + Returns + ------- + out : ndarray + The converted masked array. + + """ + a = np.asarray(a, dtype=dtype, order=order) + return np.ma.masked_array(a, mask=np.isnan(a)) +Generated code for numpy.ma.fix_invalid + + +import numpy as np + +def fix_invalid(data): + # Replace invalid values with the mean of the valid values + valid_mask = np.ma.masked_invalid(data).mask + data[valid_mask] = np.ma.masked_invalid(data).mean() + return data +Generated code for numpy.ma.masked_equal + + +import numpy as np + +# Create a sample array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Mask all elements equal to 5 +masked_arr = np.ma.masked_equal(arr, 5) + +# Print the masked array +print(masked_arr) + +# Output: +# [[1 2 3] +# [4 -- 6] +# [7 8 9]] +Generated code for numpy.ma.masked_greater + + +import numpy as np + +def masked_greater(a, value): + return np.ma.masked_where(a > value, a) + +# Usage +a = np.array([1, 2, 3, 4, 5]) +masked_greater(a, 3) +# masked_array(data=[1, 2, --, --, --], +# mask=[False, False, True, True, True], +# fill_value=999999) +Generated code for numpy.ma.masked_greater_equal + + +import numpy as np + +# Create an array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Mask elements greater than or equal to 5 +masked_arr = np.ma.masked_greater_equal(arr, 5) + +# Print the masked array +print(masked_arr) +Generated code for numpy.ma.masked_inside + + +import numpy as np + +def masked_inside(arr, vmin, vmax): + """ + Mask elements of an array that are inside a given range. + + Parameters + ---------- + arr : array_like + Input array. + vmin : scalar + Lower bound of the range. + vmax : scalar + Upper bound of the range. + + Returns + ------- + result : MaskedArray + Output array, with elements inside the range masked. + """ + result = np.ma.masked_array(arr, copy=True) + result[(arr >= vmin) & (arr <= vmax)] = np.ma.masked + return result +Generated code for numpy.ma.masked_invalid + + +import numpy as np + +def masked_invalid(arr): + return np.ma.masked_invalid(arr) + +# Example +arr = np.array([1, 2, np.nan, 3, 4]) +masked_arr = masked_invalid(arr) +print(masked_arr) +# Output: +# [1.0 2.0 -- 3.0 4.0] +Generated code for numpy.ma.masked_less + + +import numpy as np + +def masked_less(x, value): + return np.ma.masked_where(x < value, x) + +# Usage +x = np.array([1, 2, 3, 4, 5]) +masked_less(x, 3) +# masked_array(data=[--, --, 3, 4, 5], +# mask=[ True, True, False, False, False], +# fill_value=999999) +Generated code for numpy.ma.masked_less_equal + + +import numpy as np + +def masked_less_equal(x, value): + """ + Return a masked array where all elements of x less than or equal to value are masked. + + Parameters + ---------- + x : array_like + Input array. + value : scalar + Threshold value. + + Returns + ------- + out : MaskedArray + Output array. + """ + x = np.asarray(x) + mask = x <= value + return np.ma.masked_array(x, mask) +Generated code for numpy.ma.masked_not_equal + + +import numpy as np + +# Create a sample array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Mask all elements not equal to 5 +masked_arr = np.ma.masked_not_equal(arr, 5) + +# Print the masked array +print(masked_arr) +Generated code for numpy.ma.masked_object + + +import numpy as np + +def masked_object(data, mask): + """ + Mask an object array with a boolean array. + + Parameters + ---------- + data : array_like + Input data. + mask : array_like + Boolean array used to mask the data. Must be the same shape as `data`. + + Returns + ------- + masked_data : numpy.ma.MaskedArray + Masked array containing the masked data. + """ + # Convert data to a numpy array + data = np.asarray(data) + + # Create a masked array from the data and mask + masked_data = np.ma.masked_array(data, mask) + + return masked_data +Generated code for numpy.ma.masked_outside + + +import numpy as np + +# Create a sample array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Mask values outside the range [2, 8] +masked_arr = np.ma.masked_outside(arr, 2, 8) + +# Print the masked array +print(masked_arr) +Generated code for numpy.ma.masked_values + + +import numpy as np + +# Create a masked array with masked values +masked_array = np.ma.masked_values([1, 2, 3, 4, 5, 6], 3) + +# Print the masked array +print(masked_array) + +# Output: +# [1 2 -- 4 5 6] +Generated code for numpy.ma.masked_where + + +import numpy as np + +def masked_where(condition, a, copy=True): + """ + Return a masked array, where the condition is True. + + Parameters + ---------- + condition : array_like + A boolean array with the same shape as `a` + a : array_like + The data to mask + copy : bool, optional + Whether to return a copy of `a` (True) or to return a view + (False). Defaults to True. + + Returns + ------- + result : MaskedArray + A masked array containing the data in `a`, masked where + `condition` is True. + + Examples + -------- + >>> x = np.arange(5) + >>> np.ma.masked_where(x < 3, x) + masked_array(data = [-- -- -- 3 4], + mask = [ True True True False False], + fill_value = 999999) + """ + result = np.ma.array(a, copy=copy) + result[condition] = np.ma.masked + return result +Generated code for numpy.ma.compress_cols + + +import numpy as np + +def compress_cols(arr): + """ + Compress columns of a 2D array using numpy.ma.compress_cols. + + Parameters + ---------- + arr : array_like + Input array of shape (m, n). + + Returns + ------- + out : ndarray + Output array of shape (m, k) where k <= n. + """ + # Mask out columns with all zeros + mask = np.all(arr == 0, axis=0) + # Compress columns + out = np.ma.compress_cols(arr, mask) + return out +Generated code for numpy.ma.compress_rowcols + + +import numpy as np + +def compress_rowcols(arr): + # Get the shape of the array + shape = arr.shape + # Create a mask of the array + mask = np.ma.make_mask(arr) + # Create a new array with the same shape as the original + new_arr = np.zeros(shape) + # Iterate through the rows and columns of the array + for i in range(shape[0]): + for j in range(shape[1]): + # If the mask is False, set the value in the new array to the value in the original array + if not mask[i, j]: + new_arr[i, j] = arr[i, j] + # Return the new array + return new_arr +Generated code for numpy.ma.compress_rows + + +import numpy as np + +def compress_rows(arr): + # Create a mask of valid rows + mask = np.any(arr != 0, axis=1) + # Compress the array using the mask + return np.ma.compress_rows(arr, mask) + +# Example +arr = np.array([[1, 0, 0], + [0, 0, 0], + [0, 2, 3]]) + +compress_rows(arr) +# array([[1, 0, 0], +# [0, 2, 3]]) +Generated code for numpy.ma.compressed + + +import numpy as np + +def compressed(arr): + return np.ma.compressed(arr) + +# example +arr = np.ma.array([[1,2,3], [4,5,6], [7,8,9]], mask=[[0,1,0], [1,0,1], [0,1,0]]) + +print(compressed(arr)) +# Output: [1, 3, 4, 6, 7, 9] +Generated code for numpy.ma.filled + + +import numpy as np + +def numpy_ma_filled(arr): + """ + This function takes a numpy masked array and returns a new array with all + masked values filled with a given value. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be filled. + + Returns + ------- + numpy.ndarray + A new array with all masked values filled. + """ + # Create a copy of the array + filled_arr = arr.copy() + + # Get the indices of the masked values + mask_indices = np.where(filled_arr.mask) + + # Fill the masked values with the given value + filled_arr[mask_indices] = np.ma.filled(arr) + + return filled_arr +Generated code for numpy.ma.MaskedArray.compressed + + +import numpy as np + +def compressed(arr): + """ + Return a 1-D array of all the non-masked elements of the input array. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be compressed. + + Returns + ------- + numpy.ndarray + A 1-D array of all the non-masked elements of the input array. + """ + return np.ma.MaskedArray.compressed(arr) +Generated code for numpy.ma.MaskedArray.filled + + +import numpy as np + +def filled(self): + """ + Return a copy of the masked array, with the masked values filled with + a given fill_value. + + Parameters + ---------- + fill_value : scalar + Value used to fill in the masked values. + Default is 0. + + Returns + ------- + filled_array : ndarray + A copy of the original array, with the masked entries replaced + by `fill_value`. + + Examples + -------- + >>> x = np.ma.array(np.arange(5), mask=[0, 0, 1, 0, 0]) + >>> x.filled() + array([0, 1, 9, 3, 4]) + >>> x.filled(99) + array([ 0, 1, 99, 3, 4]) + """ + fill_value = 0 + filled_array = np.copy(self.data) + filled_array[self.mask] = fill_value + return filled_array +Generated code for numpy.ma.MaskedArray.tofile + + +import numpy as np + +def tofile(self, fname, sep="", format="%s"): + """ + Save a masked array to a file. + + Parameters + ---------- + fname : str + The file name. + sep : str, optional + The separator between array elements. + format : str, optional + The format of the array elements. + + Returns + ------- + None + + """ + # Open the file + f = open(fname, 'w') + + # Write the data + for i in range(self.shape[0]): + for j in range(self.shape[1]): + if not self.mask[i, j]: + f.write(format % self[i, j]) + f.write(sep) + f.write('\n') + + # Close the file + f.close() +Generated code for numpy.ma.MaskedArray.tolist + + +import numpy as np + +def tolist(arr): + if isinstance(arr, np.ma.MaskedArray): + return arr.filled(np.nan).tolist() + else: + return arr.tolist() + +# Usage +arr = np.ma.MaskedArray([1,2,3,4], mask=[True, False, False, True]) +print(tolist(arr)) # [nan, 2.0, 3.0, nan] +Generated code for numpy.ma.MaskedArray.torecords + + +import numpy as np + +def torecords(self): + """ + Return a record array view of the masked array. + + Returns + ------- + rec : numpy.recarray + A record array view of the masked array. + """ + # Create a record array from the masked array + rec = np.rec.fromarrays(self.data, names=self.dtype.names) + + # Mask the record array + for name in self.dtype.names: + rec[name] = np.ma.masked_array(rec[name], self.mask[name]) + + return rec +Generated code for numpy.ma.MaskedArray.tobytes + + +import numpy as np + +def tobytes(arr): + """ + Convert a numpy.ma.MaskedArray to a bytes object. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to convert. + + Returns + ------- + bytes + The bytes object representing the array. + """ + # Get the data type of the array + dtype = arr.dtype + + # Get the data as a numpy array + data = np.asarray(arr) + + # Get the mask as a numpy array + mask = np.asarray(arr.mask) + + # Create a bytes object from the data and mask + return np.concatenate((data.astype(dtype).tobytes(), mask.astype(np.bool).tobytes())).tobytes() +Generated code for numpy.ma.common_fill_value + + +import numpy as np + +def common_fill_value(arrays): + """ + Find the common fill value for a list of numpy arrays. + + Parameters + ---------- + arrays : list of numpy arrays + List of numpy arrays to find the common fill value for. + + Returns + ------- + fill_value : scalar + The common fill value for the list of arrays. + """ + # Get the fill values for each array + fill_values = [np.ma.getfillvalue(arr) for arr in arrays] + + # Find the most common fill value + fill_value, count = np.unique(fill_values, return_counts=True) + fill_value = fill_value[np.argmax(count)] + + return fill_value +Generated code for numpy.ma.default_fill_value + + +import numpy as np + +# Set the default fill value for masked array +np.ma.default_fill_value = -999 +Generated code for numpy.ma.maximum_fill_value + + +import numpy as np + +# Create a masked array +arr = np.ma.masked_array([1, 2, 3, 4, 5], mask=[False, False, True, False, True]) + +# Set the maximum fill value +np.ma.maximum_fill_value(arr, 10) + +# Print the array +print(arr) + +# Output: +# [1 2 10 4 10] +Generated code for numpy.ma.minimum_fill_value + + +import numpy as np + +def minimum_fill_value(arr): + """ + Returns the minimum fill value for a given array. + Parameters + ---------- + arr : array_like + Input array. + Returns + ------- + min_fill_value : float + The minimum fill value for the array. + """ + min_fill_value = np.ma.minimum_fill_value(arr) + return min_fill_value +Generated code for numpy.ma.set_fill_value + + +import numpy as np + +# Create a masked array +arr = np.ma.masked_array([1, 2, 3, 4], mask=[False, True, False, True]) + +# Set the fill value +np.ma.set_fill_value(arr, 0) + +# Print the array +print(arr) + +# Output: +# [1 -- 3 --] +Generated code for numpy.ma.MaskedArray.get_fill_value + + +import numpy as np + +def get_fill_value(arr): + """ + Returns the fill value of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to get the fill value from. + + Returns + ------- + fill_value : float + The fill value of the array. + """ + return np.ma.MaskedArray.fill_value(arr) +Generated code for numpy.ma.MaskedArray.set_fill_value + + +import numpy as np + +# Create a masked array +arr = np.ma.MaskedArray(data=[1, 2, 3, 4], + mask=[False, True, False, True]) + +# Set the fill value +arr.set_fill_value(0) + +# Print the array +print(arr) + +# Output: +# [1 -- 3 --] +Generated code for numpy.ma.MaskedArray.fill_value + + +import numpy as np + +def fill_value(arr, fill_value): + """ + Fill the masked elements of a numpy.ma.MaskedArray with the given fill_value. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to fill. + fill_value : scalar + The value to fill the masked elements with. + + Returns + ------- + numpy.ma.MaskedArray + The array with the masked elements filled. + """ + # Create a copy of the array + filled_arr = np.ma.copy(arr) + # Get the indices of the masked elements + masked_indices = np.ma.getmaskarray(arr) + # Fill the masked elements with the given fill_value + filled_arr[masked_indices] = fill_value + # Return the filled array + return filled_arr +Generated code for numpy.ma.anom + + +import numpy as np + +def anom(x): + """ + Calculate the anomaly of an array x. + + Parameters + ---------- + x : array_like + Input array. + + Returns + ------- + anom : ndarray + Anomaly of the input array. + + """ + mean = np.mean(x) + anom = x - mean + return anom +Generated code for numpy.ma.anomalies + + +import numpy as np + +def anomalies(data): + """ + Calculates the anomalies of a given array of data. + + Parameters + ---------- + data : array_like + The array of data to calculate the anomalies of. + + Returns + ------- + anomalies : numpy.ma.MaskedArray + The anomalies of the given data. + """ + mean = np.mean(data) + anomalies = np.ma.masked_array(data - mean, mask=np.isnan(data)) + return anomalies +Generated code for numpy.ma.average + + +import numpy as np + +def numpy_ma_average(arr): + # Create a masked array from the input array + masked_arr = np.ma.masked_array(arr) + + # Calculate the average of the masked array + avg = np.ma.average(masked_arr) + + # Return the average + return avg +Generated code for numpy.ma.conjugate + + +import numpy as np + +def conjugate(x): + """ + Computes the complex conjugate of a numpy array. + + Parameters + ---------- + x : numpy array + Input array. + + Returns + ------- + numpy array + Complex conjugate of the input array. + """ + return np.ma.conjugate(x) +Generated code for numpy.ma.corrcoef + + +import numpy as np + +def corrcoef(x, y): + x_mean = np.mean(x) + y_mean = np.mean(y) + x_std = np.std(x) + y_std = np.std(y) + xy_cov = np.sum((x - x_mean) * (y - y_mean)) / (len(x) - 1) + return xy_cov / (x_std * y_std) + +# Example +x = np.array([1, 2, 3, 4, 5]) +y = np.array([2, 3, 4, 5, 6]) + +print(corrcoef(x, y)) # 0.98198050606 +Generated code for numpy.ma.cov + + +import numpy as np + +def cov(x, y, bias=True): + """ + Computes the covariance matrix of two arrays. + + Parameters + ---------- + x : array_like + A 1-D or 2-D array containing multiple variables and observations. + y : array_like + A 1-D or 2-D array containing multiple variables and observations. + bias : bool, optional + If False, normalization is by (N - 1) where N is the number of observations given (unbiased estimate). + If True, then normalization is by N. + + Returns + ------- + out : ndarray + A 2-D array containing the covariance matrix of the two arrays. + + """ + x = np.asarray(x) + y = np.asarray(y) + xmean = np.mean(x, axis=0) + ymean = np.mean(y, axis=0) + xm, ym = x - xmean, y - ymean + if bias: + fact = x.shape[0] * 1.0 + else: + fact = x.shape[0] - 1.0 + return np.dot(xm.T, ym) / fact +Generated code for numpy.ma.cumsum + + +import numpy as np + +def cumsum(arr): + """ + Calculates the cumulative sum of the elements in the array. + + Parameters + ---------- + arr : array_like + Input array. + + Returns + ------- + cumsum : ndarray + A new array holding the result. + + Examples + -------- + >>> arr = np.array([1, 2, 3]) + >>> np.ma.cumsum(arr) + array([1, 3, 6]) + """ + return np.cumsum(arr) +Generated code for numpy.ma.cumprod + + +import numpy as np + +def cumprod(arr): + result = np.ones(arr.shape) + for i in range(arr.shape[0]): + for j in range(arr.shape[1]): + result[i,j] = np.prod(arr[:i+1,j]) + return result + +arr = np.array([[1,2,3],[4,5,6],[7,8,9]]) +print(cumprod(arr)) +Generated code for numpy.ma.mean + + +import numpy as np + +def numpy_ma_mean(arr): + """ + Calculates the mean of a numpy masked array. + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array for which the mean should be calculated. + Returns + ------- + mean : float + The mean of the array. + """ + # Calculate the sum of the array + sum_arr = np.ma.sum(arr) + # Calculate the number of elements in the array + num_elements = np.ma.count(arr) + # Calculate the mean + mean = sum_arr / num_elements + return mean +Generated code for numpy.ma.median + + +import numpy as np + +def median(a, axis=None, out=None, overwrite_input=False): + """ + Compute the median along the specified axis. + + Returns the median of the array elements. + + Parameters + ---------- + a : array_like + Input array or object that can be converted to an array. + axis : int, optional + Axis along which the medians are computed. The default (axis=None) + is to compute the median along a flattened version of the array. + out : ndarray, optional + Alternative output array in which to place the result. It must + have the same shape and buffer length as the expected output, + but the type (of the output) will be cast if necessary. + overwrite_input : bool, optional + If True, then allow use of memory of input array (a) for + calculations. The input array will be modified by the call to + median. This will save memory when you do not need to preserve + the contents of the input array. Treat the input as undefined, + but it will probably be fully or partially sorted. Default is + False. Note that, if `overwrite_input` is True and the input + is not already an ndarray, an error will be raised. + + Returns + ------- + median : ndarray + A new array holding the result. If the input contains + integers, or floats of smaller precision than 64, then the output + data-type is float64. Otherwise, the output data-type is the same + as that of the input. + + See Also + -------- + mean, percentile + + Notes + ----- + Given a vector V of length N, the median of V is the middle value of + a sorted copy of V, ``V_sorted`` - i.e., ``V_sorted[(N-1)/2]``, when N + is odd, and the average of the two middle values of ``V_sorted`` when N + is even. + + Examples + -------- + >>> a = np.array([[10, 7, 4], [3, 2, 1]]) + >>> np.ma.median +Generated code for numpy.ma.power + + +import numpy as np + +def power(x, y): + return np.ma.power(x, y) + +x = np.array([1, 2, 3, 4]) +y = np.array([2, 3, 4, 5]) + +result = power(x, y) + +print(result) +Generated code for numpy.ma.prod + + +import numpy as np + +def prod(a, axis=None, dtype=None, out=None, keepdims=False): + """ + Return the product of array elements over a given axis. + + Parameters + ---------- + a : array_like + Input array. + axis : None or int or tuple of ints, optional + Axis or axes along which a product is performed. The default, + axis=None, will calculate the product of all the elements in the + input array. If axis is negative it counts from the last to the + first axis. + dtype : data-type, optional + The data-type of the returned array, as well as of the accumulator + in which the elements are multiplied. By default, if `a` is of + integer type, `dtype` is set to `np.int64`; otherwise, it is + set to `np.float64`. Note that `dtype` is always the same as the + output array type. + out : ndarray, optional + Alternative output array in which to place the result. It must have + the same shape as the expected output, but the type of the output + values will be cast if necessary. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left in the + result as dimensions with size one. With this option, the result + will broadcast correctly against the original `arr`. + + Returns + ------- + product_along_axis : ndarray + An array shaped as `a` but with the specified axis removed. + Returns a reference to `out` if specified. + + See Also + -------- + numpy.prod : equivalent function + + Examples + -------- + >>> a = np.array([1,2,3]) + >>> np.ma.prod(a) + 6 + >>> np.ma.prod(a, dtype=np.float64) + 6.0 + >>> b = np.arange(24).reshape(2,3,4) + >>> np.ma.prod(b, axis=1) + array +Generated code for numpy.ma.std + + +import numpy as np + +def numpy_ma_std(arr): + return np.ma.std(arr, axis=None, dtype=None, out=None, ddof=0, keepdims=False) + +#Example +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +print(numpy_ma_std(arr)) +# Output: 2.581988897471611 +Generated code for numpy.ma.sum + + +import numpy as np + +def numpy_ma_sum(arr): + return np.ma.sum(arr, axis=None) + +# Example +arr = np.array([[1, 2], [3, 4]]) +print(numpy_ma_sum(arr)) # 10 +Generated code for numpy.ma.var + + +import numpy as np + +def ma_var(x): + """ + Calculate the variance of a masked array. + Parameters + ---------- + x : array_like + Input array or object that can be converted to an array. + Returns + ------- + var : ndarray + Variance of the input array. + """ + x = np.ma.array(x) + n = x.count() + if n == 0: + return np.ma.masked + m = np.ma.mean(x) + s = np.ma.sum((x - m)**2) + return s / (n - 1) + +# Example +x = np.ma.array([1, 2, 3, 4, 5], mask=[0, 0, 0, 1, 1]) +print(ma_var(x)) # Output: 2.5 +Generated code for numpy.ma.MaskedArray.anom + + +import numpy as np + +def anom(arr): + """ + Calculates the anomaly of a given array. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to calculate the anomaly of. + + Returns + ------- + numpy.ma.MaskedArray + The anomaly of the given array. + """ + # Calculate the mean of the array + mean = np.ma.mean(arr) + + # Subtract the mean from each element in the array + anom = arr - mean + + # Return the anomaly + return anom +Generated code for numpy.ma.MaskedArray.cumprod + + +import numpy as np + +def cumprod(arr): + """ + Computes the cumulative product of the elements of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to compute the cumulative product of. + + Returns + ------- + numpy.ma.MaskedArray + The cumulative product of the elements of the array. + """ + # Initialize the cumulative product array + cumprod_arr = np.ma.MaskedArray(np.ones_like(arr)) + + # Iterate over the array and compute the cumulative product + for i in range(arr.shape[0]): + cumprod_arr[i] = np.ma.prod(arr[:i+1]) + + return cumprod_arr +Generated code for numpy.ma.MaskedArray.cumsum + + +import numpy as np + +def cumsum(arr): + """ + Calculates the cumulative sum of the elements of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to calculate the cumulative sum of. + + Returns + ------- + numpy.ma.MaskedArray + The cumulative sum of the elements of the array. + """ + # Create a copy of the array + cumsum_arr = arr.copy() + + # Iterate over the array + for i in range(1, arr.shape[0]): + # Calculate the cumulative sum + cumsum_arr[i] = cumsum_arr[i-1] + arr[i] + + # Return the cumulative sum + return cumsum_arr +Generated code for numpy.ma.MaskedArray.mean + + +import numpy as np + +def mean(arr): + """ + Calculates the mean of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array for which the mean is to be calculated. + + Returns + ------- + mean : float + The mean of the array. + """ + # Get the sum of the array + sum_arr = np.ma.sum(arr) + + # Get the number of elements in the array + num_elements = np.ma.count(arr) + + # Calculate the mean + mean = sum_arr / num_elements + + return mean +Generated code for numpy.ma.MaskedArray.prod + + +import numpy as np + +def prod(arr, axis=None, dtype=None, out=None, keepdims=False): + """ + Return the product of the array elements over the given axis. + + Parameters + ---------- + arr : array_like + Input array. + axis : int, optional + Axis along which the product is computed. The default (`axis` = `None`) + is to compute the product of all the elements in the input array. + dtype : data-type, optional + The data-type of the returned array and of the accumulator in which + the elements are multiplied. By default, the dtype of `arr` is used. + out : ndarray, optional + Alternative output array in which to place the result. It must have + the same shape as the expected output, but the type of the output + values will be cast if necessary. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left in the + result as dimensions with size one. With this option, the result + will broadcast correctly against the original `arr`. + + Returns + ------- + prod : ndarray + A new array holding the result is returned unless `out` is + specified, in which case a reference to `out` is returned. + + See Also + -------- + numpy.ma.MaskedArray.prod : equivalent function for masked arrays. + + Examples + -------- + >>> arr = np.array([[1.,2.],[3.,4.]]) + >>> np.prod(arr) + 24.0 + >>> np.prod(arr, axis=0) + array([ 3., 8.]) + >>> np.prod(arr, axis=1) + array([ 2., 12.]) + + """ + arr = np.ma.MaskedArray(arr, copy=True) + if dtype is None: + dtype = arr.dtype + if out is None: + out = np.empty(arr.shape, dtype=dtype) + np.ma.MaskedArray.prod( +Generated code for numpy.ma.MaskedArray.std + + +import numpy as np + +def masked_array_std(arr, axis=None, dtype=None, out=None, ddof=0, keepdims=False): + """ + Calculate the standard deviation of a masked array along the specified axis. + + Parameters + ---------- + arr : array_like + Input array or object that can be converted to an array. + axis : int, optional + Axis along which the standard deviation is computed. The default is to + compute the standard deviation of the flattened array. + dtype : data-type, optional + Type to use in computing the standard deviation. For arrays of + integer type the default is float64, for arrays of float types it is + the same as the array type. + out : ndarray, optional + Alternative output array in which to place the result. It must have + the same shape as the expected output, but the type of the output + values will be cast if necessary. + ddof : int, optional + Means Delta Degrees of Freedom. The divisor used in calculations + is N - ddof, where N represents the number of elements. By default + ddof is zero. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left in the + result as dimensions with size one. With this option, the result + will broadcast correctly against the original array. + + Returns + ------- + standard_deviation : ndarray + A new array holding the result is returned unless out is + specified, in which case a reference to out is returned. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3, 4], mask=[0, 0, 1, 0]) + >>> np.ma.std(x) + 1.1180339887498949 + >>> np.ma.std(x, axis=0) + 1.1180339887498949 + >>> np.ma.std(x, axis=1) + 0.0 + + """ + arr = np.ma.asarray(arr) + if dtype is None: + dtype = np.result +Generated code for numpy.ma.MaskedArray.sum + + +import numpy as np + +def masked_array_sum(arr): + """ + Calculates the sum of all elements in a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to sum. + + Returns + ------- + float + The sum of all elements in the array. + """ + # Get the mask of the array + mask = arr.mask + + # Initialize the sum + sum = 0 + + # Iterate over the array + for i in range(arr.shape[0]): + for j in range(arr.shape[1]): + # If the element is not masked, add it to the sum + if not mask[i, j]: + sum += arr[i, j] + + return sum +Generated code for numpy.ma.MaskedArray.var + + +import numpy as np + +def masked_array_var(arr): + """ + Calculates the variance of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array for which the variance is to be calculated. + + Returns + ------- + float + The variance of the array. + """ + # Calculate the mean of the array + mean = np.ma.mean(arr) + + # Calculate the sum of the squared differences from the mean + sum_sq_diff = np.ma.sum((arr - mean)**2) + + # Calculate the variance + var = sum_sq_diff / (arr.count() - 1) + + return var +Generated code for numpy.ma.argmax + + +import numpy as np + +def numpy_ma_argmax(arr): + # Get the maximum value from the array + max_val = np.ma.max(arr) + # Get the indices of the maximum value + max_indices = np.where(arr == max_val) + # Return the first index of the maximum value + return max_indices[0][0] + +# Example +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +print(numpy_ma_argmax(arr)) # Output: 8 +Generated code for numpy.ma.argmin + + +import numpy as np + +def numpy_ma_argmin(arr): + return np.ma.argmin(arr) + +# Usage +arr = np.array([[1,2,3], [4,5,6], [7,8,9]]) +print(numpy_ma_argmin(arr)) # Output: 0 +Generated code for numpy.ma.max + + +import numpy as np + +def max_ma(arr): + # Create a masked array from the input array + ma_arr = np.ma.masked_array(arr) + + # Find the maximum value in the masked array + max_val = np.ma.max(ma_arr) + + # Return the maximum value + return max_val +Generated code for numpy.ma.min + + +import numpy as np + +arr = np.ma.array([[1,2,3], [4,5,6], [7,8,9]]) + +min_val = np.ma.min(arr) + +print(min_val) +Generated code for numpy.ma.ptp + + +import numpy as np + +def ptp(a, axis=None): + """ + Return the peak-to-peak (maximum - minimum) value along a given axis. + + Parameters + ---------- + a : array_like + Input array. + axis : int, optional + Axis along which to find the peak-to-peak (maximum - minimum) value. + By default, flatten the array. + + Returns + ------- + ptp : ndarray + A new array holding the result, unless `out` was + specified, in which case a reference to `out` is returned. + + Examples + -------- + >>> x = np.ma.array([1, 2, 3, 4, 5, 6]) + >>> np.ma.ptp(x) + 5 + >>> np.ma.ptp(x, axis=0) + 5 + >>> np.ma.ptp(x, axis=1) + 0 + """ + a = np.ma.asanyarray(a) + if axis is None: + a = a.ravel() + axis = 0 + return a.max(axis=axis) - a.min(axis=axis) +Generated code for numpy.ma.diff + + +import numpy as np + +def diff(arr): + """ + Calculates the difference between adjacent elements in a numpy array. + + Parameters + ---------- + arr : numpy array + The array to calculate the difference of. + + Returns + ------- + numpy array + The difference between adjacent elements in the array. + """ + return np.ma.diff(arr) +Generated code for numpy.ma.MaskedArray.argmax + + +import numpy as np + +def argmax(arr): + """ + Returns the indices of the maximum values in a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to find the maximum values in. + + Returns + ------- + indices : tuple + A tuple of the indices of the maximum values in the array. + """ + # Get the indices of the non-masked elements + indices = np.where(~arr.mask) + + # Get the maximum value + max_val = np.max(arr[indices]) + + # Get the indices of the maximum value + max_indices = np.where(arr == max_val) + + return max_indices +Generated code for numpy.ma.MaskedArray.argmin + + +import numpy as np + +def argmin(arr): + """ + Find the index of the minimum value in a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to search for the minimum value. + + Returns + ------- + index : int + The index of the minimum value in the array. + """ + # Get the indices of the unmasked elements + indices = np.where(~arr.mask) + + # Get the minimum value + min_val = np.min(arr[indices]) + + # Get the index of the minimum value + index = np.where(arr == min_val)[0][0] + + return index +Generated code for numpy.ma.MaskedArray.max + + +import numpy as np + +def max(arr): + # Create a masked array from the input array + masked_arr = np.ma.MaskedArray(arr) + + # Find the maximum value in the array + max_val = np.ma.max(masked_arr) + + # Return the maximum value + return max_val +Generated code for numpy.ma.MaskedArray.min + + +import numpy as np + +def masked_array_min(arr): + """ + Finds the minimum value of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to find the minimum value of. + + Returns + ------- + min_val : float + The minimum value of the array. + """ + # Get the data from the array + data = arr.data + + # Get the mask from the array + mask = arr.mask + + # Create a new array with only the unmasked values + unmasked_data = data[~mask] + + # Find the minimum value of the unmasked data + min_val = np.min(unmasked_data) + + return min_val +Generated code for numpy.ma.MaskedArray.ptp + + +import numpy as np + +def ptp(arr, axis=None): + """ + Return the range (maximum - minimum) of values along an axis. + + Parameters + ---------- + arr : array_like + Input array. + axis : int, optional + Axis along which to find the range of values. By default, + the range is computed over the entire array. + + Returns + ------- + ptp : ndarray + The range of values along the specified axis. + + See Also + -------- + numpy.ptp : equivalent function + """ + arr = np.ma.MaskedArray(arr) + return np.ptp(arr, axis) +Generated code for numpy.ma.argsort + + +import numpy as np + +# Create a numpy array +arr = np.array([[3, 2, 4], [5, 0, 1]]) + +# Use numpy.ma.argsort to sort the array +sorted_arr = np.ma.argsort(arr) + +# Print the sorted array +print(sorted_arr) + +# Output: +# [[1 2 0] +# [2 0 1]] +Generated code for numpy.ma.sort + + +import numpy as np + +# Create a numpy array +arr = np.array([[3, 2, 4], + [5, 0, 1]]) + +# Sort the array using numpy.ma.sort +sorted_arr = np.ma.sort(arr) + +# Print the sorted array +print(sorted_arr) + +# Output +# [[2 3 4] +# [0 1 5]] +Generated code for numpy.ma.MaskedArray.argsort + + +import numpy as np + +def argsort(arr): + # Create a copy of the array + arr_copy = np.ma.MaskedArray(arr) + # Sort the array + arr_copy.sort() + # Get the indices of the sorted array + indices = np.argsort(arr_copy) + # Return the indices + return indices +Generated code for numpy.ma.MaskedArray.sort + + +import numpy as np + +def masked_array_sort(arr): + """ + Sort a numpy.ma.MaskedArray in-place. + """ + # Get the indices of the unmasked elements + unmasked_indices = np.where(~arr.mask)[0] + # Sort the unmasked elements + arr[unmasked_indices] = np.sort(arr[unmasked_indices]) + # Return the sorted array + return arr +Generated code for numpy.ma.diag + + +import numpy as np + +def np_ma_diag(v, k=0): + """ + Return a 2-D array with the diagonal elements of v on the k-th diagonal. + + Parameters + ---------- + v : array_like + Input array. + k : int, optional + Diagonal in question. The default is 0. Use k>0 for diagonals above + the main diagonal, and k<0 for diagonals below the main diagonal. + + Returns + ------- + out : numpy.ma.MaskedArray + The 2-D output array. + """ + # Create a masked array from the input array + out = np.ma.masked_array(v) + + # Get the shape of the array + n = out.shape[0] + + # Create the mask + mask = np.zeros((n, n), dtype=bool) + mask[np.diag_indices(n, k=k)] = True + out.mask = mask + + return out +Generated code for numpy.ma.dot + + +import numpy as np + +def numpy_ma_dot(a, b): + """ + Computes the dot product of two arrays using numpy.ma.dot. + + Parameters + ---------- + a : array_like + First argument. + b : array_like + Second argument. + + Returns + ------- + dot : ndarray + Dot product of `a` and `b`. + """ + return np.ma.dot(a, b) +Generated code for numpy.ma.identity + + +import numpy as np + +def numpy_ma_identity(n): + return np.ma.masked_equal(np.eye(n), 0) + +# Usage +numpy_ma_identity(3) +# Output +# masked_array( +# data=[[1., 0., 0.], +# [0., 1., 0.], +# [0., 0., 1.]], +# mask=[[False, True, True], +# [ True, False, True], +# [ True, True, False]], +# fill_value=0.0, +# dtype=float32) +Generated code for numpy.ma.inner + + +import numpy as np + +def numpy_ma_inner(a, b): + """ + Computes the inner product of two arrays using numpy.ma.inner. + + Parameters + ---------- + a : array_like + First array. + b : array_like + Second array. + + Returns + ------- + out : float + Inner product of the two arrays. + """ + return np.ma.inner(a, b) +Generated code for numpy.ma.innerproduct + + +import numpy as np + +def innerproduct(a, b): + """ + Computes the inner product of two arrays using numpy.ma.innerproduct. + + Parameters + ---------- + a : array_like + The first array. + b : array_like + The second array. + + Returns + ------- + out : float + The inner product of the two arrays. + """ + return np.ma.innerproduct(a, b) +Generated code for numpy.ma.outer + + +import numpy as np + +def outer(a, b): + """ + Compute the outer product of two arrays. + + Parameters + ---------- + a : array_like + First input array. + b : array_like + Second input array. + + Returns + ------- + out : ndarray + The outer product of `a` and `b`. + + Examples + -------- + >>> a = np.array([1,2,3]) + >>> b = np.array([2,3,4]) + >>> np.ma.outer(a, b) + array([[ 2, 3, 4], + [ 4, 6, 8], + [ 6, 9, 12]]) + """ + return np.ma.outer(a, b) +Generated code for numpy.ma.outerproduct + + +import numpy as np + +def outerproduct(a, b): + """ + Computes the outer product of two vectors a and b. + + Parameters + ---------- + a : array_like + First input vector. + b : array_like + Second input vector. + + Returns + ------- + out : ndarray + Outer product of a and b. + """ + a = np.asarray(a) + b = np.asarray(b) + out = np.ma.empty((a.size, b.size)) + for i in range(a.size): + out[i, :] = a[i] * b + return out +Generated code for numpy.ma.trace + + +import numpy as np + +def trace(a): + """ + Return the sum along diagonals of the array. + + Parameters + ---------- + a : array_like + Input array, from which the diagonals are taken. + + Returns + ------- + trace : ndarray + The sum along the diagonals of the array. + + Examples + -------- + >>> a = np.arange(4).reshape(2,2) + >>> a + array([[0, 1], + [2, 3]]) + >>> np.ma.trace(a) + array([3]) + """ + return np.ma.sum(np.diagonal(a)) +Generated code for numpy.ma.transpose + + +import numpy as np + +def transpose(arr): + return np.ma.transpose(arr) + +# Example +arr = np.array([[1,2,3], [4,5,6], [7,8,9]]) +print(transpose(arr)) + +# Output +# [[1 4 7] +# [2 5 8] +# [3 6 9]] +Generated code for numpy.ma.MaskedArray.trace + + +import numpy as np + +def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None): + """ + Return the sum along diagonals of the array. + + Parameters + ---------- + a : array_like + Array from which the diagonals are taken. + offset : int, optional + Offset of the diagonal from the main diagonal. Can be both + positive and negative. Defaults to 0. + axis1, axis2 : int, optional + Axes to be used as the first and second axis of the 2-D sub-arrays + from which the diagonals should be taken. Defaults are the first + two axes of `a`. + dtype : dtype, optional + Type of the returned array and of the accumulator in which the + elements are summed. If `dtype` is not specified, it defaults + to the dtype of `a`, unless `a` has an integer dtype with a + precision less than that of the default platform integer. In + that case, the default platform integer is used. + out : ndarray, optional + Array into which the output is placed. Its type is preserved + and it must be of the right shape to hold the output. + + Returns + ------- + sum_along_diagonals : ndarray + If `out` is None, return a new array containing the sum along + the diagonals, otherwise a reference to `out` is returned. + + See Also + -------- + numpy.trace : equivalent function + + Examples + -------- + >>> a = np.arange(4).reshape(2,2) + >>> a + array([[0, 1], + [2, 3]]) + >>> np.ma.trace(a) + 3 + >>> np.ma.trace(a, offset=1) + 2 + >>> np.ma.trace(a, offset=-1) + 1 + + """ + a = np.ma.asanyarray(a) + if dtype is None: + dtype = np.find_common_type +Generated code for numpy.ma.MaskedArray.transpose + + +import numpy as np + +def transpose(arr): + """ + Transpose a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to be transposed. + + Returns + ------- + numpy.ma.MaskedArray + The transposed array. + """ + # Get the shape of the array + shape = arr.shape + # Create a new array with the transposed shape + new_arr = np.ma.masked_array(np.empty(shape[::-1]), mask=arr.mask) + # Iterate over the elements of the array + for i in range(shape[0]): + for j in range(shape[1]): + # Set the transposed element + new_arr[j, i] = arr[i, j] + # Return the transposed array + return new_arr +Generated code for numpy.ma.vander + + +import numpy as np + +def np_ma_vander(x, N=None, increasing=False): + """ + Return a Vandermonde matrix of given shape and order. + + Parameters + ---------- + x : array_like + 1-D array of values for the Vandermonde matrix. + N : int, optional + Order of the matrix. If `N` is not specified, then `N` is taken to be + ``len(x)``. + increasing : bool, optional + Order of the powers of the columns. If True, the powers increase from + left to right, if False (the default) they are in reverse order. + + Returns + ------- + out : ndarray + Vandermonde matrix. The shape of the returned matrix is ``(N, len(x))``. + + Examples + -------- + >>> x = np.array([1, 2, 3, 5]) + >>> np.ma.vander(x) + array([[ 1, 1, 1, 1], + [ 8, 4, 2, 1], + [ 27, 9, 3, 1], + [125, 25, 5, 1]]) + + >>> np.ma.vander(x, increasing=True) + array([[ 1, 1, 1, 1], + [ 1, 2, 4, 8], + [ 1, 3, 9, 27], + [ 1, 5, 25, 125]]) + + """ + x = np.asarray(x) + if N is None: + N = len(x) + + v = np.empty((N, len(x)), dtype=np.ma.masked_array) + tmp = x.reshape(x.shape[0], 1) + if increasing: + for i in range(N): + v[i] = np.ma.masked_array(tmp**(N - i - 1), mask=x.mask) + else: + for i in range(N): + v +Generated code for numpy.ma.polyfit + + +import numpy as np + +def polyfit(x, y, degree): + results = {} + + coeffs = np.ma.polyfit(x, y, degree) + + # Polynomial Coefficients + results['polynomial'] = coeffs.tolist() + + # r-squared + p = np.poly1d(coeffs) + # fit values, and mean + yhat = p(x) # or [p(z) for z in x] + ybar = np.sum(y)/len(y) # or sum(y)/len(y) + ssreg = np.sum((yhat-ybar)**2) # or sum([ (yihat - ybar)**2 for yihat in yhat]) + sstot = np.sum((y - ybar)**2) # or sum([ (yi - ybar)**2 for yi in y]) + results['determination'] = ssreg / sstot + + return results + +x = [1, 2, 3, 4, 5] +y = [2, 4, 5, 4, 5] + +print(polyfit(x, y, 2)) +Generated code for numpy.ma.around + + +import numpy as np + +def np_ma_around(x, decimals=0): + """ + Rounds the elements of an array to the given number of decimals. + + Parameters + ---------- + x : array_like + Input array. + decimals : int, optional + Number of decimals to round to (default is 0). + + Returns + ------- + out : ndarray + Output array. + """ + return np.ma.around(x, decimals) +Generated code for numpy.ma.clip + + +import numpy as np + +def clip(a, a_min, a_max, out=None): + """ + Clip (limit) the values in an array. + + Given an interval, values outside the interval are clipped to + the interval edges. For example, if an interval of ``[0, 1]`` + is specified, values smaller than 0 become 0, and values larger + than 1 become 1. + + Parameters + ---------- + a : array_like + Array containing elements to clip. + a_min : scalar or array_like + Minimum value. If `a_min` is scalar, the same `a_min` will + be applied to all elements of `a`. If `a_min` is an array, + it must be of the same shape as `a`. + a_max : scalar or array_like + Maximum value. If `a_max` is scalar, the same `a_max` will + be applied to all elements of `a`. If `a_max` is an array, + it must be of the same shape as `a`. + out : ndarray, optional + The results will be placed in this array. It may be the input + array for in-place clipping. `out` must be of the right shape + to hold the output. Its type is preserved. + + Returns + ------- + clipped_array : ndarray + An array with the elements of `a`, but clipped to the interval + ``[a_min, a_max]``. + + See Also + -------- + numpy.clip : equivalent function + + Examples + -------- + >>> a = np.arange(10) + >>> np.ma.clip(a, 1, 8) + array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8]) + + >>> a = np.arange(10) + >>> a_min = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + >>> np.ma.clip(a, a_min, 8) + array([1, 2, +Generated code for numpy.ma.round + + +import numpy as np + +def np_ma_round(x, decimals=0): + """ + Rounds the elements of an array to the given number of decimals. + Parameters + ---------- + x : array_like + Input array. + decimals : int, optional + Number of decimals to round to (default: 0). + Returns + ------- + rounded_array : numpy.ma.core.MaskedArray + Output array with rounded elements. + """ + return np.ma.masked_array(np.round(x, decimals=decimals)) +Generated code for numpy.ma.MaskedArray.clip + + +import numpy as np + +def clip(arr, min_val, max_val): + """ + Clip the values of a masked array to the given range. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to clip. + min_val : float + The minimum value to clip to. + max_val : float + The maximum value to clip to. + + Returns + ------- + numpy.ma.MaskedArray + The clipped array. + """ + return np.ma.MaskedArray(np.clip(arr.data, min_val, max_val), + mask=arr.mask) +Generated code for numpy.ma.MaskedArray.round + + +import numpy as np + +def round_masked_array(arr): + """ + Rounds the elements of a numpy.ma.MaskedArray. + + Parameters + ---------- + arr : numpy.ma.MaskedArray + The array to round. + + Returns + ------- + numpy.ma.MaskedArray + The rounded array. + """ + return np.ma.MaskedArray(np.round(arr.data), mask=arr.mask) +Generated code for numpy.ma.allequal + + +import numpy as np + +def np_ma_allequal(arr1, arr2): + """ + Checks if all elements of two numpy.ma arrays are equal. + + Parameters + ---------- + arr1 : numpy.ma array + First array to compare. + arr2 : numpy.ma array + Second array to compare. + + Returns + ------- + bool + True if all elements of the two arrays are equal, False otherwise. + """ + if arr1.shape != arr2.shape: + return False + else: + return np.all(arr1 == arr2) +Generated code for numpy.ma.allclose + + +import numpy as np + +def allclose(a, b, rtol=1e-5, atol=1e-8): + """ + Returns True if two arrays are element-wise equal within a tolerance. + + The tolerance values are positive, typically very small numbers. The + relative difference (`rtol` * abs(`b`)) and the absolute difference + `atol` are added together to compare against the absolute difference + between `a` and `b`. + + Parameters + ---------- + a, b : array_like + Input arrays to compare. + rtol : float + The relative tolerance parameter (see Notes). + atol : float + The absolute tolerance parameter (see Notes). + + Returns + ------- + allclose : bool + Returns True if the two arrays are equal within the given + tolerance; False otherwise. + + See Also + -------- + all, any, where + + Notes + ----- + If the following equation is element-wise True, then allclose returns + True. + + absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`)) + + The above equation is not symmetric in `a` and `b`, so that + `allclose(a, b)` might be different from `allclose(b, a)` in + some rare cases. + + Examples + -------- + >>> np.allclose([1e10,1e-7], [1.00001e10,1e-8]) + False + >>> np.allclose([1e10,1e-8], [1.00001e10,1e-9]) + True + >>> np.allclose([1e10,1e-8], [1.0001e10,1e-9]) + False + >>> np.allclose([1.0, np.nan], [1.0, np.nan]) + True + + """ + a = np.asanyarray(a) + b = np.asanyarray(b) + return np.all(np.less_equal(np.abs(a - b), +Generated code for numpy.ma.apply_along_axis + + +import numpy as np + +def apply_along_axis(func1d, axis, arr, *args, **kwargs): + """ + Apply a function to 1-D slices along the given axis. + Execute `func1d(a, *args, **kwargs)` where `func1d` operates + on 1-D arrays and `a` is a 1-D slice of `arr` along + the given axis. + Parameters + ---------- + func1d : function + This function should accept 1-D arrays. It is applied to 1-D + slices of `arr` along the specified axis. + axis : int + Axis along which `arr` is sliced. + arr : ndarray + Array over which the function is applied. + args : tuple + Arguments passed to `func1d`. + kwargs : dict + Keyword arguments passed to `func1d`. + Returns + ------- + apply_along_axis : ndarray + The output array. + See Also + -------- + numpy.apply_over_axes + Examples + -------- + >>> arr = np.arange(24).reshape(2,3,4) + >>> np.ma.apply_along_axis(np.mean, 0, arr) + array([[ 6., 7., 8., 9.], + [18., 19., 20., 21.]]) + """ + arr = np.asanyarray(arr) + nd = arr.ndim + ind = [0] * nd + indlist = list(range(nd)) + indlist.remove(axis) + ind = indlist + [axis] + arr = arr.transpose(ind).copy() + outshape = list(arr.shape) + outshape[-1] = 1 + out = np.empty(outshape, dtype=arr.dtype) + for i, a in enumerate(arr): + out[..., i] = func1d(a, *args, **kwargs) + return out.transpose(indlist + [nd - 1]) +Generated code for numpy.ma.apply_over_axes + + +import numpy as np + +def apply_over_axes(func, a, axes): + # Create an iterator object + it = np.nditer(a, flags=['multi_index'], op_flags=['readwrite']) + # Iterate over all elements of the array + while not it.finished: + # Get the current element and its index + element = it[0] + index = it.multi_index + # Apply the function to the element + it[0] = func(element, index, axes) + # Move to the next element + it.iternext() + return a + +# Example usage +a = np.arange(24).reshape(2, 3, 4) +axes = (1, 2) + +def func(element, index, axes): + return np.sum(element[axes]) + +result = apply_over_axes(func, a, axes) +print(result) + +# Output: +# [[[ 6 8 10 12] +# [22 24 26 28]] +# +# [[38 40 42 44] +# [54 56 58 60]]] +Generated code for numpy.ma.arange + + +import numpy as np + +# Create a numpy array of size 10 +arr = np.arange(10) + +# Create a masked array from the array +masked_arr = np.ma.masked_array(arr) + +# Create a range of values from the masked array +result = np.ma.arange(masked_arr) + +# Print the result +print(result) +Generated code for numpy.ma.choose + + +import numpy as np + +def choose(condlist, choicelist, default=0): + """ + Return an array drawn from elements in choicelist, depending on conditions. + Parameters + ---------- + condlist : list of bool ndarrays + Each boolean array corresponds to a choice array in choicelist. + choicelist : sequence of ndarrays + Choice arrays from which to draw the output array. + default : scalar, optional + Value to use when all conditions are False. + Returns + ------- + output : ndarray + The output array has the same shape as that of the boolean + arrays in condlist. + """ + output = np.ma.array(default * np.ones(condlist[0].shape)) + for cond, choice in zip(condlist, choicelist): + output = np.where(cond, choice, output) + return output +Generated code for numpy.ma.ediff1d + + +import numpy as np + +def ediff1d(arr): + """ + Calculates the first order difference of an array. + + Parameters + ---------- + arr : array_like + Input array. + + Returns + ------- + ediff1d : ndarray + The differences. The length of the output is one less than the + length of the input. + + Examples + -------- + >>> x = np.array([1, 2, 4, 7, 0]) + >>> np.ma.ediff1d(x) + array([ 1, 2, 3, -7]) + """ + return np.ma.diff(arr) +Generated code for numpy.ma.indices + + +import numpy as np + +def indices(dimensions, dtype=np.int): + """Return an array representing a grid of given dimensions. + + Parameters + ---------- + dimensions : sequence of ints + The dimensions of the grid. + dtype : data-type, optional + The desired data-type for the array. + + Returns + ------- + grid : ndarray + The array of grid coordinates, + ``grid.shape = (len(dimensions),) + tuple(dimensions)``. + + Examples + -------- + >>> np.ma.indices((2, 3)) + array([[[0, 0, 0], + [1, 1, 1]], + [[0, 1, 2], + [0, 1, 2]]]) + """ + return np.indices(dimensions, dtype=dtype) +Generated code for numpy.ma.where + + +import numpy as np + +# Create a numpy array +arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Use np.ma.where to find the indices of elements that meet a certain condition +indices = np.ma.where(arr > 5) + +# Print the indices +print(indices) + +# Output: (array([2, 2]), array([0, 1])) +Generated code for numpy.sin + + +import numpy as np + +# Calculate the sine of x +x = np.pi/4 +y = np.sin(x) + +print(y) +Generated code for numpy.cos + + +import numpy as np + +# Calculate cosine of an angle +angle = np.pi/4 +cos_angle = np.cos(angle) + +print("Cosine of angle is:", cos_angle) +Generated code for numpy.tan + + +import numpy as np + +def tan(x): + return np.tan(x) + +# Usage +x = np.pi/4 +print(tan(x)) +Generated code for numpy.arcsin + + +import numpy as np + +def arcsin(x): + return np.arcsin(x) + +x = 0.5 +print(arcsin(x)) +Generated code for numpy.arccos + + +import numpy as np + +def arccos(x): + return np.arccos(x) + +x = 0.5 +print(arccos(x)) +Generated code for numpy.arctan + + +import numpy as np + +def arctan(x): + return np.arctan(x) + +x = 1.0 +print(arctan(x)) +Generated code for numpy.hypot + + +import numpy as np + +def numpy_hypot(x, y): + return np.sqrt(x**2 + y**2) + +# Usage +x = 3 +y = 4 +print(numpy_hypot(x, y)) # Output: 5.0 +Generated code for numpy.arctan2 + + +import numpy as np + +def arctan2(y, x): + """ + Computes the element-wise arc tangent of y/x choosing the quadrant correctly. + Parameters + ---------- + y : array_like + The y-coordinates of the points. + x : array_like + The x-coordinates of the points. + Returns + ------- + angle : ndarray + The angle of the vector (y, x) in radians, shaped as y. + """ + angle = np.arctan2(y, x) + return angle +Generated code for numpy.degrees + + +import numpy as np + +def degrees(x): + return x * (180/np.pi) + +# Usage +radians = np.array([0, np.pi/2, np.pi]) +degrees(radians) +# array([ 0., 90., 180.]) +Generated code for numpy.radians + + +def numpy_radians(degrees): + return degrees * np.pi / 180.0 +Generated code for numpy.unwrap + + +import numpy as np + +def unwrap(p, discont=np.pi, axis=-1): + """ + Unwrap radian phase p by changing absolute jumps greater than discont + to their 2*pi complement along the given axis. + """ + p = np.asarray(p) + ndim = p.ndim + if ndim == 0: + return p + if axis < 0: + axis += ndim + # Find differences along the axis + dp = np.diff(p, axis=axis) + # Find jumps that are bigger than the threshold + jump_mask = np.abs(dp) > discont + # Check the sign of the jump and subtract or add 2*pi from the jump + sign_mask = np.sign(dp[jump_mask]) + dp[jump_mask] -= sign_mask * 2 * np.pi + # Integrate the differences to get the absolute phase along the axis + p = np.cumsum(dp, axis=axis) + # Add the initial value + p = np.concatenate((np.expand_dims(p[0], axis=axis), p), axis=axis) + return p +Generated code for numpy.deg2rad + + +import numpy as np + +def deg2rad(degrees): + return degrees * np.pi / 180.0 + +# Usage +radians = deg2rad(45) +print(radians) # 0.7853981633974483 +Generated code for numpy.rad2deg + + +import numpy as np + +def rad2deg(rad): + return np.rad2deg(rad) + +rad = np.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi]) +deg = rad2deg(rad) + +print(deg) +# Output: [ 0. 90. 180. 270. 360.] +Generated code for numpy.sinh + + +import numpy as np + +# Calculate sinh of an array +arr = np.array([0, np.pi/2, np.pi]) +sinh_arr = np.sinh(arr) + +# Print the sinh of the array +print(sinh_arr) +Generated code for numpy.cosh + + +import numpy as np + +def cosh(x): + return (np.exp(x) + np.exp(-x)) / 2 + +# Test +x = np.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi]) +print(cosh(x)) +# Output: [1. 1.54308063 3.76219569 11.59195327 74.20994862] +Generated code for numpy.tanh + + +import numpy as np + +def tanh(x): + return np.tanh(x) +Generated code for numpy.arcsinh + + +import numpy as np + +def arcsinh(x): + return np.log(x + np.sqrt(x**2 + 1)) + +# Usage +x = np.array([1, 2, 3]) +print(arcsinh(x)) +Generated code for numpy.arccosh + + +import numpy as np + +def arccosh(x): + return np.log(x + np.sqrt(x**2 - 1)) + +# Usage +x = 2 +print(arccosh(x)) # 1.3169578969248166 +Generated code for numpy.arctanh + + +import numpy as np + +def arctanh(x): + return np.log((1+x)/(1-x))/2 + +# Usage +x = 0.5 +print(arctanh(x)) # 0.5493061443340549 +Generated code for numpy.around + + +import numpy as np + +# Round to the nearest integer +rounded_array = np.around(np.array([1.2, 3.4, 5.6, 7.8, 9.0]), 0) + +# Round to two decimal places +rounded_array = np.around(np.array([1.2, 3.4, 5.6, 7.8, 9.0]), 2) + +# Round to the nearest multiple of 10 +rounded_array = np.around(np.array([1.2, 3.4, 5.6, 7.8, 9.0]), -1) +Generated code for numpy.rint + + +import numpy as np + +# Create an array +arr = np.array([1.1, 2.2, 3.3, 4.4, 5.5]) + +# Round off the array elements +rounded_arr = np.rint(arr) + +# Print the rounded array +print(rounded_arr) +Generated code for numpy.fix + + +import numpy as np + +def fix(x): + """ + Rounds a numpy array to the nearest integer. + + Parameters + ---------- + x : numpy array + The array to be rounded. + + Returns + ------- + numpy array + The rounded array. + """ + return np.round(x).astype(int) +Generated code for numpy.floor + + +import numpy as np + +# Create an array +arr = np.array([1.2, 2.3, 3.4, 4.5]) + +# Use numpy.floor() to round down the elements of the array +np.floor(arr) + +# Output: array([1., 2., 3., 4.]) +Generated code for numpy.ceil + + +import numpy as np + +def ceil(x): + return np.ceil(x) + +# Usage +x = 3.14 +print(ceil(x)) # 4.0 +Generated code for numpy.trunc + + +import numpy as np + +def trunc(x): + return np.trunc(x) + +x = np.array([1.2, 3.4, 5.6, 7.8]) + +print(trunc(x)) +Generated code for numpy.prod + + +import numpy as np + +def prod(arr): + + # Initialize result + prod = 1 + + # Multiply elements one by one + for i in range(0, len(arr)): + prod = prod * arr[i] + + return prod + +# Driver Code +arr = [1, 2, 3, 4] +print(prod(arr)) +Generated code for numpy.sum + + +import numpy as np + +# Create a numpy array +arr = np.array([1, 2, 3, 4]) + +# Calculate sum of all elements +total = np.sum(arr) + +# Print the result +print("Sum of all elements in the array:", total) +Generated code for numpy.nanprod + + +import numpy as np + +def nanprod(arr): + # Replace NaN with 1 + arr[np.isnan(arr)] = 1 + # Calculate product + prod = np.prod(arr) + return prod + +# Driver code +arr = np.array([1, 2, np.nan, 3, 4]) +print(nanprod(arr)) +Generated code for numpy.nansum + + +import numpy as np + +def nan_sum(arr): + return np.nansum(arr, axis=None) + +arr = np.array([[1, np.nan, 2], [3, 4, np.nan]]) + +print(nan_sum(arr)) # 10.0 +Generated code for numpy.cumprod + + +import numpy as np + +def cumprod(arr): + prod = 1 + cumprod_arr = [] + + # Iterate over the array + for i in range(0, len(arr)): + prod = prod * arr[i] + cumprod_arr.append(prod) + + return np.array(cumprod_arr) + +# Driver code +arr = [1, 2, 3, 4] +print(cumprod(arr)) +Generated code for numpy.cumsum + + +import numpy as np + +# Create an array +arr = np.array([1, 2, 3, 4]) + +# Calculate cumulative sum +result = np.cumsum(arr) + +# Print the result +print(result) +Generated code for numpy.nancumprod + + +import numpy as np + +def nanprod(arr): + result = np.cumprod(arr, dtype=float) + mask = np.isnan(arr) + result[mask] = np.nan + return result + +# Driver Code +arr = np.array([1, 2, np.nan, 3, 4]) +print(nanprod(arr)) +Generated code for numpy.nancumsum + + +import numpy as np + +def nan_cumsum(arr): + # Create a copy of the array + arr_copy = np.copy(arr) + + # Replace nan with 0 in the copy + arr_copy[np.isnan(arr_copy)] = 0 + + # Calculate cumulative sum ignoring nan + return np.cumsum(arr_copy) + +# Driver code +arr = np.array([1, np.nan, 2, 3, np.nan, 4]) +print(nan_cumsum(arr)) +Generated code for numpy.diff + + +import numpy as np + +def numpy_diff(arr): + return np.diff(arr) + +#Example +arr = np.array([1,2,3,4,5]) +print(numpy_diff(arr)) +# Output: [1 1 1 1] +Generated code for numpy.ediff1d + + +import numpy as np + +def ediff1d(arr): + """ + Calculates the first order difference of an array. + + Parameters + ---------- + arr : array_like + Input array. + + Returns + ------- + ediff1d : ndarray + The differences. The length of the array is one less than the + input array. + + Examples + -------- + >>> x = np.array([1, 2, 4, 7, 0]) + >>> np.ediff1d(x) + array([ 1, 2, 3, -7]) + """ + return np.diff(arr) +Generated code for numpy.gradient diff --git a/output2.txt b/output2.txt new file mode 100644 index 000000000..1fe3ba7ea --- /dev/null +++ b/output2.txt @@ -0,0 +1,5961 @@ +Generated code for sklearn.cluster.cluster_optics_dbscan + + +import numpy as np +from sklearn.cluster import OPTICS, cluster_optics_dbscan + +# Generate sample data +np.random.seed(0) +X = np.random.rand(12,2) + +# Compute DBSCAN +clust = OPTICS(min_samples=2, xi=.05, min_cluster_size=0.05) + +# Run the fit +clust.fit(X) + +# Extract labels +labels_050 = cluster_optics_dbscan(reachability=clust.reachability_, + core_distances=clust.core_distances_, + ordering=clust.ordering_, eps=0.5) + +labels_200 = cluster_optics_dbscan(reachability=clust.reachability_, + core_distances=clust.core_distances_, + ordering=clust.ordering_, eps=2) + +space = np.arange(len(X)) +reachability = clust.reachability_[clust.ordering_] +labels = clust.labels_[clust.ordering_] + +print(labels_050) +print(labels_200) +Generated code for sklearn.cluster.cluster_optics_xi + + +from sklearn.cluster import OPTICS + +# Create an instance of the OPTICS clustering algorithm +clustering = OPTICS(min_samples=50, xi=0.05, min_cluster_size=0.05) + +# Fit the model to the data +clustering.fit(X) + +# Extract the labels for each point in the data +labels = clustering.labels_ + +# Visualize the results +plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis') +plt.show() +Generated code for sklearn.cluster.compute_optics_graph + + +import numpy as np +from sklearn.cluster import OPTICS + +# Create a sample dataset +X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11]]) + +# Create an instance of OPTICS +clust = OPTICS(min_samples=2, xi=.05, min_cluster_size=.05) + +# Compute the OPTICS graph +clust.fit(X) + +# Compute the OPTICS graph +graph = clust.compute_optics_graph() +Generated code for sklearn.cluster.dbscan + + +# Import necessary libraries +import numpy as np +from sklearn.cluster import DBSCAN + +# Create a sample dataset +X = np.array([[1, 2], [2, 2], [2, 3], + [8, 7], [8, 8], [25, 80]]) + +# Create and fit the model +dbscan = DBSCAN(eps=3, min_samples=2).fit(X) + +# Get the labels +labels = dbscan.labels_ + +# Print the labels +print(labels) +Generated code for sklearn.cluster.estimate_bandwidth + + +from sklearn.cluster import estimate_bandwidth + +# Generate sample data +X = np.array([[1, 2], [2, 1], [3, 4], [4, 3], [5, 6], [6, 5]]) + +# Estimate the bandwidth of X +bandwidth = estimate_bandwidth(X, quantile=0.5, n_samples=len(X)) + +print("Estimated bandwidth: %f" % bandwidth) +Generated code for sklearn.cluster.k_means + + +from sklearn.cluster import KMeans + +# Create a KMeans object with 3 clusters +kmeans = KMeans(n_clusters=3) + +# Fit the model to the data +kmeans.fit(X) + +# Get the cluster labels +labels = kmeans.labels_ + +# Get the cluster centers +cluster_centers = kmeans.cluster_centers_ +Generated code for sklearn.cluster.kmeans_plusplus + + +from sklearn.cluster import KMeans + +# Create a KMeans instance with 3 clusters: kmeans +kmeans = KMeans(n_clusters=3, init='k-means++') + +# Fit the model to the data +kmeans.fit(X) + +# Predict the clusters +predicted_clusters = kmeans.predict(X) +Generated code for sklearn.cluster.mean_shift + + +import numpy as np +from sklearn.cluster import MeanShift + +# Create a random dataset +X = np.random.rand(50,2) + +# Create the MeanShift object +ms = MeanShift() + +# Fit the data +ms.fit(X) + +# Get the labels +labels = ms.labels_ + +# Get the cluster centers +cluster_centers = ms.cluster_centers_ + +# Print the results +print("Labels:", labels) +print("Cluster Centers:", cluster_centers) +Generated code for sklearn.cluster.spectral_clustering + + +from sklearn.cluster import SpectralClustering + +# Create an instance of SpectralClustering +clustering = SpectralClustering(n_clusters=3, affinity='nearest_neighbors', assign_labels='kmeans') + +# Fit the model to the data +clustering.fit(X) + +# Predict the clusters +clusters = clustering.predict(X) +Generated code for sklearn.cluster.ward_tree + + +from sklearn.cluster import WardTree + +# Create an instance of the WardTree clustering model +model = WardTree() + +# Fit the model to the data +model.fit(X) + +# Predict the clusters for each data point +clusters = model.predict(X) + +# Print the cluster labels for each data point +print(clusters) +Generated code for sklearn.compose.ColumnTransformer + + +from sklearn.compose import ColumnTransformer +from sklearn.preprocessing import StandardScaler + +# Create a ColumnTransformer object +column_transformer = ColumnTransformer( + transformers=[ + ('scaler', StandardScaler(), [0,1,2]) + ], + remainder='passthrough' +) + +# Fit the ColumnTransformer object +column_transformer.fit(X) + +# Transform the data +X_transformed = column_transformer.transform(X) +Generated code for sklearn.compose.TransformedTargetRegressor + + +from sklearn.compose import TransformedTargetRegressor + +# Create the TransformedTargetRegressor +regressor = TransformedTargetRegressor( + regressor=LinearRegression(), + transformer=MinMaxScaler()) + +# Fit the model +regressor.fit(X, y) + +# Make predictions +predictions = regressor.predict(X) +Generated code for sklearn.compose.make_column_transformer + + +from sklearn.compose import make_column_transformer +from sklearn.preprocessing import StandardScaler + +# Create a list of tuples containing the name of the transformer and the transformer class +transformers = [('scaler', StandardScaler())] + +# Create the column transformer +column_trans = make_column_transformer(transformers) +Generated code for sklearn.compose.make_column_selector + + +def make_column_selector(column_names): + """ + Create a function that takes a list of column names and returns a function + that can be used to select those columns from a dataframe. + + Parameters + ---------- + column_names : list + List of column names to select. + + Returns + ------- + selector : function + Function that takes a dataframe and returns a dataframe with only the + specified columns. + """ + def selector(df): + return df[column_names] + return selector +Generated code for sklearn.covariance.EmpiricalCovariance + + +import numpy as np +from sklearn.covariance import EmpiricalCovariance + +# Generate some random data +data = np.random.rand(100, 5) + +# Create an instance of the EmpiricalCovariance estimator +estimator = EmpiricalCovariance() + +# Fit the estimator to the data +estimator.fit(data) + +# Get the estimated covariance matrix +covariance_matrix = estimator.covariance_ +Generated code for sklearn.covariance.EllipticEnvelope + + +from sklearn.covariance import EllipticEnvelope + +# Create an instance of the EllipticEnvelope class +envelope = EllipticEnvelope() + +# Fit the model to the data +envelope.fit(X) + +# Predict the labels for the data +labels = envelope.predict(X) + +# Get the scores for the data +scores = envelope.decision_function(X) +Generated code for sklearn.covariance.GraphicalLasso + + +from sklearn.covariance import GraphicalLasso + +# Create an instance of GraphicalLasso +gl = GraphicalLasso() + +# Fit the model to the data +gl.fit(X) + +# Get the covariance matrix +cov_matrix = gl.covariance_ + +# Get the precision matrix +precision_matrix = gl.precision_ +Generated code for sklearn.covariance.GraphicalLassoCV + + +from sklearn.covariance import GraphicalLassoCV + +# Create an instance of GraphicalLassoCV +glcv = GraphicalLassoCV() + +# Fit the model to the data +glcv.fit(X) + +# Get the estimated covariance matrix +cov_matrix = glcv.covariance_ + +# Get the estimated precision matrix +precision_matrix = glcv.precision_ +Generated code for sklearn.covariance.LedoitWolf + + +from sklearn.covariance import LedoitWolf + +# Create an instance of the LedoitWolf estimator +ledoit_wolf = LedoitWolf() + +# Fit the estimator to the data +ledoit_wolf.fit(X) + +# Get the estimated covariance matrix +cov_matrix = ledoit_wolf.covariance_ + +# Get the estimated shrinkage parameter +shrinkage_param = ledoit_wolf.shrinkage_ +Generated code for sklearn.covariance.MinCovDet + + +from sklearn.covariance import MinCovDet + +# Create an instance of MinCovDet +min_cov_det = MinCovDet() + +# Fit the model to the data +min_cov_det.fit(X) + +# Get the estimated covariance matrix +cov_matrix = min_cov_det.covariance_ + +# Get the estimated robust location +location = min_cov_det.location_ + +# Get the estimated robust scale +scale = min_cov_det.scale_ +Generated code for sklearn.covariance.OAS + + +import numpy as np +from sklearn.covariance import OAS + +# Generate some random data +X = np.random.rand(100, 5) + +# Create an instance of the OAS estimator +oas = OAS() + +# Fit the data +oas.fit(X) + +# Get the covariance matrix +cov_matrix = oas.covariance_ +Generated code for sklearn.covariance.ShrunkCovariance + + +from sklearn.covariance import ShrunkCovariance + +# Create an instance of the ShrunkCovariance estimator +shrunk_cov = ShrunkCovariance() + +# Fit the estimator to the data +shrunk_cov.fit(X) + +# Get the shrunken covariance matrix +shrunk_cov_matrix = shrunk_cov.covariance_ + +# Get the shrunken precision matrix +shrunk_precision_matrix = shrunk_cov.precision_ +Generated code for sklearn.covariance.empirical_covariance + + +import numpy as np +from sklearn.covariance import empirical_covariance + +# Generate random data +data = np.random.rand(10, 5) + +# Calculate the empirical covariance +cov = empirical_covariance(data) + +# Print the covariance matrix +print(cov) +Generated code for sklearn.covariance.graphical_lasso + + +import numpy as np +from sklearn.covariance import graphical_lasso + +# Generate some random data +n_samples, n_features = 10, 5 +np.random.seed(0) +X = np.random.randn(n_samples, n_features) + +# Fit the GraphicalLasso model +model = graphical_lasso(X, alpha=0.01, mode='cd') + +# Get the precision matrix +precision_matrix = model.precision_ +Generated code for sklearn.covariance.ledoit_wolf + + +import numpy as np +from sklearn.covariance import LedoitWolf + +# Generate some random data +X = np.random.rand(100, 5) + +# Create the LedoitWolf estimator +estimator = LedoitWolf() + +# Fit the estimator to the data +estimator.fit(X) + +# Get the estimated covariance matrix +cov_matrix = estimator.covariance_ +Generated code for sklearn.covariance.oas + + +import numpy as np +from sklearn.covariance import OAS + +# Generate some random data +X = np.random.rand(100, 5) + +# Create an instance of the OAS estimator +oas = OAS() + +# Fit the data +oas.fit(X) + +# Get the covariance matrix +cov_matrix = oas.covariance_ +Generated code for sklearn.covariance.shrunk_covariance + + +import numpy as np +from sklearn.covariance import shrunk_covariance + +# Generate random data +X = np.random.rand(10, 5) + +# Compute the shrunk covariance +shrunk_cov = shrunk_covariance(X) + +# Print the result +print(shrunk_cov) +Generated code for sklearn.cross_decomposition.CCA + + +from sklearn.cross_decomposition import CCA + +# Create the CCA object +cca = CCA(n_components=2) + +# Fit the CCA object to the data +cca.fit(X, Y) + +# Transform the data +X_c, Y_c = cca.transform(X, Y) + +# Get the correlation between the transformed data +corr = np.corrcoef(X_c, Y_c)[0, 1] + +# Print the correlation +print("Correlation: {:.2f}".format(corr)) +Generated code for sklearn.cross_decomposition.PLSCanonical + + +from sklearn.cross_decomposition import PLSCanonical + +# Create an instance of the PLSCanonical class +pls_canonical = PLSCanonical() + +# Fit the model to the data +pls_canonical.fit(X, y) + +# Make predictions +y_pred = pls_canonical.predict(X) + +# Get the coefficients of the model +coef = pls_canonical.coef_ + +# Get the score of the model +score = pls_canonical.score(X, y) +Generated code for sklearn.cross_decomposition.PLSRegression + + +from sklearn.cross_decomposition import PLSRegression + +# Create an instance of the PLSRegression class +pls_reg = PLSRegression() + +# Fit the model to the training data +pls_reg.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = pls_reg.predict(X_test) + +# Calculate the mean squared error +mse = mean_squared_error(y_test, y_pred) + +# Print the mean squared error +print(mse) +Generated code for sklearn.cross_decomposition.PLSSVD + + +from sklearn.cross_decomposition import PLSSVD + +# Create PLSSVD object +pls_svd = PLSSVD(n_components=2) + +# Fit the model +pls_svd.fit(X, y) + +# Transform the data +X_transformed = pls_svd.transform(X) + +# Get the explained variance ratio +explained_variance_ratio = pls_svd.explained_variance_ratio_ + +# Get the singular values +singular_values = pls_svd.singular_values_ +Generated code for sklearn.datasets.clear_data_home + + +#importing necessary libraries +import pandas as pd +from sklearn.datasets import clear_data_home + +#loading the dataset +data = clear_data_home() + +#creating a dataframe +df = pd.DataFrame(data.data, columns=data.feature_names) + +#viewing the dataframe +df.head() +Generated code for sklearn.datasets.dump_svmlight_file + + +import numpy as np +from sklearn.datasets import dump_svmlight_file + +# Generate some random data +X = np.random.rand(100, 10) +y = np.random.randint(2, size=100) + +# Dump the data to a file +dump_svmlight_file(X, y, 'data.svmlight') +Generated code for sklearn.datasets.fetch_20newsgroups + + +from sklearn.datasets import fetch_20newsgroups + +# Fetch the 20 newsgroups dataset +newsgroups_data = fetch_20newsgroups(subset='all', shuffle=True, random_state=42) + +# Print the categories +print(newsgroups_data.target_names) + +# Print the number of documents +print(len(newsgroups_data.data)) + +# Print the first document +print(newsgroups_data.data[0]) +Generated code for sklearn.datasets.fetch_20newsgroups_vectorized + + +from sklearn.datasets import fetch_20newsgroups_vectorized + +# Load the data +newsgroups_data = fetch_20newsgroups_vectorized() + +# Get the data and target +X = newsgroups_data.data +y = newsgroups_data.target + +# Split the data into training and test sets +from sklearn.model_selection import train_test_split +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Train a classifier +from sklearn.naive_bayes import MultinomialNB +clf = MultinomialNB() +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print("Accuracy: {:.2f}%".format(accuracy*100)) +Generated code for sklearn.datasets.fetch_california_housing + + +import numpy as np +from sklearn.datasets import fetch_california_housing + +# Fetch the California housing dataset +cal_housing = fetch_california_housing() + +# Get the feature names +features = cal_housing.feature_names + +# Get the data +X = cal_housing.data + +# Get the target values +y = cal_housing.target + +# Print the shape of the data +print(X.shape) +print(y.shape) + +# Print the feature names +print(features) +Generated code for sklearn.datasets.fetch_covtype + + +from sklearn.datasets import fetch_covtype + +# Load the covtype dataset +covtype_data = fetch_covtype() + +# Get the feature names +feature_names = covtype_data.feature_names + +# Get the target names +target_names = covtype_data.target_names + +# Get the data +X = covtype_data.data +y = covtype_data.target + +# Print the shape of the data +print(X.shape) +print(y.shape) + +# Print the feature names +print(feature_names) + +# Print the target names +print(target_names) +Generated code for sklearn.datasets.fetch_kddcup99 + + +from sklearn.datasets import fetch_kddcup99 + +# Load the KDD Cup 99 dataset +kdd_data = fetch_kddcup99() + +# Get the feature names +feature_names = kdd_data.feature_names + +# Get the target names +target_names = kdd_data.target_names + +# Get the data +X = kdd_data.data +y = kdd_data.target + +# Print the shape of the data +print(X.shape) +print(y.shape) +Generated code for sklearn.datasets.fetch_lfw_pairs + + +import numpy as np +from sklearn.datasets import fetch_lfw_pairs + +# Load the data +lfw_pairs = fetch_lfw_pairs(resize=0.5) + +# Get the data and target +X = lfw_pairs.data +y = lfw_pairs.target + +# Split the data into train and test sets +from sklearn.model_selection import train_test_split +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Create a model +from sklearn.svm import SVC +model = SVC(kernel='linear') + +# Fit the model +model.fit(X_train, y_train) + +# Make predictions +y_pred = model.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print('Accuracy:', accuracy) +Generated code for sklearn.datasets.fetch_lfw_people + + +from sklearn.datasets import fetch_lfw_people + +lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) + +# introspect the images arrays to find the shapes (for plotting) +n_samples, h, w = lfw_people.images.shape + +# for machine learning we use the 2 data directly (as relative pixel +# positions info is ignored by this model) +X = lfw_people.data +n_features = X.shape[1] + +# the label to predict is the id of the person +y = lfw_people.target +target_names = lfw_people.target_names +n_classes = target_names.shape[0] +Generated code for sklearn.datasets.fetch_olivetti_faces + + +from sklearn.datasets import fetch_olivetti_faces + +# Load the Olivetti faces dataset +dataset = fetch_olivetti_faces() + +# Get the data and target +data = dataset.data +target = dataset.target + +# Print the shape of the data and target +print("Data shape:", data.shape) +print("Target shape:", target.shape) + +# Print the first 10 targets +print("First 10 targets:", target[:10]) +Generated code for sklearn.datasets.fetch_openml + + +import numpy as np +from sklearn.datasets import fetch_openml + +# Fetch the openml dataset +X, y = fetch_openml('openml_dataset', version=1, return_X_y=True) + +# Split the dataset into training and test sets +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Fit the model +model = LogisticRegression() +model.fit(X_train, y_train) + +# Make predictions +y_pred = model.predict(X_test) + +# Evaluate the model +accuracy = accuracy_score(y_test, y_pred) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.datasets.fetch_rcv1 + + +import numpy as np +from sklearn.datasets import fetch_rcv1 + +# Fetch the RCV1 dataset +rcv1 = fetch_rcv1() + +# Get the data and target +X = rcv1.data +y = rcv1.target + +# Split the data into training and test sets +from sklearn.model_selection import train_test_split +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Fit a logistic regression model +from sklearn.linear_model import LogisticRegression +clf = LogisticRegression(random_state=0).fit(X_train, y_train) + +# Make predictions on the test set +y_pred = clf.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.datasets.fetch_species_distributions + + +import numpy as np +from sklearn.datasets import fetch_species_distributions + +# Load the data +data = fetch_species_distributions() + +# Extract the features and target +X = data.data +y = data.target + +# Split the data into training and test sets +from sklearn.model_selection import train_test_split +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Fit a model +from sklearn.ensemble import RandomForestClassifier +clf = RandomForestClassifier(n_estimators=100, random_state=42) +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print('Accuracy:', accuracy) +Generated code for sklearn.datasets.get_data_home + + +import os +from sklearn.datasets import get_data_home + +data_home = get_data_home() +if not os.path.exists(data_home): + os.makedirs(data_home) + +print(data_home) +Generated code for sklearn.datasets.load_breast_cancer + + +from sklearn.datasets import load_breast_cancer + +# Load the breast cancer dataset +cancer = load_breast_cancer() + +# Print the data +print(cancer.data) + +# Print the target +print(cancer.target) + +# Print the feature names +print(cancer.feature_names) + +# Print the target names +print(cancer.target_names) +Generated code for sklearn.datasets.load_diabetes + + +from sklearn.datasets import load_diabetes + +# Load the diabetes dataset +diabetes_data = load_diabetes() + +# Get the feature names +feature_names = diabetes_data.feature_names + +# Get the data +data = diabetes_data.data + +# Get the target +target = diabetes_data.target + +# Print the feature names +print(feature_names) + +# Print the first 5 rows of the data +print(data[:5]) + +# Print the target values +print(target) +Generated code for sklearn.datasets.load_digits + + +from sklearn.datasets import load_digits + +# Load the digits dataset +digits = load_digits() + +# Get the data and target values +X = digits.data +y = digits.target + +# Print the shape of the data +print(X.shape) +print(y.shape) +Generated code for sklearn.datasets.load_files + + +from sklearn.datasets import load_files + +# Load the files +data = load_files('path/to/files') + +# Get the data +X = data.data +y = data.target + +# Get the labels +labels = data.target_names + +# Print the data +print(X) +print(y) +print(labels) +Generated code for sklearn.datasets.load_iris + + +from sklearn.datasets import load_iris + +# Load the Iris dataset +iris = load_iris() + +# Get the features and labels +X = iris.data +y = iris.target + +# Split the data into training and testing sets +from sklearn.model_selection import train_test_split +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Train a model +from sklearn.ensemble import RandomForestClassifier +clf = RandomForestClassifier(n_estimators=100, random_state=42) +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print("Accuracy: {:.2f}%".format(accuracy*100)) +Generated code for sklearn.datasets.load_linnerud + + +import numpy as np +from sklearn.datasets import load_linnerud + +# Load the linnerud dataset +linnerud_data = load_linnerud() + +# Extract the features and target +X = linnerud_data.data +y = linnerud_data.target + +# Print the shape of the data +print("Shape of X:", X.shape) +print("Shape of y:", y.shape) + +# Print the first 5 rows of the data +print("First 5 rows of X: \n", X[:5]) +print("First 5 rows of y: \n", y[:5]) +Generated code for sklearn.datasets.load_sample_image + + +from sklearn.datasets import load_sample_image + +# Load the sample image +china = load_sample_image("china.jpg") + +# Print the shape of the image +print(china.shape) + +# Show the image +import matplotlib.pyplot as plt +plt.imshow(china) +plt.show() +Generated code for sklearn.datasets.load_sample_images + + +from sklearn.datasets import load_sample_images + +# Load sample images +dataset = load_sample_images() + +# Get the images +images = dataset.images + +# Get the target +target = dataset.target + +# Print the shape of the images +print(images.shape) + +# Print the shape of the target +print(target.shape) + +# Iterate over the images and target +for image, target in zip(images, target): + # Print the image and target + print(image, target) +Generated code for sklearn.datasets.load_svmlight_file + + +from sklearn.datasets import load_svmlight_file + +# Load the data +X, y = load_svmlight_file('data.txt') + +# Print the shape of the data +print(X.shape, y.shape) + +# Print the first 5 rows of the data +print(X[:5], y[:5]) +Generated code for sklearn.datasets.load_svmlight_files + + +from sklearn.datasets import load_svmlight_files + +# Load the data +X_train, y_train, X_test, y_test = load_svmlight_files(('train.txt', 'test.txt')) + +# Print the shapes of the data +print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) + +# Fit a model +from sklearn.linear_model import LogisticRegression +clf = LogisticRegression() +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.datasets.load_wine + + +from sklearn.datasets import load_wine + +# Load the wine dataset +wine_data = load_wine() + +# Print the feature names +print(wine_data.feature_names) + +# Print the target names +print(wine_data.target_names) + +# Print the data +print(wine_data.data) + +# Print the target +print(wine_data.target) +Generated code for sklearn.datasets.make_biclusters + + +import numpy as np +from sklearn.datasets import make_biclusters + +# Generate a random bicluster data set +data, rows, columns = make_biclusters( + shape=(100, 100), n_clusters=4, noise=5, + shuffle=False, random_state=0) + +# Print the data set +print(data) + +# Print the row and column labels +print(rows) +print(columns) +Generated code for sklearn.datasets.make_blobs + + +from sklearn.datasets import make_blobs + +# Generate isotropic Gaussian blobs for clustering +X, y = make_blobs(n_samples=100, n_features=2, centers=3, cluster_std=1.0, random_state=0) + +# Plot the blobs +import matplotlib.pyplot as plt +plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='viridis') +plt.show() +Generated code for sklearn.datasets.make_checkerboard + + +import numpy as np +from sklearn.datasets import make_checkerboard + +# Generate a checkerboard dataset +X, y = make_checkerboard(shape=(8, 8), n_classes=2, noise=1.0) + +# Print the dataset +print(X) +print(y) +Generated code for sklearn.datasets.make_circles + + +import numpy as np +from sklearn.datasets import make_circles + +# Generate data +X, y = make_circles(n_samples=100, noise=0.05, random_state=0) + +# Plot the data +import matplotlib.pyplot as plt +plt.figure(figsize=(5, 5)) +plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5) +plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5) +plt.xlim(-1.5, 1.5) +plt.ylim(-1.5, 1.5) +plt.legend(['0', '1']) +plt.title("Blue circles and Red crosses") +plt.show() +Generated code for sklearn.datasets.make_classification + + +from sklearn.datasets import make_classification + +X, y = make_classification(n_samples=1000, n_features=20, n_informative=2, n_redundant=2, random_state=0) + +print(X.shape) +print(y.shape) +Generated code for sklearn.datasets.make_friedman1 + + +import numpy as np +from sklearn.datasets import make_friedman1 + +X, y = make_friedman1(n_samples=100, n_features=7, random_state=0) + +# Print the shapes of X and y +print(X.shape) +print(y.shape) + +# Print the first 5 rows of X +print(X[:5, :]) + +# Print the first 5 values of y +print(y[:5]) +Generated code for sklearn.datasets.make_friedman2 + + +import numpy as np +from sklearn.datasets import make_friedman2 + +X, y = make_friedman2(n_samples=100, noise=0.5, random_state=0) + +# Print the shapes of X and y +print(X.shape) +print(y.shape) + +# Print the first 5 rows of X +print(X[:5, :]) + +# Print the first 5 elements of y +print(y[:5]) +Generated code for sklearn.datasets.make_friedman3 + + +import numpy as np +from sklearn.datasets import make_friedman3 + +X, y = make_friedman3(n_samples=100, random_state=0) + +# Print the shapes of X and y +print(X.shape) +print(y.shape) + +# Print the first 5 rows of X +print(X[:5, :]) + +# Print the first 5 elements of y +print(y[:5]) +Generated code for sklearn.datasets.make_gaussian_quantiles + + +import numpy as np +from sklearn.datasets import make_gaussian_quantiles + +# Generate a dataset with 2 features and 3 classes +X, y = make_gaussian_quantiles(n_features=2, n_classes=3) + +# Print the shape of the dataset +print(X.shape) +print(y.shape) + +# Print the first 5 samples of the dataset +print(X[:5]) +print(y[:5]) +Generated code for sklearn.datasets.make_hastie_10_2 + + +import numpy as np +from sklearn.datasets import make_hastie_10_2 + +X, y = make_hastie_10_2(random_state=0) + +# Print the shape of the data +print("Shape of X:", X.shape) +print("Shape of y:", y.shape) + +# Print the first 5 rows of the data +print("\nFirst 5 rows of X:\n", X[:5, :]) +print("\nFirst 5 rows of y:\n", y[:5]) +Generated code for sklearn.datasets.make_low_rank_matrix + + +import numpy as np +from sklearn.datasets import make_low_rank_matrix + +# Generate a low-rank matrix +X, _ = make_low_rank_matrix(n_samples=100, n_features=20, effective_rank=5, tail_strength=0.5, random_state=42) + +# Print the shape of the matrix +print(X.shape) + +# Print the first 5 rows of the matrix +print(X[:5]) +Generated code for sklearn.datasets.make_moons + + +from sklearn.datasets import make_moons + +# Generate sample data +X, y = make_moons(n_samples=100, noise=0.1, random_state=42) + +# Plot the data +import matplotlib.pyplot as plt +plt.scatter(X[:, 0], X[:, 1], c=y) +plt.show() +Generated code for sklearn.datasets.make_multilabel_classification + + +import numpy as np +from sklearn.datasets import make_multilabel_classification + +# Generate a random multilabel dataset +X, y = make_multilabel_classification(n_samples=1000, n_features=20, + n_classes=5, n_labels=2, + length=50, allow_unlabeled=True, + sparse=False, return_indicator='dense', + return_distributions=False, + random_state=None) + +# Print the shape of the dataset +print(X.shape) +print(y.shape) +Generated code for sklearn.datasets.make_regression + + +from sklearn.datasets import make_regression + +X, y = make_regression(n_samples=100, n_features=2, noise=0.1) + +# print the shapes of X and y +print(X.shape) +print(y.shape) + +# import the necessary packages +from sklearn.linear_model import LinearRegression +from sklearn.metrics import mean_squared_error + +# create the linear regression model +model = LinearRegression() + +# fit the model to the data +model.fit(X, y) + +# make predictions +y_pred = model.predict(X) + +# calculate the mean squared error +mse = mean_squared_error(y, y_pred) + +# print the mean squared error +print("Mean Squared Error:", mse) +Generated code for sklearn.datasets.make_s_curve + + +import numpy as np +from sklearn.datasets import make_s_curve + +# Generate a random s-curve dataset +data, colors = make_s_curve(n_samples=1000) + +# Plot the s-curve +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +fig = plt.figure() +ax = fig.add_subplot(111, projection='3d') +ax.scatter(data[:,0], data[:,1], data[:,2], c=colors) +plt.show() +Generated code for sklearn.datasets.make_sparse_coded_signal + + +import numpy as np +from sklearn.datasets import make_sparse_coded_signal + +# Generate a sparse coded signal +n_samples, n_features, n_components, n_nonzero_coefs = 1000, 10, 5, 3 +X, _, _ = make_sparse_coded_signal(n_samples, n_features, n_components, n_nonzero_coefs) + +# Print the shape of the generated sparse coded signal +print(X.shape) +Generated code for sklearn.datasets.make_sparse_spd_matrix + + +import numpy as np +from scipy.sparse import random +from sklearn.datasets import make_sparse_spd_matrix + +# Generate a sparse symmetric positive definite matrix +n_dim = 10 +A = random(n_dim, n_dim, density=0.2, random_state=42, + data_rvs=np.random.randn).todense() +A = (A + A.T) / 2 +A = make_sparse_spd_matrix(A) +Generated code for sklearn.datasets.make_sparse_uncorrelated + + +import numpy as np +from sklearn.datasets import make_sparse_uncorrelated + +# Generate a random sparse uncorrelated dataset +X, y = make_sparse_uncorrelated(n_samples=100, n_features=10, random_state=42) + +# Print the shape of the dataset +print(X.shape) + +# Print the first 5 rows of the dataset +print(X[:5]) +Generated code for sklearn.datasets.make_spd_matrix + + +import numpy as np +from sklearn.datasets import make_spd_matrix + +# Generate a random symmetric, positive-definite matrix +A = make_spd_matrix(n_dim=5) + +# Check if A is symmetric +if np.allclose(A, A.T): + print("A is symmetric") + +# Check if A is positive-definite +if np.all(np.linalg.eigvals(A) > 0): + print("A is positive-definite") +Generated code for sklearn.datasets.make_swiss_roll + + +import numpy as np +from sklearn.datasets import make_swiss_roll + +X, y = make_swiss_roll(n_samples=1000, noise=0.2, random_state=42) + +# Plot the swiss roll +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +fig = plt.figure() +ax = fig.add_subplot(111, projection='3d') +ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.hot) +plt.show() +Generated code for sklearn.decomposition.DictionaryLearning + + +from sklearn.decomposition import DictionaryLearning + +# Create a DictionaryLearning object +dl = DictionaryLearning(n_components=10) + +# Fit the model to the data +dl.fit(X) + +# Transform the data +X_transformed = dl.transform(X) + +# Get the components of the model +components = dl.components_ + +# Get the dictionary of the model +dictionary = dl.dictionary_ +Generated code for sklearn.decomposition.FactorAnalysis + + +from sklearn.decomposition import FactorAnalysis + +# Create a FactorAnalysis object +fa = FactorAnalysis(n_components=2) + +# Fit the model to the data +fa.fit(X) + +# Transform the data +X_transformed = fa.transform(X) + +# Get the factor loadings +loadings = fa.components_ + +# Get the variance explained by each factor +variance_explained = fa.explained_variance_ratio_ +Generated code for sklearn.decomposition.FastICA + + +import numpy as np +from sklearn.decomposition import FastICA + +# Generate sample data +np.random.seed(0) +n_samples = 2000 +time = np.linspace(0, 8, n_samples) + +s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal +s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal +s3 = signal.sawtooth(2 * np.pi * time) # Signal 3: saw tooth signal + +S = np.c_[s1, s2, s3] +S += 0.2 * np.random.normal(size=S.shape) # Add noise + +S /= S.std(axis=0) # Standardize data +# Mix data +A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]]) # Mixing matrix +X = np.dot(S, A.T) # Generate observations + +# Compute ICA +ica = FastICA(n_components=3) +S_ = ica.fit_transform(X) # Reconstruct signals +A_ = ica.mixing_ # Get estimated mixing matrix + +# We can `prove` that the ICA model applies by reverting the unmixing. +assert np.allclose(X, np.dot(S_, A_.T) + ica.mean_) +Generated code for sklearn.decomposition.IncrementalPCA + + +from sklearn.decomposition import IncrementalPCA + +# Create an instance of IncrementalPCA +inc_pca = IncrementalPCA() + +# Fit the model with the data +inc_pca.fit(X) + +# Transform the data +X_transformed = inc_pca.transform(X) + +# Get the explained variance ratio +explained_variance_ratio = inc_pca.explained_variance_ratio_ + +# Get the components +components = inc_pca.components_ +Generated code for sklearn.decomposition.KernelPCA + + +from sklearn.decomposition import KernelPCA + +# Create a KernelPCA object +kpca = KernelPCA(kernel='rbf', gamma=15) + +# Fit the model to the data +kpca.fit(X) + +# Transform the data +X_kpca = kpca.transform(X) +Generated code for sklearn.decomposition.LatentDirichletAllocation + + +from sklearn.decomposition import LatentDirichletAllocation + +# Create an instance of the LDA model +lda = LatentDirichletAllocation(n_components=10, random_state=0) + +# Fit the model to the data +lda.fit(X) + +# Transform the data +X_transformed = lda.transform(X) + +# Print the topics +print(lda.components_) +Generated code for sklearn.decomposition.MiniBatchDictionaryLearning + + +from sklearn.decomposition import MiniBatchDictionaryLearning + +# Create an instance of the MiniBatchDictionaryLearning class +mbdl = MiniBatchDictionaryLearning(n_components=100, alpha=1, n_iter=1000) + +# Fit the model to the data +mbdl.fit(X) + +# Transform the data +X_transformed = mbdl.transform(X) + +# Get the components of the model +components = mbdl.components_ + +# Get the dictionary of the model +dictionary = mbdl.dictionary_ +Generated code for sklearn.decomposition.MiniBatchSparsePCA + + +from sklearn.decomposition import MiniBatchSparsePCA + +# Create an instance of MiniBatchSparsePCA +mb_sparse_pca = MiniBatchSparsePCA(n_components=2, batch_size=10, random_state=42) + +# Fit the model to the data +mb_sparse_pca.fit(X) + +# Transform the data +X_transformed = mb_sparse_pca.transform(X) +Generated code for sklearn.decomposition.NMF + + +from sklearn.decomposition import NMF + +# Create an NMF instance +model = NMF(n_components=2) + +# Fit the model to the data +model.fit(X) + +# Transform the data +X_transformed = model.transform(X) + +# Get the components +components = model.components_ +Generated code for sklearn.decomposition.MiniBatchNMF + + +from sklearn.decomposition import MiniBatchNMF + +# Create an instance of the MiniBatchNMF class +model = MiniBatchNMF(n_components=2, init='random', random_state=0) + +# Fit the model to the data +model.fit(X) + +# Transform the data +X_transformed = model.transform(X) + +# Get the components +components = model.components_ +Generated code for sklearn.decomposition.PCA + + +from sklearn.decomposition import PCA + +# Create an instance of PCA +pca = PCA(n_components=2) + +# Fit the data to the model +pca.fit(X) + +# Transform the data +X_pca = pca.transform(X) + +# Print the components +print(pca.components_) + +# Print the explained variance +print(pca.explained_variance_) +Generated code for sklearn.decomposition.SparsePCA + + +from sklearn.decomposition import SparsePCA + +# Create a SparsePCA object +sparse_pca = SparsePCA(n_components=2) + +# Fit the model to the data +sparse_pca.fit(X) + +# Transform the data +X_transformed = sparse_pca.transform(X) + +# Get the explained variance ratio +explained_variance_ratio = sparse_pca.explained_variance_ratio_ + +# Get the components +components = sparse_pca.components_ +Generated code for sklearn.decomposition.SparseCoder + + +from sklearn.decomposition import SparseCoder + +# Create a sparse coder object +sc = SparseCoder(transform_n_nonzero_coefs=2, transform_alpha=1.0) + +# Fit the sparse coder to the data +sc.fit(X) + +# Transform the data using the sparse coder +X_transformed = sc.transform(X) +Generated code for sklearn.decomposition.TruncatedSVD + + +from sklearn.decomposition import TruncatedSVD + +# Create an instance of TruncatedSVD +svd = TruncatedSVD(n_components=2) + +# Fit the data to the model +svd.fit(X) + +# Transform the data +X_transformed = svd.transform(X) +Generated code for sklearn.decomposition.dict_learning + + +from sklearn.decomposition import dict_learning + +# Create a matrix of data +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + +# Create a dictionary learning model +model = dict_learning.DictLearning(n_components=2) + +# Fit the model to the data +model.fit(X) + +# Get the components of the model +components = model.components_ + +# Get the reconstructed data +X_reconstructed = model.inverse_transform(components) +Generated code for sklearn.decomposition.dict_learning_online + + +from sklearn.decomposition import dict_learning_online + +# Create a data matrix +X = np.random.rand(100, 10) + +# Create a dictionary learning model +model = dict_learning_online(n_components=5, alpha=1.0, max_iter=100) + +# Fit the model to the data +model.fit(X) + +# Get the dictionary components +components = model.components_ + +# Get the sparse codes +codes = model.transform(X) +Generated code for sklearn.decomposition.fastica + + +import numpy as np +from sklearn.decomposition import FastICA + +# Generate sample data +np.random.seed(0) +n_samples = 2000 +time = np.linspace(0, 8, n_samples) + +s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal +s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal +s3 = signal.sawtooth(2 * np.pi * time) # Signal 3: saw tooth signal + +S = np.c_[s1, s2, s3] +S += 0.2 * np.random.normal(size=S.shape) # Add noise + +S /= S.std(axis=0) # Standardize data + +# Compute ICA +ica = FastICA(n_components=3) +S_ = ica.fit_transform(S) # Reconstruct signals +A_ = ica.mixing_ # Get estimated mixing matrix + +# We can `prove` that the ICA model applies by reverting the unmixing. +assert np.allclose(S, np.dot(S_, A_.T) + ica.mean_) +Generated code for sklearn.decomposition.non_negative_factorization + + +from sklearn.decomposition import non_negative_factorization + +# Create a matrix of data +X = [[1, 2, 3], + [2, 3, 4], + [3, 4, 5], + [4, 5, 6]] + +# Create the non-negative factorization model +model = non_negative_factorization(X) + +# Fit the model to the data +model.fit(X) + +# Get the factorization results +W, H = model.components_ + +# Print the results +print("W:") +print(W) +print("H:") +print(H) +Generated code for sklearn.decomposition.sparse_encode + + +import numpy as np +from sklearn.decomposition import sparse_encode + +# Generate random data +X = np.random.rand(10, 5) + +# Set the dictionary +D = np.random.rand(5, 3) + +# Encode the data +code = sparse_encode(X, D, algorithm='lasso_lars') + +# Print the encoded data +print(code) +Generated code for sklearn.discriminant_analysis.LinearDiscriminantAnalysis + + +# Importing necessary libraries +from sklearn.discriminant_analysis import LinearDiscriminantAnalysis +from sklearn.model_selection import train_test_split +import numpy as np + +# Generating random data +X = np.random.rand(100, 5) +y = np.random.randint(2, size=100) + +# Splitting the data into train and test sets +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Initializing the LinearDiscriminantAnalysis model +lda = LinearDiscriminantAnalysis() + +# Fitting the model to the training data +lda.fit(X_train, y_train) + +# Making predictions on the test set +y_pred = lda.predict(X_test) + +# Calculating the accuracy of the model +accuracy = lda.score(X_test, y_test) + +# Printing the accuracy +print("Accuracy:", accuracy) +Generated code for sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis + + +from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis + +# Create an instance of the QuadraticDiscriminantAnalysis class +qda = QuadraticDiscriminantAnalysis() + +# Fit the model to the data +qda.fit(X, y) + +# Make predictions +predictions = qda.predict(X_test) + +# Calculate the accuracy of the model +accuracy = qda.score(X_test, y_test) +Generated code for sklearn.dummy.DummyClassifier + + +from sklearn.dummy import DummyClassifier + +# Create dummy classifier +dummy_clf = DummyClassifier(strategy="most_frequent") + +# Fit the dummy classifier to the data +dummy_clf.fit(X_train, y_train) + +# Make predictions +y_pred = dummy_clf.predict(X_test) + +# Evaluate the model +accuracy = accuracy_score(y_test, y_pred) +print("Accuracy: {:.2f}".format(accuracy)) +Generated code for sklearn.dummy.DummyRegressor + + +from sklearn.dummy import DummyRegressor + +# Create a dummy regressor +dummy_regressor = DummyRegressor() + +# Fit the dummy regressor to the data +dummy_regressor.fit(X, y) + +# Make predictions +predictions = dummy_regressor.predict(X) + +# Evaluate the model +score = dummy_regressor.score(X, y) +Generated code for sklearn.ensemble.AdaBoostClassifier + + +from sklearn.ensemble import AdaBoostClassifier + +# Create an AdaBoostClassifier +clf = AdaBoostClassifier(n_estimators=100, random_state=0) + +# Train the classifier +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +accuracy = clf.score(X_test, y_test) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.ensemble.AdaBoostRegressor + + +from sklearn.ensemble import AdaBoostRegressor + +# Create the AdaBoost Regressor +regressor = AdaBoostRegressor() + +# Fit the regressor to the training data +regressor.fit(X_train, y_train) + +# Predict on the test data +y_pred = regressor.predict(X_test) + +# Calculate the accuracy +accuracy = regressor.score(X_test, y_test) +Generated code for sklearn.ensemble.BaggingClassifier + + +from sklearn.ensemble import BaggingClassifier + +# Create a BaggingClassifier +bagging_clf = BaggingClassifier(n_estimators=10, + max_samples=100, + bootstrap=True, + n_jobs=-1) + +# Fit the classifier to the training data +bagging_clf.fit(X_train, y_train) + +# Make predictions on the test data +predictions = bagging_clf.predict(X_test) + +# Evaluate the model +accuracy = accuracy_score(y_test, predictions) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.ensemble.BaggingRegressor + + +from sklearn.ensemble import BaggingRegressor + +# Create a BaggingRegressor object +bagging_regressor = BaggingRegressor() + +# Fit the regressor to the data +bagging_regressor.fit(X_train, y_train) + +# Make predictions +y_pred = bagging_regressor.predict(X_test) + +# Evaluate the model +score = bagging_regressor.score(X_test, y_test) +Generated code for sklearn.ensemble.ExtraTreesClassifier + + +from sklearn.ensemble import ExtraTreesClassifier + +# Create an ExtraTreesClassifier +clf = ExtraTreesClassifier(n_estimators=100, random_state=0) + +# Train the classifier +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +accuracy = clf.score(X_test, y_test) +print("Accuracy: %.2f%%" % (accuracy * 100.0)) +Generated code for sklearn.ensemble.ExtraTreesRegressor + + +from sklearn.ensemble import ExtraTreesRegressor + +# Create the ExtraTreesRegressor object +regressor = ExtraTreesRegressor() + +# Fit the regressor to the data +regressor.fit(X, y) + +# Make predictions +y_pred = regressor.predict(X_test) + +# Calculate the mean absolute error +mae = mean_absolute_error(y_test, y_pred) + +# Print the mean absolute error +print('Mean Absolute Error:', mae) +Generated code for sklearn.ensemble.GradientBoostingClassifier + + +#importing necessary libraries +import numpy as np +from sklearn.ensemble import GradientBoostingClassifier + +#creating the classifier +clf = GradientBoostingClassifier() + +#fitting the classifier +clf.fit(X_train, y_train) + +#predicting the labels +y_pred = clf.predict(X_test) + +#calculating the accuracy +accuracy = clf.score(X_test, y_test) + +#printing the accuracy +print("Accuracy:", accuracy) +Generated code for sklearn.ensemble.GradientBoostingRegressor + + +from sklearn.ensemble import GradientBoostingRegressor + +# Create the regressor +regressor = GradientBoostingRegressor() + +# Fit the regressor to the training data +regressor.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = regressor.predict(X_test) + +# Calculate the mean absolute error +mae = mean_absolute_error(y_test, y_pred) + +# Print the mean absolute error +print('Mean Absolute Error:', mae) +Generated code for sklearn.ensemble.IsolationForest + + +import numpy as np +from sklearn.ensemble import IsolationForest + +# Create a random dataset +rng = np.random.RandomState(42) +X = rng.rand(100, 2) + +# Create the IsolationForest object +clf = IsolationForest(random_state=rng) + +# Fit the model to the data +clf.fit(X) + +# Make predictions +y_pred = clf.predict(X) +Generated code for sklearn.ensemble.RandomForestClassifier + + +# Import the necessary libraries +from sklearn.ensemble import RandomForestClassifier +from sklearn.datasets import make_classification + +# Create the dataset +X, y = make_classification(n_samples=1000, n_features=4, + n_informative=2, n_redundant=0, + random_state=0, shuffle=False) + +# Create the Random Forest Classifier +clf = RandomForestClassifier(n_estimators=100, max_depth=2, + random_state=0) + +# Fit the model +clf.fit(X, y) + +# Make predictions +predictions = clf.predict(X) +Generated code for sklearn.ensemble.RandomForestRegressor + + +import numpy as np +from sklearn.ensemble import RandomForestRegressor + +# Create the random forest regressor +rf = RandomForestRegressor(n_estimators=100, random_state=42) + +# Train the model on training data +rf.fit(X_train, y_train) + +# Make predictions on test data +predictions = rf.predict(X_test) + +# Calculate the mean absolute error +mae = np.mean(abs(predictions - y_test)) + +print('Mean Absolute Error:', mae) +Generated code for sklearn.ensemble.RandomTreesEmbedding + + +from sklearn.ensemble import RandomTreesEmbedding + +# Create the RandomTreesEmbedding object +rt = RandomTreesEmbedding(n_estimators=100, random_state=0) + +# Fit the model to the data +rt.fit(X, y) + +# Transform the data +X_transformed = rt.transform(X) +Generated code for sklearn.ensemble.StackingClassifier + + +from sklearn.ensemble import StackingClassifier +from sklearn.linear_model import LogisticRegression +from sklearn.neighbors import KNeighborsClassifier +from sklearn.svm import SVC + +estimators = [('lr', LogisticRegression()), + ('knn', KNeighborsClassifier()), + ('svc', SVC())] + +clf = StackingClassifier(estimators=estimators) +clf.fit(X_train, y_train) +y_pred = clf.predict(X_test) +Generated code for sklearn.ensemble.StackingRegressor + + +from sklearn.ensemble import StackingRegressor +from sklearn.linear_model import LinearRegression +from sklearn.svm import SVR + +# Create the individual regressors +lr = LinearRegression() +svr_lin = SVR(kernel='linear') +svr_rbf = SVR(kernel='rbf') + +# Create the stacking regressor +regressor = StackingRegressor( + regressors=[lr, svr_lin, svr_rbf], + meta_regressor=lr +) + +# Fit the regressor +regressor.fit(X, y) + +# Make predictions +predictions = regressor.predict(X_test) +Generated code for sklearn.ensemble.VotingClassifier + + +import numpy as np +from sklearn.ensemble import VotingClassifier +from sklearn.linear_model import LogisticRegression +from sklearn.naive_bayes import GaussianNB +from sklearn.svm import SVC + +# Create the three classifiers +clf1 = LogisticRegression(random_state=1) +clf2 = GaussianNB() +clf3 = SVC(random_state=1) + +# Combine the classifiers in a voting classifier +voting_clf = VotingClassifier(estimators=[('lr', clf1), ('gnb', clf2), ('svc', clf3)], voting='hard') + +# Fit the classifier on the training set +voting_clf.fit(X_train, y_train) + +# Make predictions on the test set +y_pred = voting_clf.predict(X_test) +Generated code for sklearn.ensemble.VotingRegressor + + +from sklearn.ensemble import VotingRegressor +from sklearn.linear_model import LinearRegression +from sklearn.svm import SVR + +# Create estimators +estimators = [('lr', LinearRegression()), + ('svr', SVR(kernel='linear'))] + +# Create a voting regressor +reg = VotingRegressor(estimators) + +# Fit the regressor +reg.fit(X, y) + +# Make predictions +predictions = reg.predict(X_test) +Generated code for sklearn.ensemble.HistGradientBoostingRegressor + + +# Importing necessary libraries +import numpy as np +from sklearn.ensemble import HistGradientBoostingRegressor + +# Creating the data +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) +y = np.array([1, 2, 3, 4]) + +# Initializing the model +model = HistGradientBoostingRegressor() + +# Fitting the model +model.fit(X, y) + +# Making predictions +predictions = model.predict(X) + +# Printing the predictions +print(predictions) +Generated code for sklearn.ensemble.HistGradientBoostingClassifier + + +# Importing necessary libraries +import numpy as np +from sklearn.ensemble import HistGradientBoostingClassifier + +# Creating the model +model = HistGradientBoostingClassifier() + +# Fitting the model +model.fit(X_train, y_train) + +# Making predictions +y_pred = model.predict(X_test) + +# Evaluating the model +accuracy = model.score(X_test, y_test) + +# Printing the accuracy +print("Accuracy:", accuracy) +Generated code for sklearn.exceptions.ConvergenceWarning + + +try: + # code that may raise a ConvergenceWarning +except ConvergenceWarning: + # code to handle the ConvergenceWarning + pass +Generated code for sklearn.exceptions.DataConversionWarning + + +try: + # code that may raise DataConversionWarning +except sklearn.exceptions.DataConversionWarning as e: + # handle DataConversionWarning + print(e) +Generated code for sklearn.exceptions.DataDimensionalityWarning + + +try: + # code that may raise DataDimensionalityWarning +except sklearn.exceptions.DataDimensionalityWarning as e: + print("DataDimensionalityWarning:", e) +Generated code for sklearn.exceptions.EfficiencyWarning + + +try: + # code that may raise EfficiencyWarning +except sklearn.exceptions.EfficiencyWarning: + # code to handle EfficiencyWarning + print('EfficiencyWarning was raised!') +Generated code for sklearn.exceptions.FitFailedWarning + + +class FitFailedWarning(Warning): + """Warning used when a fitting procedure fails""" + pass +Generated code for sklearn.exceptions.NotFittedError + + +try: + # code that uses the model +except sklearn.exceptions.NotFittedError: + print("Error: Model is not fitted yet. Please fit the model first.") +Generated code for sklearn.exceptions.UndefinedMetricWarning + + +try: + # code that may raise an exception +except sklearn.exceptions.UndefinedMetricWarning: + # code to handle the exception + print("Warning: UndefinedMetricWarning encountered") +Generated code for sklearn.experimental.enable_hist_gradient_boosting + + +# Import the necessary libraries +from sklearn.ensemble import HistGradientBoostingClassifier + +# Enable Histogram-based Gradient Boosting +clf = HistGradientBoostingClassifier(max_iter=100, learning_rate=0.1, + max_depth=3, random_state=0) +Generated code for sklearn.experimental.enable_iterative_imputer + + +from sklearn.experimental import enable_iterative_imputer +from sklearn.impute import IterativeImputer + +# Create an IterativeImputer object +imputer = IterativeImputer() + +# Fit the imputer using the data +imputer.fit(X) + +# Transform the data, replacing missing values with the learned medians +X_transformed = imputer.transform(X) +Generated code for sklearn.experimental.enable_halving_search_cv + + +from sklearn.model_selection import GridSearchCV +from sklearn.experimental import enable_halving_search_cv + +# Create the parameter grid +param_grid = { + 'max_depth': [4, 8, 12], + 'min_samples_leaf': [1, 2, 4], + 'min_samples_split': [2, 4, 8], + 'n_estimators': [50, 100, 200] +} + +# Instantiate the halving search CV object +halving_search_cv = enable_halving_search_cv(estimator=RandomForestRegressor(), + param_grid=param_grid, + scoring='neg_mean_squared_error', + cv=5, + n_iter=10, + random_state=42) + +# Fit the halving search CV object +halving_search_cv.fit(X_train, y_train) + +# Print the best parameters and best score +print("Best Parameters:", halving_search_cv.best_params_) +print("Best Score:", halving_search_cv.best_score_) +Generated code for sklearn.feature_extraction.DictVectorizer + + +from sklearn.feature_extraction import DictVectorizer + +# Create a dictionary of feature names and values +data_dict = {'height': [6.0, 5.7], 'weight': [180, 190], 'age': [30, 40]} + +# Create the DictVectorizer object +dictvectorizer = DictVectorizer() + +# Transform the data into a feature matrix +features = dictvectorizer.fit_transform(data_dict) + +# Print the feature matrix +print(features.toarray()) +Generated code for sklearn.feature_extraction.FeatureHasher + + +from sklearn.feature_extraction import FeatureHasher + +# Create a FeatureHasher object +hasher = FeatureHasher(n_features=10, input_type='string') + +# Create a list of feature names +feature_names = ['color', 'size', 'shape'] + +# Create a list of feature values +feature_values = [['red', 'small', 'circle'], + ['blue', 'medium', 'square'], + ['green', 'large', 'triangle']] + +# Fit the FeatureHasher object +hasher.fit(feature_values) + +# Transform the feature values into a sparse matrix +sparse_matrix = hasher.transform(feature_values) + +# Print the sparse matrix +print(sparse_matrix) +Generated code for sklearn.feature_extraction.image.extract_patches_2d + + +from sklearn.feature_extraction.image import extract_patches_2d + +# define the size of the patches to be extracted +patch_size = (8, 8) + +# extract the patches from the image +patches = extract_patches_2d(image, patch_size) + +# print the shape of the patches +print(patches.shape) +Generated code for sklearn.feature_extraction.image.grid_to_graph + + +import numpy as np +from sklearn.feature_extraction.image import grid_to_graph + +def grid_to_graph(n_x, n_y): + """ + Generates a graph from a grid with n_x and n_y nodes. + Parameters + ---------- + n_x : int + Number of nodes in the x-direction. + n_y : int + Number of nodes in the y-direction. + Returns + ------- + graph : array of shape (n_x * n_y, n_x * n_y) + Adjacency matrix of the grid graph. + """ + # Generate the edges of the graph + edges = [] + for x in range(n_x): + for y in range(n_y): + if x < n_x-1: + edges.append([x*n_y+y, (x+1)*n_y+y]) + if y < n_y-1: + edges.append([x*n_y+y, x*n_y+(y+1)]) + + # Generate the adjacency matrix + graph = np.zeros((n_x*n_y, n_x*n_y)) + for edge in edges: + graph[edge[0], edge[1]] = 1 + graph[edge[1], edge[0]] = 1 + + return graph +Generated code for sklearn.feature_extraction.image.img_to_graph + + +from sklearn.feature_extraction.image import img_to_graph + +def img_to_graph(image, mask=None): + """ + Transforms a 2D image into a graph with the value of the + pixel as the weight of the edge. + + Parameters + ---------- + image : array_like + The input image. + mask : array_like, optional + An optional mask to limit the pixels used in the graph. + + Returns + ------- + graph : array_like + The graph of the image. + """ + + # Get the shape of the image + rows, cols = image.shape + + # Create the graph + graph = np.zeros((rows * cols, rows * cols)) + + # Iterate over the image + for row in range(rows): + for col in range(cols): + # Get the pixel value + pixel = image[row, col] + + # If the mask is not None, check if the pixel is valid + if mask is not None and not mask[row, col]: + continue + + # Get the index of the pixel + index = row * cols + col + + # Iterate over the neighbors + for i in range(row - 1, row + 2): + for j in range(col - 1, col + 2): + # Check if the neighbor is valid + if i < 0 or i >= rows or j < 0 or j >= cols: + continue + + # Get the neighbor index + neighbor_index = i * cols + j + + # If the mask is not None, check if the neighbor is valid + if mask is not None and not mask[i, j]: + continue + + # Get the neighbor pixel value + neighbor_pixel = image[i, j] + + # Set the weight of the edge + graph[index, neighbor_index] = abs(pixel - neighbor_pixel) + + return graph +Generated code for sklearn.feature_extraction.image.reconstruct_from_patches_2d + + +from sklearn.feature_extraction.image import reconstruct_from_patches_2d + +def reconstruct_from_patches_2d(patches, image_size): + """Reconstructs an image from all of its patches. + + Parameters + ---------- + patches : array, shape = (n_patches, patch_height, patch_width) + The complete set of patches. + image_size : tuple of ints (image_height, image_width) + the size of the image that will be reconstructed. + + Returns + ------- + image : array, shape = image_size + The reconstructed image. + """ + image_height, image_width = image_size + patch_height, patch_width = patches.shape[1:] + n_patches = patches.shape[0] + + # Initialize reconstructed image + image = np.zeros(image_size) + + # Calculate the number of patches per row and column + n_rows = int(np.ceil(image_height / patch_height)) + n_cols = int(np.ceil(image_width / patch_width)) + + # Iterate over patches + for i in range(n_patches): + # Calculate the row and column of the patch + row = int(i // n_cols) + col = int(i % n_cols) + + # Calculate the start and end indices of the patch + start_row = row * patch_height + end_row = min(start_row + patch_height, image_height) + start_col = col * patch_width + end_col = min(start_col + patch_width, image_width) + + # Add the patch to the reconstructed image + image[start_row:end_row, start_col:end_col] += patches[i] + + return image +Generated code for sklearn.feature_extraction.image.PatchExtractor + + +from sklearn.feature_extraction.image import PatchExtractor + +# Create an instance of the PatchExtractor class +patch_extractor = PatchExtractor(patch_size=(64, 64)) + +# Extract patches from an image +patches = patch_extractor.transform(image) + +# Reshape the patches into a 2D array +patches_2d = patches.reshape(-1, 64*64) +Generated code for sklearn.feature_extraction.text.CountVectorizer + + +from sklearn.feature_extraction.text import CountVectorizer + +# Create the CountVectorizer object +vectorizer = CountVectorizer() + +# Get text data +text_data = ["The quick brown fox jumped over the lazy dog."] + +# Fit and transform the text data +count_vector = vectorizer.fit_transform(text_data) + +# Print the resulting feature names +print(vectorizer.get_feature_names()) +Generated code for sklearn.feature_extraction.text.HashingVectorizer + + +from sklearn.feature_extraction.text import HashingVectorizer + +# Create an instance of the HashingVectorizer +vectorizer = HashingVectorizer(n_features=20) + +# Fit the vectorizer to the data +vectorizer.fit(data) + +# Transform the data into a sparse matrix +X = vectorizer.transform(data) + +# Print the sparse matrix +print(X.toarray()) +Generated code for sklearn.feature_extraction.text.TfidfTransformer + + +from sklearn.feature_extraction.text import TfidfTransformer + +class TfidfTransformer(BaseEstimator, TransformerMixin): + """Transform a count matrix to a normalized tf or tf-idf representation + Tf means term-frequency while tf-idf means term-frequency times inverse + document-frequency. This is a common term weighting scheme in information + retrieval, that has also found good use in document classification. + +The goal of using tf-idf instead of the raw frequencies of occurrence of a +token in a given document is to scale down the impact of tokens that occur +very frequently in a given corpus and that are hence empirically less +informative than features that occur in a small fraction of the training +corpus. + +Parameters +---------- +norm : 'l1', 'l2' or None, optional + Norm used to normalize term vectors. None for no normalization. + +use_idf : boolean, optional + Enable inverse-document-frequency reweighting. + +smooth_idf : boolean, optional + Smooth idf weights by adding one to document frequencies, as if an + extra document was seen containing every term in the collection + exactly once. Prevents zero divisions. + +sublinear_tf : boolean, optional + Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). + +Attributes +---------- +idf_ : array, shape (n_features) + The inverse document frequency (IDF) vector; only defined + if use_idf is True. + +idf_diag_ : array, shape (n_features, n_features) + The IDF diagonal matrix; only defined if use_idf is True. + +stop_words_ : array, shape (n_stop_words,) + Terms that were ignored because they either: + - occurred in too many documents (`max_df`) + - occurred in too few documents (`min_df`) + - were cut off by feature selection (`max_features`). + +vocabulary_ : dict + A mapping of terms to feature indices. + +Examples +-------- +>>> from sklearn.feature_extraction.text import +Generated code for sklearn.feature_extraction.text.TfidfVectorizer + + +from sklearn.feature_extraction.text import TfidfVectorizer + +# Create the vectorizer +vectorizer = TfidfVectorizer() + +# Fit the vectorizer to the data +vectorizer.fit(data) + +# Transform the data into a vector +vectorized_data = vectorizer.transform(data) +Generated code for sklearn.feature_selection.GenericUnivariateSelect + + +from sklearn.feature_selection import GenericUnivariateSelect + +# Create an instance of GenericUnivariateSelect +selector = GenericUnivariateSelect(score_func=f_classif, mode='k_best', param=10) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.get_support() + +# Print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.SelectPercentile + + +from sklearn.feature_selection import SelectPercentile + +# Create an instance of SelectPercentile +selector = SelectPercentile(percentile=50) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.get_support() + +# Print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.SelectKBest + + +#import necessary libraries +import numpy as np +from sklearn.feature_selection import SelectKBest + +#create a sample dataset +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) +y = np.array([1, 0, 1, 0]) + +#instantiate SelectKBest +selector = SelectKBest(k=2) + +#fit the selector to the data +selector.fit(X, y) + +#get the selected features +selected_features = selector.get_support() + +#print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.SelectFpr + + +from sklearn.feature_selection import SelectFpr + +# Create an instance of SelectFpr +selector = SelectFpr(score_func=f_classif) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.get_support() + +# Print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.SelectFdr + + +# Import necessary libraries +from sklearn.feature_selection import SelectFdr +from sklearn.datasets import make_classification + +# Generate a sample dataset +X, y = make_classification(n_samples=1000, n_features=20, random_state=1) + +# Create an instance of SelectFdr +selector = SelectFdr(alpha=0.05) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.get_support() + +# Print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.SelectFromModel + + +from sklearn.feature_selection import SelectFromModel +from sklearn.ensemble import RandomForestClassifier + +# Create a random forest classifier +clf = RandomForestClassifier(random_state=0) + +# Create the selector object +selector = SelectFromModel(clf) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.get_support() +Generated code for sklearn.feature_selection.SelectFwe + + +from sklearn.feature_selection import SelectFwe + +# Create an instance of SelectFwe +selector = SelectFwe(alpha=0.05) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.get_support() + +# Print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.SequentialFeatureSelector + + +from sklearn.feature_selection import SequentialFeatureSelector + +# Create the selector object +selector = SequentialFeatureSelector( + estimator=RandomForestClassifier(n_estimators=100), + k_features=10, + forward=True, + verbose=2, + scoring='accuracy', + cv=5 +) + +# Fit the selector to the data +selector.fit(X, y) + +# Get the selected features +selected_features = selector.k_feature_idx_ + +# Print the selected features +print(selected_features) +Generated code for sklearn.feature_selection.RFE + + +#importing necessary libraries +import numpy as np +from sklearn.feature_selection import RFE +from sklearn.svm import SVR + +#creating a sample dataset +X = np.array([[1,2,3,4], [2,3,4,5], [3,4,5,6], [4,5,6,7]]) +y = np.array([1,2,3,4]) + +#creating the RFE model and selecting 3 attributes +estimator = SVR(kernel="linear") +selector = RFE(estimator, 3, step=1) +selector = selector.fit(X, y) + +#printing the selected attributes +print(selector.support_) +print(selector.ranking_) +Generated code for sklearn.feature_selection.RFECV + + +from sklearn.feature_selection import RFECV +from sklearn.ensemble import RandomForestClassifier + +# Create the RFE object and compute a cross-validated score. +# The "accuracy" scoring is proportional to the number of correct classifications +rfecv = RFECV(estimator=RandomForestClassifier(), step=1, cv=5, scoring='accuracy') +rfecv.fit(X, y) + +# Print the optimal number of features +print("Optimal number of features : %d" % rfecv.n_features_) + +# Plot number of features VS. cross-validation scores +plt.figure() +plt.xlabel("Number of features selected") +plt.ylabel("Cross validation score (nb of correct classifications)") +plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_) +plt.show() +Generated code for sklearn.feature_selection.VarianceThreshold + + +from sklearn.feature_selection import VarianceThreshold + +# Create an instance of VarianceThreshold +sel = VarianceThreshold(threshold=0.5) + +# Fit the instance to the data +sel.fit(X) + +# Get the indices of the features with variance above the threshold +indices = sel.get_support(indices=True) + +# Get the feature names +features = [X.columns[i] for i in indices] + +# Print the feature names +print(features) +Generated code for sklearn.feature_selection.chi2 + + +import numpy as np +from sklearn.feature_selection import chi2 + +# Create a sample dataset +X = np.array([[1, 2, 3, 4], + [2, 4, 6, 8], + [3, 6, 9, 12], + [4, 8, 12, 16]]) +y = np.array([0, 0, 1, 1]) + +# Calculate chi-squared test +chi2_score, p_value = chi2(X, y) + +# Print the results +print("Chi-squared score:", chi2_score) +print("P-value:", p_value) +Generated code for sklearn.feature_selection.f_classif + + +import numpy as np +from sklearn.feature_selection import f_classif + +# Generate some random data +X = np.random.rand(100, 10) +y = np.random.randint(2, size=100) + +# Calculate the F-value and p-value for each feature +F, p = f_classif(X, y) + +# Print the F-value and p-value +for i in range(len(F)): + print("F-value for feature {}: {}".format(i, F[i])) + print("p-value for feature {}: {}".format(i, p[i])) +Generated code for sklearn.feature_selection.f_regression + + +from sklearn.feature_selection import f_regression + +# Create a feature matrix +X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + +# Create a target vector +y = [1, 2, 3] + +# Calculate the F-statistic and p-values +f_values, p_values = f_regression(X, y) + +# Print the F-statistic and p-values +print(f_values) +print(p_values) +Generated code for sklearn.feature_selection.r_regression + + +# Import necessary libraries +import numpy as np +from sklearn.feature_selection import RFE +from sklearn.linear_model import LinearRegression + +# Create a linear regression object +reg = LinearRegression() + +# Create the RFE object and rank each feature +rfe = RFE(reg, n_features_to_select=1) +rfe.fit(X, y) + +# Print the feature ranking +print("Feature ranking: %s" % rfe.ranking_) +Generated code for sklearn.feature_selection.mutual_info_classif + + +import numpy as np +from sklearn.feature_selection import mutual_info_classif + +# Create a random dataset +X = np.random.rand(1000, 10) +y = np.random.randint(2, size=1000) + +# Calculate mutual information +mi = mutual_info_classif(X, y) + +# Print the results +print(mi) +Generated code for sklearn.feature_selection.mutual_info_regression + + +import numpy as np +from sklearn.feature_selection import mutual_info_regression + +# Generate some random data +X = np.random.rand(1000, 10) +y = np.random.rand(1000) + +# Calculate mutual information +mi = mutual_info_regression(X, y) + +# Print the results +print(mi) +Generated code for sklearn.gaussian_process.GaussianProcessClassifier + + +from sklearn.gaussian_process import GaussianProcessClassifier + +# Create a Gaussian Process Classifier +gpc = GaussianProcessClassifier() + +# Fit the model to the data +gpc.fit(X_train, y_train) + +# Make predictions +y_pred = gpc.predict(X_test) + +# Evaluate the model +accuracy = gpc.score(X_test, y_test) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.gaussian_process.GaussianProcessRegressor + + +from sklearn.gaussian_process import GaussianProcessRegressor + +# Create the Gaussian Process Regressor +gp = GaussianProcessRegressor() + +# Fit the model to the data +gp.fit(X, y) + +# Make predictions +y_pred = gp.predict(X_test) + +# Compute the mean absolute error +mae = mean_absolute_error(y_test, y_pred) + +# Print the mean absolute error +print('Mean Absolute Error:', mae) +Generated code for sklearn.gaussian_process.kernels.CompoundKernel + + +from sklearn.gaussian_process.kernels import CompoundKernel + +# Create a CompoundKernel object +kernel = CompoundKernel() + +# Add kernels to the CompoundKernel +kernel += RBF() + WhiteKernel() + ExpSineSquared() + +# Fit the CompoundKernel to the data +gp = GaussianProcessRegressor(kernel=kernel) +gp.fit(X, y) + +# Make predictions using the CompoundKernel +y_pred = gp.predict(X_test) +Generated code for sklearn.gaussian_process.kernels.ConstantKernel + + +from sklearn.gaussian_process.kernels import ConstantKernel + +# Create a ConstantKernel instance +constant_kernel = ConstantKernel(constant_value=1.0, constant_value_bounds=(1e-05, 1e5)) + +# Fit the kernel to the data +constant_kernel.fit(X, y) + +# Make predictions using the kernel +y_pred = constant_kernel.predict(X) +Generated code for sklearn.gaussian_process.kernels.DotProduct + + +import numpy as np +from sklearn.gaussian_process.kernels import DotProduct + +# Create a DotProduct kernel +dot_product_kernel = DotProduct() + +# Generate two random vectors +x1 = np.random.rand(10) +x2 = np.random.rand(10) + +# Compute the dot product of the two vectors +dot_product = dot_product_kernel(x1, x2) + +# Print the result +print(dot_product) +Generated code for sklearn.gaussian_process.kernels.ExpSineSquared + + +import numpy as np +from sklearn.gaussian_process.kernels import ExpSineSquared + +# Define the kernel +kernel = ExpSineSquared(length_scale=1.0, periodicity=2.0, + length_scale_bounds=(1e-2, 1e3), + periodicity_bounds=(1e-2, 1e3)) + +# Generate some data +X = np.random.rand(10, 1) + +# Compute the kernel +K = kernel(X, X) + +# Print the result +print(K) +Generated code for sklearn.gaussian_process.kernels.Exponentiation + + +class Exponentiation(Kernel): + """Exponentiation kernel: k(x, y) = (sigma2)^d + where d is the degree of the polynomial and sigma2 is the variance. + Parameters + ---------- + degree : int + The degree of the polynomial. + variance : float + The variance of the kernel. + """ + + def __init__(self, degree=2, variance=1.0): + self.degree = degree + self.variance = variance + self.hyperparameters = Hyperparameter("degree", "int", (1, 10)) + self.hyperparameters.append( + Hyperparameter("variance", "float", (1e-5, 1e5)) + ) + + def __call__(self, X, Y=None, eval_gradient=False): + if Y is None: + Y = X + + d = self.degree + sigma2 = self.variance + + K = sigma2 ** d + + if eval_gradient: + if d == 1: + dK = np.zeros((X.shape[0], X.shape[0], 1)) + else: + dK = np.zeros((X.shape[0], X.shape[0], 2)) + dK[:, :, 0] = d * sigma2 ** (d - 1) + dK[:, :, 1] = d * np.log(sigma2) * sigma2 ** d + + return K, dK + else: + return K +Generated code for sklearn.gaussian_process.kernels.Hyperparameter + + +import numpy as np +from sklearn.gaussian_process.kernels import Hyperparameter + +def hyperparameter(X, Y, theta): + """ + Computes the hyperparameter for a Gaussian process. + + Parameters + ---------- + X : array-like, shape (n_samples, n_features) + The input data. + Y : array-like, shape (n_samples, n_targets) + The target values. + theta : array-like, shape (n_hyperparameters,) + The hyperparameters. + + Returns + ------- + hyperparameter : float + The hyperparameter for the Gaussian process. + """ + # Compute the kernel matrix + K = np.exp(-theta[0] * np.sum((X[:, None, :] - X[None, :, :]) ** 2, axis=-1)) + + # Compute the hyperparameter + hyperparameter = np.sum(K * Y) / np.sum(K) + + return hyperparameter +Generated code for sklearn.gaussian_process.kernels.Kernel + + +import numpy as np +from sklearn.gaussian_process.kernels import Kernel + +class MyKernel(Kernel): + def __init__(self, length_scale=1.0, length_scale_bounds=(1e-5, 1e5)): + self.length_scale = length_scale + self.length_scale_bounds = length_scale_bounds + self.hyperparameters = [ + { + 'name': 'length_scale', + 'type': 'numeric', + 'bounds': self.length_scale_bounds + } + ] + + def __call__(self, X, Y=None): + if Y is None: + Y = X + return np.exp(-0.5 * self.length_scale * np.sum((X[:, None, :] - Y[None, :, :]) ** 2, axis=-1)) + + def diag(self, X): + return np.ones(X.shape[0]) + + def is_stationary(self): + return True +Generated code for sklearn.gaussian_process.kernels.Matern + + +import numpy as np +from sklearn.gaussian_process.kernels import Matern + +# define the Matern kernel +matern_kernel = Matern(length_scale=1.0, nu=1.5) + +# generate a random sample of points +X = np.random.rand(10, 2) + +# compute the kernel matrix +K = matern_kernel(X, X) + +# print the kernel matrix +print(K) +Generated code for sklearn.gaussian_process.kernels.PairwiseKernel + + +from sklearn.gaussian_process.kernels import PairwiseKernel + +class PairwiseKernel(Kernel): + """Pairwise kernel. + + This kernel is defined as the product of two kernels, each of which is + defined on a single feature. + + Parameters + ---------- + kernel1 : Kernel + The first kernel. + kernel2 : Kernel + The second kernel. + """ + def __init__(self, kernel1, kernel2): + self.kernel1 = kernel1 + self.kernel2 = kernel2 + self.hyperparameters = self.kernel1.hyperparameters + \ + self.kernel2.hyperparameters + + def __call__(self, X, Y=None): + K1 = self.kernel1(X, Y) + K2 = self.kernel2(X, Y) + return K1 * K2 +Generated code for sklearn.gaussian_process.kernels.Product + + +from sklearn.gaussian_process.kernels import Product + +# Create a Product kernel instance +kernel = Product(k1, k2) + +# Compute the kernel value between two points +value = kernel(x1, x2) + +# Compute the gradient of the kernel value between two points +gradient = kernel.gradient(x1, x2) +Generated code for sklearn.gaussian_process.kernels.RBF + + +import numpy as np +from sklearn.gaussian_process.kernels import RBF + +# Create a radial basis function (RBF) kernel +rbf_kernel = RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e3)) + +# Generate a random sample of data +X = np.random.rand(10, 2) + +# Compute the kernel matrix +K = rbf_kernel(X, X) + +# Print the kernel matrix +print(K) +Generated code for sklearn.gaussian_process.kernels.RationalQuadratic + + +import numpy as np +from sklearn.gaussian_process.kernels import RationalQuadratic + +# define the kernel +kernel = RationalQuadratic(length_scale=1.0, alpha=1.0) + +# generate some data +X = np.random.rand(10, 2) + +# compute the kernel matrix +K = kernel(X, X) + +# print the kernel matrix +print(K) +Generated code for sklearn.gaussian_process.kernels.Sum + + +class Sum(Kernel): + """Additive kernel, also known as a linear kernel. + + The Sum kernel is a simple kernel defined as: + + .. math:: + k(x_i, x_j) = \sum_{k=1}^K k_k(x_i, x_j) + + where :math:`k_k` is a kernel function. + + Parameters + ---------- + k1, k2, ..., kn : Kernel or float + The kernels to be added. If a float is given, a ConstantKernel is + created. + + """ + def __init__(self, k1, k2, *kernels): + self.kernels = [] + for k in (k1, k2) + kernels: + if isinstance(k, Kernel): + self.kernels.append(k) + elif isinstance(k, float): + self.kernels.append(ConstantKernel(k)) + else: + raise ValueError("Expected a kernel or float") + + @property + def hyperparameters(self): + return Hyperparameter("sum_kernels", "numeric", 0, len(self.kernels)) + + def __call__(self, X, Y=None, eval_gradient=False): + if Y is None: + Y = X + + K = np.zeros((X.shape[0], Y.shape[0])) + for kernel in self.kernels: + K += kernel(X, Y, eval_gradient=eval_gradient) + + if eval_gradient: + K_gradient = np.zeros((len(self.kernels), X.shape[0], Y.shape[0])) + for i, kernel in enumerate(self.kernels): + K_gradient[i] = kernel(X, Y, eval_gradient=eval_gradient) + return K, K_gradient + else: + return K +Generated code for sklearn.gaussian_process.kernels.WhiteKernel + + +from sklearn.gaussian_process.kernels import WhiteKernel + +class WhiteKernel(WhiteKernel): + """The White kernel is a stationary kernel that can be used to + model the noise level of observations. + + Parameters + ---------- + noise_level : float, default=1.0 + Amplitude (variance) of the white noise. + + Attributes + ---------- + noise_level : float + The noise level (variance) of the kernel. + + Examples + -------- + >>> from sklearn.gaussian_process.kernels import WhiteKernel + >>> rbf = WhiteKernel(noise_level=1.0) + >>> X = [[0], [1], [2]] + >>> rbf(X, X) + array([[1. , 0.25, 0.06], + [0.25, 1. , 0.25], + [0.06, 0.25, 1. ]]) + """ + def __init__(self, noise_level=1.0): + self.noise_level = noise_level + self.theta = np.array([noise_level]) + + def __call__(self, X, Y=None): + """Return the kernel k(X, Y)""" + if Y is None: + Y = X + K = np.ones((X.shape[0], Y.shape[0])) * self.noise_level + return K +Generated code for sklearn.impute.SimpleImputer + + +#importing the SimpleImputer class from sklearn.impute +from sklearn.impute import SimpleImputer + +#creating an instance of the SimpleImputer class +imputer = SimpleImputer(missing_values=np.nan, strategy='mean') + +#fitting the imputer instance to the data +imputer.fit(X) + +#transforming the data using the imputer instance +X = imputer.transform(X) +Generated code for sklearn.impute.IterativeImputer + + +#importing the necessary libraries +import numpy as np +from sklearn.impute import IterativeImputer + +#creating a sample dataset +X = np.array([[1, 2], [3, np.nan], [7, 6], [np.nan, 9], [12, 16]]) + +#instantiating the IterativeImputer +imputer = IterativeImputer(max_iter=10, random_state=0) + +#fitting the imputer to the dataset +imputer.fit(X) + +#transforming the dataset +X_transformed = imputer.transform(X) + +#printing the transformed dataset +print(X_transformed) +Generated code for sklearn.impute.MissingIndicator + + +# Import the necessary libraries +import numpy as np +from sklearn.impute import MissingIndicator + +# Create a sample dataset +X = np.array([[1, np.nan, 3], [4, 5, np.nan], [7, 8, 9]]) + +# Create an instance of the MissingIndicator class +indicator = MissingIndicator() + +# Fit the indicator to the dataset +indicator.fit(X) + +# Transform the dataset +X_ind = indicator.transform(X) + +# Print the transformed dataset +print(X_ind) +Generated code for sklearn.impute.KNNImputer + + +from sklearn.impute import KNNImputer + +# Create an instance of KNNImputer +imputer = KNNImputer(n_neighbors=3) + +# Fit the imputer on the data +imputer.fit(X) + +# Transform the data, replacing missing values with the learned medians +X_imputed = imputer.transform(X) +Generated code for sklearn.inspection.partial_dependence + + +import numpy as np +from sklearn.inspection import partial_dependence + +def partial_dependence(estimator, X, features, response_variable=None): + """ + Compute the partial dependence of a response variable on a set of features. + + Parameters + ---------- + estimator : estimator object + This is assumed to implement the scikit-learn estimator interface. + X : array-like, shape (n_samples, n_features) + Data on which to calculate partial dependence. + features : list of length n_features + The features of interest for which to calculate partial dependence. + response_variable : string, optional (default=None) + The response variable for which to calculate partial dependence. + If None, the estimator's ``predict`` method will be used. + + Returns + ------- + partial_dependence : array-like, shape (n_features, ) + The partial dependence of the response variable on the given features. + """ + # Check that the estimator implements the scikit-learn estimator interface + if not hasattr(estimator, 'fit'): + raise TypeError('estimator must implement the scikit-learn estimator interface') + + # Check that X is a 2-dimensional array + if X.ndim != 2: + raise ValueError('X must be a 2-dimensional array') + + # Check that features is a list + if not isinstance(features, list): + raise TypeError('features must be a list') + + # Check that the length of features is equal to the number of features in X + if len(features) != X.shape[1]: + raise ValueError('length of features must be equal to the number of features in X') + + # Fit the estimator to the data + estimator.fit(X, response_variable) + + # Initialize the partial dependence array + partial_dependence = np.zeros(len(features)) + + # Iterate over the features + for i, feature in enumerate(features): + # Get the feature values for the given feature +Generated code for sklearn.inspection.permutation_importance + + +import numpy as np +from sklearn.inspection import permutation_importance + +# Create a random dataset +rng = np.random.RandomState(42) +X = rng.rand(10, 2) +y = rng.randint(2, size=(10, 1)) + +# Fit a model +clf = RandomForestClassifier(n_estimators=100, random_state=42) +clf.fit(X, y) + +# Calculate permutation importance +result = permutation_importance(clf, X, y, n_repeats=10, + random_state=42) + +# Print the results +for i in range(X.shape[1]): + print('Feature %d: %f' % (i, result.importances_mean[i])) +Generated code for sklearn.inspection.DecisionBoundaryDisplay + + +from sklearn.inspection import DecisionBoundaryDisplay + +# Create a DecisionBoundaryDisplay object +dbd = DecisionBoundaryDisplay(clf, feature_names=['x1', 'x2']) + +# Plot the decision boundary +dbd.plot() + +# Plot the decision boundary with the data points +dbd.plot(X, y) + +# Plot the decision boundary with the data points and the probabilities +dbd.plot(X, y, probabilities=True) +Generated code for sklearn.inspection.PartialDependenceDisplay + + +from sklearn.inspection import PartialDependenceDisplay + +# Create a PartialDependenceDisplay object +pdd = PartialDependenceDisplay(model, features=[0, 1]) + +# Fit the model +pdd.fit(X, y) + +# Plot the partial dependence +pdd.plot() +Generated code for sklearn.isotonic.IsotonicRegression + + +import numpy as np +from sklearn.isotonic import IsotonicRegression + +# Generate some data +x = np.arange(100) +y = np.random.randint(0, 100, size=100) + +# Fit the model +ir = IsotonicRegression() +ir.fit(x, y) + +# Make predictions +predictions = ir.predict(x) + +# Print the predictions +print(predictions) +Generated code for sklearn.isotonic.check_increasing + + +def check_increasing(x): + """ + Check if x is an increasing sequence. + + Parameters + ---------- + x : array-like + Input array. + + Returns + ------- + is_increasing : bool + True if x is an increasing sequence, False otherwise. + """ + if len(x) < 2: + return True + else: + return all(x[i] <= x[i+1] for i in range(len(x)-1)) +Generated code for sklearn.isotonic.isotonic_regression + + +import numpy as np +from sklearn.isotonic import IsotonicRegression + +# Generate data +x = np.arange(100) +y = np.random.randint(0, 100, size=100) + +# Fit the model +ir = IsotonicRegression() +y_ = ir.fit_transform(x, y) + +# Print the results +print("Original y values:", y) +print("Transformed y values:", y_) +Generated code for sklearn.kernel_approximation.AdditiveChi2Sampler + + +from sklearn.kernel_approximation import AdditiveChi2Sampler + +# Create an instance of the AdditiveChi2Sampler +sampler = AdditiveChi2Sampler(sample_steps=2) + +# Fit the sampler to the data +sampler.fit(X) + +# Transform the data using the sampler +X_transformed = sampler.transform(X) +Generated code for sklearn.kernel_approximation.Nystroem + + +from sklearn.kernel_approximation import Nystroem + +# Create a Nystroem object +nystroem = Nystroem(kernel='rbf', gamma=0.1, n_components=100) + +# Fit the Nystroem object to the data +nystroem.fit(X_train, y_train) + +# Transform the data using the Nystroem object +X_train_transformed = nystroem.transform(X_train) +X_test_transformed = nystroem.transform(X_test) + +# Train a model on the transformed data +clf = SVC(kernel='linear') +clf.fit(X_train_transformed, y_train) + +# Make predictions on the transformed data +y_pred = clf.predict(X_test_transformed) +Generated code for sklearn.kernel_approximation.PolynomialCountSketch + + +from sklearn.kernel_approximation import PolynomialCountSketch + +# Create a PolynomialCountSketch object +poly_sketch = PolynomialCountSketch(degree=2, n_components=100) + +# Fit the sketch to the data +poly_sketch.fit(X) + +# Transform the data using the sketch +X_transformed = poly_sketch.transform(X) +Generated code for sklearn.kernel_approximation.RBFSampler + + +from sklearn.kernel_approximation import RBFSampler + +# Create an instance of the RBFSampler +rbf_sampler = RBFSampler(gamma=1.0, n_components=100, random_state=1) + +# Fit the sampler to the data +rbf_sampler.fit(X) + +# Transform the data using the fitted sampler +X_transformed = rbf_sampler.transform(X) +Generated code for sklearn.kernel_approximation.SkewedChi2Sampler + + +from sklearn.kernel_approximation import SkewedChi2Sampler + +# Create a SkewedChi2Sampler object +skewed_chi2_sampler = SkewedChi2Sampler(skew=0.5) + +# Fit the sampler to the data +skewed_chi2_sampler.fit(X) + +# Transform the data using the fitted sampler +X_transformed = skewed_chi2_sampler.transform(X) +Generated code for sklearn.kernel_ridge.KernelRidge + + +from sklearn.kernel_ridge import KernelRidge + +# Create a KernelRidge object +kr = KernelRidge(alpha=1.0, kernel='rbf', gamma=0.1) + +# Fit the model to the data +kr.fit(X, y) + +# Make predictions +y_pred = kr.predict(X_test) +Generated code for sklearn.linear_model.LogisticRegression + + +# Import the necessary libraries +from sklearn.linear_model import LogisticRegression + +# Create an instance of the LogisticRegression class +logreg = LogisticRegression() + +# Fit the model to the training data +logreg.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = logreg.predict(X_test) + +# Calculate the accuracy of the model +accuracy = logreg.score(X_test, y_test) +Generated code for sklearn.linear_model.LogisticRegressionCV + + +import numpy as np +from sklearn.linear_model import LogisticRegressionCV + +# Create a logistic regression classifier +log_reg = LogisticRegressionCV(cv=5, random_state=0) + +# Train the classifier +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) +y = np.array([0, 1, 0, 1]) +log_reg.fit(X, y) + +# Make predictions +predictions = log_reg.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.PassiveAggressiveClassifier + + +# Importing the necessary libraries +from sklearn.linear_model import PassiveAggressiveClassifier +from sklearn.datasets import make_classification + +# Generating a sample dataset +X, y = make_classification(n_samples=1000, n_features=4, + n_informative=2, n_redundant=0, + random_state=0, shuffle=False) + +# Instantiating the PassiveAggressiveClassifier +pac = PassiveAggressiveClassifier(max_iter=1000, tol=1e-3) + +# Fitting the model +pac.fit(X, y) + +# Making predictions +y_pred = pac.predict(X) +Generated code for sklearn.linear_model.Perceptron + + +from sklearn.linear_model import Perceptron + +# Create a Perceptron object +clf = Perceptron() + +# Train the model using the training sets +clf.fit(X_train, y_train) + +# Predict the response for test dataset +y_pred = clf.predict(X_test) +Generated code for sklearn.linear_model.RidgeClassifier + + +from sklearn.linear_model import RidgeClassifier + +# Create an instance of the RidgeClassifier +clf = RidgeClassifier() + +# Fit the model using the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Evaluate the model +accuracy = clf.score(X_test, y_test) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.linear_model.RidgeClassifierCV + + +from sklearn.linear_model import RidgeClassifierCV + +# Create an instance of the RidgeClassifierCV class +clf = RidgeClassifierCV() + +# Fit the model to the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Calculate the accuracy of the model +accuracy = clf.score(X_test, y_test) + +# Print the accuracy +print(accuracy) +Generated code for sklearn.linear_model.SGDClassifier + + +# Import the SGDClassifier +from sklearn.linear_model import SGDClassifier + +# Create an instance of the SGDClassifier +clf = SGDClassifier() + +# Fit the model using the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Evaluate the model +accuracy = clf.score(X_test, y_test) +print("Accuracy: %.2f%%" % (accuracy * 100.0)) +Generated code for sklearn.linear_model.SGDOneClassSVM + + +from sklearn.linear_model import SGDClassifier + +# Create an instance of the SGDClassifier +sgd_clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=1000, tol=1e-3) + +# Fit the model to the data +sgd_clf.fit(X_train, y_train) + +# Make predictions +y_pred = sgd_clf.predict(X_test) + +# Evaluate the model +from sklearn.metrics import accuracy_score +accuracy = accuracy_score(y_test, y_pred) +print("Accuracy:", accuracy) +Generated code for sklearn.linear_model.LinearRegression + + +#importing the necessary libraries +import numpy as np +from sklearn.linear_model import LinearRegression + +#creating the data +x = np.array([[1, 2], [3, 4], [5, 6]]) +y = np.array([7, 8, 9]) + +#creating the model +model = LinearRegression() + +#fitting the model +model.fit(x, y) + +#predicting the output +prediction = model.predict([[7, 8]]) + +#printing the output +print(prediction) +Generated code for sklearn.linear_model.Ridge + + +# Import the necessary libraries +import numpy as np +from sklearn.linear_model import Ridge + +# Create the data +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +y = np.array([1, 2, 3]) + +# Create the Ridge model +model = Ridge() + +# Fit the model +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.RidgeCV + + +#importing the necessary libraries +import numpy as np +from sklearn.linear_model import RidgeCV + +#creating the data +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) +y = np.array([1, 3, 5, 7]) + +#creating the RidgeCV model +model = RidgeCV(alphas=[0.1, 1.0, 10.0]) + +#fitting the model +model.fit(X, y) + +#predicting the output +predictions = model.predict(X) + +#printing the predictions +print(predictions) +Generated code for sklearn.linear_model.SGDRegressor + + +from sklearn.linear_model import SGDRegressor + +# Create an instance of the SGDRegressor +sgd_reg = SGDRegressor() + +# Fit the model to the training data +sgd_reg.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = sgd_reg.predict(X_test) + +# Calculate the mean squared error +mse = mean_squared_error(y_test, y_pred) + +# Print the mean squared error +print(mse) +Generated code for sklearn.linear_model.ElasticNet + + +#importing the necessary libraries +import numpy as np +from sklearn.linear_model import ElasticNet + +#creating the data +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) +y = np.array([1, 2, 3, 4]) + +#creating the model +model = ElasticNet(alpha=0.1, l1_ratio=0.5) + +#fitting the model +model.fit(X, y) + +#predicting the output +predictions = model.predict(X) + +#printing the predictions +print(predictions) +Generated code for sklearn.linear_model.ElasticNetCV + + +from sklearn.linear_model import ElasticNetCV + +# Create an instance of ElasticNetCV +model = ElasticNetCV(cv=5, random_state=0) + +# Fit the model to the data +model.fit(X, y) + +# Print the optimal value of alpha +print("Optimal alpha value:", model.alpha_) + +# Print the optimal value of l1_ratio +print("Optimal l1_ratio:", model.l1_ratio_) + +# Print the coefficients of the model +print("Coefficients:", model.coef_) + +# Print the intercept of the model +print("Intercept:", model.intercept_) + +# Make predictions using the model +predictions = model.predict(X) +Generated code for sklearn.linear_model.Lars + + +import numpy as np +from sklearn.linear_model import Lars + +# Create a sample dataset +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +y = np.array([1, 2, 3]) + +# Create and fit the model +model = Lars() +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.LarsCV + + +import numpy as np +from sklearn.linear_model import LarsCV + +# Create a random dataset +rng = np.random.RandomState(42) +X = rng.rand(100, 10) +y = rng.rand(100) + +# Create the LarsCV model +model = LarsCV(cv=5, max_iter=500).fit(X, y) + +# Print the model coefficients +print(model.coef_) +Generated code for sklearn.linear_model.Lasso + + +# Import the necessary libraries +import numpy as np +from sklearn.linear_model import Lasso + +# Create the data +x = np.array([[1,2,3], [4,5,6], [7,8,9]]) +y = np.array([1,2,3]) + +# Create the model +model = Lasso() + +# Fit the model +model.fit(x, y) + +# Make predictions +predictions = model.predict(x) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.LassoCV + + +from sklearn.linear_model import LassoCV + +# Create an instance of LassoCV +reg = LassoCV() + +# Fit the model to the data +reg.fit(X, y) + +# Print the coefficients +print(reg.coef_) + +# Make predictions +predictions = reg.predict(X) + +# Calculate the mean squared error +mse = mean_squared_error(y, predictions) + +# Print the mean squared error +print(mse) +Generated code for sklearn.linear_model.LassoLars + + +import numpy as np +from sklearn.linear_model import LassoLars + +# Create a random dataset +x = np.random.rand(100, 5) +y = np.random.rand(100) + +# Create and fit the LassoLars model +model = LassoLars(alpha=0.1) +model.fit(x, y) + +# Make predictions +predictions = model.predict(x) + +# Print the coefficients +print(model.coef_) +Generated code for sklearn.linear_model.LassoLarsCV + + +from sklearn.linear_model import LassoLarsCV + +# Create an instance of the LassoLarsCV model +model = LassoLarsCV() + +# Fit the model to the data +model.fit(X, y) + +# Make predictions using the model +predictions = model.predict(X) + +# Print the model's coefficients +print(model.coef_) +Generated code for sklearn.linear_model.LassoLarsIC + + +from sklearn.linear_model import LassoLarsIC + +# Create an instance of the LassoLarsIC model +model = LassoLarsIC() + +# Fit the model to the data +model.fit(X, y) + +# Make predictions using the model +predictions = model.predict(X) + +# Evaluate the model +score = model.score(X, y) +Generated code for sklearn.linear_model.OrthogonalMatchingPursuit + + +from sklearn.linear_model import OrthogonalMatchingPursuit + +# Create an instance of the OrthogonalMatchingPursuit model +model = OrthogonalMatchingPursuit() + +# Fit the model to the data +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Evaluate the model +score = model.score(X, y) +Generated code for sklearn.linear_model.OrthogonalMatchingPursuitCV + + +from sklearn.linear_model import OrthogonalMatchingPursuitCV + +# Create an instance of the OrthogonalMatchingPursuitCV class +omp_cv = OrthogonalMatchingPursuitCV(cv=5, n_nonzero_coefs=10) + +# Fit the model to the data +omp_cv.fit(X, y) + +# Get the coefficients of the model +coefs = omp_cv.coef_ + +# Get the intercept of the model +intercept = omp_cv.intercept_ + +# Get the score of the model +score = omp_cv.score(X, y) +Generated code for sklearn.linear_model.ARDRegression + + +# Import the necessary libraries +import numpy as np +from sklearn.linear_model import ARDRegression + +# Create the data +X = np.array([[1, 2], [3, 4], [5, 6]]) +y = np.array([7, 8, 9]) + +# Create the ARD Regression model +model = ARDRegression() + +# Fit the model +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.BayesianRidge + + +# Import the necessary libraries +import numpy as np +from sklearn.linear_model import BayesianRidge + +# Create the data +X = np.array([[1, 2], [3, 4], [5, 6]]) +y = np.array([7, 8, 9]) + +# Create the Bayesian Ridge model +model = BayesianRidge() + +# Fit the model +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.MultiTaskElasticNet + + +import numpy as np +from sklearn.linear_model import MultiTaskElasticNet + +# Create some sample data +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +y = np.array([[1, 2], [3, 4], [5, 6]]) + +# Create and fit the model +model = MultiTaskElasticNet(alpha=1.0, l1_ratio=0.5) +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.MultiTaskElasticNetCV + + +from sklearn.linear_model import MultiTaskElasticNetCV + +# Create the model +model = MultiTaskElasticNetCV(cv=5, random_state=0) + +# Fit the model +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Get the coefficients +coefficients = model.coef_ + +# Get the intercept +intercept = model.intercept_ +Generated code for sklearn.linear_model.MultiTaskLasso + + +from sklearn.linear_model import MultiTaskLasso + +# Create the model +model = MultiTaskLasso() + +# Fit the model +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Evaluate the model +score = model.score(X, y) +Generated code for sklearn.linear_model.MultiTaskLassoCV + + +import numpy as np +from sklearn.linear_model import MultiTaskLassoCV + +# Generate some random data +n_samples, n_features, n_tasks = 100, 30, 2 +rng = np.random.RandomState(0) +X = rng.randn(n_samples, n_features) +Y = rng.randn(n_samples, n_tasks) + +# Create and fit the model +model = MultiTaskLassoCV(cv=5, random_state=0).fit(X, Y) + +# Print the coefficients +print(model.coef_) +Generated code for sklearn.linear_model.HuberRegressor + + +# Import the necessary libraries +import numpy as np +from sklearn.linear_model import HuberRegressor + +# Create the data +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) +y = np.array([1, 2, 3, 4]) + +# Create the HuberRegressor object +huber_reg = HuberRegressor() + +# Fit the model +huber_reg.fit(X, y) + +# Make predictions +predictions = huber_reg.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.linear_model.QuantileRegressor + + +import numpy as np +from sklearn.linear_model import QuantileRegressor + +# Generate some data +X = np.random.rand(100, 1) +y = np.sin(X).ravel() + +# Fit the model +quantile_regressor = QuantileRegressor() +quantile_regressor.fit(X, y) + +# Make predictions +y_pred = quantile_regressor.predict(X) +Generated code for sklearn.linear_model.RANSACRegressor + + +import numpy as np +from sklearn.linear_model import RANSACRegressor + +# Generate some random data +x = np.random.rand(100, 1) +y = 2 * x + np.random.rand(100, 1) + +# Create and fit the RANSAC regressor +ransac = RANSACRegressor() +ransac.fit(x, y) + +# Predict the values +y_pred = ransac.predict(x) + +# Calculate the score +score = ransac.score(x, y) + +# Print the results +print("RANSAC score:", score) +Generated code for sklearn.linear_model.TheilSenRegressor + + +#importing the necessary libraries +import numpy as np +from sklearn.linear_model import TheilSenRegressor + +#creating the data +x = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]) +y = np.array([1, 3, 5, 7, 9]) + +#creating the model +model = TheilSenRegressor() + +#fitting the model +model.fit(x, y) + +#predicting the output +prediction = model.predict(x) + +#printing the output +print(prediction) +Generated code for sklearn.linear_model.PoissonRegressor + + +from sklearn.linear_model import PoissonRegressor + +# Create the PoissonRegressor object +regressor = PoissonRegressor() + +# Fit the model to the data +regressor.fit(X, y) + +# Make predictions +predictions = regressor.predict(X_test) + +# Evaluate the model +score = regressor.score(X_test, y_test) +Generated code for sklearn.linear_model.TweedieRegressor + + +from sklearn.linear_model import TweedieRegressor + +# Create a TweedieRegressor object +regressor = TweedieRegressor(power=1.5, alpha=0.1, max_iter=1000) + +# Fit the model to the data +regressor.fit(X, y) + +# Make predictions +y_pred = regressor.predict(X) + +# Calculate the mean squared error +mse = mean_squared_error(y, y_pred) + +# Print the mean squared error +print(mse) +Generated code for sklearn.linear_model.GammaRegressor + + +import numpy as np +from sklearn.linear_model import GammaRegressor + +# Generate some sample data +X = np.random.rand(100, 1) +y = np.random.rand(100, 1) + +# Create the GammaRegressor model +model = GammaRegressor() + +# Fit the model to the data +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) +Generated code for sklearn.linear_model.PassiveAggressiveRegressor + + +# Importing the necessary libraries +import numpy as np +from sklearn.linear_model import PassiveAggressiveRegressor + +# Creating the dataset +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]) +y = np.array([1, 3, 5, 7, 9]) + +# Creating the model +model = PassiveAggressiveRegressor() + +# Fitting the model +model.fit(X, y) + +# Making predictions +predictions = model.predict(X) + +# Printing the predictions +print(predictions) +Generated code for sklearn.linear_model.enet_path + + +# Import necessary modules +from sklearn.linear_model import enet_path +from sklearn.datasets import make_regression + +# Generate simulated data +X, y = make_regression(n_samples=100, n_features=2, random_state=0) + +# Compute paths +_, coefs_path, _ = enet_path(X, y, l1_ratio=0.8, fit_intercept=False) + +# Print the coefficients along the path +print(coefs_path.shape) + +# Plot the paths +import matplotlib.pyplot as plt +plt.plot(coefs_path.T) +plt.xlabel('Lambda') +plt.ylabel('Coefficients') +plt.show() +Generated code for sklearn.linear_model.lars_path + + +# Importing the necessary libraries +import numpy as np +from sklearn.linear_model import Lars +from sklearn.metrics import mean_squared_error + +# Generating the data +np.random.seed(42) +X = np.random.rand(100, 1) +y = 4 + 3 * X + np.random.randn(100, 1) + +# Fitting the model +lars_reg = Lars(n_nonzero_coefs=1) +lars_reg.fit(X, y) + +# Making predictions +y_pred = lars_reg.predict(X) + +# Calculating the mean squared error +mse = mean_squared_error(y, y_pred) +print(f'Mean Squared Error: {mse}') +Generated code for sklearn.linear_model.lars_path_gram + + +import numpy as np +from sklearn.linear_model import Lars + +# Create a Lars object +lars_model = Lars() + +# Fit the model to the data +lars_model.fit(X, y) + +# Compute the lars_path_gram +lars_path_gram = lars_model.lars_path_gram(X, y) + +# Print the lars_path_gram +print(lars_path_gram) +Generated code for sklearn.linear_model.lasso_path + + +# Import necessary libraries +import numpy as np +from sklearn.linear_model import Lasso +from sklearn.linear_model.coordinate_descent import lasso_path + +# Create a random dataset +n_samples, n_features = 10, 5 +np.random.seed(0) +y = np.random.randn(n_samples) +X = np.random.randn(n_samples, n_features) + +# Compute paths +eps = 5e-3 # the smaller it is the longer is the path + +print("Computing regularization path using the lasso...") +alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False) + +# Display results +print("Alphas:") +print(alphas_lasso) +print("Coefficients:") +print(coefs_lasso) +Generated code for sklearn.linear_model.orthogonal_mp + + +import numpy as np +from sklearn.linear_model import OrthogonalMatchingPursuit + +# Create a random matrix +X = np.random.rand(100, 10) + +# Create a random vector +y = np.random.rand(100) + +# Create an instance of OrthogonalMatchingPursuit +omp = OrthogonalMatchingPursuit(n_nonzero_coefs=5) + +# Fit the model +omp.fit(X, y) + +# Get the coefficients +coef = omp.coef_ + +# Get the intercept +intercept = omp.intercept_ +Generated code for sklearn.linear_model.orthogonal_mp_gram + + +import numpy as np +from sklearn.linear_model import OrthogonalMatchingPursuitGram + +# Generate some random data +n_samples, n_features = 10, 5 +X = np.random.randn(n_samples, n_features) +y = np.random.randn(n_samples) + +# Create the Orthogonal Matching Pursuit model +omp_gram = OrthogonalMatchingPursuitGram(n_nonzero_coefs=3) + +# Fit the model +omp_gram.fit(X, y) + +# Print the coefficients +print(omp_gram.coef_) +Generated code for sklearn.linear_model.ridge_regression + + +# Importing necessary libraries +import numpy as np +from sklearn.linear_model import Ridge + +# Creating data +x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) +y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12]) + +# Creating the model +model = Ridge() + +# Fitting the model +model.fit(x.reshape(-1, 1), y.reshape(-1, 1)) + +# Printing the coefficients +print(model.coef_) +print(model.intercept_) + +# Predicting the output +predicted_output = model.predict(x.reshape(-1, 1)) + +# Printing the predicted output +print(predicted_output) +Generated code for sklearn.manifold.Isomap + + +import numpy as np +from sklearn.manifold import Isomap + +# Create a 2D array of points +points = np.array([[1,2], [3,4], [5,6], [7,8], [9,10]]) + +# Create an Isomap object +isomap = Isomap(n_components=2) + +# Fit the Isomap object to the points +isomap.fit(points) + +# Transform the points using the Isomap object +transformed_points = isomap.transform(points) + +# Print the transformed points +print(transformed_points) +Generated code for sklearn.manifold.LocallyLinearEmbedding + + +from sklearn.manifold import LocallyLinearEmbedding + +# Create the LocallyLinearEmbedding object +lle = LocallyLinearEmbedding(n_components=2, n_neighbors=10, random_state=42) + +# Fit the model to the data +lle.fit(X) + +# Transform the data +X_transformed = lle.transform(X) +Generated code for sklearn.manifold.MDS + + +from sklearn.manifold import MDS + +# Create an MDS object +mds = MDS(n_components=2, random_state=0) + +# Fit the data to the MDS object +mds_fit = mds.fit_transform(data) + +# Print the results +print(mds_fit) +Generated code for sklearn.manifold.SpectralEmbedding + + +import numpy as np +from sklearn.manifold import SpectralEmbedding + +# Create a random matrix of size (n_samples, n_features) +X = np.random.rand(n_samples, n_features) + +# Create a SpectralEmbedding object +se = SpectralEmbedding(n_components=2, random_state=0) + +# Fit the data to the model +X_transformed = se.fit_transform(X) +Generated code for sklearn.manifold.TSNE + + +import numpy as np +from sklearn.manifold import TSNE + +# Create a random array of 3-dimensional points +X = np.random.rand(100, 3) + +# Initialize and fit the model +model = TSNE(n_components=2, random_state=0) +transformed = model.fit_transform(X) + +# Print the transformed data +print(transformed) +Generated code for sklearn.manifold.locally_linear_embedding + + +from sklearn.manifold import LocallyLinearEmbedding + +# Create the LocallyLinearEmbedding object +lle = LocallyLinearEmbedding(n_neighbors=10, n_components=2, random_state=42) + +# Fit the model to the data +lle.fit(X) + +# Transform the data +X_lle = lle.transform(X) +Generated code for sklearn.manifold.smacof + + +import numpy as np +from sklearn.manifold import smacof + +# Generate random data +X = np.random.rand(100, 3) + +# Compute SMACOF +Y = smacof(X, metric=True, n_components=2) + +# Print the result +print(Y) +Generated code for sklearn.manifold.spectral_embedding + + +import numpy as np +from sklearn.manifold import spectral_embedding + +# Generate a random graph +A = np.random.rand(10, 10) + +# Compute the spectral embedding of the graph +embedding = spectral_embedding(A, n_components=2) + +# Print the embedding +print(embedding) +Generated code for sklearn.manifold.trustworthiness + + +import numpy as np +from sklearn.manifold import trustworthiness + +# Generate some random data +X = np.random.rand(100, 10) + +# Compute trustworthiness +trustworthiness_score = trustworthiness(X) + +print("Trustworthiness score:", trustworthiness_score) +Generated code for sklearn.metrics.check_scoring + + +import numpy as np +from sklearn.metrics import check_scoring + +# Generate some random data +X = np.random.rand(100, 10) +y = np.random.randint(2, size=100) + +# Define a scoring function +def my_custom_loss_func(ground_truth, predictions): + return np.mean(np.abs(ground_truth - predictions)) + +# Check if the scoring function is valid +scorers = check_scoring(my_custom_loss_func, X, y) + +# Print the results +print(scorers) +Generated code for sklearn.metrics.get_scorer + + +from sklearn.metrics import get_scorer + +def get_scorer(scoring, **kwargs): + """ + Get a scorer from a string or a callable. + + Parameters + ---------- + scoring : string, callable or None, optional (default=None) + A string (see model evaluation documentation) or + a scorer callable object / function with signature + ``scorer(estimator, X, y)``. + If None, the estimator's default scorer (if available) is used. + + **kwargs : additional arguments + Additional keyword arguments to be passed to the scorer + callable. + + Returns + ------- + scoring : callable + A scorer callable object / function with signature + ``scorer(estimator, X, y)``. + """ + if callable(scoring): + return scoring + elif isinstance(scoring, str): + return SCORERS[scoring](**kwargs) + else: + raise ValueError('scoring must be a string or a callable.') +Generated code for sklearn.metrics.get_scorer_names + + +import sklearn.metrics + +def get_scorer_names(): + return sklearn.metrics.SCORERS.keys() + +scorer_names = get_scorer_names() +print(scorer_names) +Generated code for sklearn.metrics.make_scorer + + +from sklearn.metrics import make_scorer + +def custom_scorer(y_true, y_pred): + # Calculate the score + score = 0 + # Loop through each prediction + for i in range(len(y_true)): + # If the prediction is correct + if y_true[i] == y_pred[i]: + # Increment the score + score += 1 + # Return the score + return score + +# Create the scorer +scorer = make_scorer(custom_scorer) +Generated code for sklearn.metrics.accuracy_score + + +def accuracy_score(y_true, y_pred): + """ + Calculates the accuracy score between two arrays of labels. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) labels. + + y_pred : array-like of shape (n_samples,) + Predicted labels, as returned by a classifier. + + Returns + ------- + score : float + The accuracy score between y_true and y_pred. + """ + # Check that y_true and y_pred have the same shape + if y_true.shape != y_pred.shape: + raise ValueError("y_true and y_pred must have the same shape") + + # Calculate the accuracy score + score = np.sum(y_true == y_pred) / len(y_true) + + return score +Generated code for sklearn.metrics.auc + + +import numpy as np +from sklearn.metrics import auc + +# Generate some random data +y_true = np.array([0, 0, 1, 1]) +y_scores = np.array([0.1, 0.4, 0.35, 0.8]) + +# Calculate the AUC +auc_score = auc(y_true, y_scores) + +print('AUC: %.3f' % auc_score) +Generated code for sklearn.metrics.average_precision_score + + +from sklearn.metrics import average_precision_score + +def average_precision_score(y_true, y_score): + """ + Compute average precision (AP) from prediction scores + Parameters + ---------- + y_true : array-like, shape = [n_samples] + True binary labels. + y_score : array-like, shape = [n_samples] + Target scores, can either be probability estimates of the positive + class, confidence values, or non-thresholded measure of decisions + (as returned by "decision_function" on some classifiers). + Returns + ------- + average_precision : float + """ + # Check that y_true and y_score have the same shape + if y_true.shape != y_score.shape: + raise ValueError("y_true and y_score have different shapes") + + # Sort y_score and corresponding y_true by the descending order of y_score + sorted_indices = np.argsort(y_score)[::-1] + y_true = y_true[sorted_indices] + y_score = y_score[sorted_indices] + + # Compute the precision at each threshold + precision = [] + for i in range(len(y_true)): + # Count the number of true positives + true_positives = np.sum(y_true[:i+1]) + # Count the number of false positives + false_positives = i + 1 - true_positives + # Compute precision + precision.append(true_positives / (true_positives + false_positives)) + + # Compute the average precision + average_precision = np.mean(precision) + + return average_precision +Generated code for sklearn.metrics.balanced_accuracy_score + + +def balanced_accuracy_score(y_true, y_pred, sample_weight=None): + """Compute the balanced accuracy + The balanced accuracy in binary and multiclass classification problems to + deal with imbalanced datasets. It is defined as the average of recall + obtained on each class. + +The best value is 1 and the worst value is 0 when ``normalize == True``. + +Read more in the :ref:`User Guide `. + +Parameters +---------- +y_true : 1d array-like, or label indicator array / sparse matrix +Ground truth (correct) target values. + +y_pred : 1d array-like, or label indicator array / sparse matrix +Estimated targets as returned by a classifier. + +sample_weight : array-like of shape (n_samples,), default=None +Sample weights. + +Returns +------- +balanced_accuracy : float + +Examples +-------- +>>> from sklearn.metrics import balanced_accuracy_score +>>> y_true = [0, 1, 0, 0, 1, 0] +>>> y_pred = [0, 1, 0, 0, 0, 1] +>>> balanced_accuracy_score(y_true, y_pred) +0.6666666666666666 + +""" + y_type, y_true, y_pred = _check_targets(y_true, y_pred) + check_consistent_length(y_true, y_pred, sample_weight) + if sample_weight is not None: + sample_weight = column_or_1d(sample_weight) + n_classes = len(np.unique(y_true)) + if n_classes <= 1: + raise ValueError("y_true has less than 2 classes and the " + "balanced_accuracy_score is not defined.") + + # Calculate the accuracy for each class + accuracy_per_class = np.zeros(n_classes, dtype=np.float64) + for class_idx in range(n_classes): + class_accuracy = accuracy_score(y_true, y_pred, + normalize=True, +Generated code for sklearn.metrics.brier_score_loss + + +import numpy as np +from sklearn.metrics import brier_score_loss + +# Generate some random data +y_true = np.random.randint(2, size=10) +y_prob = np.random.random(10) + +# Calculate the Brier score loss +brier_score_loss(y_true, y_prob) +Generated code for sklearn.metrics.class_likelihood_ratios + + +import numpy as np +from sklearn.metrics import confusion_matrix + +def class_likelihood_ratios(y_true, y_pred): + """ + Computes the class likelihood ratios for a given set of true and predicted labels. + + Parameters + ---------- + y_true : array-like, shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like, shape (n_samples,) + Estimated targets as returned by a classifier. + + Returns + ------- + class_likelihood_ratios : array-like, shape (n_classes,) + The class likelihood ratios for each class. + """ + cm = confusion_matrix(y_true, y_pred) + n_classes = cm.shape[0] + class_likelihood_ratios = np.zeros(n_classes) + for i in range(n_classes): + tp = cm[i,i] + fp = np.sum(cm[:,i]) - tp + fn = np.sum(cm[i,:]) - tp + tn = np.sum(cm) - tp - fp - fn + class_likelihood_ratios[i] = (tp*tn)/(fp*fn) + return class_likelihood_ratios +Generated code for sklearn.metrics.classification_report + + +from sklearn.metrics import classification_report + +y_true = [0, 1, 2, 2, 2] +y_pred = [0, 0, 2, 2, 1] +target_names = ['class 0', 'class 1', 'class 2'] + +print(classification_report(y_true, y_pred, target_names=target_names)) + +# Output +# precision recall f1-score support +# +# class 0 0.50 1.00 0.67 1 +# class 1 0.00 0.00 0.00 1 +# class 2 1.00 0.67 0.80 3 +# +# accuracy 0.60 5 +# macro avg 0.50 0.56 0.49 5 +#weighted avg 0.70 0.60 0.61 5 +Generated code for sklearn.metrics.cohen_kappa_score + + +import numpy as np +from sklearn.metrics import cohen_kappa_score + +y_true = np.array([0, 1, 2, 0, 1, 2]) +y_pred = np.array([0, 2, 1, 0, 0, 1]) + +cohen_kappa_score(y_true, y_pred) + +# Output: 0.3333333333333333 +Generated code for sklearn.metrics.confusion_matrix + + +import numpy as np +from sklearn.metrics import confusion_matrix + +# Create a confusion matrix +y_true = [2, 0, 2, 2, 0, 1] +y_pred = [0, 0, 2, 2, 0, 2] +confusion_matrix = confusion_matrix(y_true, y_pred) + +# Print the confusion matrix +print(confusion_matrix) + +# Output +# [[2 0 0] +# [0 0 1] +# [1 0 2]] +Generated code for sklearn.metrics.dcg_score + + +def dcg_score(y_true, y_score, k=None, gains="exponential"): + """Discounted cumulative gain (DCG) + Parameters + ---------- + y_true : array, shape = [n_samples] + Ground truth (true relevance labels). + y_score : array, shape = [n_samples] + Predicted scores. + k : int + Number of results to consider. If None, all results are considered. + gains : str + Whether gains should be "exponential" (default) or "linear". + Returns + ------- + DCG : float + """ + order = np.argsort(y_score)[::-1] + y_true = np.take(y_true, order[:k]) + + if gains == "exponential": + gains = 2 ** y_true - 1 + elif gains == "linear": + gains = y_true + else: + raise ValueError("Invalid gains option.") + + # highest rank is 1 so +2 instead of +1 + discounts = np.log2(np.arange(len(y_true)) + 2) + return np.sum(gains / discounts) +Generated code for sklearn.metrics.det_curve + + +import numpy as np +from sklearn.metrics import precision_recall_curve + +# Generate some data +y_true = np.array([0, 0, 1, 1]) +y_scores = np.array([0.1, 0.4, 0.35, 0.8]) + +# Calculate precision-recall curve +precision, recall, thresholds = precision_recall_curve(y_true, y_scores) + +# Plot precision-recall curve +import matplotlib.pyplot as plt +plt.plot(recall, precision) +plt.xlabel('Recall') +plt.ylabel('Precision') +plt.title('Precision-Recall Curve') +plt.show() +Generated code for sklearn.metrics.f1_score + + +from sklearn.metrics import f1_score + +# example data +y_true = [0, 1, 1, 0, 1, 0, 1, 0, 0, 1] +y_pred = [0, 0, 1, 0, 0, 0, 1, 1, 1, 1] + +# calculate F1 score +f1 = f1_score(y_true, y_pred) + +print('F1 score: %f' % f1) +Generated code for sklearn.metrics.fbeta_score + + +def fbeta_score(y_true, y_pred, beta): + """ + Computes the F-beta score, which is a weighted harmonic mean of precision and recall. + The F-beta score is the weighted harmonic mean of precision and recall, reaching its + optimal value at 1 and its worst value at 0. + The beta parameter determines the weight of precision in the combined score. + If beta < 1, precision is favored. If beta > 1, recall is favored. + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated targets as returned by a classifier. + beta : float + Weight of precision in harmonic mean. + Returns + ------- + score : float + The F-beta score. + """ + # Calculate precision and recall + precision = precision_score(y_true, y_pred) + recall = recall_score(y_true, y_pred) + + # Calculate F-beta score + score = (1 + beta**2) * (precision * recall) / (beta**2 * precision + recall) + + return score +Generated code for sklearn.metrics.hamming_loss + + +from sklearn.metrics import hamming_loss + +def hamming_loss(y_true, y_pred): + """ + Computes the Hamming loss. + The Hamming loss is the fraction of labels that are incorrectly predicted. + Parameters + ---------- + y_true : array-like of shape = [n_samples] or [n_samples, n_classes] + Ground truth (correct) labels. + y_pred : array-like of shape = [n_samples] or [n_samples, n_classes] + Predicted labels, as returned by a classifier. + Returns + ------- + loss : float + The Hamming loss. + """ + y_true, y_pred = check_arrays(y_true, y_pred) + if y_true.shape != y_pred.shape: + raise ValueError("y_true and y_pred must have the same shape") + if y_true.ndim == 1: + y_true = np.reshape(y_true, (-1, 1)) + if y_pred.ndim == 1: + y_pred = np.reshape(y_pred, (-1, 1)) + if y_true.shape[1] == 1: + y_true = np.append(1 - y_true, y_true, axis=1) + if y_pred.shape[1] == 1: + y_pred = np.append(1 - y_pred, y_pred, axis=1) + n_samples, n_classes = y_true.shape + incorrect_labels = np.sum(y_true != y_pred) + loss = incorrect_labels / (n_samples * n_classes) + return loss +Generated code for sklearn.metrics.hinge_loss + + +def hinge_loss(y_true, y_pred): + loss = np.maximum(1 - y_true * y_pred, 0) + return np.mean(loss) +Generated code for sklearn.metrics.jaccard_score + + +import numpy as np +from sklearn.metrics import jaccard_score + +y_true = np.array([0, 0, 1, 1]) +y_pred = np.array([0, 1, 0, 1]) + +jaccard_score(y_true, y_pred) + +# Output: 0.5 +Generated code for sklearn.metrics.log_loss + + +def log_loss(y_true, y_pred, eps=1e-15): + """ + Computes the logarithmic loss between y_true and y_pred. + Parameters + ---------- + y_true : array, shape = [n_samples] + Ground truth (correct) labels. + y_pred : array, shape = [n_samples] + Predicted labels, as returned by a classifier. + eps : float + Log loss is undefined for p=0 or p=1, so probabilities are + clipped to max(eps, min(1 - eps, p)). + Returns + ------- + loss : float + The logarithmic loss between y_true and y_pred. + """ + y_pred = np.clip(y_pred, eps, 1 - eps) + loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)) + return np.mean(loss) +Generated code for sklearn.metrics.matthews_corrcoef + + +def matthews_corrcoef(y_true, y_pred): + """Calculate Matthews correlation coefficient (MCC) + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated targets as returned by a classifier. + Returns + ------- + mcc : float + The Matthews correlation coefficient (+1 represents a perfect + prediction, 0 an average random prediction and -1 an inverse + prediction). + """ + y_true = np.asarray(y_true) + y_pred = np.asarray(y_pred) + + # Compute confusion matrix + tp, fp, fn, tn = confusion_matrix(y_true, y_pred).ravel() + + # Calculate MCC + mcc = ((tp * tn) - (fp * fn)) / np.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)) + + return mcc +Generated code for sklearn.metrics.multilabel_confusion_matrix + + +import numpy as np +from sklearn.metrics import multilabel_confusion_matrix + +# Generate some random data +y_true = np.array([[0, 1, 0], [1, 0, 1]]) +y_pred = np.array([[1, 0, 0], [1, 0, 1]]) + +# Calculate the multilabel confusion matrix +conf_matrix = multilabel_confusion_matrix(y_true, y_pred) + +# Print the confusion matrix +print(conf_matrix) +Generated code for sklearn.metrics.ndcg_score + + +import numpy as np +from sklearn.metrics import ndcg_score + +# Generate some dummy data +y_true = np.array([1, 0, 0, 1, 0]) +y_pred = np.array([0.75, 0.5, 0.25, 0.75, 0.25]) + +# Calculate the NDCG score +ndcg_score(y_true, y_pred) + +# Output: 0.8333333333333334 +Generated code for sklearn.metrics.precision_recall_curve + + +from sklearn.metrics import precision_recall_curve + +def precision_recall_curve(y_true, y_score): + """ + Compute precision-recall curve + Parameters + ---------- + y_true : array, shape = [n_samples] + True binary labels. + y_score : array, shape = [n_samples] + Target scores, can either be probability estimates of the positive + class, confidence values, or non-thresholded measure of decisions + (as returned by "decision_function" on some classifiers). + Returns + ------- + precision : array, shape = [n_thresholds + 1] + Precision values such that element i is the precision of + predictions with score >= thresholds[i] and the last element is 1. + recall : array, shape = [n_thresholds + 1] + Decreasing recall values such that element i is the recall of + predictions with score >= thresholds[i] and the last element is 0. + thresholds : array, shape = [n_thresholds <= len(np.unique(y_score))] + Increasing thresholds on the decision function used to compute + precision and recall. + """ + # Sort scores and corresponding truth values + desc_score_indices = np.argsort(y_score)[::-1] + y_score = y_score[desc_score_indices] + y_true = y_true[desc_score_indices] + + # y_score typically has many tied values. Here we extract + # the indices associated with the distinct values. We also + # concatenate a value for the end of the curve. + distinct_value_indices = np.where(np.diff(y_score))[0] + threshold_idxs = np.r_[distinct_value_indices, y_true.size - 1] + + # accumulate the true positives with decreasing threshold + tps = stable_cumsum(y_true)[threshold_idxs] + fps = 1 + threshold_idxs - tps + prec = tps / (tps + fps +Generated code for sklearn.metrics.precision_recall_fscore_support + + +from sklearn.metrics import precision_recall_fscore_support + +y_true = [0, 1, 2, 0, 1, 2] +y_pred = [0, 2, 1, 0, 0, 1] + +precision, recall, fscore, support = precision_recall_fscore_support(y_true, y_pred, average='macro') + +print('precision: {}'.format(precision)) +print('recall: {}'.format(recall)) +print('fscore: {}'.format(fscore)) +print('support: {}'.format(support)) +Generated code for sklearn.metrics.precision_score + + +from sklearn.metrics import precision_score + +# example data +y_true = [0, 0, 1, 1] +y_pred = [0, 1, 0, 1] + +# calculate precision score +precision_score = precision_score(y_true, y_pred) + +print('Precision score: %.3f' % precision_score) +Generated code for sklearn.metrics.recall_score + + +from sklearn.metrics import recall_score + +# example data +y_true = [0, 1, 2, 0, 1, 2] +y_pred = [0, 2, 1, 0, 0, 1] + +# calculate recall score +recall_score(y_true, y_pred) + +# output: 0.3333333333333333 +Generated code for sklearn.metrics.roc_auc_score + + +import numpy as np +from sklearn.metrics import roc_auc_score + +# Generate some dummy data +y_true = np.array([0, 0, 1, 1]) +y_scores = np.array([0.1, 0.4, 0.35, 0.8]) + +# Calculate the ROC AUC score +roc_auc_score(y_true, y_scores) + +# Output: 0.75 +Generated code for sklearn.metrics.roc_curve + + +import numpy as np +from sklearn.metrics import roc_curve + +# Generate some dummy data +y_true = np.array([0, 0, 1, 1]) +y_scores = np.array([0.1, 0.4, 0.35, 0.8]) + +# Calculate the ROC curve +fpr, tpr, thresholds = roc_curve(y_true, y_scores) + +# Print the results +print('False Positive Rate:', fpr) +print('True Positive Rate:', tpr) +print('Thresholds:', thresholds) +Generated code for sklearn.metrics.top_k_accuracy_score + + +def top_k_accuracy_score(y_true, y_pred, k=5): + """ + Computes the top-k accuracy score for a given set of true labels and predicted labels. + + Parameters + ---------- + y_true : array-like, shape = [n_samples] + Ground truth (correct) labels. + + y_pred : array-like, shape = [n_samples] + Predicted labels, as returned by a classifier. + + k : int, optional (default=5) + The number of top-k predictions to consider. + + Returns + ------- + score : float + The top-k accuracy score. + """ + # Check that the inputs are valid + if not isinstance(y_true, np.ndarray): + raise TypeError('y_true must be a numpy array') + if not isinstance(y_pred, np.ndarray): + raise TypeError('y_pred must be a numpy array') + if not isinstance(k, int): + raise TypeError('k must be an integer') + if k < 1: + raise ValueError('k must be greater than 0') + + # Compute the top-k accuracy score + num_correct = 0 + for i in range(len(y_true)): + # Get the top-k predictions for this sample + top_k_preds = np.argsort(y_pred[i])[-k:] + + # Check if the true label is in the top-k predictions + if y_true[i] in top_k_preds: + num_correct += 1 + + # Compute and return the score + score = num_correct / len(y_true) + return score +Generated code for sklearn.metrics.zero_one_loss + + +def zero_one_loss(y_true, y_pred, normalize=True): + """ + Compute the zero-one classification loss. + The zero-one loss is the fraction of misclassifications (float), + i.e. the number of incorrect predictions divided by the total number of + predictions. + Read more in the :ref:`User Guide `. + Parameters + ---------- + y_true : array-like or label indicator matrix + Ground truth (correct) labels. + y_pred : array-like or label indicator matrix + Predicted labels, as returned by a classifier. + normalize : bool, optional (default=True) + If ``False``, return the number of misclassifications. + Otherwise, return the fraction of misclassifications. + Returns + ------- + loss : float or int, + If ``normalize == True``, return the fraction of misclassifications + (float), else it returns the number of misclassifications (int). + Examples + -------- + >>> from sklearn.metrics import zero_one_loss + >>> y_pred = [1, 2, 3, 4] + >>> y_true = [2, 2, 3, 4] + >>> zero_one_loss(y_true, y_pred) + 0.25 + >>> zero_one_loss(y_true, y_pred, normalize=False) + 1 + """ + score = accuracy_score(y_true, y_pred, normalize=normalize) + return 1 - score +Generated code for sklearn.metrics.explained_variance_score + + +def explained_variance_score(y_true, y_pred): + """ + Computes the explained variance score. + + The explained variance score is the fraction of the variance in the + observed data that is explained by the model. + + Parameters + ---------- + y_true : array-like of shape = (n_samples) + Ground truth (correct) target values. + y_pred : array-like of shape = (n_samples) + Estimated target values. + + Returns + ------- + score : float + The explained variance score. + """ + y_true = np.array(y_true) + y_pred = np.array(y_pred) + numerator = np.sum(np.square(y_true - y_pred)) + denominator = np.sum(np.square(y_true - np.mean(y_true))) + score = 1 - (numerator / denominator) + return score +Generated code for sklearn.metrics.max_error + + +import numpy as np +from sklearn.metrics import max_error + +# Generate some random data +y_true = np.random.randint(0, 10, size=100) +y_pred = np.random.randint(0, 10, size=100) + +# Calculate the max error +max_err = max_error(y_true, y_pred) + +print("Max Error:", max_err) +Generated code for sklearn.metrics.mean_absolute_error + + +from sklearn.metrics import mean_absolute_error + +def mean_absolute_error(y_true, y_pred): + """ + Computes the mean absolute error between two arrays. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + + Returns + ------- + mae : float + The mean absolute error between y_true and y_pred. + """ + mae = np.mean(np.abs(y_true - y_pred)) + return mae +Generated code for sklearn.metrics.mean_squared_error + + +from sklearn.metrics import mean_squared_error + +def mean_squared_error(y_true, y_pred): + """ + Computes the mean squared error between two arrays. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + + Returns + ------- + mse : float + The mean squared error, a measure of the difference between + `y_true` and `y_pred`. + """ + mse = ((y_true - y_pred) ** 2).mean() + return mse +Generated code for sklearn.metrics.mean_squared_log_error + + +import numpy as np +from sklearn.metrics import mean_squared_log_error + +y_true = np.array([1, 2, 3, 4, 5]) +y_pred = np.array([1.2, 2.3, 3.4, 4.5, 5.6]) + +mean_squared_log_error(y_true, y_pred) + +# Output: 0.0108 +Generated code for sklearn.metrics.median_absolute_error + + +from sklearn.metrics import median_absolute_error + +def median_absolute_error(y_true, y_pred): + """ + Computes the median absolute error between two arrays. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + + Returns + ------- + median_absolute_error : float + The median absolute error between y_true and y_pred. + """ + errors = abs(y_true - y_pred) + return np.median(errors) +Generated code for sklearn.metrics.mean_absolute_percentage_error + + +def mean_absolute_percentage_error(y_true, y_pred): + y_true, y_pred = np.array(y_true), np.array(y_pred) + return np.mean(np.abs((y_true - y_pred) / y_true)) * 100 +Generated code for sklearn.metrics.r2_score + + +def r2_score(y_true, y_pred): + numerator = ((y_true - y_pred)**2).sum() + denominator = ((y_true - y_true.mean())**2).sum() + return 1 - (numerator/denominator) +Generated code for sklearn.metrics.mean_poisson_deviance + + +import numpy as np +from sklearn.metrics import mean_poisson_deviance + +# Generate random data +y_true = np.random.randint(low=0, high=10, size=100) +y_pred = np.random.randint(low=0, high=10, size=100) + +# Calculate mean poisson deviance +mean_poisson_deviance_score = mean_poisson_deviance(y_true, y_pred) + +print("Mean Poisson Deviance:", mean_poisson_deviance_score) +Generated code for sklearn.metrics.mean_gamma_deviance + + +import numpy as np +from sklearn.metrics import mean_gamma_deviance + +# Generate random data +y_true = np.random.randint(2, size=100) +y_pred = np.random.rand(100) + +# Calculate mean gamma deviance +mean_gamma_deviance(y_true, y_pred) +Generated code for sklearn.metrics.mean_tweedie_deviance + + +import numpy as np +from sklearn.metrics import mean_tweedie_deviance + +# Generate random data +y_true = np.random.randint(0, 10, size=100) +y_pred = np.random.randint(0, 10, size=100) + +# Calculate mean Tweedie deviance +mean_tweedie_dev = mean_tweedie_deviance(y_true, y_pred) + +print("Mean Tweedie Deviance:", mean_tweedie_dev) +Generated code for sklearn.metrics.d2_tweedie_score + + +def d2_tweedie_score(y_true, y_pred, power=1.5): + """ + Computes the D2 Tweedie score for a given prediction. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + power : float, optional (default=1.5) + The power parameter of the Tweedie distribution. + + Returns + ------- + score : float + The D2 Tweedie score. + """ + # Compute the residuals + residuals = y_true - y_pred + + # Compute the D2 Tweedie score + score = np.sum(np.power(residuals, 2 - power)) / np.sum(np.power(y_true, 2 - power)) + + return score +Generated code for sklearn.metrics.mean_pinball_loss + + +def mean_pinball_loss(y_true, y_pred, tau): + """ + Computes the mean pinball loss between y_true and y_pred. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + tau : float + The pinball parameter. + + Returns + ------- + loss : float + The mean pinball loss. + """ + loss = 0.0 + for i in range(len(y_true)): + loss += max(tau * (y_true[i] - y_pred[i]), (y_true[i] - y_pred[i]) * (1 - tau)) + return loss / len(y_true) +Generated code for sklearn.metrics.d2_pinball_score + + +def d2_pinball_score(y_true, y_pred, quantiles): + """ + Computes the D2 pinball score for a given set of predictions and true values. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + quantiles : array-like of shape (n_quantiles,) + Quantiles to use for computing the pinball score. + + Returns + ------- + score : float + The D2 pinball score. + """ + # Check inputs + y_true = np.asarray(y_true) + y_pred = np.asarray(y_pred) + quantiles = np.asarray(quantiles) + + # Compute the pinball score + score = 0.0 + for q in quantiles: + score += np.mean(np.maximum(y_true - y_pred, q * (y_pred - y_true))) + return score +Generated code for sklearn.metrics.d2_absolute_error_score + + +from sklearn.metrics import d2_absolute_error_score + +def d2_absolute_error_score(y_true, y_pred): + """ + Computes the D2 absolute error score between two arrays. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + + Returns + ------- + score : float + The D2 absolute error score. + """ + return np.mean(np.abs(y_true - y_pred)) +Generated code for sklearn.metrics.coverage_error + + +from sklearn.metrics import coverage_error + +def coverage_error_score(y_true, y_pred): + """ + Computes the coverage error score for a given set of true labels and predicted labels. + + Parameters + ---------- + y_true : array-like, shape = [n_samples] + True labels. + + y_pred : array-like, shape = [n_samples] + Predicted labels. + + Returns + ------- + coverage_error : float + The coverage error score. + """ + return coverage_error(y_true, y_pred) +Generated code for sklearn.metrics.label_ranking_average_precision_score + + +from sklearn.metrics import label_ranking_average_precision_score + +def label_ranking_average_precision_score(y_true, y_score): + """ + Compute the average precision score for a set of labels + Parameters + ---------- + y_true : array-like, shape = [n_samples] + Ground truth (true relevance labels). + y_score : array-like, shape = [n_samples, n_labels] + Predicted scores. + Returns + ------- + score : float + Average precision score + """ + # Check that y_true and y_score have the same shape + if y_true.shape != y_score.shape: + raise ValueError("y_true and y_score have different shapes") + + # Compute the average precision score + score = 0.0 + for i in range(y_true.shape[0]): + # Compute the precision at each label + true_labels = y_true[i] + scores = y_score[i] + precision = 0.0 + for j in range(true_labels.shape[0]): + if true_labels[j] == 1: + precision += scores[j] + # Compute the average precision + score += precision / true_labels.sum() + + # Return the average precision score + return score / y_true.shape[0] +Generated code for sklearn.metrics.label_ranking_loss + + +from sklearn.metrics import label_ranking_loss + +def label_ranking_loss(y_true, y_score): + """ + Computes the label ranking loss. + + Parameters + ---------- + y_true : array-like, shape = [n_samples] + Ground truth (true relevance labels). + + y_score : array-like, shape = [n_samples, n_labels] + Predicted scores. + + Returns + ------- + loss : float + """ + n_samples, n_labels = y_score.shape + y_true = y_true.reshape(n_samples, 1) + y_true_onehot = np.eye(n_labels)[y_true.astype(int)] + loss = np.sum(np.maximum(0, -(y_true_onehot - y_score))) / n_samples + return loss +Generated code for sklearn.metrics.adjusted_mutual_info_score + + +def adjusted_mutual_info_score(labels_true, labels_pred, average_method='arithmetic'): + """Compute the Adjusted Mutual Information between two clusterings + Adjusted Mutual Information (AMI) is an adjustment of the Mutual + Information (MI) score to account for chance. It accounts for the fact + that the MI is generally higher for two clusterings with a larger + number of clusters, regardless of whether there is actually more + agreement between the clusterings. + Read more in the :ref:`User Guide `. + Parameters + ---------- + labels_true : int array, shape = [n_samples] + A clustering of the data into disjoint subsets. + labels_pred : array, shape = [n_samples] + A clustering of the data into disjoint subsets. + average_method : string, optional + The average method used to calculate the AMI. + Supported methods are 'arithmetic' (default) and 'geometric'. + Returns + ------- + ami : float + Adjusted Mutual Information between the two clusterings + See also + -------- + adjusted_rand_score: Adjusted Rand index + mutual_info_score: Mutual Information + Examples + -------- + >>> from sklearn.metrics.cluster import adjusted_mutual_info_score + >>> adjusted_mutual_info_score([0, 0, 1, 1], [1, 1, 0, 0]) + 1.0 + """ + labels_true, labels_pred = check_clusterings(labels_true, labels_pred) + classes = np.unique(labels_true) + clusters = np.unique(labels_pred) + # Special limit cases: no clustering since the data is not split. + # This is a perfect match hence return 1.0. + if (classes.shape[0] == clusters.shape[0] == 1 + or classes.shape[0] == clusters.shape[0] == 0): + return 1.0 + contingency = contingency_matrix(labels_true, labels_pred) + +Generated code for sklearn.metrics.adjusted_rand_score + + +def adjusted_rand_score(labels_true, labels_pred): + """ + Compute the Adjusted Rand Index (ARI) of two clusterings. + + Parameters + ---------- + labels_true : int array, shape = [n_samples] + Ground truth class labels to be used as a reference + labels_pred : int array, shape = [n_samples] + Cluster labels to evaluate + + Returns + ------- + ari : float + The resulting ARI score + """ + from sklearn.metrics.cluster import contingency_matrix + from sklearn.metrics.cluster.supervised import _comb2 + from sklearn.utils.extmath import _adjusted_rand_score + + # Compute the contingency matrix + cont_mat = contingency_matrix(labels_true, labels_pred) + + # Compute the combinatorial terms + n_comb_c, n_comb_k = _comb2(cont_mat) + + # Compute the Adjusted Rand Index + ari = _adjusted_rand_score(cont_mat, n_comb_c, n_comb_k) + + return ari +Generated code for sklearn.metrics.calinski_harabasz_score + + +def calinski_harabasz_score(X, labels): + """Calculate the Calinski and Harabasz score. + + The Calinski and Harabasz score is defined as the ratio of the between-clusters dispersion mean + and the within-cluster dispersion. + + This score is valid only for clusters with more than one sample. + + Parameters + ---------- + X : array-like or sparse matrix, shape (n_samples, n_features) + List of n_features-dimensional data points. Each row corresponds + to a single data point. + + labels : array-like, shape (n_samples,) + Predicted labels for each sample. + + Returns + ------- + score : float + The resulting Calinski and Harabasz score. + """ + n_samples, _ = X.shape + n_labels = len(np.unique(labels)) + + # Calculate the between-cluster dispersion mean + mean_dispersion_b = 0.0 + for label in np.unique(labels): + X_label = X[labels == label] + mean_dispersion_b += np.sum((X_label - X_label.mean(axis=0))**2) + mean_dispersion_b /= (n_labels - 1) + + # Calculate the within-cluster dispersion + mean_dispersion_w = 0.0 + for label in np.unique(labels): + X_label = X[labels == label] + mean_dispersion_w += np.sum((X_label - X_label.mean(axis=0))**2) + mean_dispersion_w /= (n_samples - n_labels) + + # Calculate the Calinski and Harabasz score + score = mean_dispersion_b / mean_dispersion_w + + return score +Generated code for sklearn.metrics.davies_bouldin_score + + +import numpy as np +from sklearn.metrics import davies_bouldin_score + +# Generate random data +X = np.random.rand(100, 2) + +# Calculate Davies-Bouldin score +score = davies_bouldin_score(X, np.zeros(100)) + +print(score) +Generated code for sklearn.metrics.completeness_score + + +def completeness_score(y_true, y_pred, labels=None): + """Compute completeness score + The completeness score is defined as the ratio between the number of + pairs of elements of the same class in the same set and the total number + of pairs in the same set. + Read more in the :ref:`User Guide `. + Parameters + ---------- + y_true : array, shape = [n_samples] + Ground truth (correct) labels. + y_pred : array, shape = [n_samples] + Predicted labels, as returned by a classifier. + labels : array, shape = [n_classes], optional + List of labels to include when calculating the completeness score. + If ``None``, all labels are used. + Returns + ------- + completeness : float + The completeness score. + Examples + -------- + >>> from sklearn.metrics import completeness_score + >>> y_true = [0, 1, 2, 0, 1, 2] + >>> y_pred = [0, 2, 1, 0, 0, 1] + >>> completeness_score(y_true, y_pred) + 0.5 + >>> completeness_score(y_true, y_pred, labels=[0, 1]) + 1.0 + """ + y_true, y_pred = check_arrays(y_true, y_pred) + if labels is None: + labels = np.unique(np.concatenate((y_true, y_pred))) + else: + labels = np.asarray(labels) + + n_labels = labels.size + label_to_ind = dict((y, x) for x, y in enumerate(labels)) + + # convert yt, yp into index + y_pred = np.array([label_to_ind.get(x, n_labels + 1) for x in y_pred]) + y_true = np.array([label_to_ind.get(x, n_labels + 1) for x in y +Generated code for sklearn.metrics.cluster.contingency_matrix + + +def contingency_matrix(y_true, y_pred, eps=None): + classes, class_idx = np.unique(y_true, return_inverse=True) + clusters, cluster_idx = np.unique(y_pred, return_inverse=True) + n_classes = classes.shape[0] + n_clusters = clusters.shape[0] + # Using coo_matrix to accelerate simple histogram calculation, + # i.e. bins are consecutive integers + # Currently, coo_matrix is faster than histogram2d for simple cases + contingency = coo_matrix((np.ones(class_idx.shape[0]), + (class_idx, cluster_idx)), + shape=(n_classes, n_clusters), + dtype=np.int).toarray() + if eps is not None: + # don't use += as contingency is integer + contingency = contingency + eps + return contingency +Generated code for sklearn.metrics.cluster.pair_confusion_matrix + + +def pair_confusion_matrix(y_true, y_pred, labels=None): + """Compute the pairwise confusion matrix for clustering results. + + Parameters + ---------- + y_true : array-like, shape (n_samples,) + Ground truth (true) labels. + y_pred : array-like, shape (n_samples,) + Predicted labels, as returned by a clustering algorithm. + labels : array-like, shape (n_classes,), optional + List of labels to index the matrix. This may be used to reorder + or select a subset of labels. + If none is given, those that appear at least once + in ``y_true`` or ``y_pred`` are used in sorted order. + + Returns + ------- + C : array, shape (n_classes, n_classes) + The pairwise confusion matrix. + + Examples + -------- + >>> from sklearn.metrics.cluster import pair_confusion_matrix + >>> y_true = [0, 0, 1, 1, 2] + >>> y_pred = [0, 1, 1, 2, 2] + >>> pair_confusion_matrix(y_true, y_pred) + array([[2, 0, 0], + [0, 1, 1], + [0, 1, 2]]) + """ + y_true = np.asarray(y_true) + y_pred = np.asarray(y_pred) + if y_true.shape != y_pred.shape: + raise ValueError("y_true and y_pred must have the same shape") + if labels is None: + labels = np.unique(np.concatenate((y_true, y_pred))) + else: + labels = np.asarray(labels) + n_labels = labels.size + C = np.empty((n_labels, n_labels), dtype=np.int) + for i, label_i in enumerate(labels): + for j, label_j in enumerate(lab +Generated code for sklearn.metrics.fowlkes_mallows_score + + +def fowlkes_mallows_score(labels_true, labels_pred): + """Compute the Fowlkes-Mallows score. + + The Fowlkes-Mallows score is a measure of the similarity between two + clusterings. A perfect score is 1.0, whereas a score of 0.0 indicates + completely random labelings. + + Parameters + ---------- + labels_true : array, shape = [n_samples] + Ground truth class labels to be used as a reference. + labels_pred : array, shape = [n_samples] + Cluster labels to evaluate. + + Returns + ------- + fowlkes_mallows_score : float + The Fowlkes-Mallows score. + """ + # Get the unique labels + labels_true_unique = np.unique(labels_true) + labels_pred_unique = np.unique(labels_pred) + + # Compute the contingency matrix + cont_matrix = confusion_matrix(labels_true, labels_pred) + + # Compute the Fowlkes-Mallows score + num = np.sum(np.sqrt(np.sum(cont_matrix, axis=0) * np.sum(cont_matrix, axis=1))) + den = np.sum(cont_matrix) + + return num / den +Generated code for sklearn.metrics.homogeneity_completeness_v_measure + + +from sklearn.metrics import homogeneity_completeness_v_measure + +def homogeneity_completeness_v_measure(labels_true, labels_pred): + """Compute the homogeneity, completeness and V-measure + of a clustering given a ground truth. + + Parameters + ---------- + labels_true : int array, shape = [n_samples] + Ground truth class labels to be used as a reference + labels_pred : array, shape = [n_samples] + Cluster labels to evaluate + + Returns + ------- + homogeneity : float + completeness: float + v_measure: float + The homogeneity, completeness and V-measure of the + clustering + """ + from sklearn.metrics.cluster import contingency_matrix + from sklearn.metrics.cluster.supervised import _combined_index + from sklearn.utils.validation import check_consistent_length + + check_consistent_length(labels_true, labels_pred) + cont_mat = contingency_matrix(labels_true, labels_pred) + homogeneity, completeness, v_measure = _combined_index(cont_mat) + return homogeneity, completeness, v_measure diff --git a/output3.txt b/output3.txt new file mode 100644 index 000000000..5c6d8fa10 --- /dev/null +++ b/output3.txt @@ -0,0 +1,4826 @@ +Generated code for sklearn.metrics.homogeneity_score + + +from sklearn.metrics import homogeneity_score + +def homogeneity_score(labels_true, labels_pred): + """Compute the homogeneity score. + + The homogeneity score is a measure of how well the labels assigned to + the samples match the labels assigned to the clusters. A score of 1 + indicates perfect homogeneity, while a score of 0 indicates that the + labels are completely independent of the clusters. + + Parameters + ---------- + labels_true : array, shape = [n_samples] + Ground truth labels for each sample. + + labels_pred : array, shape = [n_samples] + Predicted labels for each sample. + + Returns + ------- + score : float + The homogeneity score. + """ + from sklearn.utils.linear_assignment_ import linear_assignment + from sklearn.metrics.cluster import contingency_matrix + + # Compute the contingency matrix + cm = contingency_matrix(labels_true, labels_pred) + + # Compute the linear assignment + cost, _, _, _ = linear_assignment(cm) + + # Compute the homogeneity score + score = 1.0 - cost / cm.sum() + + return score +Generated code for sklearn.metrics.mutual_info_score + + +from sklearn.metrics import mutual_info_score + +def mutual_info_score(x, y): + """ + Computes the mutual information between two variables. + + Parameters + ---------- + x : array-like, shape (n_samples, ) + The first variable. + y : array-like, shape (n_samples, ) + The second variable. + + Returns + ------- + mutual_info : float + The mutual information between x and y. + """ + # Compute the joint probability distribution of x and y + joint_dist = np.histogram2d(x, y)[0] + joint_dist /= joint_dist.sum() + + # Compute the marginal probability distributions of x and y + marginal_x = np.sum(joint_dist, axis=1) + marginal_y = np.sum(joint_dist, axis=0) + + # Compute the mutual information + mutual_info = 0.0 + for i in range(joint_dist.shape[0]): + for j in range(joint_dist.shape[1]): + if joint_dist[i, j] > 0.0: + mutual_info += joint_dist[i, j] * \ + np.log(joint_dist[i, j] / (marginal_x[i] * marginal_y[j])) + + return mutual_info +Generated code for sklearn.metrics.normalized_mutual_info_score + + +from sklearn.metrics import normalized_mutual_info_score + +def normalized_mutual_info_score(labels_true, labels_pred): + """ + Compute the normalized mutual information between two clusterings. + + Parameters + ---------- + labels_true : int array, shape = [n_samples] + A clustering of the data into disjoint subsets. + + labels_pred : array, shape = [n_samples] + A clustering of the data into disjoint subsets. + + Returns + ------- + nmi: float + Normalized Mutual Information between the two clusterings. + """ + # Compute contingency matrix + contingency = contingency_matrix(labels_true, labels_pred) + + # Compute marginal probabilities + pi = np.sum(contingency, axis=1) + pj = np.sum(contingency, axis=0) + n = np.sum(pi) + + # Compute the mutual information + mi = np.sum(contingency * np.log(contingency * n / (pi[:, None] * pj[None, :]))) + + # Compute the expected value for the mutual information + e_mi = np.sum(pi * np.log(pi / n)) + np.sum(pj * np.log(pj / n)) + + # Compute the normalized mutual information + nmi = mi / e_mi + + return nmi +Generated code for sklearn.metrics.rand_score + + +from sklearn.metrics import rand_score + +def rand_score(y_true, y_pred): + """ + Computes the Rand index between two clusterings. + + Parameters + ---------- + y_true : array-like, shape (n_samples,) + Ground truth (correct) labels. + y_pred : array-like, shape (n_samples,) + Predicted labels, as returned by a clustering algorithm. + + Returns + ------- + score : float + The Rand index between the two clusterings. + """ + y_true = np.asarray(y_true) + y_pred = np.asarray(y_pred) + + n_samples = y_true.shape[0] + + # Compute contingency matrix + # (also called confusion matrix in some contexts) + # Contingency matrix C has C_ij = # observations known to be in group i + # and predicted to be in group j. + C = np.zeros((2, 2)) + for i in range(n_samples): + C[y_true[i], y_pred[i]] += 1 + + # Compute the Rand index + # The Rand Index computes a similarity measure between two clusterings + # by considering all pairs of samples and counting pairs that are + # assigned in the same or different clusters in the predicted and + # true clusterings. + # + # The raw RI score is then "adjusted for chance" into the ARI score + # using the following relation: + # + # ARI = (RI - Expected_RI) / (max(RI) - Expected_RI) + # + # where Expected_RI is the expected number of agreements under the + # assumption of independent clustering. + n_combinations = n_samples * (n_samples - 1) / 2 + sum_combinations = sum([C[i, j] * (C[i, j] - 1) / 2 for i in range(2) for j in range(2)]) + sum_product +Generated code for sklearn.metrics.silhouette_score + + +from sklearn.metrics import silhouette_score + +# example data +X = [[0, 0], [1, 1], [2, 0], [3, 0], [4, 1]] + +# calculate silhouette score +score = silhouette_score(X, labels=None) + +print(score) +Generated code for sklearn.metrics.silhouette_samples + + +from sklearn.metrics import silhouette_samples + +def silhouette_samples(X, labels, metric='euclidean'): + """Compute the Silhouette Coefficient for each sample. + + Parameters + ---------- + X : array [n_samples_a, n_features] + Feature matrix. + labels : array, shape = [n_samples] + label values for each sample + metric : string, optional + The distance metric to use: + - euclidean + - manhattan + - cosine + + Returns + ------- + silhouette : array, shape = [n_samples] + Silhouette Coefficient for each sample. + """ + from sklearn.metrics.pairwise import pairwise_distances + from sklearn.utils import check_array + from sklearn.utils.validation import check_is_fitted + from sklearn.metrics import silhouette_score + + X = check_array(X) + check_is_fitted(labels) + + # Compute the pairwise distance matrix + dists = pairwise_distances(X, metric=metric) + + # Compute the Silhouette Coefficient for each sample + silhouette_vals = silhouette_score(dists, labels, metric=metric) + + return silhouette_vals +Generated code for sklearn.metrics.v_measure_score + + +from sklearn.metrics import v_measure_score + +def v_measure_score(y_true, y_pred): + """Compute the V-measure score. + + The V-measure score is the harmonic mean of the homogeneity and completeness + scores. The homogeneity score is a measure of the amount of clustering + structure that is shared between the predicted and true labels. The + completeness score is a measure of how completely the predicted labels + capture the true labels. + + Parameters + ---------- + y_true : array-like, shape (n_samples,) + Ground truth (correct) labels. + + y_pred : array-like, shape (n_samples,) + Predicted labels, as returned by a clustering algorithm. + + Returns + ------- + v_measure : float + The V-measure score. + + References + ---------- + .. [1] `Rosenberg and Hirschberg, 2007. V-Measure: A conditional entropy-based + external cluster evaluation measure `_ + """ + from sklearn.metrics.cluster import homogeneity_score, completeness_score + + homogeneity = homogeneity_score(y_true, y_pred) + completeness = completeness_score(y_true, y_pred) + v_measure = (2. * homogeneity * completeness) / (homogeneity + completeness) + + return v_measure +Generated code for sklearn.metrics.consensus_score + + +def consensus_score(y_true, y_pred): + """ + Computes the consensus score between two sets of labels. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) labels. + + y_pred : array-like of shape (n_samples,) + Predicted labels, as returned by a classifier. + + Returns + ------- + score : float + The consensus score between y_true and y_pred. + """ + from sklearn.metrics import accuracy_score + from sklearn.metrics import jaccard_score + from sklearn.metrics import f1_score + + # Compute accuracy + accuracy = accuracy_score(y_true, y_pred) + + # Compute Jaccard similarity + jaccard = jaccard_score(y_true, y_pred) + + # Compute F1 score + f1 = f1_score(y_true, y_pred) + + # Compute consensus score + score = (accuracy + jaccard + f1) / 3 + + return score +Generated code for sklearn.metrics.DistanceMetric + + +from sklearn.metrics import DistanceMetric + +# Create a DistanceMetric object +metric = DistanceMetric() + +# Calculate the Euclidean distance between two points +dist = metric.pairwise(X=[1,2], Y=[2,3]) + +# Print the distance +print(dist) +Generated code for sklearn.metrics.pairwise.additive_chi2_kernel + + +def additive_chi2_kernel(X, Y=None, gamma=1.0): + """Compute the additive chi-squared kernel between observations in X and Y. + The additive chi-squared kernel is a variant of the chi-squared kernel. + It is defined as: + + k(x_i, y_j) = exp(-gamma * sum((x_i - y_j)**2 / (x_i + y_j + eps))) + + where eps is a small positive number to avoid division by zero. + + Parameters + ---------- + X : array-like of shape (n_samples_X, n_features) + Input data. + + Y : array-like of shape (n_samples_Y, n_features), default=None + Input data. If None, the kernel between all pairs of samples in X is + computed. + + gamma : float, default=1.0 + Parameter of the kernel. + + Returns + ------- + K : array of shape (n_samples_X, n_samples_Y) + Kernel matrix. + """ + X = np.atleast_2d(X) + if Y is None: + Y = X + else: + Y = np.atleast_2d(Y) + n_samples_X, n_features = X.shape + n_samples_Y, _ = Y.shape + K = np.empty((n_samples_X, n_samples_Y)) + eps = 1e-10 + for i, x_i in enumerate(X): + for j, y_j in enumerate(Y): + K[i, j] = np.exp(-gamma * np.sum((x_i - y_j)**2 / (x_i + y_j + eps))) + return K +Generated code for sklearn.metrics.pairwise.chi2_kernel + + +def chi2_kernel(X, Y=None, gamma=1.0): + """Compute the chi-squared kernel between two sets of vectors. + The chi-squared kernel is computed between each pair of samples + (row in `X` and `Y`). + Read more in the :ref:`User Guide `. + Parameters + ---------- + X : array-like of shape (n_samples_X, n_features) + Left argument of the returned kernel k(X, Y) + Y : array-like of shape (n_samples_Y, n_features), (optional, default=None) + Right argument of the returned kernel k(X, Y). If None, k(X, X) + if evaluated instead. + gamma : float, (optional, default=1.0) + Parameter of the chi2 kernel. + Returns + ------- + kernel_matrix : array of shape (n_samples_X, n_samples_Y) + """ + X = np.atleast_2d(X) + if Y is None: + Y = X + else: + Y = np.atleast_2d(Y) + + kernel_matrix = np.zeros((X.shape[0], Y.shape[0])) + for i, x in enumerate(X): + for j, y in enumerate(Y): + kernel_matrix[i, j] = np.sum((x - y) ** 2 / (x + y + 1e-10)) + + return np.exp(-gamma * kernel_matrix) +Generated code for sklearn.metrics.pairwise.cosine_similarity + + +import numpy as np +from sklearn.metrics.pairwise import cosine_similarity + +# Create two vectors +vector_a = np.array([1,2,3]) +vector_b = np.array([2,3,4]) + +# Calculate cosine similarity +cos_sim = cosine_similarity([vector_a], [vector_b]) + +# Print cosine similarity +print(cos_sim) +Generated code for sklearn.metrics.pairwise.cosine_distances + + +import numpy as np +from sklearn.metrics.pairwise import cosine_distances + +# Create two vectors +x = np.array([1, 2, 3]) +y = np.array([4, 5, 6]) + +# Calculate the cosine distance +cosine_distances(x, y) + +# Output: +# array([[1.0]]) +Generated code for sklearn.metrics.pairwise.distance_metrics + + +import numpy as np +from sklearn.metrics.pairwise import distance_metrics + +# Euclidean Distance +def euclidean_distance(x, y): + return np.sqrt(np.sum((x - y) ** 2)) + +# Manhattan Distance +def manhattan_distance(x, y): + return np.sum(np.abs(x - y)) + +# Minkowski Distance +def minkowski_distance(x, y, p): + return np.sum(np.abs(x - y) ** p) ** (1 / p) + +# Chebyshev Distance +def chebyshev_distance(x, y): + return np.max(np.abs(x - y)) + +# Cosine Distance +def cosine_distance(x, y): + return 1 - np.dot(x, y) / (np.linalg.norm(x) * np.linalg.norm(y)) + +# Custom Distance +def custom_distance(x, y): + return np.sum(np.abs(x - y) ** 2) ** 0.5 + +# Register the distance functions +distance_metrics.register_metric('euclidean', euclidean_distance) +distance_metrics.register_metric('manhattan', manhattan_distance) +distance_metrics.register_metric('minkowski', minkowski_distance) +distance_metrics.register_metric('chebyshev', chebyshev_distance) +distance_metrics.register_metric('cosine', cosine_distance) +distance_metrics.register_metric('custom', custom_distance) +Generated code for sklearn.metrics.pairwise.euclidean_distances + + +import numpy as np +from sklearn.metrics.pairwise import euclidean_distances + +# Create two arrays of random numbers +x = np.random.rand(3, 4) +y = np.random.rand(3, 4) + +# Calculate the Euclidean distance between the two arrays +dist = euclidean_distances(x, y) + +# Print the distance +print(dist) +Generated code for sklearn.metrics.pairwise.haversine_distances + + +import numpy as np +from sklearn.metrics.pairwise import haversine_distances + +# Create two arrays of coordinates +coord1 = np.array([[45.3, -66.2], + [43.6, -70.2], + [43.2, -76.4]]) + +coord2 = np.array([[41.3, -81.4], + [45.5, -63.2], + [43.2, -70.2]]) + +# Calculate the haversine distances +distances = haversine_distances(coord1, coord2) + +# Print the distances +print(distances) +Generated code for sklearn.metrics.pairwise.kernel_metrics + + +import numpy as np +from sklearn.metrics.pairwise import kernel_metrics + +# Define the kernel function +def my_kernel(x, y): + return np.exp(-np.linalg.norm(x-y)**2) + +# Compute the kernel matrix +K = kernel_metrics(X, Y, metric=my_kernel) + +# Print the kernel matrix +print(K) +Generated code for sklearn.metrics.pairwise.laplacian_kernel + + +import numpy as np +from sklearn.metrics.pairwise import laplacian_kernel + +# Generate sample data +X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) + +# Compute the Laplacian kernel between X and itself +K = laplacian_kernel(X, X) + +# Print computed Laplacian kernel +print(K) +Generated code for sklearn.metrics.pairwise.linear_kernel + + +def linear_kernel(x1, x2): + return np.dot(x1, x2) + +# Example +x1 = np.array([1, 2, 3]) +x2 = np.array([4, 5, 6]) + +linear_kernel(x1, x2) # Output: 32 +Generated code for sklearn.metrics.pairwise.manhattan_distances + + +def manhattan_distances(X, Y=None, sum_over_features=True): + """ + Compute the L1 distances between the vectors in X and Y. + Read more in the :ref:`User Guide `. + Parameters + ---------- + X : ndarray + An :math:`m_1 \\times n` array of :math:`m_1` original observations in an + :math:`n`-dimensional space. + Y : ndarray, optional + An :math:`m_2 \\times n` array of :math:`m_2` original observations in an + :math:`n`-dimensional space. + sum_over_features : bool, optional + If True, the function returns the pairwise distance matrix + :math:`M_{ij} = \\sum_k |x_{ik} - y_{jk}|`. + If False, the function returns the element-wise L1 distances + :math:`M_{ij} = |x_{ij} - y_{ij}|`. + Returns + ------- + M : ndarray + A :math:`m_1 \\times m_2` distance matrix is returned. + For any :math:`i, j`, the metric ``dist(u=X[i], v=Y[j])`` is computed + and stored in the :math:`i, j` th entry. + Examples + -------- + >>> from sklearn.metrics.pairwise import manhattan_distances + >>> manhattan_distances([[3]], [[3]]) + array([[0.]]) + >>> manhattan_distances([[3]], [[2]]) + array([[1.]]) + >>> manhattan_distances([[2], [3], [4]], [[1], [2], [3]]) + array([[1., 2., 3.], + [0., 1., 2.], + [1., 0., 1.]]) + """ + X, Y = check_pairwise_arrays(X, Y) + if sum_over_features: +Generated code for sklearn.metrics.pairwise.nan_euclidean_distances + + +def nan_euclidean_distances(X, Y=None, Y_norm_squared=None, squared=False): + """ + Compute the Euclidean distances between the vectors in X and Y. + Parameters + ---------- + X : array-like, shape (n_samples_1, n_features) + Array of vectors. + Y : array-like, shape (n_samples_2, n_features) + Array of vectors. + Y_norm_squared : array-like, shape (n_samples_2,), optional + Pre-computed sum of squares of Y, to avoid redundant computation. + squared : boolean, optional + Return squared Euclidean distances. + Returns + ------- + distances : array, shape (n_samples_1, n_samples_2) + Euclidean distances between the vectors in X and Y. + """ + X = np.asarray(X) + if X.ndim == 1: + X = X[:, np.newaxis] + if Y is None: + Y = X + else: + Y = np.asarray(Y) + if Y.ndim == 1: + Y = Y[:, np.newaxis] + + # Compute the sum of squares of X + XX_norm_squared = row_norms(X, squared=True)[:, np.newaxis] + + # Compute the sum of squares of Y + if X is Y: # shortcut in the common case euclidean_distances(X, X) + YY_norm_squared = XX_norm_squared.T + elif Y_norm_squared is not None: + YY_norm_squared = Y_norm_squared + else: + YY_norm_squared = row_norms(Y, squared=True)[np.newaxis, :] + + # Compute the dot product + distances = safe_sparse_dot(X, Y.T, dense_output=True) + distances *= -2 + distances += XX_norm_squared + +Generated code for sklearn.metrics.pairwise.pairwise_kernels + + +import numpy as np +from sklearn.metrics.pairwise import pairwise_kernels + +# Create two arrays of random numbers +X = np.random.rand(10, 5) +Y = np.random.rand(10, 5) + +# Compute the pairwise kernel matrix +kernel_matrix = pairwise_kernels(X, Y, metric='rbf') + +# Print the kernel matrix +print(kernel_matrix) +Generated code for sklearn.metrics.pairwise.polynomial_kernel + + +def polynomial_kernel(X, Y, degree=3, gamma=None, coef0=1): + """ + Compute the polynomial kernel between two matrices X and Y:: + K(x, y) = (gamma + coef0)^degree + for each pair of rows x in X and y in Y. + Parameters + ---------- + X : array of shape (n_samples_X, n_features) + Y : array of shape (n_samples_Y, n_features) + degree : int + Degree of the polynomial kernel. + gamma : float, default None + If None, defaults to 1.0 / n_features. + coef0 : float, default 1 + Independent term in kernel expression. + Returns + ------- + kernel_matrix : array of shape (n_samples_X, n_samples_Y) + """ + X = np.atleast_2d(X) + Y = np.atleast_2d(Y) + n_samples_X, n_features = X.shape + n_samples_Y, _ = Y.shape + if gamma is None: + gamma = 1.0 / n_features + K = np.empty((n_samples_X, n_samples_Y), dtype=np.float64) + for i, x_i in enumerate(X): + for j, y_j in enumerate(Y): + K[i, j] = (gamma * np.dot(x_i, y_j) + coef0) ** degree + return K +Generated code for sklearn.metrics.pairwise.rbf_kernel + + +def rbf_kernel(X, Y, gamma): + """ + Computes the RBF (Radial Basis Function) kernel between two sets of vectors. + + Parameters + ---------- + X : array-like, shape (n_samples_X, n_features) + Input array. + Y : array-like, shape (n_samples_Y, n_features) + Input array. + gamma : float + Parameter of the RBF kernel. + + Returns + ------- + K : array-like, shape (n_samples_X, n_samples_Y) + The RBF kernel between X and Y. + """ + # Calculate the squared Euclidean distance between each pair of samples + sq_dists = cdist(X, Y, 'sqeuclidean') + + # Compute the RBF kernel + K = np.exp(-gamma * sq_dists) + + return K +Generated code for sklearn.metrics.pairwise.sigmoid_kernel + + +def sigmoid_kernel(X, Y, gamma=None, coef0=1): + """ + Compute the sigmoid kernel between two matrices of observations. + + Parameters + ---------- + X : array-like, shape = [n_samples_a, n_features] + Left matrix of observations. + + Y : array-like, shape = [n_samples_b, n_features] + Right matrix of observations. + + gamma : float, default=None + Gamma parameter for the sigmoid kernel. + If None, defaults to 1.0 / n_features. + + coef0 : float, default=1 + Coefficient for the sigmoid kernel. + + Returns + ------- + K : array-like, shape = [n_samples_a, n_samples_b] + Sigmoid kernel matrix. + """ + if gamma is None: + gamma = 1.0 / X.shape[1] + K = np.tanh(gamma * np.dot(X, Y.T) + coef0) + return K +Generated code for sklearn.metrics.pairwise.paired_euclidean_distances + + +import numpy as np +from sklearn.metrics.pairwise import paired_euclidean_distances + +# Create two arrays of random numbers +x = np.random.rand(10, 3) +y = np.random.rand(10, 3) + +# Calculate the pairwise Euclidean distances +distances = paired_euclidean_distances(x, y) + +# Print the distances +print(distances) +Generated code for sklearn.metrics.pairwise.paired_manhattan_distances + + +import numpy as np +from sklearn.metrics.pairwise import paired_manhattan_distances + +# Create two arrays of random numbers +x = np.random.rand(10, 3) +y = np.random.rand(10, 3) + +# Calculate the paired Manhattan distances +distances = paired_manhattan_distances(x, y) + +# Print the distances +print(distances) +Generated code for sklearn.metrics.pairwise.paired_cosine_distances + + +import numpy as np +from sklearn.metrics.pairwise import paired_cosine_distances + +# Create two arrays of random numbers +x = np.random.rand(10, 5) +y = np.random.rand(10, 5) + +# Calculate the paired cosine distances +distances = paired_cosine_distances(x, y) + +# Print the distances +print(distances) +Generated code for sklearn.metrics.pairwise.paired_distances + + +import numpy as np +from sklearn.metrics.pairwise import paired_distances + +# Create two arrays of random numbers +x = np.random.rand(10) +y = np.random.rand(10) + +# Calculate the pairwise distances +distances = paired_distances(x, y) + +# Print the distances +print(distances) +Generated code for sklearn.metrics.pairwise_distances + + +import numpy as np +from sklearn.metrics.pairwise import pairwise_distances + +def pairwise_distances(X, Y=None, metric='euclidean', n_jobs=None): + """ + Computes the pairwise distances between samples in X and Y. + + Parameters + ---------- + X : array-like, shape (n_samples_X, n_features) + Input data. + + Y : array-like, shape (n_samples_Y, n_features), optional + Input data. If not provided, the distances between each sample of X are + computed. + + metric : string or callable, default 'euclidean' + The metric to use when calculating distance between instances in a + feature array. If metric is a string, it must be one of the options + allowed by scipy.spatial.distance.pdist for its metric parameter, or + a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. + If metric is "precomputed", X is assumed to be a distance matrix and + must be square. + + n_jobs : int or None, optional (default=None) + The number of jobs to use for the computation. This works by computing + each of the n_init runs in parallel. + ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. + ``-1`` means using all processors. See :term:`Glossary ` + for more details. + + Returns + ------- + distances : array, shape (n_samples_X, n_samples_Y) + The pairwise distances between all samples in X and Y. + """ + X, Y = check_pairwise_arrays(X, Y) + if metric == "precomputed": + X, Y = check_precomputed(X, Y) + if n_jobs is None: + n_jobs = 1 + if n_jobs == -1: + n_jobs = np.max([np.min([X +Generated code for sklearn.metrics.pairwise_distances_argmin + + +def pairwise_distances_argmin(X, Y, metric='euclidean', axis=1): + """ + Compute minimum distances between one point and a set of points. + + Parameters + ---------- + X : array-like, shape (n_samples_X, n_features) + Input set. + Y : array-like, shape (n_samples_Y, n_features) + Input set. + metric : string or callable + The metric to use when calculating distance between instances in a + feature array. If metric is a string, it must be one of the options + allowed by scipy.spatial.distance.pdist for its metric parameter, or + a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. + If metric is "precomputed", X is assumed to be a distance matrix and + must be square. + axis : int + The axis along which the minimum distances are to be computed. + + Returns + ------- + min_distances : array-like, shape (n_samples_X,) + The minimum distances between each point of X and the points of Y. + + Examples + -------- + >>> from sklearn.metrics.pairwise import pairwise_distances_argmin + >>> X = [[0, 1], [3, 4]] + >>> Y = [[1, 1], [2, 2]] + >>> pairwise_distances_argmin(X, Y, axis=1) + array([0, 1]) + """ + distances = pairwise_distances(X, Y, metric=metric, axis=axis) + min_distances = np.argmin(distances, axis=axis) + return min_distances +Generated code for sklearn.metrics.pairwise_distances_argmin_min + + +from sklearn.metrics import pairwise_distances_argmin_min + +def pairwise_distances_argmin_min(X, Y, metric='euclidean', **kwds): + """Compute minimum distances between one point and a set of points. + This function computes the distances between one point and a set of + points using the specified distance metric. It returns the indices of + the points in the set which are closest to the specified point. + Parameters + ---------- + X : array_like, shape (n_samples, n_features) + Array containing the point. + Y : array_like, shape (m_samples, n_features) + Array containing the set of points. + metric : string, or callable + The metric to use when calculating distance between instances in a + feature array. If metric is a string, it must be one of the options + allowed by scipy.spatial.distance.pdist for its metric parameter, or + a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. + If metric is "precomputed", X is assumed to be a distance matrix and + must be square. X may be a sparse matrix, in which case only "nonzero" + elements may be considered neighbors for DBSCAN. + **kwds : optional keyword parameters + Any further parameters are passed directly to the distance function. + If using a scipy.spatial.distance metric, the parameters are still + metric dependent. See the scipy docs for usage examples. + Returns + ------- + distances : ndarray + Array of shape (m_samples,) containing the distances to the closest + point in Y for each point in X. + indices : ndarray + Array of shape (m_samples,) containing the indices of the points in + Y which are closest to each point in X. + """ + distances = pairwise_distances(X, Y, metric=metric, **kwds) + indices = distances.argmin(axis=1) + return distances.min(axis=1), indices +Generated code for sklearn.metrics.pairwise_distances_chunked + + +def pairwise_distances_chunked(X, Y=None, metric='euclidean', n_jobs=None, + working_memory=None, **kwds): + """Compute the pairwise distances between X and Y in chunks. + + This is more memory efficient than pairwise_distances for a large number + of samples. + + Read more in the :ref:`User Guide `. + + Parameters + ---------- + X : array, shape (n_samples_X, n_features) + + Y : array, shape (n_samples_Y, n_features), optional (default=None) + + metric : string or callable, default 'euclidean' + The metric to use when calculating distance between instances in a + feature array. If metric is a string, it must be one of the options + allowed by scipy.spatial.distance.pdist for its metric parameter, or + a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. + If metric is "precomputed", X is assumed to be a distance matrix and + must be square. X may be a sparse matrix, in which case only "nonzero" + elements may be considered neighbors for DBSCAN. + + n_jobs : int or None, optional (default=None) + The number of jobs to use for the computation. This works by breaking + down the pairwise matrix into n_jobs even slices and computing them in + parallel. + ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. + ``-1`` means using all processors. See :term:`Glossary ` + for more details. + + working_memory : float, optional (default=None) + The maximum number of elements (samples * features) in the working + memory. If None, the value is set to 2 * n_samples * n_features. + The working memory is the memory used to store the pairwise distances + of a single chunk. + + **kwds : dict + Keyword arguments to pass to the distance function. + + +Generated code for sklearn.metrics.ConfusionMatrixDisplay + + +from sklearn.metrics import ConfusionMatrixDisplay + +# Create a confusion matrix +y_true = [2, 0, 2, 2, 0, 1] +y_pred = [0, 0, 2, 2, 0, 2] +cm = confusion_matrix(y_true, y_pred) + +# Create a ConfusionMatrixDisplay +cm_display = ConfusionMatrixDisplay(cm, display_labels=['Class 0', 'Class 1', 'Class 2']) + +# Plot the confusion matrix +cm_display.plot() +Generated code for sklearn.metrics.DetCurveDisplay + + +from sklearn.metrics import DetCurveDisplay + +# Generate some data +y_true = [0, 0, 0, 0, 1, 1, 1, 1] +y_score = [0.1, 0.3, 0.2, 0.4, 0.8, 0.6, 0.7, 0.5] + +# Create DetCurveDisplay object +det_display = DetCurveDisplay(y_true, y_score) + +# Plot the DET curve +det_display.plot() +Generated code for sklearn.metrics.PrecisionRecallDisplay + + +from sklearn.metrics import precision_recall_curve + +def PrecisionRecallDisplay(y_true, y_score): + precision, recall, _ = precision_recall_curve(y_true, y_score) + plt.step(recall, precision, color='b', alpha=0.2, + where='post') + plt.fill_between(recall, precision, step='post', alpha=0.2, + color='b') + plt.xlabel('Recall') + plt.ylabel('Precision') + plt.ylim([0.0, 1.05]) + plt.xlim([0.0, 1.0]) + plt.title('2-class Precision-Recall curve') +Generated code for sklearn.metrics.PredictionErrorDisplay + + +from sklearn.metrics import mean_squared_error + +def prediction_error_display(y_true, y_pred): + """ + Calculates and displays the prediction error. + + Parameters + ---------- + y_true : array-like of shape (n_samples,) + Ground truth (correct) target values. + y_pred : array-like of shape (n_samples,) + Estimated target values. + + Returns + ------- + None + """ + mse = mean_squared_error(y_true, y_pred) + print('Mean Squared Error:', mse) +Generated code for sklearn.metrics.RocCurveDisplay + + +from sklearn.metrics import roc_curve +import matplotlib.pyplot as plt + +# Generate the data +y_true = [0, 0, 1, 1] +y_scores = [0.1, 0.4, 0.35, 0.8] + +# Compute the false positive rate, true positive rate, and thresholds +fpr, tpr, thresholds = roc_curve(y_true, y_scores) + +# Plot the ROC curve +plt.plot(fpr, tpr) +plt.xlabel('False Positive Rate') +plt.ylabel('True Positive Rate') +plt.title('ROC Curve') +plt.show() +Generated code for sklearn.calibration.CalibrationDisplay + + +from sklearn.calibration import CalibrationDisplay + +# Create a calibration display object +calibration_display = CalibrationDisplay(predictions, labels) + +# Plot the calibration curve +calibration_display.plot() + +# Get the calibration error +calibration_error = calibration_display.calibration_error + +# Get the reliability diagram +reliability_diagram = calibration_display.reliability_diagram +Generated code for sklearn.mixture.BayesianGaussianMixture + + +import numpy as np +from sklearn.mixture import BayesianGaussianMixture + +# Generate data +X = np.random.rand(100, 2) + +# Create and fit the Bayesian Gaussian Mixture model +bgm = BayesianGaussianMixture(n_components=3, covariance_type='full') +bgm.fit(X) + +# Predict the cluster for each data point +y_pred = bgm.predict(X) + +# Print the cluster labels +print(y_pred) +Generated code for sklearn.mixture.GaussianMixture + + +import numpy as np +from sklearn.mixture import GaussianMixture + +# Create a Gaussian Mixture model +gmm = GaussianMixture(n_components=3) + +# Fit the model to the data +gmm.fit(X) + +# Predict the labels for the data +labels = gmm.predict(X) + +# Compute the log-likelihood of the data +log_likelihood = gmm.score(X) + +# Compute the means of the Gaussian components +means = gmm.means_ + +# Compute the covariance matrices of the Gaussian components +covariances = gmm.covariances_ + +# Compute the weights of the Gaussian components +weights = gmm.weights_ +Generated code for sklearn.model_selection.GroupKFold + + +from sklearn.model_selection import GroupKFold + +# Create a GroupKFold object +group_kfold = GroupKFold(n_splits=5) + +# Fit the object to the data +X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) +y = np.array([1, 2, 3, 4]) +groups = np.array([1, 1, 2, 2]) +group_kfold.get_n_splits(X, y, groups) + +# Iterate through the splits +for train_index, test_index in group_kfold.split(X, y, groups): + print("TRAIN:", train_index, "TEST:", test_index) + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + print(X_train, X_test, y_train, y_test) +Generated code for sklearn.model_selection.GroupShuffleSplit + + +from sklearn.model_selection import GroupShuffleSplit + +# Create a GroupShuffleSplit object +gss = GroupShuffleSplit(n_splits=5, test_size=0.2, random_state=42) + +# Fit the object to the data +X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +groups = [1, 1, 1, 2, 2, 2, 3, 3, 3, 3] +gss.get_n_splits(X, y, groups) + +# Generate the splits +for train_index, test_index in gss.split(X, y, groups): + print("TRAIN:", train_index, "TEST:", test_index) + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + print(X_train, X_test, y_train, y_test) +Generated code for sklearn.model_selection.KFold + + +from sklearn.model_selection import KFold + +# Create a KFold object +kf = KFold(n_splits=5, shuffle=True, random_state=42) + +# Iterate through the splits +for train_index, test_index in kf.split(X): + # Generate training and testing data + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + # Fit the model and generate predictions + model.fit(X_train, y_train) + y_pred = model.predict(X_test) + + # Evaluate the model + score = model.score(X_test, y_test) + print(score) +Generated code for sklearn.model_selection.LeaveOneGroupOut + + +from sklearn.model_selection import LeaveOneGroupOut + +# Create a list of groups +groups = [1, 1, 1, 2, 2, 2, 3, 3, 3] + +# Create the LeaveOneGroupOut object +logo = LeaveOneGroupOut() + +# Generate the splits +for train_index, test_index in logo.split(X, y, groups): + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + # Fit the model and make predictions + model.fit(X_train, y_train) + predictions = model.predict(X_test) +Generated code for sklearn.model_selection.LeavePGroupsOut + + +from sklearn.model_selection import LeavePGroupsOut + +# Create LeavePGroupsOut object +lpgo = LeavePGroupsOut(n_groups=3, p=2) + +# Generate indices for training and testing sets +for train_index, test_index in lpgo.split(X, y, groups): + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + # Fit model and make predictions + model.fit(X_train, y_train) + y_pred = model.predict(X_test) +Generated code for sklearn.model_selection.LeaveOneOut + + +from sklearn.model_selection import LeaveOneOut + +# Create a LeaveOneOut object +loo = LeaveOneOut() + +# Iterate through the splits +for train_index, test_index in loo.split(X): + # Create train and test sets + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + # Fit the model + model.fit(X_train, y_train) + + # Make predictions + y_pred = model.predict(X_test) + + # Calculate accuracy + accuracy = accuracy_score(y_test, y_pred) + + # Store accuracy + accuracies.append(accuracy) +Generated code for sklearn.model_selection.LeavePOut + + +from sklearn.model_selection import LeavePOut + +# Create a dataset +X = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16]] + +# Create the LeavePOut object +lpo = LeavePOut(p=2) + +# Generate the splits +for train_index, test_index in lpo.split(X): + print("Train Index:", train_index, "\nTest Index:", test_index) + X_train, X_test = X[train_index], X[test_index] + print("X_train:", X_train, "\nX_test:", X_test) + print("\n") +Generated code for sklearn.model_selection.PredefinedSplit + + +from sklearn.model_selection import PredefinedSplit + +# Create a predefined split object +split_object = PredefinedSplit(test_fold=[-1, 0, 0, 1, 1, 1]) + +# Fit the model using the predefined split object +model.fit(X, y, predefined_split=split_object) +Generated code for sklearn.model_selection.RepeatedKFold + + +from sklearn.model_selection import RepeatedKFold + +# Create the RepeatedKFold object +rkf = RepeatedKFold(n_splits=5, n_repeats=2, random_state=42) + +# Iterate through the splits +for train_index, test_index in rkf.split(X): + # Generate training and testing sets + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + # Fit the model and generate predictions + model.fit(X_train, y_train) + y_pred = model.predict(X_test) + + # Evaluate the model + score = model.score(X_test, y_test) + print(score) +Generated code for sklearn.model_selection.RepeatedStratifiedKFold + + +from sklearn.model_selection import RepeatedStratifiedKFold + +# Create the RepeatedStratifiedKFold object +rskf = RepeatedStratifiedKFold(n_splits=5, n_repeats=2, random_state=42) + +# Iterate through the splits +for train_index, test_index in rskf.split(X, y): + print("Train:", train_index, "Test:", test_index) + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] +Generated code for sklearn.model_selection.ShuffleSplit + + +from sklearn.model_selection import ShuffleSplit + +# Create a ShuffleSplit object +shuffle_split = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0) + +# Iterate through the splits +for train_index, test_index in shuffle_split.split(X): + # Generate training and testing datasets + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + # Fit a model on the training data + model.fit(X_train, y_train) + + # Make predictions on the test data + predictions = model.predict(X_test) +Generated code for sklearn.model_selection.StratifiedKFold + + +from sklearn.model_selection import StratifiedKFold + +X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +y = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1] + +skf = StratifiedKFold(n_splits=5) + +for train_index, test_index in skf.split(X, y): + print("Train:", train_index, "Test:", test_index) + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + +# Output +# Train: [2 3 4 5 6 7 8 9] Test: [0 1] +# Train: [0 1 4 5 6 7 8 9] Test: [2 3] +# Train: [0 1 2 3 6 7 8 9] Test: [4 5] +# Train: [0 1 2 3 4 5 8 9] Test: [6 7] +# Train: [0 1 2 3 4 5 6 7] Test: [8 9] +Generated code for sklearn.model_selection.StratifiedShuffleSplit + + +from sklearn.model_selection import StratifiedShuffleSplit + +# Create the Stratified Shuffle Split object +sss = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=0) + +# Fit the SSS object to the data +sss.fit(X, y) + +# Iterate through the splits +for train_index, test_index in sss.split(X, y): + # Generate training and testing datasets + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + # Fit the model on the training data + model.fit(X_train, y_train) + + # Make predictions on the test data + y_pred = model.predict(X_test) + + # Evaluate the model + score = model.score(X_test, y_test) +Generated code for sklearn.model_selection.StratifiedGroupKFold + + +from sklearn.model_selection import StratifiedGroupKFold + +# Create a StratifiedGroupKFold object +sgkf = StratifiedGroupKFold(n_splits=5, shuffle=True, random_state=42) + +# Create a list of the groups +groups = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4] + +# Create a list of the labels +labels = [0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1] + +# Iterate through the splits +for train_index, test_index in sgkf.split(X, y, groups): + # Get the training and testing data + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + # Fit the model on the training data + model.fit(X_train, y_train) + # Make predictions on the testing data + y_pred = model.predict(X_test) + # Evaluate the model + print(metrics.accuracy_score(y_test, y_pred)) +Generated code for sklearn.model_selection.TimeSeriesSplit + + +from sklearn.model_selection import TimeSeriesSplit + +# Create TimeSeriesSplit object +tscv = TimeSeriesSplit(n_splits=5) + +# Split data into train/test sets +for train_index, test_index in tscv.split(X): + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + # Fit model on train set + model.fit(X_train, y_train) + + # Make predictions on test set + y_pred = model.predict(X_test) +Generated code for sklearn.model_selection.check_cv + + +import numpy as np +from sklearn.model_selection import check_cv + +# Generate a random dataset +X = np.random.rand(100, 5) +y = np.random.randint(2, size=100) + +# Create a cross-validation object +cv = check_cv(cv=5, X=X, y=y, classifier=True) + +# Print the results +print(cv) +Generated code for sklearn.model_selection.train_test_split + + +import numpy as np +from sklearn.model_selection import train_test_split + +# Create a data set +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) +y = np.array([1, 2, 3, 4]) + +# Split the data into training and test sets +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Print the results +print("X_train:", X_train) +print("y_train:", y_train) +print("X_test:", X_test) +print("y_test:", y_test) +Generated code for sklearn.model_selection.GridSearchCV + + +import numpy as np +from sklearn.model_selection import GridSearchCV + +# Create a classifier +clf = DecisionTreeClassifier() + +# Create a parameter grid +param_grid = {'max_depth': np.arange(3, 10), + 'min_samples_leaf': np.arange(3, 10)} + +# Instantiate the GridSearchCV object +grid_search = GridSearchCV(clf, param_grid, cv=5) + +# Fit the model +grid_search.fit(X, y) + +# Print the best parameters +print(grid_search.best_params_) +Generated code for sklearn.model_selection.HalvingGridSearchCV + + +import numpy as np +from sklearn.model_selection import HalvingGridSearchCV + +# Create a classifier +clf = DecisionTreeClassifier() + +# Create a parameter grid +param_grid = {'max_depth': np.arange(1, 10), + 'min_samples_split': np.arange(2, 10)} + +# Create a HalvingGridSearchCV object +halving_grid_search = HalvingGridSearchCV(clf, param_grid, cv=5, scoring='accuracy') + +# Fit the model +halving_grid_search.fit(X, y) + +# Print the best parameters +print(halving_grid_search.best_params_) +Generated code for sklearn.model_selection.ParameterGrid + + +from sklearn.model_selection import ParameterGrid + +param_grid = {'a': [1, 2], 'b': [True, False]} + +param_grid_list = list(ParameterGrid(param_grid)) + +for params in param_grid_list: + print(params) + +# Output: +# {'a': 1, 'b': True} +# {'a': 1, 'b': False} +# {'a': 2, 'b': True} +# {'a': 2, 'b': False} +Generated code for sklearn.model_selection.ParameterSampler + + +from sklearn.model_selection import ParameterSampler + +# define the parameter space +param_space = { + 'n_estimators': [10, 50, 100], + 'max_depth': [3, 5, 7], + 'min_samples_split': [2, 5, 10] +} + +# create the ParameterSampler +param_sampler = ParameterSampler(param_space, n_iter=10, random_state=42) + +# iterate through the parameter combinations +for params in param_sampler: + print(params) +Generated code for sklearn.model_selection.RandomizedSearchCV + + +import numpy as np +from sklearn.model_selection import RandomizedSearchCV + +# Define the parameter values that should be searched +param_grid = {'n_estimators': np.arange(10, 200, 10), + 'max_depth': np.arange(1, 10)} + +# Create a base model +model = RandomForestClassifier() + +# Instantiate the random search model +random_search = RandomizedSearchCV(estimator = model, param_distributions = param_grid, + n_iter = 10, cv = 3, verbose=2, random_state=42, n_jobs = -1) + +# Fit the random search model +random_search.fit(X_train, y_train) +Generated code for sklearn.model_selection.HalvingRandomSearchCV + + +import numpy as np +from sklearn.model_selection import HalvingRandomSearchCV + +# define the model +model = SomeModel() + +# define the parameter grid +param_grid = { + 'param1': np.arange(0, 10, 0.5), + 'param2': np.arange(0, 10, 0.5), + 'param3': np.arange(0, 10, 0.5) +} + +# define the HalvingRandomSearchCV +halving_rs = HalvingRandomSearchCV(model, param_grid, cv=5, n_iter=10, scoring='accuracy') + +# fit the model +halving_rs.fit(X, y) + +# print the best parameters +print(halving_rs.best_params_) +Generated code for sklearn.model_selection.cross_validate + + +from sklearn.model_selection import cross_validate + +def cross_validate(estimator, X, y, scoring=None, cv=None, + n_jobs=None, verbose=0, fit_params=None, + pre_dispatch='2*n_jobs', return_train_score=False): + """Evaluate a score by cross-validation + Read more in the :ref:`User Guide `. + Parameters + ---------- + estimator : estimator object implementing 'fit' + The object to use to fit the data. + X : array-like + The data to fit. Can be, for example a list, or an array. + y : array-like, optional, default: None + The target variable to try to predict in the case of + supervised learning. + scoring : string, callable, list/tuple, dict or None, default: None + A single string (see :ref:`scoring_parameter`) or a callable + (see :ref:`scoring`) to evaluate the predictions on the test set. + For evaluating multiple metrics, either give a list of (unique) strings + or a dict with names as keys and callables as values. + NOTE that when using custom scorers, each scorer should return a single + value. Metric functions returning a list/array of values can be wrapped + into multiple scorers that return one value each. + See :ref:`multimetric_grid_search` for an example. + If None, the estimator's score method is used. + cv : int, cross-validation generator or an iterable, optional + Determines the cross-validation splitting strategy. + Possible inputs for cv are: + - None, to use the default 5-fold cross-validation, + - integer, to specify the number of folds. + - :term:`CV splitter`, + - An iterable yielding (train, test) splits as arrays of indices. + For integer/None inputs, if ``y`` is binary or multiclass, + :class:`StratifiedKFold` used. +Generated code for sklearn.model_selection.cross_val_predict + + +from sklearn.model_selection import cross_val_predict + +def cross_val_predict(estimator, X, y, cv=None): + """ + Generate cross-validated estimates for each input data point + + Parameters + ---------- + estimator : estimator object implementing 'fit' + The object to use to fit the data. + + X : array-like + The data to fit. + + y : array-like + The target variable to try to predict. + + cv : int, cross-validation generator or an iterable, optional + Determines the cross-validation splitting strategy. + Possible inputs for cv are: + - None, to use the default 3-fold cross-validation, + - integer, to specify the number of folds. + - An object to be used as a cross-validation generator. + - An iterable yielding train/test splits. + + Returns + ------- + predictions : array-like + The predicted values. + """ + # Generate cross-validation folds + cv_folds = cv or 3 + kf = KFold(n_splits=cv_folds) + # Fit the model on each fold + predictions = [] + for train_index, test_index in kf.split(X): + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + estimator.fit(X_train, y_train) + predictions.append(estimator.predict(X_test)) + # Return the predictions + return np.concatenate(predictions) +Generated code for sklearn.model_selection.cross_val_score + + +import numpy as np +from sklearn.model_selection import cross_val_score +from sklearn.ensemble import RandomForestClassifier + +# Create a random forest classifier +clf = RandomForestClassifier(n_estimators=10) + +# Create a dataset +X = np.random.rand(100, 10) +y = np.random.randint(2, size=100) + +# Calculate the cross-validation score +scores = cross_val_score(clf, X, y, cv=5) + +# Print the mean score +print("Mean cross-validation score: {:.2f}".format(scores.mean())) +Generated code for sklearn.model_selection.learning_curve + + +import numpy as np +from sklearn.model_selection import learning_curve +from sklearn.svm import SVC + +# Generate a random dataset +X = np.random.rand(1000, 10) +y = np.random.randint(2, size=1000) + +# Create a Support Vector Classifier +svc = SVC(gamma='auto') + +# Generate the learning curve +train_sizes, train_scores, test_scores = learning_curve(svc, X, y, cv=10, scoring='accuracy', + n_jobs=-1, train_sizes=np.linspace(.1, 1.0, 5)) + +# Calculate the mean and standard deviation for training and testing scores +train_mean = np.mean(train_scores, axis=1) +train_std = np.std(train_scores, axis=1) +test_mean = np.mean(test_scores, axis=1) +test_std = np.std(test_scores, axis=1) + +# Plot the learning curve +plt.plot(train_sizes, train_mean, '--', color="#111111", label="Training score") +plt.plot(train_sizes, test_mean, color="#111111", label="Cross-validation score") + +# Draw bands +plt.fill_between(train_sizes, train_mean - train_std, train_mean + train_std, color="#DDDDDD") +plt.fill_between(train_sizes, test_mean - test_std, test_mean + test_std, color="#DDDDDD") + +# Create plot +plt.title("Learning Curve") +plt.xlabel("Training Set Size"), plt.ylabel("Accuracy Score"), plt.legend(loc="best") +plt.tight_layout() +plt.show() +Generated code for sklearn.model_selection.permutation_test_score + + +import numpy as np +from sklearn.model_selection import permutation_test_score + +# Generate some data +X = np.random.rand(100, 10) +y = np.random.randint(2, size=100) + +# Fit a model +model = SomeModel() +model.fit(X, y) + +# Calculate the permutation test score +score, pvalue, _ = permutation_test_score(model, X, y, cv=5, n_permutations=100) + +print("Score: %f, p-value: %f" % (score, pvalue)) +Generated code for sklearn.model_selection.validation_curve + + +from sklearn.model_selection import validation_curve + +def validation_curve_example(model, X, y, param_name, param_range): + train_scores, test_scores = validation_curve( + model, X, y, param_name=param_name, param_range=param_range, + cv=3, scoring="accuracy", n_jobs=-1) + train_mean = np.mean(train_scores, axis=1) + train_std = np.std(train_scores, axis=1) + test_mean = np.mean(test_scores, axis=1) + test_std = np.std(test_scores, axis=1) + plt.plot(param_range, train_mean, + color='blue', marker='o', + markersize=5, label='training accuracy') + plt.fill_between(param_range, train_mean + train_std, + train_mean - train_std, alpha=0.15, + color='blue') + plt.plot(param_range, test_mean, + color='green', linestyle='--', + marker='s', markersize=5, + label='validation accuracy') + plt.fill_between(param_range, + test_mean + test_std, + test_mean - test_std, + alpha=0.15, color='green') + plt.grid() + plt.xscale('log') + plt.legend(loc='lower right') + plt.xlabel('Parameter') + plt.ylabel('Accuracy') + plt.ylim([0.8, 1.0]) + plt.show() +Generated code for sklearn.model_selection.LearningCurveDisplay + + +from sklearn.model_selection import LearningCurveDisplay + +# Create a LearningCurveDisplay object +lcd = LearningCurveDisplay(estimator, + X, + y, + cv=5, + scoring='accuracy', + train_sizes=np.linspace(0.1, 1.0, 10)) + +# Fit the model +lcd.fit(X, y) + +# Plot the learning curve +lcd.poof() +Generated code for sklearn.multiclass.OneVsRestClassifier + + +from sklearn.multiclass import OneVsRestClassifier +from sklearn.svm import SVC + +# Create the model +model = OneVsRestClassifier(SVC(kernel='linear')) + +# Fit the model +model.fit(X_train, y_train) + +# Make predictions +y_pred = model.predict(X_test) +Generated code for sklearn.multiclass.OneVsOneClassifier + + +from sklearn.multiclass import OneVsOneClassifier + +# Create a classifier object +clf = OneVsOneClassifier() + +# Train the classifier +clf.fit(X_train, y_train) + +# Make predictions +y_pred = clf.predict(X_test) + +# Evaluate the model +accuracy = clf.score(X_test, y_test) +Generated code for sklearn.multiclass.OutputCodeClassifier + + +from sklearn.multiclass import OutputCodeClassifier + +# Create the OutputCodeClassifier object +clf = OutputCodeClassifier(estimator=None, code_size=2, random_state=0) + +# Fit the model using the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Evaluate the model +accuracy = clf.score(X_test, y_test) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.multioutput.ClassifierChain + + +from sklearn.multioutput import ClassifierChain + +# Create the classifier chain +clf_chain = ClassifierChain() + +# Fit the classifier chain to the training data +clf_chain.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf_chain.predict(X_test) + +# Compute accuracy +accuracy = clf_chain.score(X_test, y_test) +Generated code for sklearn.multioutput.MultiOutputRegressor + + +from sklearn.multioutput import MultiOutputRegressor + +# Create the base estimator +base_estimator = DecisionTreeRegressor() + +# Create the multi-output regressor +multi_output_regressor = MultiOutputRegressor(base_estimator) + +# Fit the multi-output regressor +multi_output_regressor.fit(X, y) + +# Make predictions +predictions = multi_output_regressor.predict(X_test) +Generated code for sklearn.multioutput.MultiOutputClassifier + + +from sklearn.multioutput import MultiOutputClassifier +from sklearn.ensemble import RandomForestClassifier + +# Create a random forest classifier +clf = RandomForestClassifier(random_state=0) + +# Create a multi-output classifier +multi_clf = MultiOutputClassifier(clf, n_jobs=-1) +Generated code for sklearn.multioutput.RegressorChain + + +from sklearn.multioutput import RegressorChain + +# Create the RegressorChain object +regressor_chain = RegressorChain() + +# Fit the model +regressor_chain.fit(X, y) + +# Make predictions +predictions = regressor_chain.predict(X_test) + +# Evaluate the model +score = regressor_chain.score(X_test, y_test) +Generated code for sklearn.naive_bayes.BernoulliNB + + +from sklearn.naive_bayes import BernoulliNB + +# Create an instance of the BernoulliNB classifier +clf = BernoulliNB() + +# Fit the classifier to the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Calculate the accuracy of the model +accuracy = clf.score(X_test, y_test) +Generated code for sklearn.naive_bayes.CategoricalNB + + +from sklearn.naive_bayes import CategoricalNB + +# Create an instance of the CategoricalNB class +clf = CategoricalNB() + +# Fit the model to the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Calculate the accuracy of the model +accuracy = clf.score(X_test, y_test) +Generated code for sklearn.naive_bayes.ComplementNB + + +from sklearn.naive_bayes import ComplementNB + +# Create an instance of the ComplementNB class +clf = ComplementNB() + +# Fit the model to the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +predictions = clf.predict(X_test) + +# Evaluate the model's performance +accuracy = clf.score(X_test, y_test) +print("Accuracy: {:.2f}%".format(accuracy*100)) +Generated code for sklearn.naive_bayes.GaussianNB + + +from sklearn.naive_bayes import GaussianNB + +# Create a Gaussian Classifier +model = GaussianNB() + +# Train the model using the training sets +model.fit(X_train, y_train) + +# Predict Output +predicted= model.predict(X_test) +Generated code for sklearn.naive_bayes.MultinomialNB + + +from sklearn.naive_bayes import MultinomialNB + +# Create an instance of the MultinomialNB classifier +clf = MultinomialNB() + +# Train the classifier using the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +predictions = clf.predict(X_test) + +# Evaluate the accuracy of the model +accuracy = clf.score(X_test, y_test) +Generated code for sklearn.neighbors.BallTree + + +from sklearn.neighbors import BallTree + +# Create a BallTree object +ball_tree = BallTree(data, leaf_size=30, metric='euclidean') + +# Query the tree for the k nearest neighbors +distances, indices = ball_tree.query(query_point, k=k) + +# Get the neighbors +neighbors = data[indices] +Generated code for sklearn.neighbors.KDTree + + +from sklearn.neighbors import KDTree + +# Create a KDTree object +kdtree = KDTree(data, leaf_size=30, metric='euclidean') + +# Query the tree for the nearest neighbors +dist, ind = kdtree.query(query_point, k=k) + +# Get the distances and indices of the k nearest neighbors +neighbors = data[ind] +distances = dist +Generated code for sklearn.neighbors.KernelDensity + + +from sklearn.neighbors import KernelDensity + +# Create the KernelDensity object +kd = KernelDensity(bandwidth=1.0) + +# Fit the data to the model +kd.fit(X) + +# Make predictions +predictions = kd.predict(X) +Generated code for sklearn.neighbors.KNeighborsClassifier + + +from sklearn.neighbors import KNeighborsClassifier + +# Create a KNeighborsClassifier object +knn = KNeighborsClassifier() + +# Fit the model using the training data +knn.fit(X_train, y_train) + +# Make predictions on the test data +predictions = knn.predict(X_test) + +# Evaluate the model +accuracy = knn.score(X_test, y_test) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.neighbors.KNeighborsRegressor + + +from sklearn.neighbors import KNeighborsRegressor + +# Create a KNeighborsRegressor object +knn_regressor = KNeighborsRegressor() + +# Fit the model to the data +knn_regressor.fit(X_train, y_train) + +# Make predictions on the test set +y_pred = knn_regressor.predict(X_test) + +# Evaluate the model +score = knn_regressor.score(X_test, y_test) +Generated code for sklearn.neighbors.KNeighborsTransformer + + +from sklearn.neighbors import KNeighborsTransformer + +# Create a KNeighborsTransformer object +knn_transformer = KNeighborsTransformer(n_neighbors=3, weights='distance') + +# Fit the transformer to the data +knn_transformer.fit(X) + +# Transform the data +X_transformed = knn_transformer.transform(X) +Generated code for sklearn.neighbors.LocalOutlierFactor + + +from sklearn.neighbors import LocalOutlierFactor + +# Create the Local Outlier Factor (LOF) model +lof_model = LocalOutlierFactor(n_neighbors=20, contamination=0.1) + +# Fit the model to the data +lof_model.fit(X) + +# Get the outlier scores +outlier_scores = lof_model.negative_outlier_factor_ + +# Get the indices of the outliers +outlier_indices = np.where(outlier_scores > 1.2)[0] + +# Print the outliers +print(X[outlier_indices]) +Generated code for sklearn.neighbors.RadiusNeighborsClassifier + + +from sklearn.neighbors import RadiusNeighborsClassifier + +# Create a RadiusNeighborsClassifier object +rnc = RadiusNeighborsClassifier(radius=1.0) + +# Fit the model using the training data +rnc.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = rnc.predict(X_test) + +# Evaluate the model +accuracy = rnc.score(X_test, y_test) +print('Accuracy: %.2f' % accuracy) +Generated code for sklearn.neighbors.RadiusNeighborsRegressor + + +from sklearn.neighbors import RadiusNeighborsRegressor + +# Create a RadiusNeighborsRegressor object +rneigh = RadiusNeighborsRegressor(radius=1.0) + +# Fit the model using the training data +rneigh.fit(X_train, y_train) + +# Make predictions using the test data +y_pred = rneigh.predict(X_test) + +# Evaluate the model +score = rneigh.score(X_test, y_test) +print("RadiusNeighborsRegressor score: %.2f" % score) +Generated code for sklearn.neighbors.RadiusNeighborsTransformer + + +from sklearn.neighbors import RadiusNeighborsTransformer + +# Create an instance of the transformer +radius_neighbors_transformer = RadiusNeighborsTransformer() + +# Fit the transformer to the data +radius_neighbors_transformer.fit(X) + +# Transform the data +X_transformed = radius_neighbors_transformer.transform(X) +Generated code for sklearn.neighbors.NearestCentroid + + +from sklearn.neighbors import NearestCentroid + +# Create a NearestCentroid object +clf = NearestCentroid() + +# Fit the model to the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +predictions = clf.predict(X_test) + +# Evaluate the model +score = clf.score(X_test, y_test) +Generated code for sklearn.neighbors.NearestNeighbors + + +from sklearn.neighbors import NearestNeighbors + +# Create a NearestNeighbors object +neigh = NearestNeighbors(n_neighbors=3) + +# Fit the model using the training data +neigh.fit(X_train) + +# Find the k-nearest neighbors of a given point +distances, indices = neigh.kneighbors([point]) + +# Get the labels of the k-nearest neighbors +labels = [y_train[i] for i in indices] +Generated code for sklearn.neighbors.NeighborhoodComponentsAnalysis + + +from sklearn.neighbors import NeighborhoodComponentsAnalysis + +# Create an instance of the NeighborhoodComponentsAnalysis class +nca = NeighborhoodComponentsAnalysis(random_state=42) + +# Fit the model to the data +nca.fit(X, y) + +# Make predictions +predictions = nca.predict(X_test) + +# Evaluate the model +score = nca.score(X_test, y_test) +Generated code for sklearn.neighbors.kneighbors_graph + + +import numpy as np +from sklearn.neighbors import kneighbors_graph + +# Generate sample data +X = np.random.random((10, 2)) + +# Compute the k-neighbors graph +knn_graph = kneighbors_graph(X, n_neighbors=3, mode='connectivity', include_self=True) + +# Print the resulting adjacency matrix +print(knn_graph.toarray()) +Generated code for sklearn.neighbors.radius_neighbors_graph + + +from sklearn.neighbors import radius_neighbors_graph + +# Create a graph of radius-based neighbors +X = [[0], [3], [1]] +radius = 1.0 +graph = radius_neighbors_graph(X, radius, mode='connectivity', include_self=True) + +# Print the graph +print(graph.toarray()) + +# Output: +# [[1. 1. 0.] +# [1. 1. 1.] +# [0. 1. 1.]] +Generated code for sklearn.neighbors.sort_graph_by_row_values + + +def sort_graph_by_row_values(graph, row_values): + """ + Sort a graph by row values. + + Parameters + ---------- + graph : numpy.ndarray + The graph to be sorted. + row_values : list + A list of row values to sort the graph by. + + Returns + ------- + sorted_graph : numpy.ndarray + The sorted graph. + """ + # Get the indices of the rows in the graph + row_indices = np.arange(graph.shape[0]) + + # Sort the row indices by the row values + sorted_row_indices = np.argsort(row_values) + + # Create a new graph with the sorted row indices + sorted_graph = graph[sorted_row_indices, :] + + return sorted_graph +Generated code for sklearn.neural_network.BernoulliRBM + + +from sklearn.neural_network import BernoulliRBM + +# Create BernoulliRBM object +rbm = BernoulliRBM(n_components=100, learning_rate=0.01, n_iter=20, random_state=0) + +# Fit the model to the data +rbm.fit(X_train) + +# Transform the data +X_transformed = rbm.transform(X_train) + +# Predict the labels +y_pred = rbm.predict(X_test) +Generated code for sklearn.neural_network.MLPClassifier + + +# Importing the necessary libraries +from sklearn.neural_network import MLPClassifier + +# Creating the MLPClassifier object +mlp = MLPClassifier(hidden_layer_sizes=(100,100,100), activation='relu', solver='adam', max_iter=500) + +# Fitting the model +mlp.fit(X_train, y_train) + +# Making predictions +y_pred = mlp.predict(X_test) + +# Evaluating the model +accuracy = mlp.score(X_test, y_test) +print("Accuracy: %.2f%%" % (accuracy * 100.0)) +Generated code for sklearn.neural_network.MLPRegressor + + +from sklearn.neural_network import MLPRegressor + +# Create the MLPRegressor object +mlp_regressor = MLPRegressor(hidden_layer_sizes=(100,), activation='relu', solver='adam', + alpha=0.0001, batch_size='auto', learning_rate='constant', + learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, + random_state=None, tol=0.0001, verbose=False, warm_start=False, + momentum=0.9, nesterovs_momentum=True, early_stopping=False, + validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08) + +# Fit the model to the data +mlp_regressor.fit(X_train, y_train) + +# Make predictions on the test set +y_pred = mlp_regressor.predict(X_test) +Generated code for sklearn.pipeline.FeatureUnion + + +from sklearn.pipeline import FeatureUnion + +class FeatureUnion(BaseEstimator, TransformerMixin): + """ + A FeatureUnion is a transformer that combines several transformer objects + to form a new transformer. + """ + def __init__(self, transformer_list): + self.transformer_list = transformer_list + + def fit(self, X, y=None): + for (name, t) in self.transformer_list: + t.fit(X, y) + return self + + def transform(self, X): + Xts = [t.transform(X) for _, t in self.transformer_list] + return np.hstack(Xts) +Generated code for sklearn.pipeline.Pipeline + + +from sklearn.pipeline import Pipeline + +# Create a pipeline object +pipeline = Pipeline([ + ('clf', SomeClassifier()), + ('scaler', StandardScaler()), + ('pca', PCA()) +]) + +# Fit the pipeline to the data +pipeline.fit(X_train, y_train) + +# Make predictions +y_pred = pipeline.predict(X_test) +Generated code for sklearn.pipeline.make_pipeline + + +from sklearn.pipeline import make_pipeline +from sklearn.preprocessing import StandardScaler +from sklearn.linear_model import LogisticRegression + +# Create a pipeline object +pipeline = make_pipeline(StandardScaler(), LogisticRegression()) + +# Fit the pipeline to the training data +pipeline.fit(X_train, y_train) + +# Predict the labels of the test set +y_pred = pipeline.predict(X_test) +Generated code for sklearn.pipeline.make_union + + +from sklearn.pipeline import make_union + +# Create a list of transformers +transformers = [('transformer1', transformer1), + ('transformer2', transformer2), + ('transformer3', transformer3)] + +# Create the union +union = make_union(*transformers) +Generated code for sklearn.preprocessing.Binarizer + + +from sklearn.preprocessing import Binarizer + +# Create an instance of the Binarizer class +binarizer = Binarizer() + +# Fit the binarizer to the data +binarizer.fit(X) + +# Transform the data +X_binarized = binarizer.transform(X) +Generated code for sklearn.preprocessing.FunctionTransformer + + +from sklearn.preprocessing import FunctionTransformer + +def custom_function(X): + # Custom function to be applied to the data + return X**2 + +transformer = FunctionTransformer(custom_function) + +# Fit the transformer to the data +transformer.fit(X) + +# Transform the data +X_transformed = transformer.transform(X) +Generated code for sklearn.preprocessing.KBinsDiscretizer + + +from sklearn.preprocessing import KBinsDiscretizer + +# Create the discretizer +discretizer = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform') + +# Fit the discretizer to the data +discretizer.fit(X) + +# Transform the data +X_binned = discretizer.transform(X) +Generated code for sklearn.preprocessing.KernelCenterer + + +class KernelCenterer: + """ + Centering a kernel matrix + """ + def __init__(self): + self.mean_ = None + + def fit(self, K): + """ + Fit the centering operation on the kernel matrix + Parameters + ---------- + K : array-like, shape (n_samples, n_samples) + Kernel matrix + Returns + ------- + self : returns an instance of self. + """ + n_samples = K.shape[0] + self.mean_ = np.sum(K, axis=0) / n_samples + return self + + def transform(self, K): + """ + Center the kernel matrix + Parameters + ---------- + K : array-like, shape (n_samples, n_samples) + Kernel matrix + Returns + ------- + K_new : array-like, shape (n_samples, n_samples) + Centered kernel matrix + """ + K_new = K - self.mean_[:, np.newaxis] - self.mean_[np.newaxis, :] + K_new += np.mean(self.mean_) + return K_new +Generated code for sklearn.preprocessing.LabelBinarizer + + +from sklearn.preprocessing import LabelBinarizer + +# Create an instance of LabelBinarizer +lb = LabelBinarizer() + +# Fit the LabelBinarizer to the data +lb.fit(data) + +# Transform the data +transformed_data = lb.transform(data) + +# Print the transformed data +print(transformed_data) +Generated code for sklearn.preprocessing.LabelEncoder + + +from sklearn.preprocessing import LabelEncoder + +# Create an instance of LabelEncoder +label_encoder = LabelEncoder() + +# Fit the encoder to the data +label_encoder.fit(data) + +# Transform the data +transformed_data = label_encoder.transform(data) + +# Print the transformed data +print(transformed_data) +Generated code for sklearn.preprocessing.MultiLabelBinarizer + + +from sklearn.preprocessing import MultiLabelBinarizer + +mlb = MultiLabelBinarizer() + +# Fit the MultiLabelBinarizer to the data +mlb.fit(data) + +# Transform the data into a binary matrix +binary_matrix = mlb.transform(data) + +# Print the binary matrix +print(binary_matrix) +Generated code for sklearn.preprocessing.MaxAbsScaler + + +from sklearn.preprocessing import MaxAbsScaler + +# Create an instance of MaxAbsScaler +scaler = MaxAbsScaler() + +# Fit the scaler to the data +scaler.fit(X) + +# Transform the data +X_scaled = scaler.transform(X) +Generated code for sklearn.preprocessing.MinMaxScaler + + +from sklearn.preprocessing import MinMaxScaler + +# Create an instance of the MinMaxScaler +scaler = MinMaxScaler() + +# Fit the scaler to the data +scaler.fit(data) + +# Transform the data +scaled_data = scaler.transform(data) +Generated code for sklearn.preprocessing.Normalizer + + +from sklearn.preprocessing import Normalizer + +# Create an instance of the Normalizer class +normalizer = Normalizer() + +# Fit the Normalizer to the data +normalizer.fit(X) + +# Transform the data +X_normalized = normalizer.transform(X) +Generated code for sklearn.preprocessing.OneHotEncoder + + +from sklearn.preprocessing import OneHotEncoder + +# Create an instance of the OneHotEncoder +onehot_encoder = OneHotEncoder() + +# Fit the encoder to the data +onehot_encoder.fit(X) + +# Transform the data +X_onehot = onehot_encoder.transform(X) +Generated code for sklearn.preprocessing.OrdinalEncoder + + +from sklearn.preprocessing import OrdinalEncoder + +# Create an instance of the OrdinalEncoder +ordinal_encoder = OrdinalEncoder() + +# Fit the encoder to the data +ordinal_encoder.fit(X) + +# Transform the data +X_encoded = ordinal_encoder.transform(X) +Generated code for sklearn.preprocessing.PolynomialFeatures + + +from sklearn.preprocessing import PolynomialFeatures + +# Create an instance of the PolynomialFeatures class +poly = PolynomialFeatures(degree=2, include_bias=False) + +# Fit the instance to the data +X = [[1, 2], [3, 4]] +poly.fit(X) + +# Transform the data +X_poly = poly.transform(X) + +# Print the transformed data +print(X_poly) + +# Output: +# [[ 1. 2. 1. 2. 4.] +# [ 3. 4. 9. 12. 16.]] +Generated code for sklearn.preprocessing.PowerTransformer + + +from sklearn.preprocessing import PowerTransformer + +# Create an instance of the PowerTransformer +pt = PowerTransformer() + +# Fit the PowerTransformer to the data +pt.fit(X) + +# Transform the data using the PowerTransformer +X_transformed = pt.transform(X) +Generated code for sklearn.preprocessing.QuantileTransformer + + +import numpy as np +from sklearn.preprocessing import QuantileTransformer + +# Create a random array +X = np.random.rand(10, 5) + +# Create the transformer +qt = QuantileTransformer(output_distribution='normal') + +# Fit and transform the data +X_transformed = qt.fit_transform(X) + +# Print the transformed data +print(X_transformed) +Generated code for sklearn.preprocessing.RobustScaler + + +from sklearn.preprocessing import RobustScaler + +# Create an instance of the RobustScaler +scaler = RobustScaler() + +# Fit the scaler to the data +scaler.fit(X) + +# Transform the data +X_scaled = scaler.transform(X) +Generated code for sklearn.preprocessing.SplineTransformer + + +from sklearn.preprocessing import SplineTransformer + +# Create an instance of the SplineTransformer +spline_transformer = SplineTransformer() + +# Fit the transformer to the data +spline_transformer.fit(X) + +# Transform the data +X_transformed = spline_transformer.transform(X) +Generated code for sklearn.preprocessing.StandardScaler + + +from sklearn.preprocessing import StandardScaler + +# Create an instance of the StandardScaler class +scaler = StandardScaler() + +# Fit the scaler to the data +scaler.fit(data) + +# Transform the data +scaled_data = scaler.transform(data) +Generated code for sklearn.preprocessing.add_dummy_feature + + +import numpy as np +from sklearn.preprocessing import add_dummy_feature + +# Create a sample array +X = np.array([[1, 2, 3], [4, 5, 6]]) + +# Add a dummy feature +X_dummy = add_dummy_feature(X) + +# Print the new array +print(X_dummy) +Generated code for sklearn.preprocessing.binarize + + +import numpy as np +from sklearn.preprocessing import binarize + +# Create a sample array +x = np.array([[1, -1, 2], + [2, 0, 0], + [0, 1, -1]]) + +# Binarize the array +binarized_x = binarize(x, threshold=1.5) + +# Print the binarized array +print(binarized_x) +Generated code for sklearn.preprocessing.label_binarize + + +from sklearn.preprocessing import label_binarize + +def label_binarize(y, classes): + """ + Binarize labels in a one-vs-all fashion. + + Parameters + ---------- + y : array-like of shape (n_samples,) + Target values. + + classes : array-like of shape (n_classes,) + Unique class labels. + + Returns + ------- + Y : array of shape (n_samples, n_classes) + Binarized labels. + """ + y = np.asarray(y) + classes = np.asarray(classes) + n_classes = classes.shape[0] + Y = np.zeros((y.shape[0], n_classes)) + for i in range(n_classes): + Y[y == classes[i], i] = 1 + return Y +Generated code for sklearn.preprocessing.maxabs_scale + + +from sklearn.preprocessing import MaxAbsScaler + +# Create an instance of MaxAbsScaler +max_abs_scaler = MaxAbsScaler() + +# Fit and transform the data +scaled_data = max_abs_scaler.fit_transform(data) + +# Print the scaled data +print(scaled_data) +Generated code for sklearn.preprocessing.minmax_scale + + +def minmax_scale(X, feature_range=(0, 1), axis=0, copy=True): + """ + Scales all values in a numpy array X to be between a given + minimum and maximum value. + + Parameters + ---------- + X : array-like, shape [n_samples, n_features] + The data to scale. + feature_range : tuple (min, max), default=(0, 1) + Desired range of transformed data. + axis : int (0 by default) + Axis along which to scale. + copy : bool, default=True + Set to False to perform inplace scaling. + + Returns + ------- + X_scaled : ndarray, shape [n_samples, n_features] + Scaled data. + """ + # Check input + X = check_array(X, copy=copy, ensure_2d=False) + if X.ndim == 1: + X = X.reshape(X.shape[0], 1) + n_samples, n_features = X.shape + + # Get min and max + data_min = np.min(X, axis=axis, keepdims=True) + data_max = np.max(X, axis=axis, keepdims=True) + data_range = data_max - data_min + if np.all(data_range == 0): + data_range = np.ones(n_features) + + # Scale + min, max = feature_range + X_scaled = (X - data_min) / data_range * (max - min) + min + + return X_scaled +Generated code for sklearn.preprocessing.normalize + + +from sklearn.preprocessing import normalize + +# Create a 2D array +X = [[1, 2], + [3, 4]] + +# Normalize the array +normalized_X = normalize(X, norm='l2') + +print(normalized_X) + +# Output +# [[0.4472136 0.89442719] +# [0.6172134 0.78683274]] +Generated code for sklearn.preprocessing.quantile_transform + + +from sklearn.preprocessing import quantile_transform + +# Create an array of data +data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +# Transform the data using quantile_transform +transformed_data = quantile_transform(data, n_quantiles=4, random_state=0) + +# Print the transformed data +print(transformed_data) +Generated code for sklearn.preprocessing.robust_scale + + +from sklearn.preprocessing import robust_scale + +# Create an array +X = [[ 1., -2., 2.], + [ -2., 1., 3.], + [ 4., 1., -2.]] + +# Apply robust scaling +robust_scaled_X = robust_scale(X) + +# Print the result +print(robust_scaled_X) +Generated code for sklearn.preprocessing.scale + + +from sklearn.preprocessing import scale + +def scale_data(data): + """ + Scales the data using sklearn.preprocessing.scale + :param data: The data to be scaled + :return: The scaled data + """ + return scale(data) +Generated code for sklearn.preprocessing.power_transform + + +from sklearn.preprocessing import power_transform + +# Sample data +data = [1, 2, 3, 4, 5] + +# Transform data using Yeo-Johnson power transform +transformed_data = power_transform(data, method='yeo-johnson') + +# Print transformed data +print(transformed_data) +Generated code for sklearn.random_projection.GaussianRandomProjection + + +import numpy as np +from sklearn.random_projection import GaussianRandomProjection + +# Create a GaussianRandomProjection object +grp = GaussianRandomProjection(n_components=2) + +# Generate random data +X = np.random.rand(10, 5) + +# Fit the data to the model +grp.fit(X) + +# Transform the data +X_transformed = grp.transform(X) + +# Print the transformed data +print(X_transformed) +Generated code for sklearn.random_projection.SparseRandomProjection + + +import numpy as np +from sklearn.random_projection import SparseRandomProjection + +# Create a random matrix of size (n_samples, n_features) +X = np.random.rand(1000, 10) + +# Create a SparseRandomProjection object +transformer = SparseRandomProjection(n_components=5) + +# Fit the random matrix to the transformer +X_new = transformer.fit_transform(X) + +# Print the new matrix +print(X_new) +Generated code for sklearn.random_projection.johnson_lindenstrauss_min_dim + + +import numpy as np +from sklearn.random_projection import johnson_lindenstrauss_min_dim + +# Generate a random matrix of size (n_samples, n_features) +X = np.random.rand(1000, 100) + +# Calculate the minimum number of dimensions required to preserve the +# pairwise distances between the samples +min_dim = johnson_lindenstrauss_min_dim(n_samples=1000, eps=0.1) + +# Print the result +print("Minimum number of dimensions required to preserve the pairwise distances:", min_dim) +Generated code for sklearn.semi_supervised.LabelPropagation + + +import numpy as np +from sklearn.semi_supervised import LabelPropagation + +# Create a random dataset +rng = np.random.RandomState(0) +X = rng.rand(100, 2) + +# Create labels +y = np.random.randint(2, size=(100,)) + +# Create a label propagation model +lp_model = LabelPropagation(kernel='rbf', gamma=20, n_neighbors=7) + +# Fit the model to the data +lp_model.fit(X, y) + +# Predict labels for the data +predicted_labels = lp_model.predict(X) +Generated code for sklearn.semi_supervised.LabelSpreading + + +from sklearn.semi_supervised import LabelSpreading + +# Create the LabelSpreading model +model = LabelSpreading(kernel='rbf', gamma=20, n_neighbors=7) + +# Fit the model to the data +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) +Generated code for sklearn.semi_supervised.SelfTrainingClassifier + + +from sklearn.semi_supervised import SelfTrainingClassifier + +class SelfTrainingClassifier: + def __init__(self, estimator, n_iter=10, initial_labeled_samples=None): + self.estimator = estimator + self.n_iter = n_iter + self.initial_labeled_samples = initial_labeled_samples + + def fit(self, X, y): + # Initialize the labeled and unlabeled sets + if self.initial_labeled_samples is None: + labeled_samples = [] + unlabeled_samples = list(range(X.shape[0])) + else: + labeled_samples = self.initial_labeled_samples + unlabeled_samples = [i for i in range(X.shape[0]) if i not in self.initial_labeled_samples] + + # Iterate over the number of iterations + for _ in range(self.n_iter): + # Train the model on the labeled data + self.estimator.fit(X[labeled_samples], y[labeled_samples]) + + # Make predictions on the unlabeled data + predictions = self.estimator.predict(X[unlabeled_samples]) + + # Add the most confident predictions to the labeled set + max_confidence = 0 + max_confidence_index = None + for i, prediction in enumerate(predictions): + if prediction[1] > max_confidence: + max_confidence = prediction[1] + max_confidence_index = i + + if max_confidence_index is not None: + labeled_samples.append(unlabeled_samples[max_confidence_index]) + del unlabeled_samples[max_confidence_index] + + # Return the trained model + return self.estimator +Generated code for sklearn.svm.LinearSVC + + +# Import the necessary libraries +from sklearn.svm import LinearSVC +from sklearn.datasets import make_classification + +# Generate a sample dataset +X, y = make_classification(n_samples=1000, n_features=4, random_state=0) + +# Create a LinearSVC model +clf = LinearSVC(random_state=0, tol=1e-5) + +# Fit the model to the data +clf.fit(X, y) + +# Make predictions +predictions = clf.predict(X) + +# Print the accuracy of the model +print("Accuracy:", clf.score(X, y)) +Generated code for sklearn.svm.LinearSVR + + +# Import necessary libraries +from sklearn.svm import LinearSVR +import numpy as np + +# Create a LinearSVR object +svr_model = LinearSVR() + +# Create some sample data +X = np.array([[1,2,3], [4,5,6], [7,8,9]]) +y = np.array([1,2,3]) + +# Fit the model to the data +svr_model.fit(X, y) + +# Make predictions +predictions = svr_model.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.svm.NuSVC + + +# Import necessary libraries +from sklearn.svm import NuSVC +from sklearn.datasets import make_classification + +# Generate a sample dataset +X, y = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0) + +# Create an instance of NuSVC +clf = NuSVC(gamma='auto') + +# Fit the model to the data +clf.fit(X, y) + +# Make predictions +predictions = clf.predict(X) + +# Evaluate the model +score = clf.score(X, y) +print("Model accuracy:", score) +Generated code for sklearn.svm.NuSVR + + +# Import necessary libraries +from sklearn.svm import NuSVR +import numpy as np + +# Create a sample dataset +X = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]]) +y = np.array([1,2,3,4]) + +# Create a NuSVR model +model = NuSVR() + +# Fit the model to the data +model.fit(X, y) + +# Make predictions +predictions = model.predict(X) + +# Print predictions +print(predictions) +Generated code for sklearn.svm.OneClassSVM + + +#importing the necessary libraries +import numpy as np +from sklearn.svm import OneClassSVM + +#creating the data +X = np.array([[1,2], [2,4], [3,6], [4,8], [5,10], [6,12], [7,14], [8,16], [9,18], [10,20]]) + +#creating the model +model = OneClassSVM(gamma='auto') + +#fitting the model +model.fit(X) + +#predicting the labels +labels = model.predict(X) + +#printing the labels +print(labels) +Generated code for sklearn.svm.SVC + + +from sklearn.svm import SVC + +# Create a SVC classifier using a linear kernel +clf = SVC(kernel='linear') + +# Train the classifier using the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Calculate the accuracy of the model +accuracy = clf.score(X_test, y_test) +Generated code for sklearn.svm.SVR + + +from sklearn.svm import SVR + +# Create a Support Vector Regression (SVR) model +svr_model = SVR() + +# Train the model using the training sets +svr_model.fit(X_train, y_train) + +# Predict the response for test dataset +y_pred = svr_model.predict(X_test) + +# Evaluate the model +score = svr_model.score(X_test, y_test) +print("Score: %.2f" % score) +Generated code for sklearn.svm.l1_min_c + + +# Import necessary libraries +from sklearn.svm import LinearSVC +from sklearn.datasets import make_classification + +# Generate a synthetic dataset +X, y = make_classification(n_samples=1000, n_features=20, random_state=42) + +# Create an instance of the LinearSVC class +clf = LinearSVC(penalty='l1', dual=False, random_state=42) + +# Fit the model to the data +clf.fit(X, y) + +# Get the minimum value of C +l1_min_c = clf.C_[0] + +# Print the minimum value of C +print('The minimum value of C is:', l1_min_c) +Generated code for sklearn.tree.DecisionTreeClassifier + + +#importing necessary libraries +import pandas as pd +from sklearn.tree import DecisionTreeClassifier + +#loading the dataset +df = pd.read_csv('dataset.csv') + +#defining the features and target +X = df.drop('target', axis=1) +y = df['target'] + +#instantiating the model +model = DecisionTreeClassifier() + +#fitting the model +model.fit(X, y) + +#making predictions +predictions = model.predict(X) + +#evaluating the model +score = model.score(X, y) + +#printing the results +print('Model Score:', score) +print('Predictions:', predictions) +Generated code for sklearn.tree.DecisionTreeRegressor + + +from sklearn.tree import DecisionTreeRegressor + +# Create the Decision Tree regressor object +regressor = DecisionTreeRegressor() + +# Fit the regressor to the data +regressor.fit(X, y) + +# Make predictions +y_pred = regressor.predict(X_test) +Generated code for sklearn.tree.ExtraTreeClassifier + + +# Import the necessary libraries +from sklearn.tree import ExtraTreeClassifier +from sklearn.model_selection import train_test_split +from sklearn.metrics import accuracy_score + +# Load the data +X = [[0, 0], [1, 1]] +y = [0, 1] + +# Split the data into training and test sets +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Create the ExtraTreeClassifier +clf = ExtraTreeClassifier() + +# Fit the model to the training data +clf.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = clf.predict(X_test) + +# Calculate the accuracy +accuracy = accuracy_score(y_test, y_pred) + +# Print the accuracy +print("Accuracy:", accuracy) +Generated code for sklearn.tree.ExtraTreeRegressor + + +# Import the necessary libraries +from sklearn.tree import ExtraTreeRegressor +import numpy as np + +# Create the data +X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +y = np.array([1, 2, 3]) + +# Create the ExtraTreeRegressor +regressor = ExtraTreeRegressor() + +# Fit the model +regressor.fit(X, y) + +# Make predictions +predictions = regressor.predict(X) + +# Print the predictions +print(predictions) +Generated code for sklearn.tree.export_graphviz + + +import graphviz +from sklearn.tree import export_graphviz + +def visualize_tree(tree, feature_names): + with open("dt.dot", 'w') as f: + export_graphviz(tree, out_file=f, + feature_names=feature_names) + + command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"] + try: + subprocess.check_call(command) + except: + exit("Could not run dot, ie graphviz, to " + "produce visualization") + +visualize_tree(tree, feature_names) +Generated code for sklearn.tree.export_text + + +from sklearn.tree import export_text + +def export_tree_text(tree, feature_names): + tree_str = export_text(tree, feature_names=feature_names) + lines = tree_str.split('\n') + for i in range(len(lines)): + lines[i] = ' ' * i + lines[i] + return '\n'.join(lines) + +print(export_tree_text(tree, feature_names)) +Generated code for sklearn.tree.plot_tree + + +import matplotlib.pyplot as plt +from sklearn.tree import plot_tree + +# Create a decision tree classifier +clf = DecisionTreeClassifier(random_state=0) + +# Train the classifier +clf.fit(X_train, y_train) + +# Plot the decision tree +plt.figure(figsize=(15,7)) +plot_tree(clf, filled=True, rounded=True, class_names=['Negative', 'Positive'], feature_names=X_train.columns) +plt.show() +Generated code for sklearn.utils.Bunch + + +import sklearn + +class Bunch(dict): + """Container object for datasets: dictionary-like object that + exposes its keys as attributes.""" + + def __init__(self, **kwargs): + dict.__init__(self, kwargs) + self.__dict__ = self + + +if __name__ == '__main__': + data = Bunch(name='Foo', age=20, height=180) + print(data.name) + print(data.age) + print(data.height) +Generated code for sklearn.utils.arrayfuncs.min_pos + + +def min_pos(arr): + """ + Find the minimum positive value in an array. + + Parameters + ---------- + arr : array_like + Input array. + + Returns + ------- + min_pos : float + The minimum positive value in the array. + If there are no positive values, returns 0. + """ + min_pos = 0 + for val in arr: + if val > 0 and (min_pos == 0 or val < min_pos): + min_pos = val + return min_pos +Generated code for sklearn.utils.as_float_array + + +def as_float_array(X): + """Convert an array-like to an array of floats + Parameters + ---------- + X : array-like + The input array. + Returns + ------- + X_float : ndarray + An array of floats. + """ + X_float = np.array(X, dtype=float) + return X_float +Generated code for sklearn.utils.assert_all_finite + + +def assert_all_finite(X): + """Throw a ValueError if X contains NaN or infinity. + Parameters + ---------- + X : array or sparse matrix + """ + X = np.asanyarray(X) + # First try an O(n) time, O(1) space solution for the common case that + # everything is finite; this is expected to turn up most of the time. + if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum()) + and not np.isfinite(X).all()): + raise ValueError("Input contains NaN, infinity" + " or a value too large for %r." % X.dtype) +Generated code for sklearn.utils.check_X_y + + +def check_X_y(X, y, accept_sparse=False, accept_large_sparse=True, dtype="numeric", order=None, copy=False, force_all_finite=True, ensure_2d=True, allow_nd=False, multi_output=False, ensure_min_samples=1, ensure_min_features=1, y_numeric=False, estimator=None): + """Input validation for standard estimators. + Checks X and y for consistent length, enforces X to be 2D and y 1D. By default, + X is checked to be non-empty and containing only finite values. Standard input + checks are also applied to y, such as checking that y does not have np.nan or + np.inf targets. For multi-label y, set multi_output=True to allow 2D and sparse + y. If the dtype of X is object, attempt converting to float, raising on failure. + Parameters + ---------- + X : nd-array, list or sparse matrix + Input data. + y : nd-array, list or sparse matrix + Labels. + accept_sparse : string, boolean or list/tuple of strings (default=False) + String[s] representing allowed sparse matrix formats, such as 'csc', + 'csr', etc. If the input is sparse but not in the allowed format, + it will be converted to the first listed format. True allows the input + to be any format. False means that a sparse matrix input will + raise an error. + accept_large_sparse : bool (default=True) + If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by + accept_sparse, accept_large_sparse=False will cause it to be accepted + only if its indices are stored with a 32-bit dtype. + dtype : string, type, list of types or None (default="numeric") + Data type of result. If None, the dtype of the input is preserved. + If "numeric", dtype is preserved unless array.dtype is object. + order : 'F', +Generated code for sklearn.utils.check_array + + +def check_array(array, accept_sparse=False, accept_large_sparse=True, + dtype="numeric", order=None, copy=False, force_all_finite=True, + ensure_2d=True, allow_nd=False, ensure_min_samples=1, + ensure_min_features=1, warn_on_dtype=False, + estimator=None): + """Input validation on an array, list, sparse matrix or similar. + By default, the input is converted to an at least 2nd numpy array. + If the dtype of the array is object, attempt converting to float, + raising on failure. + Parameters + ---------- + array : object + Input object to check / convert. + accept_sparse : string, boolean or list/tuple of strings (default=False) + String[s] representing allowed sparse matrix formats, such as 'csc', + 'csr', etc. If the input is sparse but not in the allowed format, + it will be converted to the first listed format. True allows the input + to be any format. False means that a sparse matrix input will + raise an error. + accept_large_sparse : bool (default=True) + If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by + accept_sparse, accept_large_sparse=False will cause it to be accepted + only if its indices are stored with a 32-bit dtype. + dtype : string, type, list of types or None (default="numeric") + Data type of result. If None, the dtype of the input is preserved. + If "numeric", dtype is preserved unless array.dtype is object. + If dtype is a list of types, conversion on the first type is only + performed if the dtype of the input is not in the list. + order : 'F', 'C' or None (default=None) + Whether an array will be forced to be fortran or c-style. + copy : boolean (default=False) + Whether a forced copy will be triggered. If copy= +Generated code for sklearn.utils.check_scalar + + +def check_scalar(x, name=None): + """ + Check if x is a scalar. + Parameters + ---------- + x : any + The object to check. + name : str, optional + The name of the object to check, for error messages. + Returns + ------- + is_scalar : bool + True if x is a scalar, False otherwise. + """ + is_scalar = True + if not isinstance(x, (int, float, complex)): + is_scalar = False + if name is None: + name = 'x' + raise TypeError('{0} is not a scalar'.format(name)) + return is_scalar +Generated code for sklearn.utils.check_consistent_length + + +def check_consistent_length(*arrays): + """Check that all arrays have consistent first dimensions. + Checks whether all objects in arrays have the same shape or length. + Parameters + ---------- + *arrays : list or tuple of input objects. + Objects that will be checked for consistent length. + Returns + ------- + consistent : bool + True if objects in arrays have same shape or length. + """ + lengths = [len(X) for X in arrays if X is not None] + uniques = np.unique(lengths) + if len(uniques) > 1: + raise ValueError("Input arrays must have consistent lengths. " + "Found lengths: %r" % (lengths,)) + return len(uniques) == 1 +Generated code for sklearn.utils.check_random_state + + +import numpy as np + +def check_random_state(seed): + """Turn seed into a np.random.RandomState instance + Parameters + ---------- + seed : None | int | instance of RandomState + If seed is None, return the RandomState singleton used by np.random. + If seed is an int, return a new RandomState instance seeded with seed. + If seed is already a RandomState instance, return it. + Otherwise raise ValueError. + """ + if seed is None or seed is np.random: + return np.random.mtrand._rand + if isinstance(seed, (int, np.integer)): + return np.random.RandomState(seed) + if isinstance(seed, np.random.RandomState): + return seed + raise ValueError('%r cannot be used to seed a numpy.random.RandomState' + ' instance' % seed) +Generated code for sklearn.utils.class_weight.compute_class_weight + + +import numpy as np +from sklearn.utils import class_weight + +# define the labels +labels = np.array([0, 1, 2, 3, 4, 5]) + +# define the class weights +class_weights = class_weight.compute_class_weight('balanced', labels, labels) + +# print the class weights +print(class_weights) +Generated code for sklearn.utils.class_weight.compute_sample_weight + + +def compute_sample_weight(class_weight, y, indices=None): + """Estimate sample weights by class for unbalanced datasets. + + Parameters + ---------- + class_weight : dict, {class_label: weight} + Weights associated with classes. If not given, all classes + are supposed to have weight one. + + y : array-like, shape (n_samples,) + Array of original class labels per sample. + + indices : array-like, shape (n_samples,), optional + Array of sample indices to be used instead of ``y``. + + Returns + ------- + sample_weight : array-like, shape (n_samples,) + Array of sample weights. + """ + if indices is None: + indices = np.arange(y.shape[0]) + sample_weight = np.ones(y.shape[0], dtype=np.float64) + for class_ in np.unique(y): + if class_ in class_weight: + sample_weight[indices[y == class_]] = class_weight[class_] + return sample_weight +Generated code for sklearn.utils.deprecated + + +import warnings + +def deprecated(func): + """This is a decorator which can be used to mark functions + as deprecated. It will result in a warning being emitted + when the function is used.""" + def new_func(*args, **kwargs): + warnings.warn("Call to deprecated function {}.".format(func.__name__), + category=DeprecationWarning) + return func(*args, **kwargs) + new_func.__name__ = func.__name__ + new_func.__doc__ = func.__doc__ + new_func.__dict__.update(func.__dict__) + return new_func +Generated code for sklearn.utils.estimator_checks.check_estimator + + +def check_estimator(estimator): + """Check if estimator adheres to scikit-learn conventions. + Parameters + ---------- + estimator : estimator instance + Estimator to check. + Returns + ------- + errors : list + List of strings, describing problems found. + """ + from sklearn.base import clone + from sklearn.exceptions import NotFittedError + from sklearn.utils.estimator_checks import check_parameters + from sklearn.utils.estimator_checks import check_no_attributes_set_in_init + from sklearn.utils.estimator_checks import check_estimators_dtypes + from sklearn.utils.estimator_checks import check_fit_score_takes_y + from sklearn.utils.estimator_checks import check_dtype_object + from sklearn.utils.estimator_checks import check_sample_weights_pandas_series + from sklearn.utils.estimator_checks import check_sample_weights_list + from sklearn.utils.estimator_checks import check_estimators_fit_returns_self + from sklearn.utils.estimator_checks import check_estimators_fit_returns_score + from sklearn.utils.estimator_checks import check_estimators_predict + from sklearn.utils.estimator_checks import check_estimators_transform + from sklearn.utils.estimator_checks import check_estimators_pickle + from sklearn.utils.estimator_checks import check_estimators_data_not_an_array + from sklearn.utils.estimator_checks import check_estimators_empty_data_messages + from sklearn.utils.estimator_checks import check_estimators_nan_inf + from sklearn.utils.estimator_checks import check_estimators_overwrite_params + from sklearn.utils.estimator_checks import check_estimator_sparse_data + from sk +Generated code for sklearn.utils.estimator_checks.parametrize_with_checks + + +import inspect +import numpy as np + +from sklearn.utils.estimator_checks import check_estimator + +def parametrize_with_checks(estimator_class, **parameters): + """ + Parametrize an estimator class with the given parameters and check it. + + Parameters + ---------- + estimator_class : estimator class + The estimator class to parametrize and check. + + **parameters : keyword arguments + The parameters to parametrize the estimator class with. + + Returns + ------- + estimator : estimator instance + An instance of the estimator class with the given parameters. + """ + # Get the signature of the estimator class + signature = inspect.signature(estimator_class) + + # Create a list of keyword arguments to pass to the estimator class + kwargs = { + name: value + for name, value in parameters.items() + if name in signature.parameters + } + + # Create an instance of the estimator class with the given parameters + estimator = estimator_class(**kwargs) + + # Check the estimator + check_estimator(estimator) + + return estimator +Generated code for sklearn.utils.estimator_html_repr + + +def estimator_html_repr(estimator): + """ + Generates an HTML representation of an estimator. + + Parameters + ---------- + estimator : estimator object + The estimator to generate an HTML representation for. + + Returns + ------- + html_str : str + An HTML representation of the estimator. + """ + # Get the estimator's name + estimator_name = estimator.__class__.__name__ + + # Get the estimator's parameters + estimator_params = estimator.get_params() + + # Generate the HTML string + html_str = '

{}

\n'.format(estimator_name) + html_str += '
    \n' + for param_name, param_value in estimator_params.items(): + html_str += '
  • {}: {}
  • \n'.format(param_name, param_value) + html_str += '
' + + return html_str +Generated code for sklearn.utils.extmath.safe_sparse_dot + + +def safe_sparse_dot(a, b, dense_output=False): + """Dot product that handle the sparse matrix case correctly + This is a drop-in replacement for np.dot. + Parameters + ---------- + a : array or sparse matrix + b : array or sparse matrix + dense_output : boolean (optional), default False + When False, the output is a sparse matrix. + When True, the output is a dense matrix. + Returns + ------- + dot_product : array or sparse matrix + sparse if a or b is sparse and dense_output=False. + """ + from scipy import sparse + if sparse.issparse(a) or sparse.issparse(b): + ret = a * b + if dense_output: + ret = ret.toarray() + return ret + else: + return np.dot(a, b) +Generated code for sklearn.utils.extmath.randomized_range_finder + + +def randomized_range_finder(A, size, n_iter, random_state=None): + """Computes an orthonormal matrix whose range approximates the range of A. + + Parameters + ---------- + A : ndarray or sparse matrix + The input matrix to be decomposed + size : int + Size of the return array + n_iter : int + Number of power iterations used to stabilize the result + random_state : int, RandomState instance or None, optional (default=None) + If int, random_state is the seed used by the random number generator; + If RandomState instance, random_state is the random number generator; + If None, the random number generator is the RandomState instance used + by `np.random`. + + Returns + ------- + Q : ndarray or sparse matrix + Orthonormal basis for the range of A. + """ + random_state = check_random_state(random_state) + Q = random_state.normal(size=(A.shape[1], size)) + + # Perform power iterations with Q to further 'imprint' the top + # singular vectors of A in Q + for i in range(n_iter): + Q, _ = linalg.lu(A @ Q, permute_l=True) + Q, _ = linalg.lu(A.T @ Q, permute_l=True) + + # Sample the range of A using by computing QQ^T A + Q, _ = linalg.qr(A @ Q, mode='economic') + + return Q +Generated code for sklearn.utils.extmath.randomized_svd + + +import numpy as np +from scipy import linalg + +def randomized_svd(M, n_components, n_oversamples=10, n_iter=0): + """Computes a truncated randomized SVD + Parameters + ---------- + M : array-like, shape (n_samples, n_features) + Input data. + n_components : int + Number of singular values and vectors to extract. + n_oversamples : int (default is 10) + Additional number of random vectors to sample the range of M so as + to ensure proper conditioning. The total number of random vectors + used to find the range of M is n_components + n_oversamples. + n_iter : int (default is 0) + Number of power iterations (can be used to deal with very noisy + problems). + Notes + ----- + This algorithm finds a (usually very good) approximate truncated + singular value decomposition using randomization to speed up the + computations. It is particularly fast on large matrices on the + order of hundreds of thousands of rows. + References + ---------- + * Finding structure with randomness: Stochastic algorithms for constructing + approximate matrix decompositions + Halko, et al., 2009 http://arxiv.org/abs/arXiv:0909.4061 + """ + n_random = n_components + n_oversamples + n_samples, n_features = M.shape + + # Generate random sample + random_state = np.random.RandomState(None) + R = random_state.normal(size=(n_features, n_random)) + + # Compute the matrix product + Y = np.dot(M, R) + del R + + # Compute the QR factorization + Q, _ = linalg.qr(Y, mode='economic') + del Y + + # Compute the singular values + B = np.dot(Q.T, M) + Uhat, s, V = linalg.svd(B, full_matrices=False) + del B + U = np. +Generated code for sklearn.utils.extmath.fast_logdet + + +def fast_logdet(A): + """Compute log(det(A)) for A symmetric + Parameters + ---------- + A : array_like, shape (N, N) + Symmetric positive definite input matrix + Returns + ------- + logdet : float + Determinant of `A`. + """ + sign, ld = np.linalg.slogdet(A) + if not sign > 0: + raise ValueError("A must be positive definite") + return ld +Generated code for sklearn.utils.extmath.density + + +def density(w, **kwargs): + """Compute density of a sparse vector. + + Parameters + ---------- + w : {array-like, sparse matrix}, shape (n_features,) or (n_samples, n_features) + Input array. + + Returns + ------- + density : float + Density of input array. + """ + w = np.asarray(w) + if w.ndim == 1: + w = w[np.newaxis, :] + n_samples, n_features = w.shape + density = 0. + for i in range(n_samples): + density += np.count_nonzero(w[i, :]) / n_features + return density / n_samples +Generated code for sklearn.utils.extmath.weighted_mode + + +def weighted_mode(a, w, axis=0): + """ + Calculates a weighted mode of an array. + + Parameters + ---------- + a : array_like + Input array. + w : array_like + Array of weights, same shape as `a`. + axis : int, optional + Axis along which to operate. Default is 0. + + Returns + ------- + mode : ndarray + The weighted mode of `a`. + + Notes + ----- + This function is based on scipy.stats.mode. + """ + if axis is None: + a = np.ravel(a) + w = np.ravel(w) + axis = 0 + else: + a = np.asarray(a) + w = np.asarray(w) + if a.shape != w.shape: + raise ValueError('The input array and weights must have the same shape.') + if len(a) == 0: + return np.nan + if np.all(w == 0): + return np.nan + bins = np.unique(a) + weights = np.array([np.sum(w[a == b]) for b in bins]) + ind = np.argmax(weights) + return bins[ind] +Generated code for sklearn.utils.gen_batches + + +def gen_batches(n_samples, batch_size): + """Generator to create slices containing batch_size elements, from 0 to n_samples.""" + start = 0 + for _ in range(int(n_samples // batch_size)): + end = start + batch_size + yield slice(start, end) + start = end + if start < n_samples: + yield slice(start, n_samples) +Generated code for sklearn.utils.gen_even_slices + + +def gen_even_slices(n, n_packs): + """Generate n_packs slices of size n/n_packs. + + Parameters + ---------- + n : int + The number of elements in the array to be divided into slices. + n_packs : int + The number of slices to generate. + + Returns + ------- + slices : list of slice objects + The list of slices generated. + """ + start = 0 + stop = n + step = n // n_packs + slices = [] + for i in range(n_packs): + slices.append(slice(start, stop, step)) + start += step + stop += step + return slices +Generated code for sklearn.utils.graph.single_source_shortest_path_length + + +def single_source_shortest_path_length(G, source): + """Compute the shortest path length between source and all other reachable + nodes for a weighted graph. + + Parameters + ---------- + G : NetworkX graph + + source : node + Starting node for path + + Returns + ------- + lengths : dictionary + Dictionary of shortest path lengths keyed by target. + + Examples + -------- + >>> G=nx.path_graph(5) + >>> length=nx.single_source_shortest_path_length(G,0) + >>> length[4] + 4 + >>> print(length) + {0: 0, 1: 1, 2: 2, 3: 3, 4: 4} + + Notes + ----- + Edge weight attributes must be numerical. + Distances are calculated as sums of weighted edges traversed. + + """ + seen = {source: 0} + level = 0 + nextlevel = {source} + while nextlevel: + thislevel = nextlevel + nextlevel = set() + for v in thislevel: + for w, edgedata in G[v].items(): + if w not in seen: + seen[w] = level + edgedata.get('weight', 1) + nextlevel.add(w) + level = level + 1 + return seen +Generated code for sklearn.utils.indexable + + +import numpy as np +from sklearn.utils import indexable + +def indexable_data(data): + """ + Convert a data array into an indexable object. + + Parameters + ---------- + data : array-like + The data to be converted. + + Returns + ------- + indexable : indexable object + The indexable object. + """ + # Check if data is already indexable + if isinstance(data, indexable): + return data + + # Convert data to numpy array + data = np.array(data) + + # Check if data is a valid array + if not isinstance(data, np.ndarray): + raise ValueError('Data must be an array-like object.') + + # Create indexable object + indexable_data = indexable(data) + + return indexable_data +Generated code for sklearn.utils.metaestimators.available_if +_attribute + +def available_if_attribute(estimator, attr, default=True): + """ + Check if an estimator has an attribute with a given value. + + Parameters + ---------- + estimator : estimator object + Estimator object to check for the attribute. + + attr : string + Attribute name to check for. + + default : bool, optional (default=True) + Value to return if the attribute is not present. + + Returns + ------- + out : bool + True if the attribute is present and has the given value, + False otherwise. + """ + if hasattr(estimator, attr): + return getattr(estimator, attr) == default + else: + return default +Generated code for sklearn.utils.multiclass.type_of_target + + +def type_of_target(y): + """Determine the type of data indicated by target y + Parameters + ---------- + y : array-like + The target to be classified + Returns + ------- + target_type : string + One of: 'continuous', 'binary', 'multiclass', 'multilabel-indicator' + """ + valid_types = (np.integer, np.floating, np.bool, np.object) + if isinstance(y, valid_types): + if y.dtype.kind in ('O', 'b'): + y = np.asarray(y) + if y.ndim == 1: + if np.issubdtype(y.dtype, np.bool): + return 'binary' + elif np.issubdtype(y.dtype, np.floating): + return 'continuous' + elif np.issubdtype(y.dtype, np.integer): + if len(np.unique(y)) == 2: + return 'binary' + else: + return 'multiclass' + elif y.ndim == 2: + if np.array_equal(y, np.eye(y.shape[1])): + return 'multilabel-indicator' + else: + raise ValueError("Unknown label type") + else: + raise ValueError("Unknown label type") + else: + raise ValueError("Unknown label type") +Generated code for sklearn.utils.multiclass.is_multilabel + + +def is_multilabel(y): + """ + Check if ``y`` is in a multilabel format. + Parameters + ---------- + y : numpy array of shape [n_samples] or sequence of sequences + Target values. In the multilabel case the nested sequences can + have variable lengths. + Returns + ------- + out : bool, + Return ``True``, if ``y`` is in a multilabel format, else ```False``. + """ + if hasattr(y, '__iter__') and not isinstance(y, str): + y_type = type(y[0]) + if y_type is tuple or y_type is list: + return True + else: + return False + else: + return False +Generated code for sklearn.utils.multiclass.unique_labels + + +def unique_labels(y): + """ + Find the unique labels in a label array. + + Parameters + ---------- + y : array-like of shape (n_samples,) + Target values. + + Returns + ------- + out : ndarray of shape (n_unique_labels,) + Unique labels in `y`. + """ + y = np.asarray(y) + if y.ndim == 1: + labels = np.unique(y) + else: + labels = np.unique(y.reshape(-1)) + return labels +Generated code for sklearn.utils.murmurhash3_32 + + +def murmurhash3_32(data, seed=0): + """ + Computes a 32-bit MurmurHash3 checksum of the given data. + The data is assumed to be an array of bytes. + The seed is an optional integer that can be used to alter the + resulting hash value. + """ + c1 = 0xcc9e2d51 + c2 = 0x1b873593 + + length = len(data) + h1 = seed + roundedEnd = (length & 0xfffffffc) # round down to 4 byte block + for i in range(0, roundedEnd, 4): + # little endian load order + k1 = (data[i] & 0xff) | ((data[i + 1] & 0xff) << 8) | \ + ((data[i + 2] & 0xff) << 16) | (data[i + 3] << 24) + k1 *= c1 + k1 = (k1 << 15) | ((k1 & 0xffffffff) >> 17) # ROTL32(k1,15) + k1 *= c2 + h1 ^= k1 + h1 = (h1 << 13) | ((h1 & 0xffffffff) >> 19) # ROTL32(h1,13) + h1 = h1 * 5 + 0xe6546b64 + + # tail + k1 = 0 + val = length & 0x03 + if val == 3: + k1 = (data[roundedEnd + 2] & 0xff) << 16 + # fallthrough + if val in [2, 3]: + k1 |= (data[roundedEnd + 1] & 0xff) << 8 + # fallthrough + if val in [1, 2, 3]: + k1 |= data[roundedEnd] & 0xff + k1 *= c1 + k1 = (k1 << 15) | ((k1 & 0xffffffff) >> 17) # ROTL32(k1,15) + k1 *= c2 + h1 ^= k1 + + +Generated code for sklearn.utils.resample + + +import numpy as np +from sklearn.utils import resample + +# data sample +data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + +# resample with replacement +resample_with_replacement = resample(data, replace=True, n_samples=5) + +# resample without replacement +resample_without_replacement = resample(data, replace=False, n_samples=5) + +print('Resample with replacement:', resample_with_replacement) +print('Resample without replacement:', resample_without_replacement) +Generated code for sklearn.utils._safe_indexing + + +def _safe_indexing(X, indices): + """Return items or rows from X using indices. + + Allows simple indexing of lists or arrays. + + Parameters + ---------- + X : array-like, sparse-matrix, list, pandas.DataFrame, pandas.Series. + Data from which to sample rows or items. + indices : array-like of int + Indices according to which X will be subsampled. + + Returns + ------- + subset + Subset of X on first axis. + """ + if hasattr(X, "iloc"): + # Work-around for indexing with read-only indices in pandas + indices = indices if indices.flags.writeable else np.asarray(indices) + return X.iloc[indices] + elif hasattr(X, "shape"): + if hasattr(X, 'take') and (hasattr(indices, 'dtype') and + indices.dtype.kind == 'i'): + # This is often substantially faster than X[indices] + return X.take(indices, axis=0) + else: + return X[indices] + else: + return [X[idx] for idx in indices] +Generated code for sklearn.utils.safe_mask + + +def safe_mask(X, mask): + """ + Return a mask which is safe to use on X. + + Parameters + ---------- + X : ndarray + Input data. + mask : array_like + Mask to be used on X. + + Returns + ------- + safe_mask : ndarray + A mask which is safe to use on X. + """ + X = np.asarray(X) + mask = np.asarray(mask) + if X.shape != mask.shape: + raise ValueError("X and mask must have the same shape. Got %s and %s" + % (X.shape, mask.shape)) + safe_mask = np.zeros_like(mask, dtype=bool) + safe_mask[mask] = True + return safe_mask +Generated code for sklearn.utils.safe_sqr + + +def safe_sqr(x): + """ + Computes the square of a number x, but returns 0 if x is negative. + + Parameters + ---------- + x : float + The number to be squared. + + Returns + ------- + float + The square of x if x is positive, 0 otherwise. + """ + if x >= 0: + return x**2 + else: + return 0 +Generated code for sklearn.utils.shuffle + + +def shuffle(arr, random_state=None): + """ + Shuffle an array in-place. + + Parameters + ---------- + arr : array-like + The array to be shuffled. + random_state : int, RandomState instance or None, optional (default=None) + If int, random_state is the seed used by the random number generator; + If RandomState instance, random_state is the random number generator; + If None, the random number generator is the RandomState instance used + by `np.random`. + + Returns + ------- + arr : array-like + The shuffled array. + """ + rng = np.random.RandomState(random_state) + rng.shuffle(arr) + return arr +Generated code for sklearn.utils.sparsefuncs.incr_mean_variance_axis + + +def incr_mean_variance_axis(X, axis=0): + """Compute mean and variance incrementally along the given axis. + Parameters + ---------- + X : array-like, shape (n_samples, n_features) + Input data. + axis : int, optional (default=0) + Axis along which the incremental mean and variance are computed. + Returns + ------- + mean : array, shape (n_features,) + Incremental mean. + var : array, shape (n_features,) + Incremental variance. + """ + n_samples, n_features = X.shape + mean = np.zeros(n_features) + var = np.zeros(n_features) + for i in range(n_samples): + x_i = X[i, :] + delta = x_i - mean + mean += delta / (i + 1) + var += delta * (x_i - mean) + var /= (n_samples - 1) + return mean, var +Generated code for sklearn.utils.sparsefuncs.inplace_column_scale + + +def inplace_column_scale(X, scale): + """Scale the columns of a sparse matrix inplace. + + Parameters + ---------- + X : {scipy.sparse.csr_matrix, scipy.sparse.csc_matrix} + Sparse matrix to scale. + scale : array-like, shape (n_features,) + Per-feature scaling factors. + """ + if not isinstance(X, (scipy.sparse.csr_matrix, scipy.sparse.csc_matrix)): + raise ValueError("X should be a sparse matrix") + if len(scale) != X.shape[1]: + raise ValueError("scale should have length equal to the number of columns of X") + for i in range(X.shape[1]): + X.data[X.indptr[i]:X.indptr[i + 1]] *= scale[i] +Generated code for sklearn.utils.sparsefuncs.inplace_row_scale + + +def inplace_row_scale(X, scale): + """Scale rows of a CSR matrix inplace. + + Parameters + ---------- + X : {csr_matrix, csc_matrix} + Input matrix. + scale : array, shape (n_samples,) + Per-sample scaling factors. + """ + if not isinstance(X, (sparse.csr_matrix, sparse.csc_matrix)): + raise ValueError("X should be a CSR or CSC matrix") + + n_samples = X.shape[0] + if len(scale) != n_samples: + raise ValueError("scale should have length n_samples") + + # Multiply each row by corresponding scaling factor + for i in range(n_samples): + X.data[X.indptr[i]:X.indptr[i + 1]] *= scale[i] +Generated code for sklearn.utils.sparsefuncs.inplace_swap_row + + +def inplace_swap_row(X, i, j): + """Swap row i and row j of a CSR or CSC matrix + Parameters + ---------- + X : {csr_matrix, csc_matrix} + Matrix + i : int + Row to swap with row j + j : int + Row to swap with row i + """ + if not (hasattr(X, 'indptr') and hasattr(X, 'indices')): + raise ValueError("Swapping is only supported for CSR and CSC matrices") + + n_rows, n_cols = X.shape + if i >= n_rows or j >= n_rows: + raise ValueError("`i` and `j` should be smaller than n_rows") + + # Swap data + if hasattr(X, 'data'): + temp = X.data[X.indptr[i]:X.indptr[i + 1]].copy() + X.data[X.indptr[i]:X.indptr[i + 1]] = X.data[X.indptr[j]:X.indptr[j + 1]] + X.data[X.indptr[j]:X.indptr[j + 1]] = temp + + # Swap indices + temp = X.indices[X.indptr[i]:X.indptr[i + 1]].copy() + X.indices[X.indptr[i]:X.indptr[i + 1]] = X.indices[X.indptr[j]:X.indptr[j + 1]] + X.indices[X.indptr[j]:X.indptr[j + 1]] = temp + + # Swap indptr + temp = X.indptr[i] + X.indptr[i] = X.indptr[j] + X.indptr[j] = temp +Generated code for sklearn.utils.sparsefuncs.inplace_swap_column + + +def inplace_swap_column(X, i, j): + """Swap columns in a CSR or CSC matrix + Swap column i and j in the CSR or CSC matrix X. + Parameters + ---------- + X : {csr_matrix, csc_matrix} + Matrix + i : int + Column i + j : int + Column j + Returns + ------- + X : {csr_matrix, csc_matrix} + Matrix with columns i and j swapped + """ + if not (hasattr(X, 'tocsr') or hasattr(X, 'tocsc')): + raise ValueError("Expected csr_matrix or csc_matrix, got %s" % + type(X)) + + # Check that the indices are valid + n_cols = X.shape[1] + if i >= n_cols or j >= n_cols: + raise ValueError("Column index out of range") + + # Do the swap + if hasattr(X, 'tocsr'): + data = X.data + indices = X.indices + indptr = X.indptr + for row_idx in range(X.shape[0]): + # Find the start and end of the row + row_start = indptr[row_idx] + row_end = indptr[row_idx + 1] + + # Find the indices of the elements in column i and j + i_idx = row_start + np.searchsorted(indices[row_start:row_end], i) + j_idx = row_start + np.searchsorted(indices[row_start:row_end], j) + + # Swap the elements + data[i_idx], data[j_idx] = data[j_idx], data[i_idx] + indices[i_idx], indices[j_idx] = indices[j_idx], indices[i_idx] + + X = X.tocsr() + + elif hasattr(X, 'tocsc' +Generated code for sklearn.utils.sparsefuncs.mean_variance_axis + + +def mean_variance_axis(X, axis=0): + """Compute mean and variance along an axis of a sparse matrix. + Parameters + ---------- + X : {scipy.sparse.csr_matrix, scipy.sparse.csc_matrix} + Input sparse matrix. + axis : int (default=0) + Axis along which the mean and variance are to be computed. + Returns + ------- + mean, variance : tuple of ndarrays + The mean and variance along the specified axis. + """ + if axis not in (0, 1): + raise ValueError("axis should be 0 or 1, got %r" % axis) + + if axis == 0: + # axis 0 is the rows + X = X.tocsc() + else: + # axis 1 is the columns + X = X.tocsr() + + mean = np.zeros(X.shape[axis]) + variance = np.zeros(X.shape[axis]) + + for i in range(X.shape[axis]): + if axis == 0: + # axis 0 is the rows + row = X.getrow(i) + else: + # axis 1 is the columns + row = X.getcol(i) + + mean[i] = row.mean() + variance[i] = row.var() + + return mean, variance +Generated code for sklearn.utils.sparsefuncs.inplace_csr_column_scale + + +def inplace_csr_column_scale(X, scale): + """ + Scale the columns of a CSR matrix in-place. + + Parameters + ---------- + X : {csr_matrix} + Input matrix. + scale : {array-like, sparse matrix} + Scaling factors for each column. + + Returns + ------- + X : {csr_matrix} + Scaled matrix. + """ + n_samples, n_features = X.shape + if not isinstance(scale, np.ndarray): + scale = np.asarray(scale) + if scale.shape[0] != n_features: + raise ValueError("Incorrect shape for scale.") + for i in range(n_features): + start, end = X.indptr[i], X.indptr[i + 1] + X.data[start:end] *= scale[i] + return X +Generated code for sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l1 + + +def inplace_csr_row_normalize_l1(csr, copy=False): + """ + Normalize the rows of a CSR matrix by their L1 norm. + This is an inplace operation that modifies the matrix ``csr``. + Parameters + ---------- + csr : scipy.sparse.csr_matrix + Input matrix. + copy : bool, optional + Whether to copy ``csr`` or perform normalization inplace. + The inplace version might be slightly faster. + Returns + ------- + csr : scipy.sparse.csr_matrix + Normalized matrix. + """ + if copy: + csr = csr.copy() + + n_samples, n_features = csr.shape + row_sums = np.asarray(csr.sum(axis=1)).ravel() + + # compute the normalization factors + nonzeros = row_sums != 0 + row_inv_sums = np.zeros(n_samples) + row_inv_sums[nonzeros] = 1.0 / row_sums[nonzeros] + + # apply the normalization + csr.data *= row_inv_sums[csr.indices] + + return csr +Generated code for sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l2 + + +def inplace_csr_row_normalize_l2(csr, copy=False): + """ + Normalize the rows of a CSR matrix by their L-2 norm. + This is faster than scipy.sparse.linalg.norm but less flexible. + Parameters + ---------- + csr : scipy.sparse.csr_matrix + Input matrix. + copy : bool, optional + If True, returns a copy of the input matrix. + Otherwise, the input matrix is modified in-place. + Returns + ------- + csr : scipy.sparse.csr_matrix + Normalized matrix. + """ + if copy: + csr = csr.copy() + n_samples, n_features = csr.shape + row_norms = np.sqrt(np.array(csr.power(2).sum(1))).reshape(-1, 1) + row_norms[row_norms == 0] = 1 + csr /= row_norms + return csr +Generated code for sklearn.utils.random.sample_without_replacement + + +def sample_without_replacement(population, k): + # population is a list of elements from which to choose + # k is the number of elements to choose + n = len(population) + if not 0 <= k <= n: + raise ValueError("Sample larger than population or is negative") + + # Choose indices of sampled elements + indices = [] + for i in range(k): + j = random.randrange(n - i) + indices.append(j) + population[j], population[n - i - 1] = population[n - i - 1], population[j] + + # Return the sampled elements + return [population[i] for i in indices] +Generated code for sklearn.utils.validation.check_is_fitted + + +def check_is_fitted(estimator, attributes, msg=None, all_or_any=all): + """Perform is_fitted validation for estimator. + + Checks if the estimator is fitted by verifying the presence of + "all_or_any" of the passed attributes and raises a NotFittedError with the + given message. + + Parameters + ---------- + estimator : estimator instance. + estimator instance for which the check is performed. + + attributes : attribute name(s) given as string or a list/tuple of strings + Eg. : ["coef_", "estimator_", ...], "coef_" + + msg : string + The default error message is, "This %(name)s instance is not fitted + yet. Call 'fit' with appropriate arguments before using this method." + + For custom messages if "%(name)s" is present in the message string, + it is substituted for the estimator name. + + Eg. : "Estimator, %(name)s, must be fitted before sparsifying". + + all_or_any : {all, any}, default all + Specify whether all or any of the given attributes must exist. + + Returns + ------- + None + + Raises + ------ + NotFittedError + If the attributes are not found. + """ + if msg is None: + msg = ("This %(name)s instance is not fitted yet. Call 'fit' with " + "appropriate arguments before using this method.") + + if not hasattr(estimator, 'fit'): + raise TypeError("%s is not an estimator instance." % (estimator)) + + if not isinstance(attributes, (list, tuple)): + attributes = [attributes] + + if all_or_any == 'all': + if not all(hasattr(estimator, attr) for attr in attributes): + raise NotFittedError(msg % {'name': type(estimator).__name__}) + elif all_or_any == 'any': + if not any(hasattr(estimator, attr) for attr +Generated code for sklearn.utils.validation.check_memory + + +def check_memory(estimator, X, y=None): + """Check that the estimator doesn't exceed the memory limit. + Parameters + ---------- + estimator : estimator instance + Estimator to validate. + X : array-like, sparse matrix + Input data. + y : array-like, optional (default=None) + Target data. + Returns + ------- + memory_usage : float + Memory usage of the estimator. + """ + from sklearn.utils.validation import _check_estimator + _check_estimator(estimator) + + # Get the size of the input data + X_size = X.shape[0] * X.shape[1] * X.dtype.itemsize + + # Get the size of the target data + if y is not None: + y_size = y.shape[0] * y.dtype.itemsize + else: + y_size = 0 + + # Get the size of the estimator + estimator_size = estimator.get_params()['memory_limit'] + + # Calculate the total memory usage + memory_usage = X_size + y_size + estimator_size + + # Check if the memory usage is within the limit + if memory_usage > estimator_size: + raise MemoryError('Estimator exceeds memory limit.') + + return memory_usage +Generated code for sklearn.utils.validation.check_symmetric + + +def check_symmetric(array, tol=1e-8): + """ + Checks if an array is symmetric. + + Parameters + ---------- + array : array_like + Input array to check. + tol : float + Tolerance for comparison. + + Returns + ------- + is_symmetric : bool + True if array is symmetric, False otherwise. + """ + if array.shape[0] != array.shape[1]: + return False + for i in range(array.shape[0]): + for j in range(array.shape[1]): + if abs(array[i, j] - array[j, i]) > tol: + return False + return True +Generated code for sklearn.utils.validation.column_or_1d + + +def column_or_1d(y, warn=False): + """ Ravel column or 1d numpy array, else raises an error + + Parameters + ---------- + y : array-like + + warn : boolean, default False + To control display of warnings. + + Returns + ------- + y : array + + """ + shape = np.shape(y) + if len(shape) == 1: + return np.ravel(y) + if len(shape) == 2 and shape[1] == 1: + if warn: + warnings.warn("A column-vector y was passed when a 1d array was" + " expected. Please change the shape of y to " + "(n_samples, ), for example using ravel().", + DataConversionWarning, stacklevel=2) + return np.ravel(y) + + raise ValueError("y should be a 1d array, or a column vector. " + "Reshape your array to (n_samples, )") +Generated code for sklearn.utils.validation.has_fit_parameter + + +def has_fit_parameter(estimator, parameter): + """ + Check if an estimator has a given parameter. + + Parameters + ---------- + estimator : estimator object + Estimator object to check. + parameter : str + Parameter name to check for. + + Returns + ------- + has_parameter : bool + True if the estimator has the parameter, False otherwise. + """ + try: + estimator.get_params()[parameter] + return True + except KeyError: + return False +Generated code for sklearn.utils.discovery.all_estimators + + +from sklearn.utils.discovery import all_estimators + +estimators = all_estimators() + +for name, class_ in estimators: + if hasattr(class_, 'fit'): + print(name) +Generated code for sklearn.utils.discovery.all_displays + + +import sklearn +from sklearn.utils import discovery + +def all_displays(): + """ + Return a list of all available display functions. + """ + displays = [] + for name, obj in discovery.iter_modules(sklearn.utils.discovery): + if hasattr(obj, 'display'): + displays.append(obj.display) + return displays +Generated code for sklearn.utils.discovery.all_functions + + +import sklearn.utils.discovery + +# Get all functions in sklearn.utils.discovery +all_functions = sklearn.utils.discovery.all_functions() + +# Print all functions +for func in all_functions: + print(func) +Generated code for sklearn.utils.parallel.delayed + + +import multiprocessing +from functools import partial + +def parallel_delayed(func, args, n_jobs=1): + """ + Execute a function in parallel using multiprocessing. + Parameters + ---------- + func : callable + The function to execute in parallel. + args : list + A list of arguments to pass to the function. + n_jobs : int, optional (default=1) + The number of jobs to run in parallel. + Returns + ------- + results : list + A list of results from the function calls. + """ + pool = multiprocessing.Pool(n_jobs) + results = pool.map(partial(func, *args)) + pool.close() + pool.join() + return results +Generated code for sklearn.utils.parallel_backend + + +import multiprocessing +from sklearn.utils import parallel_backend + +# Define the number of cores to use +n_jobs = multiprocessing.cpu_count() + +# Set the backend for parallel processing +with parallel_backend('multiprocessing', n_jobs=n_jobs): + # Your code here + pass +Generated code for sklearn.utils.register_parallel_backend + + +import sklearn +from sklearn.utils import register_parallel_backend + +def register_parallel_backend(backend, n_jobs=1, **backend_args): + """Register a parallel backend for joblib. + + Parameters + ---------- + backend : str + The name of the backend to register. + n_jobs : int, optional (default=1) + The number of jobs to use for the backend. + **backend_args : dict + Additional keyword arguments to pass to the backend. + + Returns + ------- + registered_backend : joblib.parallel.BackendBase + The registered backend instance. + """ + from joblib import parallel + backend_cls = parallel.get_backend_cls(backend) + registered_backend = backend_cls(n_jobs=n_jobs, **backend_args) + parallel.register_parallel_backend(backend, registered_backend) + return registered_backend +Generated code for sklearn.utils.parallel.Parallel + + +import multiprocessing +from sklearn.utils.parallel import Parallel + +def parallel_function(data, func, n_jobs=1, backend='threading', verbose=0): + """ + Execute a function in parallel. + + Parameters + ---------- + data : iterable + The data to be processed. + func : callable + The function to be applied to each element of the data. + n_jobs : int, optional (default=1) + The number of jobs to use for the computation. + backend : str, optional (default='threading') + The backend to use for the computation. + verbose : int, optional (default=0) + The verbosity level. + + Returns + ------- + results : list + The results of the computation. + """ + pool = multiprocessing.Pool(n_jobs, backend=backend) + results = pool.map(func, data) + pool.close() + pool.join() + if verbose > 0: + print("Done parallel processing") + return results + +Parallel(n_jobs=1, backend='threading', verbose=0)( + [delayed(parallel_function)(data, func) for data, func in zip(data_list, func_list)] +) +Generated code for sklearn.utils.metaestimators.if_delegate_has_method + + +def if_delegate_has_method(delegate, method_name): + """ + Checks if the given delegate has the given method. + + Parameters + ---------- + delegate : object + The object to check for the given method. + method_name : str + The name of the method to check for. + + Returns + ------- + bool + True if the delegate has the given method, False otherwise. + """ + if hasattr(delegate, method_name): + return True + else: + return False