Why we can use train_test_split without an object creation but using linear_models like LinearRegression or StandardScaler() would need an instance - sklearn-pandas

I am new to Data Science in Python and while importing different libraries I can see they are being used in 2 different ways:
`import statsmodels.api as sm
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression`
Even within sklearn train_test_split and StandardScaler are called differently before use

As you can see from the source code of Scikit-learn libraries
(link)
train_test_split is defined as a quick utility to divide arrays or matrices into random train and test subsets. It does not need much parameters to define its behaviour.
More complex utilities, like StandardScaler() or LinearRegression(), are composed by multiple methods and attributes, they give a more friendly way to the developer to manage the process he is interested into.

Related

How can I remove an unwanted import alias in Go?

I found that very useful Go library in the web https://github.com/deckarep/golang-set that tries to port Python sets to Go.
Thanks to Visual Studio Code I eventually got it to work by importing the library import "github.com/deckarep/golang-set" and calling a function in my code:
mySet := mapset.NewSet()
VS Code automatically recognizes the alias and replaces the import directive:
import mapset "github.com/deckarep/golang-set"
However, being someone who finds those aliases confusing, I was trying to remove it but doing so, VSCode removes it from both the import statements and my code. VS Code then tells me:
undeclared name: NewSet compiler(UndeclaredName)
The package name from NewSet(...) is also package mapset. So I thought I could simply remove it. But it does not work.
I also tried to work analogously to other 3rd party packages and call the functions by the repository's name:
mySet := golang-set.NewSet()
This also leads to an error. Is the removing of the alias not possible here due to the hyphen maybe or am I overseeing something else?
Several things here:
mapset is the package name. You can see this by looking at the package source code.
While the import alias, in this case, is not strictly needed from a language standpoint, it's added for clarity, since the package name (mapset) does not match the import path (golang-set). Without the alias in the import statement, there's no way to tell how the package is referenced. This is why it's important for it to be there.
You cannot use golang-set as your import name, becuase the - character is not permitted in an import alias. However, if you really want to, you could use golang_set or similar, by explicitly providing this as your alias:
import golang_set "github.com/deckarep/golang-set"
Note that this goes against naming conventions, in that packages should not have _ characters in the name. But it should still be valid.
Best practice would be just to use mapset as the alias. It's the least confusing of all the available options (which is why it's automatically selected).

AttributeError, ImportError on scipy.misc image functions (e.x imread, imresize, imsave, imshow etc.)

I have came across two kinds of errors when trying to import or directly use any of the image functions included in the scipy.misc module. Here are two error examples with the imread() function:
>>> from scipy.misc import imread
ImportError: cannot import name 'imread' from 'scipy.misc'
and
>>> import scipy.misc
>>> scipy.misc.imread
AttributeError: module 'scipy.misc' has no attribute 'imread'
What am I doing wrong?
You are not doing anything wrong. This is due to the removal of the image functions from the scipy.misc module since SciPy Version 1.2.0. I don't know why did they deem those functions deprecated and removed them, but if you want to use them, you can rollback to a previous SciPy version by uninstalling the current one and installing a previous one:
pip uninstall scipy
pip install scipy==1.1.0
Make sure you have Pillow installed too:
pip install Pillow
If you don't want to use an old version of SciPy, then you will need to change your code. According to the official docs of each deprecated function, here is what SciPy suggests:
fromimage(im) -> np.asarray(im)
imfilter() -> Use Pillow filtering functionality directly.
imread() -> imageio.imread()
imsave() -> imageio.imwrite()
imresize() -> numpy.array(Image.fromarray(arr).resize())
imrotate -> skimage.transform.rotate()
imshow() -> matplotlib.pyplot.imshow()
toimage() -> Image.fromarray()
It assumes to install the below libraries:
pip install numpy Pillow scikit-image imageio matplotlib
and import them:
import numpy as np, Pillow, skimage, imageio, matplotlib
In addition, I quote two sources I found, mentioning the deprecation of the scipy.misc image I/O functionality:
From scipy.github.io:
The following functions in scipy.misc are deprecated: bytescale, fromimage, imfilter, imread, imresize, imrotate, imsave, imshow and toimage. Most of those functions have unexpected behavior (like rescaling and type casting image data without the user asking for that). Other functions simply have better alternatives.
From imageio.readthedocs.io (especially for imread):
Transitioning from Scipy’s imread
Scipy is deprecating their image I/O functionality.
This document is intended to help people coming from Scipy to adapt to
Imageio’s imread function. We recommend reading the user api and
checkout some examples to get a feel of imageio.
Imageio makes use of variety of plugins to support reading images (and
volumes/movies) from many different formats. Fortunately, Pillow is
the main plugin for common images, which is the same library as used
by Scipy’s imread. Note that Imageio automatically selects a plugin
based on the image to read (unless a format is explicitly specified),
but uses Pillow where possible.
In short terms: For images previously read by Scipy’s imread, imageio
should generally use Pillow as well, and imageio provides the same
functionality as Scipy in these cases. But keep in mind:
Instead of mode, use the pilmode keyword argument.
Instead of flatten, use the as_gray keyword argument.
The documentation for the above arguments is not on imread, but on the docs of the individual formats, e.g. PNG.
Imageio’s functions all return numpy arrays, albeit as a subclass (so that meta data can be attached).

Selecting right import based on string in Go

Consider the following
import ( "library_a"
"library_b"
...
"library_z"
I want to be able to select the right library based on the letter.
So if I have z as input it will pick library_z.
What is a way to do this without requiring a massive switch statement?
If you want to use different libraries then you would have to import all of them and actually use them in your code.
From the docs:
It is illegal for a package to import itself, directly or indirectly, or to directly import a package without referring to any of its exported identifiers.
You will have to use a switch if you want to use different package implementations!

Reference a symbol in a Go package without knowing if you are in that package?

Assume there is a function F in package A that some code I'm creating needs to call. How do I call it?
If I'm Calling it from outside package A, then I uses A.F(...) and if I'm inside A I uses F(...). But what if Murphy prevents me from knowing which is true or requires a byte identical line work in both?
[note: I'm taking it as a given that such a case will occur because, in my experience and observations, that is generally a safe assumption. Even in the absence of technical reasons for it, PHBs and legislators are good sources of the ridiculous.]
There is no such syntax. Observe the following things:
Cyclical imports are forbidden. This especially means that a package cannot import itself. Thus, a package cannot refer to one of its symbols S with the pkg.S syntax because it will not be able to import itself.
Even if you solved that problem, observe that packages can be given an arbitrary name once imported. For instance, you could do:
import bar "foo"
Which imports S from package "foo" as bar.S as opposed to the expected foo.S.
The following things could be used to work around this:
In the package "foo", create an internal object foo whose members are the symbols foo exports. This allows you to use the foo.S syntax in foo itself, but is a horrible kludge.
Use an import declaration like
import . "foo"
which allows you to use symbol S from package "foo" as S, i. e. without prefix. Notice that this kind of import declaration, called dot imports, is considered bad style and might break things if the set of symbols you declare / the package you import declares changes.

Vim settings to detect includes in D source

I am trying to add some support for D programming language to my vim config. For autocompletion I need to detect packages that are included. This is not exactly hard to do in simple case:
import std.stdio;
import std.conv;
My config:
set include=^\\s*import
set includeexpr=substitute(v:fname,'\\.','/','g')
Works great.
However, imports can have more complicated format, for example:
package import std.container, std.stdio = io, std.conv;
I was not able to find a simple way to parse this with include and includeexpr.
Also there is a second problem: import can have different access modifiers, like public and private. VIM scans included files recursively, import statements from included files are parsed too. But I need to distinguish between the file I am working with now and files which are scanned automatically: in current file all imports should be detected, but in other files only public import statements should add more files to the search.
Thanks for help.
Update
It's a shame if this can not be done without full parsers. Essentially, I only need two things:
ability to return an array from includeexpr instead of one file name
ability to distinguish between includes in current and other files
I think only way to do it reliably is to use complete parser and semantic analyzer. D Completion Daemon (https://github.com/Hackerpilot/DCD/tree/master/editors/vim ) has vim plugin and is not very resource-hungry.
Vim's include mechanism and 'includeexpr' are heavily influenced by the C programming language and only work for single files. You cannot return a list of filenames, so it won't be possible to support D's complex include mechanism with Vim. Use an IDE that is fully tailored to support the programming language, not a general-purpose text editor.

Resources