How To Install Python Package Numpy, Pandas, Scipy, Matplotlib On Windows, Mac And Linux

If you want to do data analysis in python, you always need to use python packages like Numpy, Pandas, Scipy and Matplotlib, etc. All those python packages are so powerful and useful to do Base N-dimensional array computing( Numpy ), Data structures & analysis ( Pandas ), scientific computing ( Scipy ), and Comprehensive 2D Plotting ( Matplotlib ). But the first step is to install the related packages on your OS, this article will tell you how to install them on Windows, Mac, and Linux.

1. Install Numpy, Pandas, Scipy, Matplotlib With Anaconda.

  1. Anaconda is a python edition that is used in scientific areas, so if you install Anaconda, all the above packages will be installed automatically. So please read the article How To Install Anaconda On Linux, Windows, macOS Correctly to install anaconda first.
  2. After installation, you can run command conda in a terminal to list the above packages to make sure it has been installed correctly like below.
    ~$ conda list pandas
    # packages in environment at /home/zhaosong/anaconda3:
    #
    # Name                    Version                   Build  Channel
    pandas                    0.23.4           py37h04863e7_0
  3. To list all installed anaconda packages, just run the command $ conda list.
  4. Run $ conda -h to list the conda command help information.
  5. If you want to remove/uninstall a package, run $ conda remove <package name>

2. Install Numpy, Pandas, Scipy, Matplotlib By PIP Command.

  1. First, make sure pip has been installed on your OS. If it is not installed, please refer article How To Install Python/Pip On Windows.
    ~$ pip --version
    pip 18.1 from /home/zhaosong/anaconda3/lib/python3.7/site-packages/pip (python 3.7)
    
  2. Run pip install command to install related packages.
    pip install numpy
    
    pip install pandas
    
    pip install scipy
    
    pip install matplotlib
    
    
  3. Run pip uninstall command to uninstall related packages.
    pip uninstall numpy
    
    pip uninstall pandas
    
    pip uninstall scipy
    
    pip uninstall matplotlib
  4. Run pip show command to display package install information.
    ~$ pip show pandas
    Name: pandas
    Version: 0.23.4
    Summary: Powerful data structures for data analysis, time series, and statistics
    Home-page: http://pandas.pydata.org
    Author: None
    Author-email: None
    License: BSD
    Location: /home/zhaosong/anaconda3/lib/python3.7/site-packages
    Requires: python-dateutil, pytz, numpy
    Required-by: seaborn, odo

3. How To Install Correct Numpy, Scipy, Matplotlib Package For Multiple Python Versions.

  1. I have 2 python versions installed on my Ubuntu Linux OS, they are python 2.7 and python 3.8. The python 2.7 is a built-in python version when I installed Ubuntu. And I install python 3.8 manually.
  2. Now I want to install the Scipy library on my Ubuntu Linux OS, and I find the below command in scipy.org, then I run the below command in a terminal.
    python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
  3. But when the above command execution was complete, I found it only install the NumPy, Scipy, Matplotlib, iPython, etc for python 2.7. It does not install the packages for python 3.8 as I want, how can I resolve this issue? Thanks a lot.
  4. This is because Ubuntu Linux installed python 2.7 by default, then the default pip command is also of python version 2.7.
  5. So when you run the python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose command, it will use python 2.7’s pip command to install.
  6. To fix this issue, you need to first install the pip command for python 3.8 ( sudo apt-get install python3-pip ), and then run the pip3 command to install all the Scipy packages for python 3.8 ( python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose ).

4. How To Fix The Error: Setup Script Exited With Pandas Requires Numpy >= 1.6 Due To Datetime64 Dependency During The Installation Of Python Pandas.

  1. I use macOS version 10.15 to develop a python program, and I want to install pandas in it.
  2. I run the command sudo easy_install pandas to install it, and it returns an error message like below.
    error: Setup script exited with pandas requires NumPy >= 1.18 due to datetime64 dependency
  3. So I get my Numpy version by executing the command pip show numpy like below, I find the installed Numpy version is less than 1.18.
    $ pip show numpy
    Name: numpy
    Version: 1.16.1
    Summary: NumPy is the fundamental package for array computing with Python.
    Home-page: https://www.numpy.org
    Author: Travis E. Oliphant et al.
    Author-email: None
    License: BSD
    Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
  4. So I run the command sudo easy_install numpy to install the python NumPy package and its success.
  5. When I run the command sudo easy_install pandas to install the python pandas package again, the error message still exists.
  6. I finally find the below method to fix this error.
  7. Open a dos window, then run the command python to go to the python interactive console.
    (MyPythonEnv) C:\Users\zhaosong>python
    Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  8. Then run the below command to get the existing Numpy package installation directory.
    >>> import numpy
    >>> print(numpy)
    <module 'numpy' from 'C:\\Users\\zhaosong\\anaconda3\\envs\\MyPythonEnv\\lib\\site-packages\\numpy\\__init__.py'>
  9. Go to the python Numpy module installed directory and remove the entire directory.
  10. Now you can install the latest version of Numpy with the command sudo easy_install numpy or sudo pip install numpy.

Subscribe to receive more programming tricks.

We don’t spam!

close

Subscribe to receive more programming tricks.

We don’t spam!

3.8 4 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Amanuel
Amanuel

Practical 1: Line
import matplotlib.pyplot as plt
import numpy as np
#input values
x = np.array([10,9,2,15,10,16,11,16])
y = np.array([95,80,10,50,45,98,38,93])
for i in range(0,len(x)):
plt.plot(x[i],y[i],”gX”)
#slope intercept cal
slope, intercept = np.polyfit(x, y, 1)
y= slope*x + intercept
#plotting co-ordinates
for i in range(0,len(x)):
plt.plot(x[i],y[i],”bo”)
plt.plot(x, y, ‘-r’, label=’y=mx+b’)
plt.ylabel(‘Risk Score on a scale of 0-100’)
plt.xlabel(‘NO. of hours spent on Driving ‘)
plt.title(“Linear Regression”)
plt.show()

Practical 2: Decision Tree
import numpy as np
import pandas as pd
print(“\n\n”)
#reading dataset
dataset = pd.read_csv(“DT.csv”)
x = dataset.iloc[:,:-1]
y = dataset.iloc[:,5].values
#perform label encoding
from sklearn.preprocessing import LabelEncoder
labelencoder_X = LabelEncoder()
x= x.apply(LabelEncoder().fit_transform)
print(x)
from sklearn.tree import DecisionTreeClassifier
regressor = DecisionTreeClassifier()
regressor.fit(x.iloc[:,1:5].values,y)
#predict value for the given expression
x_in =np.array([1,1,0,0])
Y_pred = regressor.predict([x_in])
print(“\n———————————————“)
print (“\n\nPrediction of given Test Data is {}
“.format(Y_pred[0]))
print(“\n———————————————“)
#from sklearn.externals.six import StringIO
from six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(regressor, out_file = dot_data, filled =
True,rounded = True,special_characters = True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png(“tree.png”)

Practical 3: KNN
import numpy as np
import pandas as pd
#reading dataset
dataset = pd.read_csv(“data.csv”)
x = dataset.iloc[:,:-1]
y = dataset.iloc[:,2].values
#perform label encoding
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=3)
classifier.fit(x.values,y)
#predict value for the given expression
X_in =np.array([6,2])
y_pred = classifier.predict([X_in])
print(“\n\n —————————————–
———————————-\n”)
print (“\tPrediction of the Given Values [6,2] is :
{} “.format(y_pred[0]))
classifier =
KNeighborsClassifier(n_neighbors=3,weights=”distance”
)
classifier.fit(x.values,y)
y_pred = classifier.predict([X_in])
print (“\n\tDistance Weight KNN: “, y_pred)

Practical 4: K-means
from statistics import mode
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Data Set
X = [ [0.1,0.6],[0.15,0.71],[0.08,0.9],[0.16,0.85],
[0.2,0.3],[0.25,0.5],[0.24,0.1],[0.3,0.2]
]
#Initalize Centre Points
centers = np.array( [ [0.1,0.6] , [0.3,0.2] ] )
print(“\n\nInitial Centriods -> {} and
{}”.format(centers[0],centers[1]))
#Generating the Model
from sklearn.cluster import KMeans
model = KMeans(n_clusters=2,init= centers, n_init=1)
model.fit(X)
print(“Labels -> {} “.format(model.labels_))
print(“\n —————————————————
————————————————————
“)
print(“\n\t\t — Answer of Given Questions –“)
# Which cluster does P6 belongs to?
print(“\n\tP6 Belongs to Cluster : {}
“.format(model.labels_[5]))
# What is the population of cluster around m2?
print(“\n\tPopulation around Cluster ‘m2 = [0.15,0.71]’ : {}
“.format(np.count_nonzero(model.labels_== 1)))
# What is updated value of m1 and m2(New Centriods)?
print(“\n\tUpdates Values of m1 and m2 ‘New Centriods’ : {}
and
{}”.format(model.cluster_centers_[0],model.cluster_centers_[1
]))

Amanuel
Amanuel
Reply to  Amanuel

Practical 1: SDES
from Crypto.Cipher import DES
from secrets import token_bytes
key = token_bytes(8)
def encrypt(message):
cipher = DES.new(key , DES.MODE_EAX)
nonce = cipher.nonce
ciphertext
,tag=cipher.encrypt_and_digest(message.encode(‘ascii’
))
return nonce, ciphertext, tag
def decrypt(nonce , ciphertext,tag):
cipher=DES.new(key , DES.MODE_EAX , nonce=nonce)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return plaintext.decode(‘ascii’)
except:
return False
pt = input(“Enter the Message -> “)
nonce,ciphertext,tag=encrypt(pt)
print(“\n\t\t\t\t\t– !! DES ENCRYPTION !! –“)
print(f”\n\tCipher Text of ‘{pt}’ ->
“+str(ciphertext))
plaintext = decrypt(nonce,ciphertext,tag)
if not plaintext:
print(“Corrupted Message”)
else:
print(“\n\tPlain Test Decrypted -> “+str(plaintext))

Practical 2: AES
from Crypto.Cipher import AES
key = b’SIXTEEN BYTE KEY’
def encrypt(message):
cipher = AES.new(key , AES.MODE_EAX)
nonce = cipher.nonce
ciphertext
,tag=cipher.encrypt_and_digest(message.encode(‘ascii’))
return nonce, ciphertext, tag
def decrypt(nonce , ciphertext,tag):
cipher=AES.new(key , AES.MODE_EAX , nonce=nonce)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return plaintext.decode(‘ascii’)
except:
return False
pt = input(“Enter the Message -> “)
nonce,ciphertext,tag=encrypt(pt)
print(“\n———————————————————
——————————————————————
——————“)
print(“\n\t\t\t\t\t– !! AES ENCRYPTION !! –“)
print(f”\n\tCipher Text of ‘{pt}’ -> “+str(ciphertext))
print(“\n———————————————————
——————————————————————
——————“)
plaintext = decrypt(nonce,ciphertext,tag)
if not plaintext:
print(“Corrupted Message”)
else:
print(“\n—————————————————-
——————————————————————
——————-“)
print(“\n\tPlain Test Decrypted -> “+str(plaintext))

Practical 3: Diffie Hillman
import math
import time
print(“———————————————————————
—————————-“)
print(“\n\t\tInput”)
p = int(input(“\nEnter the Global Value – ‘p’ -> “))
g = int(input(“\nEnter the Global Value – ‘g’ -> “))
print(“\n\t\tPrivate Key Input”)
xa = int(input(“\nEnter the Private Key of user ‘A ‘ : ‘Xa’ -> “))
xb = int(input(“\nEnter the Private Key of user ‘B ‘ : ‘Xb’ -> “))
print(“\n——————————————————————-
——————————“)
ya= math.fmod((g**xa) , p)
yb= math.fmod((g**xb) , p)
print(“———————————————————————
—————————-“)
print(“\n\t\tOutput\t\t”)
print(“\n\tPrivate Keys\n”)
print(“\n\tPrivate key of user ‘A’ -> “+str(xa))
print(“\n\tPrivate key of user ‘B’ -> “+str(xb))
print(“\n\n\tPublic Keys”)
print(“\n\tPublic key of user ‘A’ -> “+str(ya))
print(“\n\tPublic key of user ‘B’ -> “+str(yb))
print(“\n——————————————————————-
——————————“)
print(“\n\t–!! Exchanging Public Keys !!–“)
time.sleep(3)
print(“\n——————————————————————-
——————————“)
ka= math.fmod((yb**xa) , p)
kb= math.fmod((ya**xb) , p)
print(“\n——————————————————————-
——————————“)
print(“\n\tSecret key of user ‘A’ -> “+str(ka))
print(“\n\tSecret key of user ‘B’ -> “+str(kb))
print(“\n——————————————————————-
——————————“)
if ka==kb :
print(“\n————————————————————-
————————————“)
print(“\n\t!!! The Seceret Keys are Verified and Sucessfully
Exchanged Public Keys !!!” )
print(“\n————————————————————-
————————————“)

Practical 4: RSA
import math
print(“\n———————————————————
–“)
print(“\n\t\t\t INPUT VALUES”)
p = int(input(“\n\tEnter First Prime NO. ‘p’ -> “))
q = int(input(“\n\tEnter Second Prime NO. ‘q’-> “))
print(“\n———————————————————
–\n”)
m = int(input(“\n\tEnter the Message to Cypher -> “))
print(“\n———————————————————
–\n”)
# cal n and phi(n)
n = p*q
phin = (p-1)*(q-1)
#for finding e
e= 0
for i in range(2,phin):
if((math.gcd(i,phin)==1)):
e= i
break
#for finding d
d = 1
for i in range(e,phin):
if((math.fmod((i*e),phin)==1)):
d = i
break
#ciypher text
c = int(math.fmod((m**e),n))
print(“\n\n\n\n—————————————————
——–\n”)
print(“\n\t\t\t Output VALUES”)
print(“\n\tFollowing is the Cypher Text ‘c’-> “,c)
print(“\n———————————————————
–\n”)
#decrypt
decrypt = int( math.fmod((c**d),n))
print(“\n\tFollowing is the decyphered message -> “,decrypt)

Practical 5: Elliptic Curve
from tinyec import registry
import secrets
import time
#For Display Purpose
def compress(pubKey):
return hex(pubKey.x) + hex(pubKey.y % 2)[2:]
#Curve Selection
curve = registry.get_curve(‘secp192r1’)
print(“\nThe Curve Used is -> secp192r1 “)
print(“\n———————————————————
———————————-“)
print(“\n\t\t\t PUBLIC KEYS”)
#User A
APrivKey = secrets.randbelow(curve.field.n) #na
APubKey = APrivKey * curve.g #PA = na * G
print(“\n\n\tUser ‘A’ public key:”, compress(APubKey))
#User B
BPrivKey = secrets.randbelow(curve.field.n) #nb
BPubKey = BPrivKey * curve.g #PB = nb * G
print(“\n\tUser ‘B’ public key:”, compress(BPubKey))
print(“\n———————————————————
———————————-“)
print(“\n\t\t\tNow exchange the public keys”)
time.sleep(3)
print(“\n———————————————————
———————————-“)
print(“\n\t\t\t Shared KEYS”)
#Display Shared Key ‘K’
ASharedKey = APrivKey * BPubKey #K = na * PB
print(“\n\tUser ‘A’ Shared key:”, compress(ASharedKey))
BSharedKey = BPrivKey * APubKey #K = nb* PA
print(“\n\tUser ‘B’ Shared key:”, compress(BSharedKey))
print(“\n———————————————————
———————————“)

2
0
Would love your thoughts, please comment.x
()
x