text:
# Reflection coefficient

In physics and electrical engineering the reflection coefficient is a parameter that describes how much of a wave is reflected by an impedance discontinuity in the transmission medium. It is equal to the ratio of the amplitude of the reflected wave to the incident wave, with each expressed as phasors. For example, it is used in optics to calculate the amount of light that is reflected from a surface with a different index of refraction, such as a glass surface, or in an electrical transmission line to calculate how much of the electromagnetic wave is reflected by an impedance. The reflection coefficient is closely related to the transmission coefficient. The reflectance of a system is also sometimes called a "reflection coefficient".

A wave experiences partial transmittance and partial reflectance when the medium through which it travels suddenly changes. The reflection coefficient determines the ratio of the reflected wave amplitude to the incident wave amplitude.

Different specialties have different applications for the term.

## Transmission lines

In telecommunications and transmission line theory, the reflection coefficient is the ratio of the complex amplitude of the reflected wave to that of the incident wave. The voltage and current at any point along a transmission line can always be resolved into forward and reflected traveling waves given a specified reference impedance Z0. The reference impedance used is typically the characteristic impedance of a transmission line that's involved, but one can speak of reflection coefficient without any actual transmission line being present. In terms of the forward and reflected waves determined by the voltage and current, the reflection coefficient is defined as the complex ratio of the voltage of the reflected wave (${\displaystyle V^{-}}$) to that of the incident wave (${\displaystyle V^{+}}$). This is typically represented with a ${\displaystyle \Gamma }$ (capital gamma) and can be written as:

${\displaystyle \Gamma ={\frac {V^{-}}{V^{+}}}}$

It can as well be defined using the currents associated with the reflected and forward waves, but introducing a minus sign to account for the opposite orientations of the two currents:

${\displaystyle \Gamma =-{\frac {I^{-}}{I^{+}}}={\frac {V^{-}}{V^{+}}}}$

The reflection coefficient may also be established using other field or circuit pairs of quantities whose product defines power resolvable into a forward and reverse wave. For instance, with electromagnetic plane waves, one uses the ratio of the electric fields of the reflected to that of the forward wave (or magnetic fields, again with a minus sign); the ratio of each wave's electric field E to its magnetic field H is again an impedance Z0 (equal to the impedance of free space in a vacuum). Similarly in acoustics one uses the acoustic pressure and velocity respectively.

Simple circuit configuration showing measurement location of reflection coefficient.

In the accompanying figure, a signal source with internal impedance ${\displaystyle Z_{S}\,}$ possibly followed by a transmission line of characteristic impedance ${\displaystyle Z_{S}\,}$ is represented by its Thévenin equivalent, driving the load ${\displaystyle Z_{L}}$. For a real (resistive) source impedance ${\displaystyle Z_{S}}$, if we define ${\displaystyle \Gamma }$ using the reference impedance ${\displaystyle Z_{0}}$=${\displaystyle Z_{S}\,}$ then the source's maximum power is delivered to a load ${\displaystyle Z_{L}}$=${\displaystyle Z_{0}}$, in which case ${\displaystyle \Gamma =0}$ implying no reflected power. More generally, the squared-magnitude of the reflection coefficient ${\displaystyle |\Gamma |^{2}}$ denotes the proportion of that power that is "reflected" and absorbed by the source, with the power actually delivered to the load thus reduced by ${\displaystyle 1-|\Gamma |^{2}}$.

Anywhere along an intervening (lossless) transmission line of characteristic impedance ${\displaystyle Z_{0}}$, the magnitude of the reflection coefficient ${\displaystyle |\Gamma |}$ will remain the same (the powers of the forward and reflected waves stay the same) but with a different phase. In the case of a short circuited load (${\displaystyle Z_{L}=0}$), one finds ${\displaystyle \Gamma =-1}$ at the load. This implies the reflected wave having a 180° phase shift (phase reversal) with the voltages of the two waves being opposite at that point and adding to zero (as a short circuit demands).

The reflection coefficient corresponds directly to a specific impedance as seen at the point it is measured. A load impedance of ${\displaystyle Z_{L}}$ (using a reference impedance ${\displaystyle Z_{0}\,}$) corresponds to a reflection coefficient of

${\displaystyle \Gamma ={Z_{L}-Z_{0} \over Z_{L}+Z_{0}}}$ .

If that load, ${\displaystyle Z_{L}}$, were measured not directly but through a transmission line, then the magnitude of the reflection coefficient is identical (as are the powers in the forward and reflected waves). However its phase will have shifted according to

${\displaystyle \Gamma '=\Gamma e^{-i\,2\phi }}$

where ${\displaystyle \phi }$ is the electrical length (expressed as phase) of that length of transmission line at the frequency considered. Note that the phase of the reflection coefficient is changed by twice the phase length of the attached transmission line. That is to take into account not only the phase delay of the reflected wave, but the phase shift that had first been applied to the forward wave, with the reflection coefficient being the quotient of these. The reflection coefficient so measured, ${\displaystyle \Gamma '}$, corresponds to an impedance which is generally dissimilar to ${\displaystyle Z_{L}}$ present at the far side of the transmission line.

The complex reflection coefficient (in the region ${\displaystyle |\Gamma |\leq 1}$, corresponding to passive loads) may be displayed graphically using a Smith chart. The Smith chart is a polar plot of ${\displaystyle \Gamma }$, therefore the magnitude of ${\displaystyle \Gamma }$ is given directly by the distance of a point to the center (with the edge of the Smith chart corresponding to ${\displaystyle |\Gamma |=1}$). Its evolution along a transmission line is likewise described by a rotation of ${\displaystyle 2\phi }$ around the chart's center. Using the scales on a Smith chart, the resulting impedance (normalized to ${\displaystyle Z_{0}}$) can directly be read. Before the advent of modern electronic computers, the Smith chart was of particular use as a sort of analog computer for this purpose.

### Standing wave ratio

The standing wave ratio (SWR) is determined solely by the magnitude of the reflection coefficient:

${\displaystyle SWR={1+|\Gamma | \over 1-|\Gamma |}}$ .

Along a lossless transmission line of characteristic impedance Z0, the SWR signifies the ratio of the voltage (or current) maxima to minima (or what it would be if the transmission line were long enough to produce them). The above calculation assumes that ${\displaystyle \Gamma }$ has been calculated using Z0 as the reference impedance. Since it uses only the magnitude of ${\displaystyle \Gamma }$, the SWR intentionally ignores the specific value of the load impedance ZL responsible for it, but only the magnitude of the resulting impedance mismatch. That SWR remains the same wherever measured along a transmission line (looking towards the load) since the addition of a transmission line length to a load ${\displaystyle Z_{L}}$ only changes the phase, not magnitude of ${\displaystyle \Gamma }$. While having a one-to-one correspondence with reflection coefficient, SWR is the most commonly used figure of merit in describing the mismatch affecting a radio antenna or antenna system. It is most often measured at the transmitter side of a transmission line, but having, as explained, the same value as would be measured at the antenna (load) itself.

## Seismology

Reflection coefficient is used in feeder testing for reliability of medium.

## Optics and microwaves

In optics and electromagnetics in general, "reflection coefficient" can refer to either the amplitude reflection coefficient described here, or the reflectance, depending on context. Typically, the reflectance is represented by a capital R, while the amplitude reflection coefficient is represented by a lower-case r. These related concepts are covered by Fresnel equations in classical optics.

## Acoustics

Acousticians use reflection coefficients to understand the effect of different materials on their acoustic environments.


text:
# Tag Info

7

On a general basis, you want to keep encryption and signature keys disjoint, because they tend to have distinct life cycles. In broad terms, an encryption key should be escrowed, because loss of the private key implies loss of the data which is encrypted relatively to the public key. However, a signature key must not be escrowed, since the proof value of a ...

6

Despite their theoretical security advantages, Schnorr signatures aren't very popular. Probably because they were patented. Since the patents expired in 2008 they might rise a bit in popularity. But probably only in the elliptic curve form, and not in finite fields. I don't know of any application actually using Schnorr signatures, but I know several that ...

4

The paper "On the Joint Security of Encryption and Signature in EMV" shows that ECIES and EC-Schnorr signatures can be used together without compromising security: In the random oracle model ECIES-KEM and EC-Schnorr are jointly secure if the gap-DLP problem and gap-DH problem are both hard Ed25519 is extremely similar to EC-Schnorr, and both ECIES ...

3

Yes, there are some examples of Schnorr signature in real world applications, although I can not provide you the names of the products. [edit - OpenSSL contains a reference implementation in schnorr.c]. The good feature of Schnorr signature is that by design it does not require lot of computations on the signer side. Therefore, you can use it even on a ...

3

First of all, while Schnorr Signatures are usually described that way, the two primes are not necessary for it to work. In principle, Schnorr works in any cyclic group. However, to achieve security we need that the discrete logarithm problem in that group is hard. So the reason for the choice of $q$ (which is the group order) is that DL is believed to be ...

Only top voted, non community-wiki answers of a minimum length are eligible

format: plain


text:
# How to find an equation for all points (x,y)(x,y) for which f(x,y)f(x,y) has a maximum rate of change in a direction parallel to a given vector vv?

Find an equation for all the points $(x,y)$ for which the function $f(x,y)=x^2+y^2−3x−y$ has a maximum rate of change in a direction parallel to the vector $u = (1, 1)$

And here is my attempt to solve it:

• we take the partial derivatives: $$\dfrac {\partial}{\partial x} = 2x-3$$ $$\dfrac {\partial}{\partial y} = 2y-1$$

• now we find the unit vector $\dfrac {\left|\vec{v} \right|} {\left|v \right|}$ $= \dfrac {1} {\sqrt 2}$

• now use the formula that says: $$D_uf(a,b) = f_u′ (a,b) = u·\nabla f(a,b) = u·(f_x(a,b),f_y(a,b))$$

• Hence the equation is $$\dfrac {2x+2y-4}{\sqrt 2} = \sqrt 2 \{x+y-2\}$$ which is wrong according to my professor. Any help will be highly appreciated.

• To typeset the character $\partial$ use the command \partial. – DMcMor Apr 15 '17 at 21:22

The direction of maximum rate of change parallel to the gradient vector, which in this case is $\vec\nabla f= (2x-3,2y-1).$ So we need this to be proportional to $(1,1).$ This just means that $2x-3 = 2y-1$ which gives $y = x-1.$ So the solutions are all the points on this line.

• i sent a PM to my teacher and he said the following : Your equation/answer can be rewritten as 0x + 0y = 0 which is trivially true for ALL points (x,y) in R^2, so, no - this is not the answer. The problem requires that grad f is parallel to (1,1), which can be written as grad f = k(1,1). Eliminate k and you should get the linear eq. y = x - 1. how do i eliminate k – Reddevil Apr 15 '17 at 21:29
• @Reddevil Write out the two equations and it ought to become obvious. – amd Apr 15 '17 at 21:45
• @Reddevil It's an equation for each component. $\nabla f = (k,k)$ means $2x-3 = k$ and $2y-1 = k.$ So eliminate $k$ from those two equations and you get the answer. This is what I meant when I said the vector being proportional to $(1,1)$ means its components are equal. – spaceisdarkgreen Apr 15 '17 at 21:54

See directional derivative, this question is fairly routine.

$$\nabla_{\mathbf v} f(x,y) = \nabla f(x,y) \cdot \mathbf v$$ gives the rate of change in direction $\mathbf v$ and is itself a map from $\Bbb R^2 \to \Bbb R$. The maximum is a critical point of the total derivative of the directional derivative above, or boundary point.

$$\nabla_{\mathbf v} f(x,y) = \frac{\partial f(x,y)}{\partial x} v_x + \frac{\partial f(x,y) }{\partial y} v_y = (2x-3)v_x+(2y-1)v_y=2x+2y-4$$

Seems there is a min at negative infinity. Intuitively it seems $f(x,y)$ is a infinite parabola or extruded parabola, and has no maximum rate of change in direction $(1,1)$


text:
9 months ago

donor acceptor impurities

donor acceptor impurities in semiconductors

describe difference between donor acceptor impurities

define donor and acceptor impurities

example of donor and acceptor impurities

explain donor and acceptor impurities

donor and acceptor impurities pdf

differences between donor and acceptor impurities

donor and acceptor impurities ppt

acceptor and donor impurities donate

donor and acceptor impurities in silicon

donor and acceptor impurities in semiconductors definition

difference donor and acceptor impurities

Donors and acceptors . Donor: . besides impurity atoms: . doped semiconductors a) energy band diagram in space b) .. Organic DonorAcceptor Complexes as . growing crystals of DA complexes with densely packed structures will reduce impurities and traps from . (both PDF and HTML .. impurities Will discuss electrons and holes, as . $Energy$Band$Diagrams$and$Doping$ Donor Doping -Electron and Hole Densities .. Combined effects of donor and acceptor impurities on the thermoelectric properties of cadmium antimonide. A simple model accounts for the observed surface concentration in both donor and acceptor compounds.. The maximum effect of donor and acceptor molecules occurs near 1 monolayer coverage. Effect of Adsorbed Donor and Acceptor Molecules on Electron Stimulated Desorption: O2/TiO2(110) .. Determination of donor and acceptor impurity concentrations in n-InP . to considerable errors in the calculation of the impurity . Download PDF. Cite article. How .. Our method can be widely applied in the calculation of the electronic structures and binding energy levels of a hydrogenic acceptor impurity in semiconductor nano-structures of other shapes. Donoracceptor chemistry in the main group E. Rivard . Download Author version (PDF) Citation: Dalton Trans., 2014, 43, 8577-8586 .. structure for both on- and o-center donor and acceptor impurities in GaN NCs of diameter up to 13.5 nm.. CARRIER CONCENTRATIONS IN SEMICONDUCTORS Donors and Acceptors . aarree calledcalled aass dopantsdopants impurities impurities.. Impurity atoms are classified as either donor or acceptor atoms based on the effect they have on the intrinsic semiconductor. Donor impurity atoms have .. Journal of Materials Chemistry C; Donor and acceptor impurity-driven switching of magnetic ordering in MnSb 2 .. 5 July 1989 Determination Of Donor And Acceptor Concentrations In GaAs By Electronic Raman Scattering And Selective Luminescence. Full-text (PDF) We study transport in a Weyl semimetal with donor and acceptor impurities. At sufficiently high temperatures transport is dominated by electron .. The electrical resistivity of galena, pyrite, and chalcopyrite . and bismuth impurities, and sulfur vacancies; acceptor . dominant donors and acceptors, .. compensated semiconductro.pdf - Free download . equals the impurity concentration. For donors this . between the donor and acceptor .. Abstract: We study transport in a Weyl semimetal with donor and acceptor impurities.. This identification was confirmed by the observation of As-donorB-acceptor pair luminescence in an As-doped sample.. Doped semiconductors are . A semiconductor doped with impurities which . equal to the difference between the donor and acceptor concentration if .. We investigate acceptor and donor states in GaN nanocrystals doped with a . Acceptor and donor impurities in GaN . Scanner Internet Archive Python .. Color online Donor and acceptor transition energies for 3d impurities in a In2O3 and b ZnO. Donor and acceptor transitions are denoted by blue and red lines, respectively.. 5 July 1989 Determination Of Donor And Acceptor Concentrations In GaAs By Electronic Raman Scattering And Selective Luminescence. .. . electron donor impurities create states near the conduction band while electron acceptor impurities . the sum of the donor and acceptor ions. Doping in .. Effect of co-doping of donor and acceptor impurities in the ferromagnetic semiconductor Zn1-xCrxTe studied by . PDF download. download 1 file . Acceptor and donor .. Full-text (PDF) Calculations of the electronic states of donor and acceptor impurities in nanowires show that the ionization energy of the impurities is strongly .. Recommended methods for the purification of solvents and tests for impurities: .. Download details: IP Address . A Mossbauer study of the electronic configuration of Sb donors and acceptors in . accumulated on the acceptor impurity than for the .. MidwayUSA is a privately held American retailer of various hunting and outdoor-related products.. Donor and Acceptor Concentrations in Degenerate InN . The identities of the donors and acceptors . concentrations of impurities, .. Trapping and de-trapping of the acceptor-donor impurity pairs in semiconductors have been studied . PDF (350 KB) PDF . THERMALLY INDUCED RE-TRAPPING OF IMPURITY . 51f937b7a3
Ohona ekto kotha khona mp3


text:
# MUTAN: Multimodal Tucker Fusion for Visual Question Answering

## WHY?

While bilinear model is an effective method for capturing the relationship between two spaces, often the number of parameters is intractable. This paper suggests to reduce the number of parameters by controlling the rank of the matrix with Turker decomposition.

## Note

With \times_i$\times_i$ denotes the i-mode product between a tensor and a matrix, Tucker decomposition of a tensor \tau$\tau$ is as follows.

\mathbf{\tau} \in \mathbb{R}^{d_q \times d_v \times |\mathcal{A}|}\\
\mathbf{\tau} = ((\mathbf{\tau_c} \times_1 \mathbf{W}_q) \times_2 \mathbf{W}_v) \times_3 \mathbf{W}_O

## WHAT?

The Tucker decomposition showed that a tensor can be represented with a limited number of parameters. The bilinear relationship between question vectors and image vectors can be represented in a Tucker Fusion form.

\mathbf{\tau} = ((\mathbf{\tau_c} \times_1 (\mathbf{q}^{\top}\mathbf{W}_q)) \times_2 (\mathbf{v}^{\top}\mathbf{W}_v)) \times_3 \mathbf{W}_O\\
\mathbf{z} = (\mathbf{\tau}_c \times_1 \mathbf{\tilde{q}}) \times_2 \mathbf{\tilde{v}} \in \mathbb{R}^{t_o}\\
\mathbf{y} = \mathbf{z}^{\top}\mathbf{W}_O

We can control the sparsity and expressiveness of bilinear model by controlling the rank of a slice of \mathbf{\tau}_c$\mathbf{\tau}_c$. If we impose the rank of a slice of the core matrix to be R, then each slice can be represented with the sum of R rank one matrices.

\mathbf{z}[k] = \tilde{\mathbf{q}}^{\top}\mathbf{\tau}_c[:,:,k]\tilde{\mathbf{v}}\\
\mathbf{\tau}_c[:,:,k] = \sum_{r=1}^R \mathbf{m}_r^k \otimes \mathbf{n}_r^{k\top}\\
\mathbf{z}[k] = \sum_{r=1}^R(\tilde{\mathbf{q}}^{\top}\mathbf{m}_r^k)(\tilde{\mathbf{v}}^{\top}\mathbf{n}_r^k)\\
\mathbf{z} = \sum_{r=1}^R \mathbf{z}_r\\
\mathbf{z}_r = (\tilde{\mathbf{q}}^{\top}\mathbf{M}_r)*(\tilde{\mathbf{v}}^{\top}\mathbf{N}_r)

MCB and MLB can be represented with a generalized form of MUTAN.

## So?

MUTAN can represent rich multimodal representation. Achieved SOTA results on VQA dataset.

Powered by aiden


text:
Journal topic
Geosci. Instrum. Method. Data Syst., 8, 285–291, 2019
https://doi.org/10.5194/gi-8-285-2019
Geosci. Instrum. Method. Data Syst., 8, 285–291, 2019
https://doi.org/10.5194/gi-8-285-2019

Research article 05 Dec 2019

Research article | 05 Dec 2019

# How many solar wind data are sufficient for accurate fluxgate magnetometer offset determinations?

How many solar wind data are sufficient for accurate fluxgate magnetometer offset determinations?
Ferdinand Plaschke Ferdinand Plaschke
• Space Research Institute, Austrian Academy of Sciences, Graz, Austria

Correspondence: Ferdinand Plaschke (ferdinand.plaschke@oeaw.ac.at)

Abstract

Accurate magnetic field measurements by fluxgate magnetometers onboard spacecraft require ground and regular in-flight calibration activities. Therewith, the parameters of a coupling matrix and an offset vector are adjusted; they are needed to transform raw magnetometer outputs into calibrated magnetic field measurements. The components of the offset vector are typically determined by analyzing Alfvénic fluctuations in the solar wind if solar wind measurements are available. These are characterized by changes in the field components, while the magnetic field modulus stays constant. In this paper, the following question is answered: how many solar wind data are sufficient for accurate fluxgate magnetometer offset determinations? It is found that approximately 40 h of solar wind data are sufficient to achieve offset accuracies of 0.2 nT, and about 20 h suffice for accuracies of 0.3 nT or better if the magnetometer offsets do not drift within these time intervals and if the spacecraft fields do not vary at the sensor position. Offset determinations with uncertainties lower than 0.1 nT, however, would require at least hundreds of hours of solar wind data.

1 Introduction

In situ investigations of the plasma environments of planets, moons, comets, or other solar system bodies require magnetic field measurements by spacecraft magnetometers. Typically, fluxgate magnetometers are used for scientific applications. The required measurements can only be provided if those magnetometers are accurately calibrated. This means that a coupling matrix C and an offset vector O have to be accurately known in order to transform raw magnetometer outputs Braw into calibrated magnetic field measurements B :

$\begin{array}{}\text{(1)}& \mathbit{B}=\mathbf{C}\cdot {\mathbit{B}}_{\mathrm{raw}}-\mathbit{O}.\end{array}$

Both C and O should be determined on the ground and in flight, as calibration parameters, in particular the offset components, are known to change over time. Offset changes may be associated with instrument drifts or with variations of the spacecraft-generated magnetic fields at the magnetometer sensor, as the offsets are the outputs of a magnetometer in vanishing ambient field conditions.

If the spacecraft is spin stabilized, then the spin plane offset components are easily determined by minimizing the spin tone content in the despun spin plane magnetic field measurements . If the spacecraft is non-spinning, i.e., three-axis stabilized, then the following methods can be used for offset determination: (1) Alfvénic fluctuations that are abundant in the solar wind are characterized by changes in the magnetic field components, while the field magnitude stays constant. An analysis of such fluctuations allows for an adjustment of the offsets through minimization of the changes in the magnetic field magnitude (e.g., Belcher1973; Hedgecock1975; Leinweber et al.2008). This is the typical method for offset determination if solar wind measurements are available. (2) Compressional fluctuations can also be used to determine magnetometer offsets through the application of the mirror-mode method . In this case the fact that the maximum variance direction of the fluctuations should coincide with the average magnetic field direction is used. Any mismatch may be attributed to incorrect offsets. (3) Furthermore, offsets may be obtained by comparing fluxgate magnetometer magnetic field measurements to (i) measurements from an electron drift instrument (EDI) or from an absolute magnetometer , (ii) otherwise known fields, e.g., when the spacecraft is in a diamagnetic cavity , or (iii) field estimates from a field model such as the International Geomagnetic Reference Field (e.g., Thébault et al.2015).

This paper deals with option (1). It shall address the following question: how many solar wind data are needed to obtain all three components of the offset vector with a certain accuracy? It shall be assumed that the magnetometer is otherwise perfectly calibrated (C accurately determined), that the offset components are not drifting (i.e., non-drifting instrument and non-varying spacecraft fields at the magnetometer sensor position), and that the magnetometer is mounted on a non-spinning spacecraft. The latter assumption means that the spacecraft spin cannot be used to support the determination of the spin plane offset components.

2 Data, methods, and results

To answer the question posed in the Introduction, well-calibrated magnetometer measurements in the solar wind are needed. In this paper, measurements from the National Aeronautics and Space Administration (NASA) Magnetospheric Multiscale (MMS) mission are used . The mission consists of four spin-stabilized spacecraft launched on 13 March 2015 into highly elliptical and roughly equatorial orbits. The goal of the mission is to explore the small-scale physics of magnetic reconnection. To achieve this goal, the spacecraft are required to fly in close configuration (spacecraft separations down to a few kilometers) in regions where reconnection is likely to take place, at the dayside magnetopause and in the geomagnetic tail. Due to the small spacecraft separations, high-cadence measurements and the most accurate calibrations of all instruments are key. Otherwise, differences between spacecraft cannot be resolved.

The MMS spacecraft are equipped with instruments to measure particle distribution functions as well as electric and magnetic fields . Here, only measurements by the MMS fluxgate magnetometers (FGMs) are used. Each spacecraft has two fluxgate magnetometers, an analog fluxgate and a digital fluxgate magnetometer (AFG and DFG), mounted at the ends of two separate 5 m long booms . The instruments and, in particular, the offsets pertaining to AFG and DFG on all spacecraft are very well calibrated: as the MMS spacecraft are spinning, the spin plane offsets can be and are dynamically adjusted in low fields, e.g., in the solar wind . Thereby, temperature-dependent offset variations on the order of 0.2 nT are corrected. As a result, the absolute uncertainties of the magnetic field measurements in the spin plane components can realistically be assumed to be on the order of some 10 pT. The spin axis offsets are updated once per orbit by comparison with MMS EDI measurements . The accuracy of this offset determination is on the order of 0.1 nT. Unfortunately, the comparison of EDI and FGM measurements cannot be performed continuously. Hence, the spin axis offsets cannot be dynamically adjusted. As they are likely to drift by 0.2 nT, just as the spin plane offsets do, the absolute uncertainties pertaining to the spin axis components can realistically be assumed to be on the same order of 0.2 to maximally 0.3 nT. Due to the small spacecraft separations, inter-spacecraft and inter-instrument (AFG versus DFG) comparisons allow for further fine tuning in the spin axis offsets. Any additional spin plane or spin axis offsets determined from these data should ideally vanish within the uncertainty limits (spin plane: some 10 pT; spin axis: 0.2 to 0.3 nT). Deviations from 0 above those uncertainty levels are hence indicative of the accuracy of the offset determinations.

Figure 1Sketch of the MMS extended mission phase 3B orbits between 14 November 2017 and 13 March 2018. Locations where MMS surveyed the solar wind in that phase are displayed in yellow. The Sun is to the left.

Long-duration solar wind measurements are obtained by MMS during the extended mission phase, when the MMS orbit apogees are located dayside of the terminator at distances of 25 RE (Earth radii) from the Earth's center. The perigee distances in this phase are just over 1000 km above the ground (see Fig. 1). Fully calibrated MMS 1 FGM survey mode data (AFG measurements) from the dayside extended mission phase 3B (14 November 2017 to 13 March 2018, i.e., 119 days) are used in despun major principal axis (DMPA) coordinates. In this coordinate system, the major principal axis of inertia (i.e., the spin axis) points in the z direction and the spacecraft–Sun vector lies in the xz plane. The DMPA system is closely aligned with the geocentric solar ecliptic (GSE) system, as the spin axis (z) points essentially normal to the ecliptic and the spacecraft–Sun vector almost coincides with the DMPA x axis. The data are available in 16 Hz resolution. Alfvénic fluctuations in the solar wind are of significantly lower frequency. Therefore, the data are resampled to 1 Hz to reduce computational efforts.

The data are subdivided into 171 360 nonoverlapping 1 min intervals. Note that 1 min is a multiple of the spacecraft spin period of 20 s . Solar wind intervals are easily identified by $|\mathbit{B}|<\mathrm{10}$ nT: Ntot= 116 914 intervals fulfill this criterion. For each of these intervals, 3-D offset vector estimates O are determined by minimization of the standard deviation of $|\mathbit{B}-\mathbit{O}|$, a variant of the Davis–Smith method . Offset components Ox, Oy, and Oz are required to be within ±10 nT around 0. This is fulfilled in NO= 68 324 cases. This offset criterion selects intervals containing Alfvénic fluctuations. In these cases, a minimum of the standard deviation of $|\mathbit{B}-\mathbit{O}|$ can be found for small offset corrections O. In contrast, if there are compressional fluctuations, maximizing the offset component in the minimum variance direction will yield the smallest standard deviations of $|\mathbit{B}-\mathbit{O}|$. But then at least one component of O is likely to be found outside ±10 nT, or no convergence is found on any O vector.

For an offset component estimate to be meaningful (e.g., Ox), the magnetic field in that component (e.g., Bx) should be fluctuating during the 1 min interval of interest. Hence, offset components pertaining to intervals are selected for which the standard deviations σ of the respective component of B are larger than a certain threshold σc. The numbers N of intervals selected are shown in Fig. 2a. N(σc= 0) is obviously NO= 68 324 for all x, y, and z components (shown in blue, green, and red, respectively); N decreases if higher threshold values σc are used. This decrease is not exactly the same for all components. Apparently, magnetic field fluctuations in Bx are slightly weaker than in the other components so that ${N}_{x}<{N}_{y}<{N}_{z}$ for any given σc≠0. Note that the DMPA x component corresponds to the radial direction to the Sun, which has previously been reported to feature lower levels of fluctuations (Belcher1973).

Figure 2(a) Numbers N of 1 min intervals selected when using the threshold σc on component standard deviations σ in the magnetic field; N pertaining to x, y, and z components shown in blue, green, and red, respectively. (b) Minutes of solar wind data T required to obtain one selected interval as a function of σc, derived from x component N(σc) values.

The numbers N are fractions of all 1 min intervals of solar wind data Ntot, where $|\mathbit{B}|<\mathrm{10}$ nT. Furthermore, using magnetic field data from NASA's OMNI high-resolution data set for the same period of time (14 November 2017 to 13 March 2018), it is possible to obtain the fraction of solar wind with $|\mathbit{B}|<\mathrm{10}$ nT: it is 88.4 %. Therewith, it is possible to compute the number of solar wind measurements (T in minutes) required to obtain one interval featuring an offset estimate within ±10 nT and σ>σc in a magnetic field component:

$\begin{array}{}\text{(2)}& T=\frac{{N}_{\mathrm{tot}}}{\mathrm{0.884}\phantom{\rule{0.125em}{0ex}}{N}_{x}}.\end{array}$

This function T(σc) is shown in Fig. 2b. If σc=0.5 nT, then obtaining one suitable offset estimate in any component requires almost 10 min of solar wind measurements.

Figure 3Offset uncertainties Omax,xy (a) and Omax,z (b) as a function of σc and W. White vertical lines mark σc=0.15 nT in both panels.

Note that the OMNI solar wind data set from NASA's Goddard Space Flight Center (GSFC) is based on measurements by different solar wind monitors (e.g., the Advanced Composition Explorer (ACE) and the Wind spacecraft). These measurements are propagated in time to represent observations at the Earth's bow shock nose. The OMNI data set pertains to and is distributed by the NASA/GSFC Space Physics Data Facility.

The offset estimates from any particular selected interval are almost certainly not accurate, but a sample of those intervals can yield an accurate offset. From W offsets pertaining to one component (x, y, or z) from intervals with σ>σc, a final offset Of can be computed by using the kernel density estimator (KDE) method. From the W offsets (index: i=1…W), a probability density function P can be determined as follows (e.g., Plaschke and Narita2016):

$\begin{array}{}\text{(3)}& P\left(\stackrel{\mathrm{̃}}{O}\right)=\frac{\mathrm{1}}{\sqrt{\mathrm{2}\mathit{\pi }}Wh}\sum _{i=\mathrm{1}}^{W}\mathrm{exp}\left[-\frac{\mathrm{1}}{\mathrm{2}}{\left(\frac{\stackrel{\mathrm{̃}}{O}-{O}_{i}}{h}\right)}^{\mathrm{2}}\right].\end{array}$

The parameter h is a bandwidth set to 1 nT. Then ${O}_{\mathrm{f}}=\stackrel{\mathrm{̃}}{O}$ where P maximizes.

For σc=0…0.5 nT, W=10… 10 000 offsets from each component are randomly selected 1000 times from the available N(σc) samples. Hence, for each combination of σc and W, 1000 estimates (index j=1…1000) of ${O}_{{x}_{\mathrm{f}j}}$, ${O}_{{y}_{\mathrm{f}j}}$, and ${O}_{{z}_{\mathrm{f}j}}$ are computed. The maximum offsets (deviations from 0) pertaining to the spin plane (x and y) and spin axis (z) components are stored separately.

$\begin{array}{}\text{(4)}& & {O}_{\mathrm{max},xy}\left({\mathit{\sigma }}_{\mathrm{c}},W\right)=max\left(|{O}_{{a}_{\mathrm{f}j}}|\phantom{\rule{0.125em}{0ex}}:\phantom{\rule{0.125em}{0ex}}a\in \mathit{\left\{}x,y\mathit{\right\}},\phantom{\rule{0.125em}{0ex}}j=\mathrm{1}\mathrm{\dots }\mathrm{1000}\right)\text{(5)}& & {O}_{\mathrm{max},z}\left({\mathit{\sigma }}_{\mathrm{c}},W\right)=max\left(|{O}_{{z}_{\mathrm{f}j}}|\phantom{\rule{0.125em}{0ex}}:\phantom{\rule{0.125em}{0ex}}j=\mathrm{1}\mathrm{\dots }\mathrm{1000}\right)\end{array}$

These are the upper-limit estimates of the uncertainty in offset determination in the spin plane and spin axis components. The values of Omax,xy and Omax,z are displayed in Fig. 3; they top out at 2 nT. The minima of Omax,xy and Omax,z are 0.05 and 0.12 nT, respectively. Unsurprisingly, larger sample sizes W of offset estimates yield more accurate offsets, i.e., lower uncertainties Omax. Furthermore, for constant W, both Omax,xy and Omax,z decrease if σc is increased. No improvement in Omax,xy is apparent, however, for σc>0.15 nT. This limit is marked by white vertical lines in both panels of Fig. 3.

The more offset estimates W from 1 min intervals are used, the more solar wind measurements are required to obtain them in the first place. Multiplying T(σc) by W yields the minimum solar wind measurement time. It is displayed in Fig. 4. The minimum TW is just over 19 min (for σc=0 nT and W=10) and the maximum TW in the figure, just over 1600 h, pertains to σc=0.5 nT and W=10 000.

Figure 4Required solar wind measurement time T(σc) W to obtain W offset estimates from intervals with σ>σc, from which final offsets may be computed in all components.

From the data underlying both figures it is possible to find the lowest required solar wind measurement times TW for given ${O}_{\mathrm{max}}\le {O}_{\mathrm{max},\mathrm{c}}$ as follows: find all combinations of σc and W in Fig. 3 (panel a or b) fulfilling ${O}_{\mathrm{max}}\le {O}_{\mathrm{max},\mathrm{c}}$. From those combinations, identify the one associated with the lowest time TW in Fig. 4. The identified parameters σc and W, as well as the minimum times TW, are shown for different limits Omax,c applied to Omax,xy (Fig. 3a) in Table 1. Here, Omax,c is a threshold value for the uncertainty in the offset determination.

Table 1Optimal parameters σc and W as well as minimum solar wind measurement times TW to achieve offset uncertainties $\le {O}_{\mathrm{max},\mathrm{c}}$ based on Omax,xy values shown in Fig. 3a.

3 Discussion and conclusions

As can be seen in Table 1, σc=0.15 nT seems to be an optimal choice. This is already visible in Fig. 3a, where Omax,xy values appear to stay relatively constant for σc≥0.15 nT but are noticeably larger for lower threshold values. For σc=0.15 nT, Omax,xy and Omax,z values are shown as a function of W or, alternatively, TW in Fig. 5 in blue and red, respectively. They are cuts of Fig. 3a and b along the vertical white lines.

Figure 5 shows that the determination of DMPA z component offsets with a certain accuracy (>0.3 nT) requires fewer solar wind data than the determination of x or y component offsets. The reason might be the use of Alfvénic, i.e., transverse, fluctuations of the solar wind magnetic field. As that field is typically lying in the ecliptic xy plane (Parker spiral interplanetary magnetic field), the transverse fluctuations will be most apparent in the normal z component (Belcher1973). Accuracies better than 0.2 nT are, however, easier to achieve for the spin plane x and y components due to the 0.2 to 0.3 nT uncertainty and variability in the MMS spin axis offsets. Note that the σc=0.15 nT limit does not seem to be related to this uncertainty, as it is more visible in the Omax,xy than in the Omax,z results presented in Fig. 3a and b, respectively. Instead, it may be hypothesized that the optimal choice of σc is rather related to typical fluctuation amplitudes of the solar wind magnetic field.

Figure 5Offset uncertainties Omax,xy (blue) and Omax,z (red) as a function of W or TW for fixed σc=0.15 nT.

Values of ${O}_{\mathrm{max},xy}\ge \mathrm{0.1}$ nT should be unaffected by x and y component calibration uncertainties of some 10 pT. Hence, with the help of Table 1 and Fig. 5 (blue line), both showing Omax,xy results, it is possible to give an answer to the question posed in the Introduction section: how many solar wind data are needed to obtain all three components of the offset vector with a certain accuracy?

Offset determinations with uncertainties better than ${O}_{\mathrm{max},\mathrm{c}}=\mathrm{0.2}$ nT are possible based on less than 2 days (41.3 h) of solar wind measurements. If only 20.2 h of data are available, then offsets may be determined more accurately than ${O}_{\mathrm{max},\mathrm{c}}=\mathrm{0.3}$ nT. Ensuring uncertainties to be on the order of or less than ${O}_{\mathrm{max},\mathrm{c}}=\mathrm{0.1}$ nT, however, may require prohibitively long solar wind measurement intervals of several hundred hours, over which the instrument offsets and spacecraft fields at the magnetometer sensor need to stay constant to within Omax,c. Otherwise, intrinsic offset drifts and field variations over time would limit the attainable accuracy irrespective of the amount of solar wind data used.

Although MMS 1 data are used as a high-quality standard to ascertain the accuracy of the offset determination with the outlined method, the results shown in Table 1 and Fig. 5 (blue line) should not be MMS specific. They should be applicable to any magnetometer–spacecraft configuration, as long as spacecraft-generated magnetic field variations within 1 min intervals are of significantly lower amplitude in the magnetometer data than the natural magnetic field variations of solar wind Alfvénic fluctuations. Those spacecraft-generated field variations may be sufficiently reduced by making use of double-sensor gradiometer measurements.

Data availability
Data availability.

MMS FGM level 2 survey data are publicly available at https://lasp.colorado.edu/mms/sdc/public (last access: 30 January 2019). OMNI high-resolution solar wind data are publicly available at https://omniweb.gsfc.nasa.gov (last access: 30 January 2019).

Competing interests
Competing interests.

The author declares that there is no conflict of interest.

Acknowledgements
Acknowledgements.

The dedication and expertise of the Magnetospheric Multiscale (MMS) development and operations teams are greatly appreciated. The use of level 2 survey fluxgate magnetometer (FGM) data is acknowledged. The use of the NASA/GSFC Space Physics Data Facility OMNIWeb service and OMNI data is acknowledged.

Financial support
Financial support.

This work is financially supported by the Austrian Space Applications Programme (ASAP) at the Austrian Research Promotion Agency (FFG), ASAP 14 MERMAG-4, under contract 865967.

Review statement
Review statement.

This paper was edited by Marina Díaz-Michelena and reviewed by two anonymous referees.

References

Auster, H. U., Glassmeier, K. H., Magnes, W., Aydogar, O., Baumjohann, W., Constantinescu, D., Fischer, D., Fornaçon, K. H., Georgescu, E., Harvey, P., Hillenmaier, O., Kroth, R., Ludlam, M., Narita, Y., Nakamura, R., Okrafka, K., Plaschke, F., Richter, I., Schwarzl, H., Stoll, B., Valavanoglou, A., and Wiedemann, M.: The THEMIS Fluxgate Magnetometer, Space Sci. Rev., 141, 235–264, https://doi.org/10.1007/s11214-008-9365-9, 2008. a

Balogh, A., Carr, C. M., Acuña, M. H., Dunlop, M. W., Beek, T. J., Brown, P., Fornacon, K.-H., Georgescu, E., Glassmeier, K.-H., Harris, J., Musmann, G., Oddy, T., and Schwingenschuh, K.: The Cluster Magnetic Field Investigation: overview of in-flight performance and initial results, Ann. Geophys., 19, 1207–1217, https://doi.org/10.5194/angeo-19-1207-2001, 2001. a

Belcher, J. W.: A variation of the Davis-Smith method for in-flight determination of spacecraft magnetic fields, J. Geophys. Res., 78, 6480–6490, https://doi.org/10.1029/JA078i028p06480, 1973. a, b, c, d

Blake, J. B., Mauk, B. H., Baker, D. N., Carranza, P., Clemmons, J. H., Craft, J., Crain, W. R., Crew, A., Dotan, Y., Fennell, J. F., Friedel, R. H., Friesen, L. M., Fuentes, F., Galvan, R., Ibscher, C., Jaynes, A., Katz, N., Lalic, M., Lin, A. Y., Mabry, D. M., Nguyen, T., Pancratz, C., Redding, M., Reeves, G. D., Smith, S., Spence, H. E., and Westlake, J.: The Fly's Eye Energetic Particle Spectrometer (FEEPS) Sensors for the Magnetospheric Multiscale (MMS) Mission, Space Sci. Rev., 199, 309–329, https://doi.org/10.1007/s11214-015-0163-x, 2016. a

Bromund, K. R., Plaschke, F., Strangeway, R. J., Anderson, B. J., Huang, B. G., Magnes, W., Fischer, D., Nakamura, R., Leinweber, H. K., Russell, C. T., Baumjohann, W., Chutter, M., Torbert, R. B., Le, G., Slavin, J. A., and Kepko, L.: In-Flight Calibration Methods for Temperature-Dependendent Offsets in the MMS Fluxgate Magnetometers, in: AGU Fall Meeting Abstracts, SM21A–2455, 2016. a

Burch, J. L., Moore, T. E., Torbert, R. B., and Giles, B. L.: Magnetospheric Multiscale Overview and Science Objectives, Space Sci. Rev., 199, 5–21, https://doi.org/10.1007/s11214-015-0164-9, 2016. a

Davis, L. and Smith, E. J.: The in-flight determination of spacecraft field zeros, EOS Trans. AGU, 49, 57–375, https://doi.org/10.1029/TR049i001p00057, 1968. a

Ergun, R. E., Tucker, S., Westfall, J., Goodrich, K. A., Malaspina, D. M., Summers, D., Wallace, J., Karlsson, M., Mack, J., Brennan, N., Pyke, B., Withnell, P., Torbert, R., Macri, J., Rau, D., Dors, I., Needell, J., Lindqvist, P.-A., Olsson, G., and Cully, C. M.: The Axial Double Probe and Fields Signal Processing for the MMS Mission, Space Sci. Rev., 199, 167–188, https://doi.org/10.1007/s11214-014-0115-x, 2016. a

Farrell, W. M., Thompson, R. F., Lepping, R. P., and Byrnes, J. B.: A method of calibrating magnetometers on a spinning spacecraft, IEEE T. Magn., 31, 966–972, https://doi.org/10.1109/20.364770, 1995. a

Fornaçon, K.-H., Auster, H. U., Georgescu, E., Baumjohann, W., Glassmeier, K.-H., Haerendel, G., Rustenbach, J., and Dunlop, M.: The magnetic field experiment onboard Equator-S and its scientific possibilities, Ann. Geophys., 17, 1521–1527, https://doi.org/10.1007/s00585-999-1521-3, 1999. a

Georgescu, E., Vaith, H., Fornaçon, K.-H., Auster, U., Balogh, A., Carr, C., Chutter, M., Dunlop, M., Foerster, M., Glassmeier, K.-H., Gloag, J., Paschmann, G., Quinn, J., and Torbert, R.: Use of EDI time-of-flight data for FGM calibration check on CLUSTER, in: Cluster and Double Star Symposium, vol. 598, ESA Special Publicatio, 2006. a

Goetz, C., Koenders, C., Hansen, K. C., Burch, J., Carr, C., Eriksson, A., Frühauff, D., Güttler, C., Henri, P., Nilsson, H., Richter, I., Rubin, M., Sierks, H., Tsurutani, B., Volwerk, M., and Glassmeier, K. H.: Structure and evolution of the diamagnetic cavity at comet 67P/Churyumov-Gerasimenko, Mon. Not. R. Astron. Soc., 462, S459–S467, https://doi.org/10.1093/mnras/stw3148, 2016a. a

Goetz, C., Koenders, C., Richter, I., Altwegg, K., Burch, J., Carr, C., Cupido, E., Eriksson, A., Güttler, C., Henri, P., Mokashi, P., Nemeth, Z., Nilsson, H., Rubin, M., Sierks, H., Tsurutani, B., Vallat, C., Volwerk, M., and Glassmeier, K.-H.: First detection of a diamagnetic cavity at comet 67P/Churyumov-Gerasimenko, Astron. Astrophys., 588, A24, https://doi.org/10.1051/0004-6361/201527728, 2016b. a

Hedgecock, P. C.: A correlation technique for magnetometer zero level determination, Space Sci. Instrum., 1, 83–90, 1975. a

Kepko, E. L., Khurana, K. K., Kivelson, M. G., Elphic, R. C., and Russell, C. T.: Accurate determination of magnetic field gradients from four point vector measurements, I. Use of natural constraints on vector data obtained from a single spinning spacecraft, IEEE T. Magn., 32, 377–385, https://doi.org/10.1109/20.486522, 1996. a

King, J. H. and Papitashvili, N. E.: Solar wind spatial scales in and comparisons of hourly Wind and ACE plasma and magnetic field data, J. Geophys. Res., 110, A02104, https://doi.org/10.1029/2004JA010649, 2005. a

Le Contel, O., Leroy, P., Roux, A., Coillot, C., Alison, D., Bouabdellah, A., Mirioni, L., Meslier, L., Galic, A., Vassal, M. C., Torbert, R. B., Needell, J., Rau, D., Dors, I., Ergun, R. E., Westfall, J., Summers, D., Wallace, J., Magnes, W., Valavanoglou, A., Olsson, G., Chutter, M., Macri, J., Myers, S., Turco, S., Nolin, J., Bodet, D., Rowe, K., Tanguy, M., and de la Porte, B.: The Search-Coil Magnetometer for MMS, Space Sci. Rev., 199, 257–282, https://doi.org/10.1007/s11214-014-0096-9, 2016. a

Leinweber, H. K., Russell, C. T., Torkar, K., Zhang, T. L., and Angelopoulos, V.: An advanced approach to finding magnetometer zero levels in the interplanetary magnetic field, Meas. Sci. Technol., 19, 055104, https://doi.org/10.1088/0957-0233/19/5/055104, 2008. a

Lindqvist, P.-A., Olsson, G., Torbert, R. B., King, B., Granoff, M., Rau, D., Needell, G., Turco, S., Dors, I., Beckman, P., Macri, J., Frost, C., Salwen, J., Eriksson, A., Åhlén, L., Khotyaintsev, Y. V., Porter, J., Lappalainen, K., Ergun, R. E., Wermeer, W., and Tucker, S.: The Spin-Plane Double Probe Electric Field Instrument for MMS, Space Sci. Rev., 199, 137–165, https://doi.org/10.1007/s11214-014-0116-9, 2016. a

Mauk, B. H., Blake, J. B., Baker, D. N., Clemmons, J. H., Reeves, G. D., Spence, H. E., Jaskulek, S. E., Schlemm, C. E., Brown, L. E., Cooper, S. A., Craft, J. V., Fennell, J. F., Gurnee, R. S., Hammock, C. M., Hayes, J. R., Hill, P. A., Ho, G. C., Hutcheson, J. C., Jacques, A. D., Kerem, S., Mitchell, D. G., Nelson, K. S., Paschalidis, N. P., Rossano, E., Stokes, M. R., and Westlake, J. H.: The Energetic Particle Detector (EPD) Investigation and the Energetic Ion Spectrometer (EIS) for the Magnetospheric Multiscale (MMS) Mission, Space Sci. Rev., 199, 471–514, https://doi.org/10.1007/s11214-014-0055-5, 2016. a

Nakamura, R., Plaschke, F., Teubenbacher, R., Giner, L., Baumjohann, W., Magnes, W., Steller, M., Torbert, R. B., Vaith, H., Chutter, M., Fornaçon, K.-H., Glassmeier, K.-H., and Carr, C.: Interinstrument calibration using magnetic field data from the flux-gate magnetometer (FGM) and electron drift instrument (EDI) onboard Cluster, Geosci. Instrum. Method. Data Syst., 3, 1–11, https://doi.org/10.5194/gi-3-1-2014, 2014. a

Plaschke, F. and Narita, Y.: On determining fluxgate magnetometer spin axis offsets from mirror mode observations, Ann. Geophys., 34, 759–766, https://doi.org/10.5194/angeo-34-759-2016, 2016. a, b

Plaschke, F., Nakamura, R., Leinweber, H. K., Chutter, M., Vaith, H., Baumjohann, W., Steller, M., and Magnes, W.: Flux-gate magnetometer spin axis offset calibration using the electron drift instrument, Meas. Sci. Technol., 25, 105008, https://doi.org/10.1088/0957-0233/25/10/105008, 2014. a, b

Plaschke, F., Goetz, C., Volwerk, M., Richter, I., Frühauff, D., Narita, Y., Glassmeier, K.-H., and Dougherty, M. K.: Fluxgate magnetometer offset vector determination by the 3D mirror mode method, Mon. Not. R. Astron. Soc., 469, S675–S684, https://doi.org/10.1093/mnras/stx2532, 2017. a

Plaschke, F., Auster, H.-U., Fischer, D., Fornaçon, K.-H., Magnes, W., Richter, I., Constantinescu, D., and Narita, Y.: Advanced calibration of magnetometers on spin-stabilized spacecraft based on parameter decoupling, Geosci. Instrum. Method. Data Syst., 8, 63–76, https://doi.org/10.5194/gi-8-63-2019, 2019. a, b

Pollock, C., Moore, T., Jacques, A., Burch, J., Gliese, U., Saito, Y., Omoto, T., Avanov, L., Barrie, A., Coffey, V., Dorelli, J., Gershman, D., Giles, B., Rosnack, T., Salo, C., Yokota, S., Adrian, M., Aoustin, C., Auletti, C., Aung, S., Bigio, V., Cao, N., Chandler, M., Chornay, D., Christian, K., Clark, G., Collinson, G., Corris, T., De Los Santos, A., Devlin, R., Diaz, T., Dickerson, T., Dickson, C., Diekmann, A., Diggs, F., Duncan, C., Figueroa-Vinas, A., Firman, C., Freeman, M., Galassi, N., Garcia, K., Goodhart, G., Guererro, D., Hageman, J., Hanley, J., Hemminger, E., Holland, M., Hutchins, M., James, T., Jones, W., Kreisler, S., Kujawski, J., Lavu, V., Lobell, J., LeCompte, E., Lukemire, A., MacDonald, E., Mariano, A., Mukai, T., Narayanan, K., Nguyan, Q., Onizuka, M., Paterson, W., Persyn, S., Piepgrass, B., Cheney, F., Rager, A., Raghuram, T., Ramil, A., Reichenthal, L., Rodriguez, H., Rouzaud, J., Rucker, A., Saito, Y., Samara, M., Sauvaud, J.-A., Schuster, D., Shappirio, M., Shelton, K., Sher, D., Smith, D., Smith, K., Smith, S., Steinfeld, D., Szymkiewicz, R., Tanimoto, K., Taylor, J., Tucker, C., Tull, K., Uhl, A., Vloet, J., Walpole, P., Weidner, S., White, D., Winkert, G., Yeh, P.-S., and Zeuch, M.: Fast Plasma Investigation for Magnetospheric Multiscale, Space Sci. Rev., 199, 331–406, https://doi.org/10.1007/s11214-016-0245-4, 2016. a

Russell, C. T., Anderson, B. J., Baumjohann, W., Bromund, K. R., Dearborn, D., Fischer, D., Le, G., Leinweber, H. K., Leneman, D., Magnes, W., Means, J. D., Moldwin, M. B., Nakamura, R., Pierce, D., Plaschke, F., Rowe, K. M., Slavin, J. A., Strangeway, R. J., Torbert, R., Hagen, C., Jernej, I., Valavanoglou, A., and Richter, I.: The Magnetospheric Multiscale Magnetometers, Space Sci. Rev., 199, 189–256, https://doi.org/10.1007/s11214-014-0057-3, 2016.  a, b

Thébault, E., Finlay, C. C., Beggan, C. D., Alken, P., Aubert, J., Barrois, O., Bertrand, F., Bondar, T., Boness, A., Brocco, L., Canet, E., Chambodut, A., Chulliat, A., Coïsson, P., Civet, F., Du, A., Fournier, A., Fratter, I., Gillet, N., Hamilton, B., Hamoudi, M., Hulot, G., Jager, T., Korte, M., Kuang, W., Lalanne, X., Langlais, B., Léger, J.-M., Lesur, V., Lowes, F. J., Macmillan, S., Mandea, M., Manoj, C., Maus, S., Olsen, N., Petrov, V., Ridley, V., Rother, M., Sabaka, T. J., Saturnino, D., Schachtschneider, R., Sirol, O., Tangborn, A., Thomson, A., Tøffner-Clausen, L., Vigneron, P., Wardinski, I., and Zvereva, T.: International Geomagnetic Reference Field: the 12th generation, Earth Planets Space, 67, 79, https://doi.org/10.1186/s40623-015-0228-9, 2015. a

Tooley, C. R., Black, R. K., Robertson, B. P., Stone, J. M., Pope, S. E., and Davis, G. T.: The Magnetospheric Multiscale Constellation, Space Sci. Rev., 199, 23–76, https://doi.org/10.1007/s11214-015-0220-5, 2016. a

Torbert, R. B., Russell, C. T., Magnes, W., Ergun, R. E., Lindqvist, P.-A., Le Contel, O., Vaith, H., Macri, J., Myers, S., Rau, D., Needell, J., King, B., Granoff, M., Chutter, M., Dors, I., Olsson, G., Khotyaintsev, Y. V., Eriksson, A., Kletzing, C. A., Bounds, S., Anderson, B., Baumjohann, W., Steller, M., Bromund, K., Le, G., Nakamura, R., Strangeway, R. J., Leinweber, H. K., Tucker, S., Westfall, J., Fischer, D., Plaschke, F., Porter, J., and Lappalainen, K.: The FIELDS Instrument Suite on MMS: Scientific Objectives, Measurements, and Data Products, Space Sci. Rev., 199, 105–135, https://doi.org/10.1007/s11214-014-0109-8, 2016a. a, b

Torbert, R. B., Vaith, H., Granoff, M., Widholm, M., Gaidos, J. A., Briggs, B. H., Dors, I. G., Chutter, M. W., Macri, J., Argall, M., Bodet, D., Needell, J., Steller, M. B., Baumjohann, W., Nakamura, R., Plaschke, F., Ottacher, H., Hasiba, J., Hofmann, K., Kletzing, C. A., Bounds, S. R., Dvorsky, R. T., Sigsbee, K., and Kooi, V.: The Electron Drift Instrument for MMS, Space Sci. Rev., 199, 283–305, https://doi.org/10.1007/s11214-015-0182-7, 2016b. a, b

Torkar, K., Nakamura, R., Tajmar, M., Scharlemann, C., Jeszenszky, H., Laky, G., Fremuth, G., Escoubet, C. P., and Svenes, K.: Active Spacecraft Potential Control Investigation, Space Sci. Rev., 199, 515–544, https://doi.org/10.1007/s11214-014-0049-3, 2016. a

Young, D. T., Burch, J. L., Gomez, R. G., De Los Santos, A., Miller, G. P., Wilson, P., Paschalidis, N., Fuselier, S. A., Pickens, K., Hertzberg, E., Pollock, C. J., Scherrer, J., Wood, P. B., Donald, E. T., Aaron, D., Furman, J., George, D., Gurnee, R. S., Hourani, R. S., Jacques, A., Johnson, T., Orr, T., Pan, K. S., Persyn, S., Pope, S., Roberts, J., Stokes, M. R., Trattner, K. J., and Webster, J. M.: Hot Plasma Composition Analyzer for the Magnetospheric Multiscale Mission, Space Sci. Rev., 199, 407–470, https://doi.org/10.1007/s11214-014-0119-6, 2016. a


text:
## Deep Learning

Deep learning emerged from a decade’s explosive computational growth as a serious contender in the field. Thus, deep learning is a particular kind of machine learning whose algorithms are inspired by the structure and function of human brain.

### Machine Learning vs Deep Learning

Deep learning is the most powerful machine learning technique these days. It is so powerful because they learn the best way to represent the problem while learning how to solve the problem. A comparison of Deep learning and Machine learning is given below −

### Data Dependency

The first point of difference is based upon the performance of DL and ML when the scale of data increases. When the data is large, deep learning algorithms perform very well.

### Machine Dependency

Deep learning algorithms need high-end machines to work perfectly. On the other hand, machine learning algorithms can work on low-end machines too.

### Feature Extraction

Deep learning algorithms can extract high level features and try to learn from the same too. On the other hand, an expert is required to identify most of the features extracted by machine learning.

### Time of Execution

Execution time depends upon the numerous parameters used in an algorithm. Deep learning has more parameters than machine learning algorithms. Hence, the execution time of DL algorithms, specially the training time, is much more than ML algorithms. But the testing time of DL algorithms is less than ML algorithms.

### Approach to Problem Solving

Deep learning solves the problem end-to-end while machine learning uses the traditional way of solving the problem i.e. by breaking down it into parts.

[wpsbx_html_block id=1891]

### Convolutional Neural Network (CNN)

Convolutional neural networks are the same as ordinary neural networks because they are also made up of neurons that have learnable weights and biases. Ordinary neural networks ignore the structure of input data and all the data is converted into 1-D array before feeding it into the network. This process suits the regular data, however if the data contains images, the process may be cumbersome.

CNN solves this problem easily. It takes the 2D structure of the images into account when they process them, which allows them to extract the properties specific to images. In this way, the main goal of CNNs is to go from the raw image data in the input layer to the correct class in the output layer. The only difference between an ordinary NNs and CNNs is in the treatment of input data and in the type of layers.

### Architecture Overview of CNNs

Architecturally, the ordinary neural networks receive an input and transform it through a series of hidden layer. Every layer is connected to the other layer with the help of neurons. The main disadvantage of ordinary neural networks is that they do not scale well to full images.

The architecture of CNNs have neurons arranged in 3 dimensions called width, height and depth. Each neuron in the current layer is connected to a small patch of the output from the previous layer. It is similar to overlaying a 𝑵×𝑵 filter on the input image. It uses M filters to be sure about getting all the details. These M filters are feature extractors which extract features like edges, corners, etc.

### Layers used to construct CNNs

Following layers are used to construct CNNs −

• Input Layer − It takes the raw image data as it is.
• Convolutional Layer − This layer is the core building block of CNNs that does most of the computations. This layer computes the convolutions between the neurons and the various patches in the input.
• Rectified Linear Unit Layer − It applies an activation function to the output of the previous layer. It adds non-linearity to the network so that it can generalize well to any type of function.
• Pooling Layer − Pooling helps us to keep only the important parts as we progress in the network. Pooling layer operates independently on every depth slice of the input and resizes it spatially. It uses the MAX function.
• Fully Connected layer/Output layer − This layer computes the output scores in the last layer. The resulting output is of the size 𝟏×𝟏×𝑳 , where L is the number training dataset classes.

### Installing Useful Python Packages

You can use Keras, which is an high level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK or Theno. It is compatible with Python 2.7-3.6. You can learn more about it from https://keras.io/.

Use the following commands to install keras −

pip install keras

On conda environment, you can use the following command −

conda install –c conda-forge keras

### Building Linear Regressor using ANN

In this section, you will learn how to build a linear regressor using artificial neural networks. You can use KerasRegressor to achieve this. In this example, we are using the Boston house price dataset with 13 numerical for properties in Boston. The Python code for the same is shown here −

Import all the required packages as shown −

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold

Now, load our dataset which is saved in local directory.

dataframe = pandas.read_csv("/Usrrs/admin/data.csv", delim_whitespace = True, header = None)
dataset = dataframe.values

Now, divide the data into input and output variables i.e. X and Y −

X = dataset[:,0:13]
Y = dataset[:,13]

Since we use baseline neural networks, define the model −

def baseline_model():

Now, create the model as follows −

model_regressor = Sequential()
model_regressor.add(Dense(13, input_dim = 13, kernel_initializer = 'normal',
activation = 'relu'))
model_regressor.add(Dense(1, kernel_initializer = 'normal'))

Next, compile the model −

model_regressor.compile(loss='mean_squared_error', optimizer='adam')
return model_regressor

Now, fix the random seed for reproducibility as follows −

seed = 7
numpy.random.seed(seed)

The Keras wrapper object for use in scikit-learn as a regression estimator is called KerasRegressor. In this section, we shall evaluate this model with standardize data set.

estimator = KerasRegressor(build_fn = baseline_model, nb_epoch = 100, batch_size = 5, verbose = 0)
kfold = KFold(n_splits = 10, random_state = seed)
baseline_result = cross_val_score(estimator, X, Y, cv = kfold)
print("Baseline: %.2f (%.2f) MSE" % (Baseline_result.mean(),Baseline_result.std()))

The output of the code shown above would be the estimate of the model’s performance on the problem for unseen data. It will be the mean squared error, including the average and standard deviation across all 10 folds of the cross validation evaluation.

### Image Classifier: An Application of Deep Learning

Convolutional Neural Networks (CNNs) solve an image classification problem, that is to which class the input image belongs to. You can use Keras deep learning library. Note that we are using the training and testing data set of images of cats and dogs from following link https://www.kaggle.com/c/dogs-vs-cats/data.

Import the important keras libraries and packages as shown −

The following package called sequential will initialize the neural networks as sequential network.

from keras.models import Sequential

The following package called Conv2D is used to perform the convolution operation, the first step of CNN.

from keras.layers import Conv2D

The following package called MaxPoling2D is used to perform the pooling operation, the second step of CNN.

from keras.layers import MaxPooling2D

The following package called Flatten is the process of converting all the resultant 2D arrays into a single long continuous linear vector.

from keras.layers import Flatten

The following package called Dense is used to perform the full connection of the neural network, the fourth step of CNN.

from keras.layers import Dense

Now, create an object of the sequential class.

S_classifier = Sequential()

Now, next step is coding the convolution part.

S_classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

Here relu is the rectifier function.

Now, the next step of CNN is the pooling operation on the resultant feature maps after convolution part.

S-classifier.add(MaxPooling2D(pool_size = (2, 2)))

Now, convert all the pooled images into a continuous vector by using flattering −

S_classifier.add(Flatten())

Next, create a fully connected layer.

S_classifier.add(Dense(units = 128, activation = 'relu'))

Here, 128 is the number of hidden units. It is a common practice to define the number of hidden units as the power of 2.

Now, initialize the output layer as follows −

S_classifier.add(Dense(units = 1, activation = 'sigmoid'))

Now, compile the CNN, we have built −

S_classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

Here optimizer parameter is to choose the stochastic gradient descent algorithm, loss parameter is to choose the loss function and metrics parameter is to choose the performance metric.

Now, perform image augmentations and then fit the images to the neural networks −

train_datagen = ImageDataGenerator(rescale = 1./255,shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)

training_set =
(64, 64),batch_size = 32,class_mode = 'binary')

test_set =
test_datagen.flow_from_directory('test_set',target_size =
(64, 64),batch_size = 32,class_mode = 'binary')

Now, fit the data to the model we have created −

classifier.fit_generator(training_set,steps_per_epoch = 8000,epochs =
25,validation_data = test_set,validation_steps = 2000)

Here steps_per_epoch have the number of training images.

Now as the model has been trained, we can use it for prediction as follows −

from keras.preprocessing import image

target_size = (64, 64))

test_image = image.img_to_array(test_image)

test_image = np.expand_dims(test_image, axis = 0)

result = classifier.predict(test_image)

training_set.class_indices

if result[0][0] == 1:
prediction = 'dog'

else:
prediction = 'cat'

## Computer Vision

Computer vision is a discipline that studies how to reconstruct, interrupt and understand a 3d scene from its 2d images, in terms of the properties of the structure present in the scene.

### Computer Vision Hierarchy

Computer vision is divided into three basic categories as following −

• Low-level vision − It includes process image for feature extraction.
• Intermediate-level vision − It includes object recognition and 3D scene interpretation
• High-level vision − It includes conceptual description of a scene like activity, intention and behavior.

### Computer Vision Vs Image Processing

• Image processing studies image to image transformation. The input and output of image processing are both images.
• Computer vision is the construction of explicit, meaningful descriptions of physical objects from their image. The output of computer vision is a description or an interpretation of structures in 3D scene.

### Applications

Computer vision finds applications in the following fields −

Robotics

• Localization-determine robot location automatically
• Obstacles avoidance
• Assembly (peg-in-hole, welding, painting)
• Manipulation (e.g. PUMA robot manipulator)
• Human Robot Interaction (HRI): Intelligent robotics to interact with and serve people

Medicine

• Classification and detection (e.g. lesion or cells classification and tumor detection)
• 2D/3D segmentation
• 3D human organ reconstruction (MRI or ultrasound)
• Vision-guided robotics surgery

Security

• Biometrics (iris, finger print, face recognition)
• Surveillance-detecting certain suspicious activities or behaviors

Transportation

• Autonomous vehicle
• Safety, e.g., driver vigilance monitoring

[wpsbx_html_block id=1891]

Industrial Automation Application

• Industrial inspection (defect detection)
• Assembly
• Barcode and package label reading
• Object sorting
• Document understanding (e.g. OCR)

### Installing Useful Packages

For Computer vision with Python, you can use a popular library called OpenCV (Open Source Computer Vision). It is a library of programming functions mainly aimed at the real-time computer vision. It is written in C++ and its primary interface is in C++. You can install this package with the help of the following command −

pip install opencv_python-X.X-cp36-cp36m-winX.whl

Here X represents the version of Python installed on your machine as well as the win32 or 64 bit you are having.

If you are using the anaconda environment, then use the following command to install OpenCV −

conda install -c conda-forge opencv

### Reading, Writing and Displaying an Image

Most of the CV applications need to get the images as input and produce the images as output. In this section, you will learn how to read and write image file with the help of functions provided by OpenCV.

### OpenCV functions for Reading, Showing, Writing an Image File

OpenCV provides the following functions for this purpose −

• imread() function − This is the function for reading an image. OpenCV imread() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
• imshow() function − This is the function for showing an image in a window. The window automatically fits to the image size. OpenCV imshow() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
• imwrite() function − This is the function for writing an image. OpenCV imwrite() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

### Example

This example shows the Python code for reading an image in one format − showing it in a window and writing the same image in other format. Consider the steps shown below −

Import the OpenCV package as shown −

import cv2

image = cv2.imread('image_flower.jpg')

For showing the image, use the imshow() function. The name of the window in which you can see the image would be image_flower.

cv2.imshow('image_flower',image)
cv2.destroyAllwindows()

Now, we can write the same image into the other format, say .png by using the imwrite() function −

cv2.imwrite('image_flower.png',image)

The output True means that the image has been successfully written as .png file also in the same folder.

True

Note − The function destroyallWindows() simply destroys all the windows we created.

### Color Space Conversion

In OpenCV, the images are not stored by using the conventional RGB color, rather they are stored in the reverse order i.e. in the BGR order. Hence the default color code while reading an image is BGR. The cvtColor() color conversion function in for converting the image from one color code to other.

### Example

Consider this example to convert image from BGR to grayscale.

Import the OpenCV package as shown −

import cv2

image = cv2.imread('image_flower.jpg')

Now, if we see this image using imshow() function, then we can see that this image is in BGR.

cv2.imshow('BGR_Penguins',image)

Now, use cvtColor() function to convert this image to grayscale.

image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_penguins',image)

### Edge Detection

Humans, after seeing a rough sketch, can easily recognize many object types and their poses. That is why edges play an important role in the life of humans as well as in the applications of computer vision. OpenCV provides very simple and useful function called Canny()for detecting the edges.

### Example

The following example shows clear identification of the edges.

Import OpenCV package as shown −

import cv2
import numpy as np

image = cv2.imread('Penguins.jpg')

Now, use the Canny () function for detecting the edges of the already read image.

cv2.imwrite(‘edges_Penguins.jpg’,cv2.Canny(image,200,300))

Now, for showing the image with edges, use the imshow() function.

cv2.imshow(‘edges’, cv2.imread(‘‘edges_Penguins.jpg’))

This Python program will create an image named edges_penguins.jpg with edge detection.

### Face Detection

Face detection is one of the fascinating applications of computer vision which makes it more realistic as well as futuristic. OpenCV has a built-in facility to perform face detection. We are going to use the Haar cascade classifier for face detection.

We need data to use the Haar cascade classifier. You can find this data in our OpenCV package. After installing OpenCv, you can see the folder name haarcascades. There would be .xml files for different application. Now, copy all of them for different use and paste then in a new folder under the current project.

Example

The following is the Python code using Haar Cascade to detect the face of Amitabh Bachan shown in the following image −

Import the OpenCV package as shown −

import cv2
import numpy as np

Now, use the HaarCascadeClassifier for detecting face −

face_detection=
haarcascade_frontalface_default.xml')

img = cv2.imread('AB.jpg')

Now, convert it into grayscale because it would accept gray images −

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Now, using face_detection.detectMultiScale, perform actual face detection

faces = face_detection.detectMultiScale(gray, 1.3, 5)

Now, draw a rectangle around the whole face −

for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,0),3)
cv2.imwrite('Face_AB.jpg',img)

This Python program will create an image named Face_AB.jpg with face detection as shown

### Eye Detection

Eye detection is another fascinating application of computer vision which makes it more realistic as well as futuristic. OpenCV has a built-in facility to perform eye detection. We are going to use the Haar cascade classifier for eye detection.

### Example

The following example gives the Python code using Haar Cascade to detect the face of Amitabh Bachan given in the following image −

Import OpenCV package as shown −

import cv2
import numpy as np

Now, use the HaarCascadeClassifier for detecting face −

eye_cascade = cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/haarcascade_eye.xml')

img = cv2.imread('AB_Eye.jpg')

Now, convert it into grayscale because it would accept grey images −

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Now with the help of eye_cascade.detectMultiScale, perform actual face detection

eyes = eye_cascade.detectMultiScale(gray, 1.03, 5)

Now, draw a rectangle around the whole face −

for (ex,ey,ew,eh) in eyes:
img = cv2.rectangle(img,(ex,ey),(ex+ew, ey+eh),(0,255,0),2)
cv2.imwrite('Eye_AB.jpg',img)

This Python program will create an image named Eye_AB.jpg with eye detection as shown −

## Reinforcement Learning

This type of learning is used to reinforce or strengthen the network based on critic information. That is, a network being trained under reinforcement learning, receives some feedback from the environment. However, the feedback is evaluative and not instructive as in the case of supervised learning. Based on this feedback, the network performs the adjustments of the weights to obtain better critic information in future. This learning process is similar to supervised learning but we might have very less information. The following figure gives the block diagram of reinforcement learning.

### Building Blocks: Environment and Agent

Environment and Agent are main building blocks of reinforcement learning in AI. This section discusses them in detail −

### Agent

An agent is anything that can perceive its environment through sensors and acts upon that environment through effectors.

• human agent has sensory organs such as eyes, ears, nose, tongue and skin parallel to the sensors, and other organs such as hands, legs, mouth, for effectors.
• robotic agent replaces cameras and infrared range finders for the sensors, and various motors and actuators for effectors.
• software agent has encoded bit strings as its programs and actions.

### Agent Terminology

The following terms are more frequently used in reinforcement learning in AI −

• Performance Measure of Agent − It is the criteria, which determines how successful an agent is.
• Behavior of Agent − It is the action that agent performs after any given sequence of percepts.
• Percept − It is agent’s perceptual inputs at a given instance.
• Percept Sequence − It is the history of all that an agent has perceived till date.
• Agent Function − It is a map from the precept sequence to an action.

### Environment

• Some programs operate in an entirely artificial environment confined to keyboard input, database, computer file systems and character output on a screen.
• In contrast, some software agents, such as software robots or softbots, exist in rich and unlimited softbot domains. The simulator has a very detailed, and complex environment. The software agent needs to choose from a long array of actions in real time.
• For example, a softbot designed to scan the online preferences of the customer and display interesting items to the customer works in the real as well as an artificial environment.

### Properties of Environment

The environment has multifold properties as discussed below −

• Discrete/Continuous − If there are a limited number of distinct, clearly defined, states of the environment, the environment is discrete , otherwise it is continuous. For example, chess is a discrete environment and driving is a continuous environment.
• Observable/Partially Observable − If it is possible to determine the complete state of the environment at each time point from the percepts, it is observable; otherwise it is only partially observable.
• Static/Dynamic − If the environment does not change while an agent is acting, then it is static; otherwise it is dynamic.
• Single agent/Multiple agents − The environment may contain other agents which may be of the same or different kind as that of the agent.
• Accessible/Inaccessible − If the agent’s sensory apparatus can have access to the complete state of the environment, then the environment is accessible to that agent; otherwise it is inaccessible.
• Deterministic/Non-deterministic − If the next state of the environment is completely determined by the current state and the actions of the agent, then the environment is deterministic; otherwise it is non-deterministic.
• Episodic/Non-episodic − In an episodic environment, each episode consists of the agent perceiving and then acting. The quality of its action depends just on the episode itself. Subsequent episodes do not depend on the actions in the previous episodes. Episodic environments are much simpler because the agent does not need to think ahead.

### Constructing an Environment with Python

For building reinforcement learning agent, we will be using the OpenAI Gym package which can be installed with the help of the following command −

pip install gym

There are various environments in OpenAI gym which can be used for various purposes. Few of them are Cartpole-v0, Hopper-v1, and MsPacman-v0. They require different engines. The detail documentation of OpenAI Gym can be found on https://gym.openai.com/docs/#environments.

The following code shows an example of Python code for cartpole-v0 environment −

import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
env.render()
env.step(env.action_space.sample())

You can construct other environments in a similar way.

### Constructing a learning agent with Python

For building reinforcement learning agent, we will be using the OpenAI Gym package as shown −

import gym
env = gym.make('CartPole-v0')
for _ in range(20):
observation = env.reset()
for i in range(100):
env.render()
print(observation)
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
print("Episode finished after {} timesteps".format(i+1))
break

Observe that the cartpole can balance itself.

[wpsbx_html_block id=1891]

## Unsupervised Learning Clustering

Unsupervised machine learning algorithms do not have any supervisor to provide any sort of guidance. That is why they are closely aligned with what some call true artificial intelligence. In unsupervised learning, there would be no correct answer and no teacher for the guidance. Algorithms need to discover the interesting pattern in data for learning.

### Clustering:

Basically, it is a type of unsupervised learning method and a common technique for statistical data analysis used in many fields. Clustering mainly is a task of dividing the set of observations into subsets, called clusters, in such a way that observations in the same cluster are similar in one sense and they are dissimilar to the observations in other clusters. In simple words, we can say that the main goal of clustering is to group the data on the basis of similarity and dissimilarity.

For example, the following diagram shows similar kind of data in different clusters −

### Algorithms for Clustering the Data

Following are a few common algorithms for clustering the data −

### K-Means algorithm

K-means clustering algorithm is one of the well-known algorithms for clustering the data. We need to assume that the numbers of clusters are already known. This is also called flat clustering. It is an iterative clustering algorithm. The steps given below need to be followed for this algorithm:

Step 1 − We need to specify the desired number of K subgroups.

Step 2 − Fix the number of clusters and randomly assign each data point to a cluster. Or in other words we need to classify our data based on the number of clusters.

In this step, cluster centroids should be computed.

As this is an iterative algorithm, we need to update the locations of K centroids with every iteration until we find the global optima or in other words the centroids reach at their optimal locations.

The following code will help in implementing K-means clustering algorithm in Python. We are going to use the Scikit-learn module.

[wpsbx_html_block id=1891]

Let us import the necessary packages −

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans

The following line of code will help in generating the two-dimensional dataset, containing four blobs, by using make_blob from the sklearn.dataset package.

from sklearn.datasets.samples_generator import make_blobs

X, y_true = make_blobs(n_samples = 500, centers = 4,
cluster_std = 0.40, random_state = 0)

We can visualize the dataset by using the following code −

plt.scatter(X[:, 0], X[:, 1], s = 50);
plt.show()

Here, we are initializing kmeans to be the KMeans algorithm, with the required parameter of how many clusters (n_clusters).

kmeans = KMeans(n_clusters = 4)

We need to train the K-means model with the input data.

kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c = y_kmeans, s = 50, cmap = 'viridis')

centers = kmeans.cluster_centers_

The code given below will help us plot and visualize the machine’s findings based on our data, and the fitment according to the number of clusters that are to be found.

plt.scatter(centers[:, 0], centers[:, 1], c = 'black', s = 200, alpha = 0.5);
plt.show()

### Mean Shift Algorithm

It is another popular and powerful clustering algorithm used in unsupervised learning. It does not make any assumptions hence it is a non-parametric algorithm. It is also called hierarchical clustering or mean shift cluster analysis. Followings would be the basic steps of this algorithm −

• First of all, we need to start with the data points assigned to a cluster of their own.
• Now, it computes the centroids and update the location of new centroids.
• By repeating this process, we move closer the peak of cluster i.e. towards the region of higher density.
• This algorithm stops at the stage where centroids do not move anymore.

With the help of following code we are implementing Mean Shift clustering algorithm in Python. We are going to use Scikit-learn module.

Let us import the necessary packages −

import numpy as np
from sklearn.cluster import MeanShift
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")

The following code will help in generating the two-dimensional dataset, containing four blobs, by using make_blob from the sklearn.dataset package.

from sklearn.datasets.samples_generator import make_blobs

We can visualize the dataset with the following code

centers = [[2,2],[4,5],[3,10]]
X, _ = make_blobs(n_samples = 500, centers = centers, cluster_std = 1)
plt.scatter(X[:,0],X[:,1])
plt.show()

Now, we need to train the Mean Shift cluster model with the input data.

ms = MeanShift()
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_

The following code will print the cluster centers and the expected number of cluster as per the input data −

print(cluster_centers)
n_clusters_ = len(np.unique(labels))
print("Estimated clusters:", n_clusters_)
[[ 3.23005036 3.84771893]
[ 3.02057451 9.88928991]]
Estimated clusters: 2

The code given below will help plot and visualize the machine’s findings based on our data, and the fitment according to the number of clusters that are to be found.

colors = 10*['r.','g.','b.','c.','k.','y.','m.']
for i in range(len(X)):
plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize = 10)
plt.scatter(cluster_centers[:,0],cluster_centers[:,1],
marker = "x",color = 'k', s = 150, linewidths = 5, zorder = 10)
plt.show()

### Measuring the Clustering Performance

The real world data is not naturally organized into number of distinctive clusters. Due to this reason, it is not easy to visualize and draw inferences. That is why we need to measure the clustering performance as well as its quality. It can be done with the help of silhouette analysis.

### Silhouette Analysis

This method can be used to check the quality of clustering by measuring the distance between the clusters. Basically, it provides a way to assess the parameters like number of clusters by giving a silhouette score. This score is a metric that measures how close each point in one cluster is to the points in the neighboring clusters.

### Analysis of silhouette score

The score has a range of [-1, 1]. Following is the analysis of this score −

• Score of +1 − Score near +1 indicates that the sample is far away from the neighboring cluster.
• Score of 0 − Score 0 indicates that the sample is on or very close to the decision boundary between two neighboring clusters.
• Score of -1 − Negative score indicates that the samples have been assigned to the wrong clusters.

### Calculating Silhouette Score

In this section, we will learn how to calculate the silhouette score.

Silhouette score can be calculated by using the following formula −

$$silhouette score = \frac{\left ( p-q \right )}{max\left ( p,q \right )}$$

Here, 𝑝 is the mean distance to the points in the nearest cluster that the data point is not a part of. And, 𝑞 is the mean intra-cluster distance to all the points in its own cluster.

For finding the optimal number of clusters, we need to run the clustering algorithm again by importing the metrics module from the sklearn package. In the following example, we will run the K-means clustering algorithm to find the optimal number of clusters −

Import the necessary packages as shown −

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans

With the help of the following code, we will generate the two-dimensional dataset, containing four blobs, by using make_blob from the sklearn.dataset package.

from sklearn.datasets.samples_generator import make_blobs

X, y_true = make_blobs(n_samples = 500, centers = 4, cluster_std = 0.40, random_state = 0)

Initialize the variables as shown −

scores = []
values = np.arange(2, 10)

We need to iterate the K-means model through all the values and also need to train it with the input data.

for num_clusters in values:
kmeans = KMeans(init = 'k-means++', n_clusters = num_clusters, n_init = 10)
kmeans.fit(X)

Now, estimate the silhouette score for the current clustering model using the Euclidean distance metric −

score = metrics.silhouette_score(X, kmeans.labels_,
metric = 'euclidean', sample_size = len(X))

The following line of code will help in displaying the number of clusters as well as Silhouette score.

print("\nNumber of clusters =", num_clusters)
print("Silhouette score =", score)
scores.append(score)

You will receive the following output −

Number of clusters = 9
Silhouette score = 0.340391138371

num_clusters = np.argmax(scores) + values[0]
print('\nOptimal number of clusters =', num_clusters)

Now, the output for optimal number of clusters would be as follows −

Optimal number of clusters = 2

### Finding Nearest Neighbors

The concept of finding nearest neighbors may be defined as the process of finding the closest point to the input point from the given dataset. The main use of this KNN)K-nearest neighbors) algorithm is to build classification systems that classify a data point on the proximity of the input data point to various classes.

The Python code given below helps in finding the K-nearest neighbors of a given data set −

Import the necessary packages as shown below. Here, we are using the NearestNeighbors module from the sklearn package

import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestNeighbors

Let us now define the input data −

A = np.array([[3.1, 2.3], [2.3, 4.2], [3.9, 3.5], [3.7, 6.4], [4.8, 1.9],
[8.3, 3.1], [5.2, 7.5], [4.8, 4.7], [3.5, 5.1], [4.4, 2.9],])

Now, we need to define the nearest neighbors −

k = 3

We also need to give the test data from which the nearest neighbors is to be found −

test_data = [3.3, 2.9]

The following code can visualize and plot the input data defined by us −

plt.figure()
plt.title('Input data')
plt.scatter(A[:,0], A[:,1], marker = 'o', s = 100, color = 'black')

Now, we need to build the K Nearest Neighbor. The object also needs to be trained

knn_model = NearestNeighbors(n_neighbors = k, algorithm = 'auto').fit(X)
distances, indices = knn_model.kneighbors([test_data])

Now, we can print the K nearest neighbors as follows

print("\nK Nearest Neighbors:")
for rank, index in enumerate(indices[0][:k], start = 1):
print(str(rank) + " is", A[index])

We can visualize the nearest neighbors along with the test data point

plt.figure()
plt.title('Nearest neighbors')
plt.scatter(A[:, 0], X[:, 1], marker = 'o', s = 100, color = 'k')
plt.scatter(A[indices][0][:][:, 0], A[indices][0][:][:, 1],
marker = 'o', s = 250, color = 'k', facecolors = 'none')
plt.scatter(test_data[0], test_data[1],
marker = 'x', s = 100, color = 'k')
plt.show()

### Output

K Nearest Neighbors

1 is [ 3.1 2.3]
2 is [ 3.9 3.5]
3 is [ 4.4 2.9]

### K-Nearest Neighbors Classifier

A K-Nearest Neighbors (KNN) classifier is a classification model that uses the nearest neighbors algorithm to classify a given data point. We have implemented the KNN algorithm in the last section, now we are going to build a KNN classifier using that algorithm.

### Concept of KNN Classifier

The basic concept of K-nearest neighbor classification is to find a predefined number, i.e., the ‘k’ − of training samples closest in distance to a new sample, which has to be classified. New samples will get their label from the neighbors itself. The KNN classifiers have a fixed user defined constant for the number of neighbors which have to be determined. For the distance, standard Euclidean distance is the most common choice. The KNN Classifier works directly on the learned samples rather than creating the rules for learning. The KNN algorithm is among the simplest of all machine learning algorithms. It has been quite successful in a large number of classification and regression problems, for example, character recognition or image analysis.

Example

We are building a KNN classifier to recognize digits. For this, we will use the MNIST dataset. We will write this code in the Jupyter Notebook.

Import the necessary packages as shown below.

Here we are using the KNeighborsClassifier module from the sklearn.neighbors package −

from sklearn.datasets import *
import pandas as pd
%matplotlib inline
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import numpy as np

The following code will display the image of digit to verify what image we have to test −

def Image_display(i):
plt.imshow(digit['images'][i],cmap = 'Greys_r')
plt.show()

Now, we need to load the MNIST dataset. Actually there are total 1797 images but we are using the first 1600 images as training sample and the remaining 197 would be kept for testing purpose.

digit = load_digits()
digit_d = pd.DataFrame(digit['data'][0:1600])

Now, on displaying the images we can see the output as follows −

Image_display(0)

### Image_display(0)

Image of 0 is displayed as follows −

### Image_display(9)

Image of 9 is displayed as follows −

### digit.keys()

Now, we need to create the training and testing data set and supply testing data set to the KNN classifiers.

train_x = digit['data'][:1600]
train_y = digit['target'][:1600]
KNN = KNeighborsClassifier(20)
KNN.fit(train_x,train_y)

The following output will create the K nearest neighbor classifier constructor −

KNeighborsClassifier(algorithm = 'auto', leaf_size = 30, metric = 'minkowski',
metric_params = None, n_jobs = 1, n_neighbors = 20, p = 2,
weights = 'uniform')

We need to create the testing sample by providing any arbitrary number greater than 1600, which were the training samples.

test = np.array(digit['data'][1725])
test1 = test.reshape(1,-1)
Image_display(1725)

### Image_display(6)

Image of 6 is displayed as follows −

Now we will predict the test data as follows −

KNN.predict(test1)

The above code will generate the following output −

array([6])

Now, consider the following −

digit['target_names']

The above code will generate the following output −

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

## Supervised Learning: Regression

Regression is one of the most important statistical and machine learning tools. We would not be wrong to say that the journey of machine learning starts from regression. It may be defined as the parametric technique that allows us to make decisions based upon data or in other words allows us to make predictions based upon data by learning the relationship between input and output variables. Here, the output variables dependent on the input variables, are continuous-valued real numbers. In regression, the relationship between input and output variables matters and it helps us in understanding how the value of the output variable changes with the change of input variable. Regression is frequently used for prediction of prices, economics, variations, and so on.

### Building Regressors in Python

In this section, we will learn how to build single as well as multivariable regressor.

### Linear Regressor/Single Variable Regressor

Let us important a few required packages −

import numpy as np
from sklearn import linear_model
import sklearn.metrics as sm
import matplotlib.pyplot as plt

Now, we need to provide the input data and we have saved our data in the file named linear.txt.

input = 'D:/ProgramData/linear.txt'

input_data = np.loadtxt(input, delimiter=',')
X, y = input_data[:, :-1], input_data[:, -1]

The next step would be to train the model. Let us give training and testing samples.

training_samples = int(0.6 * len(X))
testing_samples = len(X) - num_training

X_train, y_train = X[:training_samples], y[:training_samples]

X_test, y_test = X[training_samples:], y[training_samples:]

Now, we need to create a linear regressor object.

reg_linear = linear_model.LinearRegression()

Train the object with the training samples.

reg_linear.fit(X_train, y_train)

We need to do the prediction with the testing data.

y_test_pred = reg_linear.predict(X_test)

Now plot and visualize the data.

plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_test, y_test_pred, color = 'black', linewidth = 2)
plt.xticks(())
plt.yticks(())
plt.show()

### Output

Now, we can compute the performance of our linear regression as follows −

print("Performance of Linear regressor:")
print("Mean absolute error =", round(sm.mean_absolute_error(y_test, y_test_pred), 2))
print("Mean squared error =", round(sm.mean_squared_error(y_test, y_test_pred), 2))
print("Median absolute error =", round(sm.median_absolute_error(y_test, y_test_pred), 2))
print("Explain variance score =", round(sm.explained_variance_score(y_test, y_test_pred),
2))
print("R2 score =", round(sm.r2_score(y_test, y_test_pred), 2))

### Output

Performance of Linear Regressor −

Mean absolute error = 1.78
Mean squared error = 3.89
Median absolute error = 2.01
Explain variance score = -0.09
R2 score = -0.09

In the above code, we have used this small data. If you want some big dataset then you can use sklearn.dataset to import bigger dataset.

2,4.82.9,4.72.5,53.2,5.56,57.6,43.2,0.92.9,1.92.4,
3.50.5,3.41,40.9,5.91.2,2.583.2,5.65.1,1.54.5,
1.22.3,6.32.1,2.8

### Multivariable Regressor

First, let us import a few required packages −

import numpy as np
from sklearn import linear_model
import sklearn.metrics as sm
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures

Now, we need to provide the input data and we have saved our data in the file named linear.txt.

input = 'D:/ProgramData/Mul_linear.txt'

input_data = np.loadtxt(input, delimiter=',')
X, y = input_data[:, :-1], input_data[:, -1]

The next step would be to train the model; we will give training and testing samples.

training_samples = int(0.6 * len(X))
testing_samples = len(X) - num_training

X_train, y_train = X[:training_samples], y[:training_samples]

X_test, y_test = X[training_samples:], y[training_samples:]

Now, we need to create a linear regressor object.

reg_linear_mul = linear_model.LinearRegression()

Train the object with the training samples.

reg_linear_mul.fit(X_train, y_train)

Now, at last we need to do the prediction with the testing data.

y_test_pred = reg_linear_mul.predict(X_test)

print("Performance of Linear regressor:")
print("Mean absolute error =", round(sm.mean_absolute_error(y_test, y_test_pred), 2))
print("Mean squared error =", round(sm.mean_squared_error(y_test, y_test_pred), 2))
print("Median absolute error =", round(sm.median_absolute_error(y_test, y_test_pred), 2))
print("Explain variance score =", round(sm.explained_variance_score(y_test, y_test_pred), 2))
print("R2 score =", round(sm.r2_score(y_test, y_test_pred), 2))

### Output

Performance of Linear Regressor −

Mean absolute error = 0.6
Mean squared error = 0.65
Median absolute error = 0.41
Explain variance score = 0.34
R2 score = 0.33

Now, we will create a polynomial of degree 10 and train the regressor. We will provide the sample data point.

polynomial = PolynomialFeatures(degree = 10)
X_train_transformed = polynomial.fit_transform(X_train)
datapoint = [[2.23, 1.35, 1.12]]
poly_datapoint = polynomial.fit_transform(datapoint)

poly_linear_model = linear_model.LinearRegression()
poly_linear_model.fit(X_train_transformed, y_train)
print("\nLinear regression:\n", reg_linear_mul.predict(datapoint))
print("\nPolynomial regression:\n", poly_linear_model.predict(poly_datapoint))

### Output

Linear regression:

[2.40170462]

Polynomial regression:

[1.8697225]

In the above code, we have used this small data. If you want a big dataset then, you can use sklearn.dataset to import a bigger dataset.

2,4.8,1.2,3.22.9,4.7,1.5,3.62.5,5,2.8,23.2,5.5,3.5,2.16,5,
2,3.27.6,4,1.2,3.23.2,0.9,2.3,1.42.9,1.9,2.3,1.22.4,3.5,
2.8,3.60.5,3.4,1.8,2.91,4,3,2.50.9,5.9,5.6,0.81.2,2.58,
3.45,1.233.2,5.6,2,3.25.1,1.5,1.2,1.34.5,1.2,4.1,2.32.3,
6.3,2.5,3.22.1,2.8,1.2,3.6

[wpsbx_html_block id=1891]