Pycharm is a very popular python IDE which is a cross-platform IDE that was developed by JetBrains to use it for python development. Many people these days believe that python is the best language where a user can build software applications by writing clean and readable code. Python is the favourite language for many, especially people working in Data Science and Machine Learning. Big giants like Facebook, Amazon, and Twitter use Pycharm as their IDE for writing codes in python. Pycharm supports python 2.0 and python 3.0 also once can work with Pycharm on Mac and Windows as well. There are many advantages of using Pycharm that include making it easier for the people to code quickly and efficiently using different software applications provided by Pycharm.
This article will demonstrate everything you need to know before writing your first code in Pycharm IDE. The article will take you from installing the software and dependencies to writing your first code in Pycharm followed by a classification problem on iris data set where we will classify which class does the flower belong to.
What you will learn from this article?
- Why should we use Pycharm
- How to install Pycharm and do the configuration for the first project
- Advantages of Pycharm
- How to install python packages in Pycharm
- Implementation of a classification model in Pycharm
1. Why should we use Pycharm IDE?
2. How to install Pycharm IDE and do the configuration for the first project?
Let’s start with installing Pycharm. Visit the below link to download the Pycharm software. There are two different versions available for installing it on Windows one is a Professional version that is paid one which comes with advanced features and another is a community that can be downloaded that is free of cost. You can download it and install it. Below is the image that describes the instructions.
Once you have installed the software, if you run the application you will see something like this as shown in the image below. You will be asked for UI themes. You can choose anyone you want to go with and then click on Next Features plugins.
Once you click on next steps it will automatically give you a welcome screen interface that is shown below having few of the options that include:-
- New Project
- Open a project
If you are already working on an existing project you can open that by using the open options or if you want to create a new project you can make a new project as your selection.
As we do not have any of the existing projects we will create a new project. Once you click on the new project you will see an output screen as shown below where we have to define the name of our project and choose the environment. I have given the project name as an example and environment as conda whereas you can give whatever name you like and the same goes for the environment. After doing the same we need to click on create.
After you create your project you will see a new interface as shown below. The left side is everything about your project and on the right-hand side, you will be able to code. This code is written in the main.py that will arise automatically.
Since we have set up a new project lets add a new python file in our project and write our first python code in Pycharm.
Right Click on Example and choose New -> Click on Python file-> Name your file.
I have created a file with a name test. Now we will write our code in this file. You can check the below image for your reference that shows you a test.py where we have written a code to print a welcome message. After writing the code you can run it by right-clicking on the file name and click on run. The output of the code would be shown below at the last of the interface.
3. Advantages of Pycharm
Now we will explore some of the helpful features and advantages of Pycharm that are listed below:-
a. We can replicate a copy of code using the shortcut key Ctrl + D.
b. We can also refract a variable. Suppose we have to change the name of a variable and want it to reflect in all our code. We can do that by refracting.
Right click on the variable you want to refract-> Click on Refactor -> Rename -> Choose new name of variable.
c. We don’t have to write each and every line of code. Pycharm gives predictive suggestions like shown in the below image.
4. How to install Python Packages in Pycharm?
Now let’s see how we can import different packages in Pycharm. Follow the below steps to install your desired python package.
- Select your project
- Go to Settings-> Under your project name select -> Python Interpreter
Once we click on the interpreter we will see the interface shown below in the image that describes all the packages that are already installed. To install our package we need to click on the + icon shown on the right side of the image.
After clicking on that you will see a new window where you can search for the package and install it. Check the below image for your reference where we are installing the pandas.
Once it is installed we will see a message below that says that the package has installed successfully as shown in the below image.
This way we can install multiple packages that we require for our project.
5. Implementation of Classification Model in Pycharm
Let us now build a classification model in Pycharm IDE. We need to first save the data file inside the project folder that was created titled ‘example’. We have added a Pima diabetes data set inside that folder and now we will read that data. The output for the same is shown in the image below the code.
import pandas as pd df= pd.read_csv(‘pima.csv’) print(df)
After importing the data we have checked whether there are any missing values or not.
print(df.isnull().sum()) print(df.columns) print(df.columns)
After checking the column we have defined the dependent and independent variable X and y respectively followed by splitting the dataset into training and test sets.
X = df.drop('class', axis=1) y = df['class'] from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) print(X_train.shape) print(y_train.shape) print(X_test.shape) print(y_test.shape)
After checking the shape we have then defined the classifier that would be used for classifying. We will be using a random forest classifier. After that, we have fitted the training data to the classifier and made predictions on testing data. At last, we have evaluated the model using different metrics like accuracy score, confusion matrix, recall, and precision. Use the below code to the same.
from sklearn.ensemble import RandomForestClassifier rfcl = RandomForestClassifier() rfcl.fit(X_train,y_train) y_pred = rfcl.predict(X_test)
from sklearn.metrics import accuracy_score,confusion_matrix,precision_score,recall_score print(accuracy_score(y_pred,y_test)) print(confusion_matrix(y_pred,y_test)) print(precision_score(y_pred,y_test)) print(recall_score(y_pred,y_test))
I would like to conclude the article by hoping that you have now understood the whole working of Pycharm from installing Pycharm to building a classification model. Pycharm is a very good IDE due to its capabilities of predictive suggestions whereas you can also explore other coding platforms like Jupyter Notebook and Google Colab. Google Colab even provides GPUs for fast processing when dealing with image data. Also, see a similar article where I build a CNN model using GPU on Google Colab. “How to build CNN model for finger count classification”. Also, you can read more about Pycharm here in this article.