<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Sloan Digital Sky Survey Classification
In this notebook - through a logistic regression classifier - we classify observations of space objects to be either stars, galaxies or quasars. 
We are using data from the Sloan Digital Sky Survey (Release 14)
Data Acquisition
Step1:  The class identifies an object to be either a galaxy, star or quasar.
This will be the output variable which we will predict. A multi-class target variable.
Step2:  The dataset has 10000 examples, 17 features and 1 target.
Step3:  No missing values!
Data preprocessing
Remove unnecessary features
Step4:  From the project website, we can see that objid and specobjid are just identifiers for accessing the rows in the original database. Therefore we will not need them for classification as they are not related to the outcome.
Moreover, the features run, rerun, camcol and field are values which describe parts of the camera at the moment when making the observation, e.g. 'run' represents the corresponding scan which captured the object.
We will drop these columns as any correlation to the outcome would be coincidentally.
Step5:  Training
Split the dataset
We will split the data into a training and a test part (70-30). The models will be trained on the training data set and tested on the test data set
Step6:  Data scaling
Scaling all values to be within the (0, 1) interval will reduce the distortion due to exceptionally high values and make some algorithms converge faster.
Step7:  fit one-versus-all model
The One-versus-all model is extremely simple and is exactly what you expect of the words
Step8:  A very simple classifier like this has already a high accuracy of 90%
fit a LR model with cross-entropy loss function
Sklearn offers also a classifier that uses the cross-entropy as loss function.
The loss minimised is the multinomial loss fit across the entire probability distribution.
Note that it does not work for liblinear solver so we are going to use the Newton optimisation algorithm but new in version 0.18 of sklearn is the Stochastic Average Gradient Descent solver (can be useful to try other solvers when the model is not converging).
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
sdss = pd.read_csv('../datasets/Skyserver_SQL2_27_2018 6_51_39 PM.csv', skiprows=1)
sdss.head(2)
Explanation: Sloan Digital Sky Survey Classification
In this notebook - through a logistic regression classifier - we classify observations of space objects to be either stars, galaxies or quasars. 
We are using data from the Sloan Digital Sky Survey (Release 14)
Data Acquisition
End of explanation
sdss['class'].value_counts()
sdss.info()
Explanation: The class identifies an object to be either a galaxy, star or quasar.
This will be the output variable which we will predict. A multi-class target variable.
End of explanation
sdss.describe()
Explanation: The dataset has 10000 examples, 17 features and 1 target.
End of explanation
sdss.columns.values
Explanation: No missing values!
Data preprocessing
Remove unnecessary features
End of explanation
sdss.drop(['objid', 'run', 'rerun', 'camcol', 'field', 'specobjid'], axis=1, inplace=True)
sdss.head(2)
# Extract output values
y = sdss['class'].copy() # copy “y” column values out
sdss.drop(['class'], axis=1, inplace=True) # then, drop y column
Explanation: From the project website, we can see that objid and specobjid are just identifiers for accessing the rows in the original database. Therefore we will not need them for classification as they are not related to the outcome.
Moreover, the features run, rerun, camcol and field are values which describe parts of the camera at the moment when making the observation, e.g. 'run' represents the corresponding scan which captured the object.
We will drop these columns as any correlation to the outcome would be coincidentally.
End of explanation
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(sdss, y, test_size=0.3)
Explanation: Training
Split the dataset
We will split the data into a training and a test part (70-30). The models will be trained on the training data set and tested on the test data set
End of explanation
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled  = scaler.transform(X_test)
Explanation: Data scaling
Scaling all values to be within the (0, 1) interval will reduce the distortion due to exceptionally high values and make some algorithms converge faster.
End of explanation
from sklearn.linear_model import LogisticRegression
# Create One-vs-rest logistic regression object
ovr = LogisticRegression(multi_class='ovr', solver='liblinear')
modelOvr = ovr.fit(X_train_scaled, y_train)
modelOvr.score(X_test_scaled, y_test)
Explanation: fit one-versus-all model
The One-versus-all model is extremely simple and is exactly what you expect of the words: you train a classifier for each category.  
So for example, you train one that says the positive category is going to be all the Stars and the negative category is going to be everything else (in our example, galaxies and quasars); it is  just trying to learn a classifier that separates most of the stars from the other sky objects.  
We'll learn a model for each one of these cases; after Stars will be Galaxies and then Quasars. 
As a prediction, whatever class has the highest probability wins. So in other words, if the probability that an input is a star against everything else is higher then the object is a star.  
Everything is performed by the sklearn classifier when multiclass is ovr (for one-versus-rest):
End of explanation
# Create cross-entropy-loss logistic regression object
xe = LogisticRegression(multi_class='multinomial', solver='newton-cg')
# Train model
modelXE = xe.fit(X_train_scaled, y_train)
preds = modelXE.predict(X_test_scaled)
modelXE.score(X_test_scaled, y_test)
Explanation: A very simple classifier like this has already a high accuracy of 90%
fit a LR model with cross-entropy loss function
Sklearn offers also a classifier that uses the cross-entropy as loss function.
The loss minimised is the multinomial loss fit across the entire probability distribution.
Note that it does not work for liblinear solver so we are going to use the Newton optimisation algorithm but new in version 0.18 of sklearn is the Stochastic Average Gradient Descent solver (can be useful to try other solvers when the model is not converging).
End of explanation
<END_TASK>