3

There are some simple codes (like data clustering etc.) that I use again and again, and every time either I search online or look at my previous codes.

Then I thought of creating functions for the codes and writing them to a module. So, I created an myfunctions.py file with content like:

import numpy as np
import pandas as pd
from sklearn.cluster import KMeans

def kmeans(data,elbowplot=False,maxcluster=10,
           nclusters,scatter2=False,dotsize=50,colormap='viridis'):

    data = pd.DataFrame(data)

    if elbowplot == True:
        sum_of_squares = []
        K = range(1,maxcluster)
        for k in K:
            kmeansAlgo = KMeans(n_clusters=k).fit(data)
            sum_of_squares.append(kmeansAlgo.inertia_)

        plt.plot(K, sum_of_squares, 'bo-')
        plt.xlabel('number of clusters')
        plt.ylabel('Sum of squared distances')
        plt.title('Elbow Method For Optimal k')
        plt.show()

    kmeansAlgo = KMeans(n_clusters=nclusters)
    kmeansAlgo.fit(data)
    kmeansgroups = kmeansAlgo.predict(data)

    if scatter2 == True:
        x = data.iloc[:,0]
        y = data.iloc[:,1]
        plt.scatter(x, y, c=kmeansgroups, s=dotsize, cmap=colormap)

    return kmeansgroups

Then, in another file, I simply type

import myfunctions

and start using my functions. However, I suspect that there is something inefficient in this approach. For example, I have to import the modules (like pandas) again in my code.

So, my question is, why do I have to import the modules again? And is using this approach inefficient?

ThePortakal
  • 229
  • 2
  • 10
  • Re-importing the same module just results in a new reference to the module, so it shouldn't cause issues (https://stackoverflow.com/questions/19077381/what-happens-when-a-module-is-imported-twice). If you're only doing this for personal use it should be fine. – clubby789 Oct 15 '19 at 13:09

3 Answers3

1

Strictly speaking you don't have to import the modules again as you could access them as e.g.

myfunctions.np

This would take more code as instead of just typing something like

a = np.zeros(shape=(30,20))

you're now typing

a = myfunctions.np.zeros(shape=(30,20))

Besides being impractical there is another reason why you shouldn't do this: you should try to do all your imports at the top of your file in such a way that anyone reading your code knows what modules you are importing and what names you are using for them.

In general, if you're using certain functions again and again for various projects or in various files within one project, it is not a bad idea to put those functions into a module. But don't think of writing the import statements both in this module and in the other files as inefficient, think of it as improving readability.

simon
  • 796
  • 1
  • 6
  • 11
1

Nothing serious happens, if you import a form that has already been imported it is simply not loaded again, you will simply get a reference to the form you have already imported.

Massifox
  • 4,369
  • 11
  • 31
0

No this is the exact use case for modules and importable libraries in python. Also importing a module multiple times does not actually import it that many times in python.

This is the standard practice in python and not inefficient at all.

rahul tyagi
  • 643
  • 1
  • 7
  • 20