Numpy is a very useful library. It stands for Numerical Python. In this blog, I will cover everything you need to know to use NumPy.
It is a package used for Scientific Calculation for Python. Calculations are an integral part when analyzing the dataset. Simple mean, median or standard deviations are something that is used quite often in Datasets.
But can’t we calculate using Python? So yes you can, but it will only slow you down.
Why Should We Use Numpy ?
- 1 Why Should We Use Numpy ?
- 2 Let’s Dive in Numpy and its Functions:
- 3 A Simple Project using Numpy:
- 4 How many rides were ever recorded ?
- 5 Average Tip Given To Drivers:
- 6 Tips That were more than 50 Dollars :
- 7 Number Of Online Payments Vs Cash Payments:
- 8 Longest Trip Distance Ever Recorded:
- 9 Fare of Longest Trip :
It is Fast :
It is written in C, and it is really fast. Let’s say if it takes you 100 days to calculate something, with the help of NumPy, It will take a single day. Isn’t it Fantastic? Let me show you:
Python lists and loops took 152 milliseconds while NumPy took 1.6
Ease Of Use :
You can write small, concise, and intuitive mathematical expressions like np. dot() rather than using loops.
Let’s Dive in Numpy and its Functions:
It is used in many fields such as :
But you will be using statistical computing more than anything else as a Data Analyst.
A Simple Project using Numpy:
Before We go ahead , Download This CSV file , so that we can analyze it. It is Data of New York Taxis and it looks something like this :
Step 1 :
Generating Matrix/Array with the help of Numpy:
taxi_data = np.genfromtxt('nyc_taxis.csv',delimiter=',',skip_header=True)
How many rides were ever recorded ?
Average Tip Given To Drivers:
Index Number of Column tip_amount is 12, so we need to slice the whole column:
array([11.65, 8. , 0. , ..., 5. , 8.95, 0. ])
Tips That were more than 50 Dollars :
tip_amount = taxi_data[:,12] fifty = tip_amount>50 tip_amount[fifty]
array([ 52.8 , 60. , 59.34, 80. , 70. , 60. , 55. , 65. , 80. , 62. , 100. , 58. , 62. , 75.7 , 60. , 70. ])
Number Of Online Payments Vs Cash Payments:
So before coding, let me tell you , as you can see , Last column is Payment type and it has two values:
1 means Cash Payment
2 means Online Payments
online_payments = taxi_data[:,-1]==1 online_payments.sum()
cash_payments = taxi_data[:,-1]==2 cash_payments.sum()
Longest Trip Distance Ever Recorded:
trip_distance = taxi_data[:,7] maximum_distance = max(trip_distance) maximum_distance
Fare of Longest Trip :
trip_distance = taxi_data[:,7] maximum_distance = max(trip_distance) index_of = np.where(trip_distance==182.9) fare_amount = taxi_data[:,9] fare_amount[index_of]
So these are few analysis, you can perform with the help of Numpy.