pynir package¶
Submodules¶
pynir.Calibration module¶
Created on Wed Sep 28 11:00:35 2022
@author: chinn
- class pynir.Calibration.lsvc(penalty='l2', loss='squared_hinge', *, dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None, max_iter=1000)[source]¶
Bases:
LinearSVC
- pynir.Calibration.plot_confusion_matrix(cm, target_names, title='Confusion matrix', cmap=None, normalize=True)[source]¶
given a sklearn confusion matrix (cm), make a nice plot
Arguments¶
cm: confusion matrix from sklearn.metrics.confusion_matrix
- target_names: given classification classes such as [0, 1, 2]
the class names, for example: [‘high’, ‘medium’, ‘low’]
title: the text to display at the top of the matrix
- cmap: the gradient of the values displayed from matplotlib.pyplot.cm
see http://matplotlib.org/examples/color/colormaps_reference.html plt.get_cmap(‘jet’) or plt.cm.Blues
- normalize: If False, plot the raw numbers
If True, plot the proportions
Usage¶
- plot_confusion_matrix(cm = cm, # confusion matrix created by
# sklearn.metrics.confusion_matrix
normalize = True, # show proportions target_names = y_labels_vals, # list of names of the classes title = best_estimator_name) # title of graph
Citiation¶
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
- class pynir.Calibration.pls(n_components=2)[source]¶
Bases:
object
- crossValidation_predict(nfold=10)[source]¶
Predict dependent variable using cross validation method.
Parameters: nfold (int): number of folds for cross validation (default is 10)
Returns: yhat (np.array): predicted dependent variable based on cross validation method
- fit(X, y)[source]¶
Fit the PLS model to training data.
Parameters: X (np.array): independent variable(s) of the training data y (np.array): dependent variable of the training data
Returns: self: the PLS model
- predict(Xnew, n_components=None)[source]¶
Predict the dependent variable based on independent variable(s).
Parameters: Xnew (np.array): independent variable(s) of the new data to predict n_components (int): number of components used to predict
- (default is None, which uses the optimal number of
components obtained by cross validation)
Returns: ynew_hat (np.array): predicted dependent variable based on Xnew
- class pynir.Calibration.plsda(n_components=2, scale=True, **kwargs)[source]¶
Bases:
PLSRegression
- fit(X, y)[source]¶
Fit model to data.
Parameters¶
- Xarray-like of shape (n_samples, n_features)
Training vectors, where n_samples is the number of samples and n_features is the number of predictors.
- Yarray-like of shape (n_samples,) or (n_samples, n_targets)
Target vectors, where n_samples is the number of samples and n_targets is the number of response variables.
Returns¶
- selfobject
Fitted model.
- predict(X)[source]¶
Predict targets of given samples.
Parameters¶
- Xarray-like of shape (n_samples, n_features)
Samples.
- copybool, default=True
Whether to copy X and Y, or perform in-place normalization.
Returns¶
- y_predndarray of shape (n_samples,) or (n_samples, n_targets)
Returns predicted values.
Notes¶
This call requires the estimation of a matrix of shape (n_features, n_targets), which may be an issue in high dimensional space.
- class pynir.Calibration.rf(n_estimators=100, *, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='sqrt', max_leaf_nodes=None, min_impurity_decrease=0.0, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight=None, ccp_alpha=0.0, max_samples=None)[source]¶
Bases:
RandomForestClassifier
- pynir.Calibration.sampleSplit_KS(X, test_size=0.25, metric='euclidean', *args, **kwargs)[source]¶
Kennard Stone Sample Split method Parameters ———- spectra: ndarray, shape of i x j
i spectrums and j variables (wavelength/wavenumber/ramam shift and so on)
- test_sizefloat, int
if float, then round(i x (1-test_size)) spectrums are selected as test data, by default 0.25 if int, then test_size is directly used as test data size
- metricstr, optional
The distance metric to use, by default ‘euclidean’ See scipy.spatial.distance.cdist for more infomation
Returns¶
- select_pts: list
index of selected spetrums as train data, index is zero based
- remaining_pts: list
index of remaining spectrums as test data, index is zero based
References¶
Kennard, R. W., & Stone, L. A. (1969). Computer aided design of experiments. Technometrics, 11(1), 137-148. (https://www.jstor.org/stable/1266770)
- pynir.Calibration.simpls(X, y, n_components)[source]¶
Partial Least Squares, SIMPLS Ref https://github.com/freesiemens/SpectralMultivariateCalibration/blob/master/pypls.py :param X: independent variables, numpy array of shape (n_samples, n_variables) :param y: dependent variable, numpy array of shape (n_samples,) or (n_samples, 1) :param n_components: number of latent variables to decompose the data into :return: dictionary containing the results of the SIMPLS algorithm
pynir.CalibrationTransfer module¶
Created on Wed Sep 28 11:00:35 2022
@author: chinn
- class pynir.CalibrationTransfer.BS[source]¶
Bases:
object
- Ref Osborne B. G., Fearn T. Collaborative evaluation of universal
calibrations for the measurement of protein and moisture in flour by near infrared reflectance [J]. International Journal of Food Science & Technology, 1983, 18(4): 453-460.
- class pynir.CalibrationTransfer.PDS(halfWindowSize=7, regType='mlr', **kwargs)[source]¶
Bases:
object
- class pynir.CalibrationTransfer.SST(n_components=2)[source]¶
Bases:
object
pynir.FeatureSelection module¶
- class pynir.FeatureSelection.MSVC(x, y, ncomp=1, nrep=7000, ncut=50, testSize=0.2)[source]¶
Bases:
object
pynir.OutlierDection module¶
Created on Wed Sep 28 11:00:35 2022
@author: chinn
pynir.Preprocessing module¶
Created on Wed Sep 28 11:01:16 2022
@author: chinn
- class pynir.Preprocessing.SG_filtering(window_length=13, polyorder=2, **kwargs)[source]¶
Bases:
object
- class pynir.Preprocessing.derivate(deriv=1, window_length=13, polyorder=2, **kwargs)[source]¶
Bases:
object
pynir.utils module¶
Created on Wed Sep 28 11:02:36 2022
@author: Jin Zhang (zhangjin@mail.nankai.edu.cn)
- pynir.utils.simulateNIR(nSample=100, n_components=3, refType=1, noise=0.0, seeds=1)[source]¶
simulating NIR spectra
Parameters¶
- nSampleint, optional
number of samples. The default is 100.
- n_componentsint, optional
number of componnet for spectral simulation. The default is 3.
- refTypeint, optional
type of reference value None for no reference value output 1 for contious values as reference value output 2 or the larger integer for binary or class output.
- seedsint, optimal
random seed for generating spectra and reference values. The default is 1.
Returns¶
X: matrix, simulated NIR spectra matrix. y: array, concentration or class of all samples.
- pynir.utils.simulateNIR_calibrationTransfer(nSample=100, n_components=3, shifts=0.01, refType=1, noise=0.0, seeds=1)[source]¶
simulating NIR spectra for calibration transfer
Parameters¶
- nSampleint, optional
number of samples. The default is 100.
- n_componentsint, optional
number of componnet for spectral simulation. The default is 3.
- shifts: float, optimal
shift level of base peaks for simulte secondary NIR spectra data
- refTypeint, optional
type of reference value None for no reference value output 1 for contious values as reference value output 2 or the larger integer for binary or class output.
- seedsint, optimal
random seed for generating spectra and reference values. The default is 1.
Returns¶
X: matrix, simulated NIR spectra matrix. y: array, concentration or class of all samples.