You are reading the article Examples To Implement Mysql Mod() updated in December 2023 on the website Achiashop.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Examples To Implement Mysql Mod()
Introduction to MySQL MOD()MySQL MOD() function allows us to divide a literal number with another numeric expression to return the remainder of it. The MOD() takes numeric values as arguments for the function to execute and produce the result. The arguments can be said as dividend and divisor as in Maths which works on the fractional numeric values to output the special remainder. If the divisor or, say, the second parameter of the MySQL MOD() function is set to 0 (zero), the result will be NULL. This function can be used safely with BIGINT data type values, but mainly it is used for fractional parts to perform the division and show the remainder after calculating logically.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
SyntaxThe following syntax code introduces the structure to apply the Math function MOD() in MySQL:
MOD(a,b) a MOD b a % bWe have the above three syntaxes to use the MOD() function within the queries in MySQL.
Here, the ‘a’ and ‘b’ parameters are the required numeric values that can be either fractional part or BIGINT data type values with the help of the MOD() function’s division process. After the completion of the command query then, the return value is the remainder balanced within numbers as in Maths calculation problems.
Also, we can define the first argument value (a) as dividends and the other one-second argument (b) as the divisor for the MOD() calculation to produce the result of the division.
Thus, with the literal numbers, the MOD() function returns the remains of the division, and if the divisor is zero like MOD(dividend,0), the result will be NULL.
How does MySQL MOD() Function work?As per the syntax above, dividing a numeric expression by another literal number will result in the remainder of this MOD() function in MySQL.
This performs the same work as Modulus calculation is calculated in Maths that we do manually using logic and basic concepts. In addition, we also use SELECT query statement to supplement the MOD() function used in MySQL, so we use the MOD() function as:
SELECT MOD(a,b);Code: Let us take a query using the MOD() function for MySQL to illustrate the function syntaxes that can be executed in three forms-
SELECT MOD(152, 3);Or,
SELECT 152 % 3;Or,
SELECT 152 MOD 3;Output:
Explanation: As you can see that the results show the remainder of each query form that is executed on the server. So, like this, the Math function implements the logic to produce the modulus process result.
Examples of implementing MOD() function in MySQLLet us illustrate the MySQLMOD() function with the help of the following examples and show executions results generated simultaneously:
1. Some Simple Examples Using MySQL MOD() functionCode #1
SELECT MOD(21,2);Output:
It can be noticed that MySQL permits to use of the modulus operator, i.e., %, which can be considered as the synonym for this MOD() function to give the result also like this:
Code #2
SELECT 21 % 2;Output:
Also, look at the results gained using the MOD() function that accepts fractional elements to return the remainder of the division process. The query for this is as follows:
Code #3
SELECT MOD(15.8, 3);Output:
By utilizing both a dividend and divisor with fractional values, the following code demonstrates how to obtain the result:
SELECT MOD(8.452, 2.14);Output:
The result of the remainder is also with fractional values. Also, view the below query with a divisor in fractional value only:
Code #5
SELECT 25 MOD 2.3;Output:
2. Using MOD() function for zero divisor valueSuppose we have set the divisor as zero and dividend like any literal numeric expression; then the MOD() function applied to it will produce the following result as a reminder:
Code #1
SELECT MOD(17,0);Output:
You can view that the remainder of the above query with a 0 divisor value is NULL when executed on the MySQL server.
Let us try out the result of the MOD() function in MySQL when we set zero value for dividend and non-zero or, say, any literal number for divisor using the succeeding query:
Code #2
SELECT 0 % 5;So, do remember that if we divide the 0 value with any numeric expression, mathematically, it will give the result zero, just the opposite if we reverse the statement values when the divisor is zero. If the dividend is non-zero, then the output will be NULL.
3. Using MOD() function on Database Table fieldsSupposing we have created a table named ‘Books’ where we will implement the MOD() function in MySQL. To accomplish this, you can utilize the following code for table creation, which includes the fields BookID, BookName, Language, and Price:
CREATE TABLE Orders(BookID INT PRIMARY KEY AUTO_INCREMENT, BookNameVarchar(255) NOT NULL, LanguageVarchar(255) NOT NULL, Price INT NOT NULL);Let us enter some data records in the table Books with the following SQL query:
Code #1
INSERT INTO TABLE (BookID, BookName, Language, Price) VALUES('101','Algebraic Maths','English','2057');And so on.
Output: View the table here:
Now, we will use MySQLMOD() query on the table column ‘Price’ to check whether the values are even or odd:
Code #2
SELECT BookID, BookName, Language, Price, IF (MOD(Price,2), 'Odd_Price','Even_Price') AS Odd_Even_Prices FROM Books GROUP BY BookID ORDER BY BookID;Output:
In this illustration:
In this case, we utilize the price column to fetch its values for performing the MOD() operation, specifically on numeric values.
When we apply the MOD() function, the values in the Price column will undergo division by 2 as the divisor, resulting in the retrieval of the remainder. The query will evaluate odd or even according to the results produced in zero or one.
At last, we have applied the IF() function that, when executed, will show Odd and Even prices string based on the result of the MOD() operation.
Conclusion
MySQL provides the MOD() function, which actively evaluates the given values and generates the remainder after dividing the specified arguments.
The MySQL MOD() thus implements a modulo operation on calling the function to provide the remainder of two numeric values passed in the function by the division method.
Recommended ArticlesWe hope that this EDUCBA information on “MySQL MOD()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Examples To Implement Mysql Mod()
How To Implement Autofill In Your Android Apps
How does autofill work?
Providing hints for autofillIf your app uses standard Views, then by default it should work with any autofill service that uses heuristics to determine the type of data that each View expects. However, not all autofill services use these kind of heuristics; some rely on the View itself to declare the type of data that it expects.
To ensure your app can communicate with the Autofill Framework regardless of the autofill service that the user has installed on their device, you’ll need to add an “android:autofillHints” attribute to every View that’s capable of sending and receiving autofill data.
Let’s take a look at how you’d update a project to provide autofill hints. Create a new project that targets Android Oreo, and then create a basic login screen consisting of two EditTexts that accept a username and a password:
android:layout_width=”match_parent” android:layout_height=”match_parent” <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:textSize=”30sp” android:text=”Login” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintHorizontal_bias=”0.462″ app:layout_constraintLeft_toLeftOf=”parent” app:layout_constraintRight_toRightOf=”parent” app:layout_constraintTop_toTopOf=”parent” <EditText android:id=”@+id/username” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:hint=”Enter Name” app:layout_constraintBottom_toTopOf=”@+id/password” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintHorizontal_bias=”0.056″ app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toTopOf=”parent” <EditText android:id=”@+id/password” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginBottom=”324dp” android:hint=”Password” android:inputType=”textPassword” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintHorizontal_bias=”0.054″
The Username EditText expects a username, so add android:autofillHints=”username”
The Password EditText expects a password, so we need to add android:autofillHints=”password”
Later in this article we’ll be covering different ways of optimizing your app for autofill, but since this is enough to provide basic autofill support, let’s look at how you’d put this updated application to the test.
Testing your app with autofill Build and install Google’s Autofill Framework sample project
Android Studio will now import the Autofill Framework app as a new project. If Android Studio prompts you to upgrade your Gradle plugin, select ‘Update.’
At the time of writing, this project still uses the Java 8.0 support provided by the deprecated Jack compiler, so open the module-level build.gradle file and remove the following:
Code
jackOptions { enabled true }If you look at the Manifest, you’ll see that this project has two launcher Activities:
Code
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" <activity android:name=".app.MainActivity" <activity ... ... ... <activity android:name=".multidatasetservice.settings.SettingsActivity" android:exported="true" android:label="@string/settings_name" Activate Android Oreo’s AutofillAutofill is disabled by default; to enable it, you’ll need to specify the autofill service that you want to use:
Open your device’s ‘Settings’ app.
Select ‘Multi-Dataset Autofill Service,’ which is Google’s autofill service application.
Supply some dataIf we’re going to test our app’s ability to receive data from an autofill service, then the autofill service is going to need some data that it can supply to this application.
There’s an easy way to feed data to an autofill service:
Load any other application that expects the data in question – in this instance, that’s any application where we can enter a username and password.
Enter this data into the application.
When prompted, save this data to the autofill service.
Switch to the application that you want to test.
Select the View that you want to test, and then see whether autofill kicks in and offers to complete this View for you.
Conveniently, the Autofill Sample app contains a login Activity that expects a username and password combo:
Launch the Autofill Sample app.
Select ‘Sample Login Using EditTexts.’
Enter a fake username and password. Note that a quirk of this Activity is that the username and password must be exactly the same for it to accept your input, so if you use “testing” as your username, then you’ll also have to use “testing” as your password. Also be aware that Google’s autofill service stores its data in SharedPreferences, so anyone with root access to your device can potentially see this data.
Launch the login screen application we created earlier in this tutorial.
Tap the ‘username’ View. At this point the autofill picker should appear.
Select the dataset you want to use, and all Views present in this dataset will be autofilled, so the username and password Views should be autofilled simultaneously.
Optimizing your app for autofillWhile this is enough to implement basic autofill functionality in your app, there’s some additional steps you can take to ensure your application is providing the best possible autofill experience.
In this final section I’m going to look at several ways that you can optimize your app for autofill.
Is a View important, or unimportant?
“auto.” Android is free to decide whether this View is important for autofill – essentially, this is the system’s default behavior.
“yes.” This View and all of its child Views are important for autofill.
“no.” This View is unimportant for autofill. Occasionally, you may be able to improve the user experience by marking certain Views as unimportant, for example if your app includes a CAPTCHA, then focusing on this field could trigger the autofill picker menu, which is just unnecessary onscreen clutter, distracting the user from what they’re trying to accomplish. In this scenario, you can improve the user experience by marking this View as android:importantForAutofill=“no.”
“noExcludeDescendants.” The View and all of its children are unimportant for autofill.
“yesExcludeDescendants.” The View is important for autofill, but all of its child Views are unimportant.
Alternatively, you can use the setImportantForAutofill method, which accepts the following:
IMPORTANT_FOR_AUTOFILL_AUTO.
IMPORTANT_FOR_AUTOFILL_YES.
IMPORTANT_FOR_AUTOFILL_NO.
IMPORTANT_FOR_AUTOFILL_YES_EXCLUDE_DESCENDANTS
IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS.
For example:
Code
.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS); Force an autofill requestMost of the time, the autofill lifecycle is started automatically in response to notifyViewEntered(View), which is called when the user enters a View that supports autofill. However, sometimes you may want to trigger an autofill request in response to user action, for example if the user long-presses a field.
You can force an autofill request using requestAutofill(), for example:
Code
public void eventHandler(View view) { AutofillManager afm = context.getSystemService(AutofillManager.class); if (afm != null) { afm.requestAutofill(); } } Check whether autofill is enabledYou may decide to offer additional features when autofill is enabled, for example an ‘Autofill’ item in your app’s contextual overflow menu. However, since it’s never a good idea to mislead users by offering features that your app can’t currently deliver, you should always check whether autofill is currently enabled and then adjust your application accordingly, for example removing ‘Autofill’ from your context menu if autofill is disabled.
You can check whether autofill is available, by calling the isEnabled() method of the AutofillManager object:
Code
if (getSystemService(android.view.autofill.AutofillManager.class).isEnabled()) { Sharing data between your website and application
Open the Android project that you want to associate with your website.
Enter the domain that you want to associate with your application.
Enter your app’s signing config, or select a keystore file. Note that if you use a debug config or keystore, then eventually you’ll need to generate and upload a new Digital Asset Links file that uses your app’s release key.
Wrapping UpCompressed Work Schedules And How To Implement Them
What is a compressed work schedule?
A compressed work schedule is when a full-time employee works a traditional workweek, generally consisting of 35 to 40 hours, in fewer than five days. A compressed work schedule is a flexible option that allows employees to work more efficiently and gain a better work-life balance without sacrificing a full-time salary and benefits.
There are various ways to set up a compressed work schedule, with one of the most common being the 4/10 work schedule. In the 4/10 schedule, employees work 10 hours per day Monday through Thursday to earn the day off each Friday.
Another alternative is the 9/80 schedule. This is when, over a two-week period, an employee works nine hours per day for eight days and eight hours per day for one day to accumulate one day off every two weeks. While there are various ways to make a compressed schedule, these are some of the most common and attainable schedules used.
FYI
According to a study by Robert Half, 66% of employees would prefer a compressed work schedule, but only 17% of employers offer that as an option.
2. There can be greater morale and productivity.An extra day off can boost employees’ morale and lead them to be more driven and productive in their work, with fewer interruptions. They will feel more motivated and empowered to do better in their roles, knowing they have control over their personal time and don’t need to worry that important tasks are falling by the wayside. Employers also benefit, as employees are less likely to take time off for personal reasons, which should result in better attendance at work.
3. There’s more time for family and passions.By working a shorter week, employees can organize their schedules to suit their lives. This gives them extra time to spend however they choose, whether that’s relaxing, spending time with family or taking care of personal matters. With the flexibility of a compressed work schedule, employees can get an additional scheduled day off without losing their full-time income and employee benefits.
A compressed workweek can be a great draw to attract and retain talent and can help improve company morale due to its flexibility. Employees want to have more personal time and not feel stuck in a job with no improvement or options in sight. The efficiency of a compressed workweek gives that additional personal time to employees while also increasing productivity when they are in the office. It also allows businesses to broaden their hours of operation by having employees in the office on an extended schedule, rather than all employees working the same hours.
Did You Know?
A compressed workweek is a form of flextime. Other flextime examples are remote work and job-sharing.
What are the drawbacks of compressed work schedules?Compressed work schedules aren’t suitable for every employee and business. Here are some drawbacks.
1. Longer workdays may be a hard adjustment.While compressed work schedules offer some great benefits, they can also be challenging to adjust to if you’re used to a standard eight-hour workday. Longer workdays can be more efficient, but without the proper management, they can come at a toll both mentally and physically.
If an employee isn’t used to working a compressed schedule, it can be hard to push through the extended hours while their 9-to-5 co-workers head out for the day. The longer workweeks can drag on and lead to a lack of motivation if the employee doesn’t manage their time right.
For some, the long shifts, lack of supervision and absence of co-workers during certain work hours can make it hard to push through the day, causing them to slack off and be unproductive.
“Some employees … are able to maintain consistently strong work for an entire 10-hour shift, [while] others may check out mentally after hour six or seven, meaning you get fewer useful work hours from them under a compressed schedule,” said Matt Erhard, managing partner at recruiting firm Summit Search Group. “It all comes down to knowing your team, their work style and the type of work they do when you’re deciding whether compressed schedules can work for your team.”
2. There’s potential for burnout.If an employee is overwhelmed and feels the work will never end, they should step back and take a break to reset with some personal time. By not addressing these feelings, employees will exhaust themselves and head toward burnout, severely hindering their work and demeanor.
By finding ways to prepare for this adjustment mentally and maximizing their out-of-office time, employees can avoid burnout and keep the momentum going to maintain a compressed work schedule.
Tip
To help employees avoid burnout on a compressed schedule, allow them to set boundaries and schedule breaks into their workday, regardless of how long it is.
3. There are potential child care and scheduling issues.Although a compressed schedule can be very beneficial to some, certain employees may find it to be more of an inconvenience. While a compressed schedule gives employees extra personal time and opens up their calendar, it also requires them to work hours that may be inconvenient for coordinating with others.
Scheduling conflicts can often arise when using public transportation, when getting to places such as the bank or DMV that run on strict schedules, or when dropping off or picking up children from school or daycare.
With a compressed workweek, extended hours can cause an employee’s daily availability to be out of sync with other businesses that operate on a 9-to-5 basis. With issues like child care, this could force employees to find alternative solutions, which can mean added expenses.
How do you plan a compressed work schedule?If you’re considering implementing a compressed work schedule, consider these best practices.
1. Ask the right questions to ensure the plan is solid.When planning and implementing a compressed work schedule, you need to ask the right questions to avoid issues once an employee’s schedule changes.
You should also consider the customer to ensure this new schedule does not interfere with current business demands, and whether the workload will still be healthy during extended hours. A compressed work schedule may be OK for an overworked employee in the short term, but it could negatively affect them in the long run.
Tip
The best employee scheduling software can help you manage schedules across multiple locations and gauge when you’ll need more workers to cover a busy shift.
2. Get employee buy-in.To keep staff motivated, it’s important to get your employees’ buy-in so that everybody is on the same page and supportive of any changes being implemented. After all, the employees will be most affected by the change.
You must consider what’s right for the company if employees are divided on using a compressed work schedule. If a small minority doesn’t want it, will the policy change, or is it an elective schedule shift? Be sure not to force longer hours on an employee who can’t or doesn’t want to change the agreed-upon schedule in their contract.
3. Communicate with employees.To ensure a compressed schedule works for all parties, you should openly communicate with your employees on a compressed schedule and monitor the arrangement to see if it’s going smoothly. Giving employees the chance to be open, preferably in a one-on-one setting, will help them see you’re on their side and that they’ll have the support to get through any issues or challenges that arise.
Make sure your whole team is aware of any compressed schedules being implemented. The schedule change could affect the entire team because of some employees’ differing hours and availability. It’s essential to keep everyone’s needs in mind and help the entire team adjust to the new schedule.
Watch for signs that a compressed work schedule arrangement is negatively affecting your employees, including symptoms of overwork and expressed lack of support. It’s critical to ensure all your employees’ needs are being addressed.
How to implement a successful compressed work scheduleFollow these best practices to implement a compressed work schedule.
1. Check your local labor laws.Labor laws may come into play in certain states, particularly overtime pay laws. For example, California labor laws say a compressed workweek with shifts longer than 10 hours may mean some employees must be compensated for overtime.
Check your state’s rules and regulations regarding overtime and the legal limits for how many hours an employee can work per day to ensure you don’t violate any labor laws.
Did You Know?
In California, employers that wish to implement a compressed workweek must put it to a vote and receive at least two-thirds approval from all affected employees via secret ballot.
2. Use a time and attendance system to keep track of schedules.To prevent staffing issues due to specific employees’ compressed work schedules and ensure adequate coverage, keep a close eye on your whole team’s hours. The best time and attendance systems allow you to manage schedules, create attendance policies, and change employee timecards as needed if there are last-minute adjustments.
3. Ask for employee feedback.To make sure a compressed work schedule is beneficial for all parties, request feedback regularly from your employees to see if they can recommend any improvements to the arrangement. Keep track of this feedback and implement suggested changes as often as possible.
4. Modify and remain flexible.Both employers and employees will need to adjust to compressed work schedules. As an employer, you should remain flexible and open to change to make sure this plan is working for everyone. Keep an open mind and be willing to try different things to figure out the plan that suits each employee best. Don’t be afraid of trial and error or going back to a standard schedule if the schedule shift doesn’t work.
How To Configure Mod Rewrite For Apache On Centos 7
Output: Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.fibergrid.in * extras: mirror.fibergrid.in * updates: mirror.fibergrid.in Resolving Dependencies Dependencies Resolved ================================================================================================= Package Arch Version Repository Size ================================================================================================= Installing: httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M Installing for dependencies: apr x86_64 1.4.8-3.el7 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-40.el7.centos.4 updates 83 k mailcap noarch 2.1.41-2.el7 base 31 k Transaction Summary ================================================================================================= Install 1 Package (+4 Dependent packages) Total download size: 3.0 M Installed size: 10 M Is this ok [y/d/N]: y Downloading packages: ————————————————————————————————- Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-3.el7.x86_64 1/5 Installing : apr-util-1.5.2-6.el7.x86_64 2/5 Installing : mailcap-2.1.41-2.el7.noarch 4/5 Verifying : apr-1.4.8-3.el7.x86_64 2/5 Verifying : mailcap-2.1.41-2.el7.noarch 3/5 Verifying : apr-util-1.5.2-6.el7.x86_64 5/5 Installed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Dependency Installed: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7 Output: httpd.service – The Apache HTTP Server Active: active (running) since Wed 2023-11-09 12:52:17 IST; 4s ago man:apachectl(8) Status: “Processing requests…” Nov 09 12:52:17 localhost.localdomain systemd[1]: Starting The Apache HTTP Server… Nov 09 12:52:17 localhost.localdomain systemd[1]: Started The Apache HTTP Server. Output: Main DocumentRoot: “/var/www/html” Mutex mpm-accept: using_defaults Mutex authdigest-opaque: using_defaults Mutex proxy-balancer-shm: using_defaults Mutex rewrite-map: using_defaults Mutex authdigest-client: using_defaults Mutex proxy: using_defaults Mutex authn-socache: using_defaults Define: DUMP_RUN_CFG User: name=”apache” id=48 Output: # # Server itself. # LoadModule access_compat_module modules/mod_access_compat.so LoadModule actions_module modules/mod_actions.so LoadModule alias_module modules/mod_alias.so LoadModule allowmethods_module modules/mod_allowmethods.so LoadModule auth_basic_module modules/mod_auth_basic.so LoadModule auth_digest_module modules/mod_auth_digest.so LoadModule authn_anon_module modules/mod_authn_anon.so …. … … LoadModule rewrite_module modules/mod_rewrite.so …. … Output: … … # Further relax access to the default document root: # # Possible values for the Options directive are “None”, “All”, # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # doesn’t give it to you. # # The Options directive is both complicated and important. Please see # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be “All”, “None”, or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride ALL # # Controls who can get stuff from this server. # Require all granted .. RewriteEngine On
Save the fine and exit from the vi editor.
RewriteEngine Conditions with LogicRewriteCond is a directive used if a rewiteCond is enabled, then the Apache will follow the RewriteRule.
The apache will handle the request for an invalid web pace or URL it will display the 404 Page Not Found, instead of displaying the 404 error page, we can redirect the URL back to the home page or other web page.
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^admin/(.*)$ /home IP address RestrictionWe can use Rewite Condition to allow or block the traffic from a particular IP address.
For demo, below is the code where we want to block the traffic from all and accept the traffic from one address.
RewriteCond %{REMOTE_ADDR} !^(198.168.100.20)$ RewriteRule (.*) - [F,L]Options Used.
%{REMOTE_ADDR} - is the address string. !^(198.168.100.20)$ - the IP address where to allow the web traffic. F – Flage deny the access L – Which indicates the last rules to run.In this above article, we have enabled the RewriteRules and RewriteCond on the Apache where we want to redirect the traffic with or without conditions and also, the mod_rewrite is an important directive where the Apache web server uses and redirects the web traffic to other site or other location with or without condition.
How Can We Implement Logistic Regression?
This article was published as a part of the Data Science Blogathon
IntroductionIn this article, we will be learning about how we can implement logistic regression by writing Python code. You must be wondering what is logistic regression and what is the theory behind it? What python packages are involved while implementing logistic regression? You must be coming up with many more questions but I will try to answer as many as questions possible. Well, you have chosen the right article. So let’s begin our journey for logistic regression.
Topics to be covered in this article:
Introduction to Logistic Regression
How can we implement it?
Train your dataset
Sigmoid function
Calculating probability and making predictions
Calculating the cost
Reducting the cost using Gradient Descent
Testing you model
Predicting the values
Introduction to logistic regressionNow we proceed to see how this algorithm can be implemented.
How can we implement it?This algorithm can be implemented in two ways. The first way is to write your own functions i.e. you code your own sigmoid function, cost function, gradient function, etc. instead of using some library. The second way is, of course as I mentioned, to use the Scikit-Learn library. The Scikit-Learn library makes our life easier and pretty good. All functions are already built-in, you just need to call those functions by passing the required parameters into it. However, if you are learning logistic regression for the first time, then I would suggest you write your own code instead of using the sci-kit-learn library. It does not mean that the mentioned library is not useful, I only want to make you learn the core concepts of this algorithm.
Train your datasetBefore you start working on a project, it is important for us to visualize the dataset. Once we have visualized your dataset, then we will move to the major part of the project. After visualization, we will train our dataset. The training process includes calculating the probability and the cost, and then reduce the cost on the available dataset. The given dataset helps us to train our model so that accurate predictions could be made. Once the model is trained, we check our accuracy on the validation set (this is the part of the dataset, usually we use 80% of our dataset as a training set and the rest 20% as a validation set.) A validation set is required to measure the accuracy of our trained model i.e. how our model will behave when it is exposed to similar unseen data. We compare the results of a validation set with their actual labels mentioned in the dataset.
So let’s visualize our data.
First load the data from the CSV file.
loan= pd.read_csv("loan_test.csv")Then, have a look at the dataset with the following command:
The above image is an output of some dataset that aims to predict loan eligibility.
Of course, I recommend everyone who is learning ML and want to pursue a career in Data Science to learn plotting graphs using the Matplotlib library. It is going to be useful. Trust me!
By plotting your data on a graph, we can visualize the importance as well as the distribution of a particular factor.
Now in the next section, we’ll learn to make predictions using the sigmoid function.
Making predictions and implementing sigmoid function(Image source: Google search)
Calculating probability and making predictionsNow you can clearly imagine what is sigmoid function from the above graph. The y-axis is the sigmoid function and the x-axis is the dot product of theta vector and X vector.
Now we’ll write a code for it:
def calc_sigmoid(z): p=1/(1+ np.exp(-z)) p=np.minimum(p, 0.9999) p = np.maximum(p, 0.0001) return pThis is a sigmoid function that accepts a parameter z which is a dot product from the following function:
def calc_pred_func(theta,x): y=np.dot(theta,np.transpose(x)) return calc_sigmoid(y)The above function returns a probability value between [0,1].
Calculating the costNow the most important part is to reduce the cost of the predictions we made. So you must be wondering what is cost? It is the error in the calculations made from the existing labels. So we have to reduce our costs gradually. First, we calculate it using the given function:
def calc_error(y_pred, y_label): len_label=len(y_label) cost= (-y_label*np.log(y_pred) - (1-y_label)*np.log(1-y_pred)).sum()/len_label return costNow, we will work to reduce our cost using gradient descent
Reducing the cost using Gradient DescentJust to tell you about the gradient descent, the graph looks like this for this technique:
Image source: google search
Now we will implement the gradient descent technique:
def gradient_descent(y_pred,y_label,x, learning_rate, theta): len_label=len(y_label) J= (-(np.dot(np.transpose(x),(y_label-y_pred)))/len_label) theta-= learning_rate*J #print("theta_0 shape: ",np.shape(theta),np.shape(J)) return theta Training Your Model: def train(y_label,x, learning_rate, theta, iterations): list_cost=[] for i in range(iterations): y_pred=calc_pred_func(theta,x) theta=gradient_descent(y_pred,y_label,x, learning_rate, theta) if i%100==0: print("n iteration",i) print("y_label:",y_pred) print("theta:",theta) cost=calc_error(y_pred, y_label) list_cost.append(cost) print("final cost list: ",list_cost) return theta,list_cost,y_predWe will call the train function to call all functions above and you’ll see the following graph when you plot costs per iterations:
We can see the graph declining i.e. the cost is reducing.
Testing your modelNow we will test our model on the validation set:
def classify(y_test): return np.around(y_test) def predict(x_test,theta): y_test=np.dot(theta,np.transpose(x_test)) #print(y_test) p=calc_sigmoid(y_test) print(p) return p #for calculating accuracy based on validation set ''' Description of variables: x_test:numpy array (no. of rows in dataset, no. of features/columns) z_test: is added as column in x_test for the purpose of theta_0 y_label_test: actual labels y_test= numpy array of probability (note: these are probabilities NOT classes) y_test_pred_labels= numpy array of classes based of probability (note: here it is classes NOT probability) ''' x_test=loan.iloc[500:, 1:10].values x_rows_test, x_columns_test= x_test.shape z_test = np.ones((x_rows_test,1), dtype=float) x_test=np.append(x_test,z_test,axis=1) y_label_test=loan.loc[500:,"Status_New" ].values y_label_test=y_label_test.astype(float) x_test=x_test.astype(float) y_test=predict(x_test,theta) y_test_pred_labels=classify(y_test)This will make a prediction on the validation set and you will get your output as the label 0 or 1.
I hope you enjoyed my article. This was all about its implementation.
About the Author:Hi! I am Sarvagya Agrawal. I am pursuing B.Tech. from the Netaji Subhas University Of Technology. ML is my passion and feels proud to contribute to the community of ML learners through this platform. Feel free to contact me by visiting my website: sarvagyaagrawal.github.io
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.
Related
Explain Implement A Memoization Helper Function
Memoization is a helper function, or we can say a technique for improving the efficiency of a program by Keeping track of the values that the function has already calculated in the past. In this article, we will discuss the Memoization Helper function with different examples and discuss all the examples in detail so that we can get a better understanding of Memoization.
Now let’s discuss the Memoization helper function in the deep in the below section and also see their implementation with an explanation.
Introduction to Memoization Helper FunctionMemoization is a technique of programming that is used to improve the time complexity and space complexity of a program by Keeping track of the values that the function has already calculated in the past. By saving the outcomes of function calls in a cache, the program becomes more efficient. By repeatedly running the function with the same parameter that has previously been calculated, we frequently waste time. We can then cache the calculated value and just return it when the function is called with the same parameter.
Implementation of a Memoization helper functionHere, we’re going to explore a tonne of examples and explanations to help you understand the memoization helper function better.
Example1Let’s see the working of the memoization helper function with this example in which we will discuss the code, output, and explanation to get a better understanding of the concept −
function add(num1,num2){ for(let i=0;i<100000;i++){ } return num1+num2; } console.log(add(5,4)); console.log(add(5,4));Here we define function add by passing two parameters in it num1 and num2 for doing the addition of integer num1 and num2. In this function, we have run for loop and after that, we have to return the sum of both the integer.
In this case, we have called the addition function but our function taking time because of for loop. And we are calling the function again and again with the same parameter. So, if there we are using memoization by storing the value of addition so we are able to save time then we will return the cached value. We don’t need to calculate the added value for the same parameters.
Example 2Let’s see how long it took our function to determine the value of add (5,4) with the help of code and explanation −
function add(num1,num2){ for(let i=0;i<100000;i++){ } return num1+num2; } console.time("Time taken"); console.log(add(5, 4)); console.timeEnd("Time taken");Our function taken 14.441ms time to add the integers 5 and 4.
By using the memoization technique, we may improve the efficiency of the function by caching the value that has already been calculated and then returning it when the function is called with the same parameter.
Example 3Let’s now discuss how we can utilise the memoization technique to shorten the time it takes to repeatedly execute a function with the same parameter.
function memoizeFunction(func) { let storage = {}; return function (val1, val2) { const val = val1.toString() + val2.toString(); if (!storage[val]) { storage[val] = func(val1, val2); } return storage[val]; } } function add(num1, num2) { for (let i = 0; i < 10000000; i++) { } return num1 + num2; } console.time("First time, time taken"); let func = memoizeFunction(add); console.log(func(5, 4)); console.timeEnd("First time, time taken"); console.time("Second time, time taken"); func = memoizeFunction(add); console.log(func(5, 4)); console.timeEnd("Second time, time taken"); console.time("Third time, time taken"); func = memoizeFunction(add); console.log(func(5, 4)); console.timeEnd("Third time, time taken");Note − the length of time needed to finish a task can change.
In this instance, we cached the value we earlier calculated using a memoized function. When we initially use func(4,5), the parameter is first converted to string form and then saved within the object ‘storage’ along with the calculated value.
Additionally, when the function is called with the same parameter, it first determines whether or not it already exists in the object ‘storage’. If it is already calculated, it won’t do it again and will instead simply return the value that is contained inside the object ‘storage’.
As we can see from the output, each time we used the function with the same parameter, the time it took to add 5 and 4 was less.
Time is taken each time −
98.885 ms 83.375 ms 13.071 msTherefore, it is evident from the output that the memoization technique assisted in shortening the time each time we repeatedly called the function with the same argument.
Example 4Let’s discuss another example of a memorization helper function by the Fibonacci number.
function memoizeFunction(func) { let storage = {}; return function (val) { const value = val.toString(); if (!storage[value]) { storage[value] = func(val); } return storage[value]; } } function fibonacci(num) { return num; else return fibonacci(num - 1) + fibonacci(num - 2); } console.time("First time, time taken"); let func = memoizeFunction(fibonacci); console.log(func(6)); console.timeEnd("First time, time taken"); console.time("Second time, time taken"); func = memoizeFunction(fibonacci); console.log(func(6)); console.timeEnd("Second time, time taken"); console.time("Third time, time taken"); func = memoizeFunction(fibonacci); console.log(func(6)); console.timeEnd("Third time, time taken");Fibonacci series take an exponential time to execute if all the steps are performed without the help of memorization technique. By storing the previous results, we can get the pre-defined results to reduce the further checking of the calculated results and can take number of steps to linear.
ConclusionIn this article, we have learned that Memoization is a helper function or a technique for improving the efficiency of a program by Keeping track of the values that the function has already calculated in the past. By saving the outcomes of function calls in a cache, the program becomes more efficient. We can then cache the calculated value and just return it when the function is called with the same parameter.
Update the detailed information about Examples To Implement Mysql Mod() on the Achiashop.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!