Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

flassger

def train_and_save_model():
    '''This function creates and saves a Binary Logistic Regression
    Classifier in the current working directory
    named as LogisticRegression.pkl
    '''
    ## Creating Dummy Data for Classificaton from sklearn.make_classification
    ## n_samples = number of rows/number of samples
    ## n_features = number of total features
    ## n_classes = number of classes - two in case of binary classifier
    X,y = make_classification(n_samples = 1000,n_features = 4,n_classes = 2)
    ## Train Test Split for evaluation of data - 20% stratified test split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42,stratify=y)
    ## Building Model
    logistic_regression = LogisticRegression(random_state=42)
    ## Training the Model
    logistic_regression.fit(X_train,y_train)
    ## Getting Predictions
    predictions = logistic_regression.predict(X_test)
    ## Analyzing valuation Metrics
    print("Accuracy Score of Model : "+str(accuracy_score(y_test,predictions)))
    print("Classification Report : ")
    print(str(classification_report(y_test,predictions)))
    ## Saving Model in pickle format
    ## Exports a pickle file named Logisitc Regrssion in current working directory
    output_path = os.getcwd()
    file_name = '/LogisticRegression.pkl'
    output  = open(output_path+file_name,'wb')
    pickle.dump(logistic_regression,output)
    output.close()
Comment

PREVIOUS NEXT
Code Example
Python :: allowed_hosts error ecs django 
Python :: turn index back to column 
Python :: pyttsx3 listen to events 
Python :: Pyturch training along with source code 
Python :: extracting bounding box from xml file python 
Python :: print same index and value on each iteration of the for loop in Python 
Python :: how to un register DefaultAdminSite in django 
Python :: Command to import the Schema interface from voluptuous 
Python :: PHP echo multi lines Using Heredoc variable 
Python :: Code to find maximum number using if else 
Python :: numpy random sin 
Python :: write console output in same place 
Python :: meter replacement application 
Python :: word search engine in python 
Python :: python sqlite select where 
Python :: ouvrir fichier txt python et le lire 
Python :: grab element based on text from html page in python 
Python :: Python NumPy transpose Function Example with use of tuples 
Python :: get nodes of xml in python 
Python :: Python NumPy asanyarray Function Example Scalar to an array 
Python :: Python NumPy row_stack Function Example with 2d array 
Python :: Python NumPy vsplit Function 
Python :: Python __sub__ magic method 
Python :: using Canvas with tkinger 
Python :: NumPy right_shift Syntax 
Python :: center pyfiglet to terminal 
Python :: penggunaan fromkeys di python 
Python :: login to sso.accounts.dowjones.com for wsj.com "python" 
Python :: pandas dataframe select columns multiple cell value 
Python :: map function in pyhton 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =