def main(): from sklearn import datasets iris = datasets.load_iris() X = iris.data[: [2, 3]] y = iris.target from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y) nb = NaiveBayes() nb.fit(X_train, y_train) import pandas as pd import matplotlib.pyplot as plt import numpy as np from Perceptron import plot_decision_regions plot_decision_regions(X, y, classifier=nb) plt.tittle('Naive Bayes Trial') plt.Xlabel('Sepal Length[standardized]') plt.ylabel('Petal Length[Standardized]') plt.show()
#plot data plt.scatter(x1, y1, color='green') #apply polynomial regression with degree 5 from sklearn.preprocessing import PolynomialFeatures pol_reg = PolynomialFeatures(degree=5) x1 = x1.reshape(-1, 1) x_pol = pol_reg.fit_transform(x1) #split data set for training and testing X_train, X_test, y_train, y_test = train_test_split(x_pol, y1, test_size=0.2, random_state=5) #apply model model = LinearRegression() model.fit(X_train, y_train) b1 = model.intercept_ m1 = model.coef_ print("intercept", b1) print("slope", m1) ac1 = model.score(X_test, y_test) print("accuracy", ac1) y_pred1 = model.predict(X_test) print("prediction", y_pred1) plt.scatter(x1, y1, color='red') plt.plot(x1, model2.predict(pol_reg.fit_transform(x1)), color='blue') plt.tittle("Truth or bbluff (linear regression)") plt.xlabel("squarfit_living") plt.ylabel("price") plt.show()
digits = load_digits() #Analyzing one image. pl.gray() pl.matshow(digits.images[0]) pl.show() #Visualizing first 15 images with their labels. data = list(zip(digits.images, digits.target)) plt.figure(figsize=(5, 5)) for item, (img, label) in enumerate(data[:15]): plt.subplot(3, 5, item + 1) plt.axis('off') plt.imgshow(img, cmap=plt.cm.gray_r, interpolation='nearest') plt.tittle('%i' % label) import random from sklean import ensemble #Dividing our data in order to use it as a supervised learning. n = len(digits.images) x = digits.images.reshape((n, -1)) y = digits.target #Random indices. sample_index = random.sample(range(len(x)), len(x) / 5) valid_index = [i for i in range(len(x)) if i not in sample_index] #Images and targets to work. sample_images = [x[i] for i in sample_index]
def normal_pdf(x: float, mu: float = 0, sigma: float = 1) -> float: return (math.exp(-(x - mu)**2 / 2 / sigma**2) / (SQRT_TWO_PI * sigma)) import matplotlib.pyplot as plt xs = [x / 10.0 for x in range(-50, 50)] plt.plot(xs, [normal_pfd(x, sigma=1) for x in xs], '-', label='mu=0, sigma=1') plt.plot(xs, [normal_pfd(x, sigma=2) for x in xs], '-', label='mu=0, sigma=2') plt.plot(xs, [normal_pfd(x, sigma=0.5) for x in xs], '-', label='mu=0, sigma=0.5') plt.plot(xs, [normal_pfd(x, mu=1) for x in xs], '-', label='mu=1, sigma=1') plt.legend() plt.tittle("Various Normal pdfs ") plt.show() def normal_cdf(x: float, mu: float = 0, sigma: float = 1) -> float: return (1 + math.erf((x - mu) / math.sqrt(2) / sigma)) / 2 xs = [x / 10.0 for x in range(-50, 50)] plt.plot(xs, [normal_cfd(x, sigma=1) for x in xs], '-', label='mu=0, sigma=1') plt.plot(xs, [normal_cfd(x, sigma=2) for x in xs], '-', label='mu=0, sigma=2') plt.plot(xs, [normal_cfd(x, sigma=0.5) for x in xs], '-', label='mu=0, sigma=0.5') plt.plot(xs, [normal_cfd(x, mu=1) for x in xs], '-', label='mu=1, sigma=1') plt.legend(loc=4) # bottom right
posicion = index while posicion > 0 and n_lista[posicion - 1] > actual: times += 1 n_lista[posicion] = n_lista[posicion - 1] posicion = posicion - 1 n_lista[posicion] = actual return n_lista TAM = 101 eje_x = list(range(1, TAM, 1)) eje_y = [] lista_variable = [] for num in eje_x: lista_variable = random.sample(range(0, 1000), num) times = 0 lista_variable = insertSort_graph(lista_variable) eje_y.append(times) fig, ax = plt.subplots(facecolor='w', edgecolor='k') ax.plot(eje_x, eje_y, marker="o", color="b", lineStyle='None') ax.set_xlabel('x') ax.set_ylabel('y') ax.grid(True) ax.legend(["Insertion Sort"]) plt.tittle('Insertion sort') plt.show()
#Esta línea se ocupa para que las gráficas que se generen queden embedidas dentro de la página %pylab inline #importando las bibliotecas import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D #Datos de entrada x = linscape(0,5,20) #Generando 10 puntos entre 0 y 5 fig, ax = plt.subplots(facecolor='w', edgecolor='k') ax.plot(x, sin(x), maker="o", color="r", linestyle="None") ax.grid(True) ax.set_xlabel('X') #Etiqueta el eje x ax.set_ylabel('Y') #Etiqueta el eje y ax.grid(True) ax.legend(["y = x**2"]) plt.tittle('Puntos') plt.show() fig.savefig("Gráfica.png") #Guardando la gráfica
from matplotlib import pyplot as plt from collections import Counter variance = [1, 2, 4, 8, 16, 32, 64, 128, 256] bias_squared = [256, 128, 64, 32, 16, 8, 4, 2, 1] total_error = [x + y for x, y in zip(variance, bias_squared)] xs = [i for i, _ in enumerate(variance)] #한 차트에 여러 개의 선을 그리기 위해 plt.plot을 여러 번 호출할 수 있다. plt.plot(xs, variance, 'g-', label='variance') #실선 plt.plot(xs, bias_squared, 'g-', label='bias^2') #일점쇄선 plt.plot(xs, total_error, 'g-', label='total error') #점선 #각 선에 레이블을 미리 달아놔서 범례(legend)를 쉽게 그릴 수있다. plt.legend(loc=9) plt.xlabel("model comlexity") plt.xticks([]) plt.tittle("The Bias-Variance Tradeoff") plt.show()
#to avoid Dymmy trap x=x[:,1:] # Splitting the dataset into the Training set and Test set from sklearn.cross_validation import train_test_split x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0) #multi linear regression from sklearn.linear_model import LinearRegression regressor=LinearRegression() regressor.fit(x_train,y_train) #predict y_pred=regressor.predict(x_test) #**x-y must be the same size so it wont plot** #plot the result plt.scatter(x_train,y_train,color='red') plt.plot(x_train,regressor.predict(x_train),color='blue') plt.tittle('multi regression model') plt.xlabel('state-years of experience') plt.ylabel('profit') plt.show()
x = dataset.iloc[:, [3, 4]].values from sklearn.cluster import KMeans wcss = [] for i in range(1, 11): kmeans = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10, random_state=0) kmeans.fit(x) wcss.append(kmeans.inertia_) plt.plot(range(1, 11), wcss) plt.tittle('The elbow method') plt.xlabel('Number of clusters') plt.ylabel('wcss - square of cluster distances') plt.show #Applying Kmens to mall data set kmeans = KMeans(n_clusters=5, init='k-means++', max_iter=300, n_init=10, random_state=0) y_kmeans = kmeans.fit_predict(x) #visulize the plot plt.scatter(x[y_kmeans == 0, 0],
from matplotlib import pyplot as plt years = [1950, 1960, 1970, 1980, 1990, 2000, 2010] gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3] #x축에 연도, y축에 GDP가 있는 석ㄴ그래프를 만들자. plt.plot(years, gdp, color='green', marker='o', linestyle='solid') # 제목을 더하자. plt.tittle("Nominal GDP") # y축에 레이블을 추가하자. plt.ylable("Billions of $") plt.show()
x = x[:, 1:] #split_test_train from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.25, random_state=0) #multi linear regression from sklearn.linear_model import LinearRegression regressor = LinearRegression() regressor.fit(x_train, y_train) #plot the result plt.scatter(x_train, y_train, color='black') plt.plot(x_train, regressor.predict(x_train), color='green') plt.tittle('startup') plt.xlabel('independent varaibles') plt.ylabel('profits') #backwards emlination import statsmodels.api as sm x = np.append(np.ones((50, 1)).astype(int), x, axis=1) x_opt = x[:, [0, 1, 2, 3, 4]] regressor_ols = sm.OLS(endog=y, exog=x_opt).fit() regressor_ols.summary()
#dans la vidéo y_pred=sy_y.inverse_transform(regressor.predict(sc.X.transform(np.array([6,5])))) y_pred= sv_y.inverse_transform(regressor.predict(sc_x.transform(np.array(([6,5])))) # Visualising the SVR results plt.scatter(X, y, color = 'red') plt.plot(X, regressor.predict(X), color = 'blue') plt.title('Truth or Bluff (SVR)') plt.xlabel('Position level') plt.ylabel('Salary') plt.show() plt.scatter(X,Y, color='blue') plt.plot (regressorr.predict(X), color='red') plt.tittle('') plt.xlabel('') plt.ylabel('') plt.show('') # Visualising the SVR results (for higher resolution and smoother curve) X_grid = np.arange(min(X), max(X), 0.01) # choice of 0.01 instead of 0.1 step because the data is feature scaled X_grid = X_grid.reshape((len(X_grid), 1)) plt.scatter(X, y, color = 'red') plt.plot(X_grid, regressor.predict(X_grid), color = 'blue') plt.title('Truth or Bluff (SVR)') plt.xlabel('Position level') plt.ylabel('Salary') plt.show()
#Fitting Linear Regression to dataset from sklearn.linear_model import LinearRegression lin_reg = LinearRegression() lin_reg.fit(X, y) #Fitting Polynomial regression to dataset from sklearn.preprocessing import PolynomialFeatures poly_reg = PolynomialFeatures(degree=4) X_poly = poly_reg.fit_transform(X) lin_reg2 = LinearRegression() lin_reg2.fit(X_poly, y) #Visualiizing linear regression results plt.scatter(X, y, color='red') plt.plot(X, lin_reg.predict(X), color='blue') plt.tittle('Truth or Bluff (LR)') plt.xlabel('Position level') plt.ylabel('salary') plt.show() #Visualizing Polynomial regression results X_grid = np.arange(min(X), max(X), 0.1) X_grid = X_grid.reshape(len(X_grid), 1) plt.scatter(X, y, color='green') plt.plot(X_grid, lin_reg2.predict(poly_reg.fit_transform(X_grid)), color='yellow') plt.tittle('Truth or Bluff (PR)') plt.xlabel('Position level') plt.ylabel('salary') plt.show()
#data processing data=pd.read_csv('Salary_Data.csv') x=data.iloc[:,:-1].values y=data.iloc[:,1].values #train_split_test from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=1/3,random_state=0) #linear regression from sklearn.linear_model import LinearRegression linearregression=LinearRegression() linearregression.fit(x_train,y_train) #predict the model y_pred=linearregression.predict(x_test) #plot the result plt.scatter(x_train,y_train,color='red') plt.plot(x_train,linearregression.predict(x_train),color='blue') plt.tittle('linear regression') plt.xlabel('years of experience') plt.ylabel('salary') plt.show()
from matplotlib import pyplot as plt movies = ["Annie Hall", "Ben_Hue", "Casablacna", "Gandhi", "West Side Story"] num_oscars = [5, 11, 3, 8, 10] # 막대의 x 좌표는 [0, 1, 2, 3, 4], y좌표는 [num_oscars]로 설정 plt.bar(range(len(movies)), num_oscars) plt.tittle("My Favorite Movies") plt.ylabel("# of Academy Awards") plt.xticks(range(len(movies)), movies) plt.show()
lista_qs = lista_is.copy() t0 = time() #Se queda el tiempo inicial insertionSort_time(lista_is) tiempo_is.append(round(time()-t0,6)) t0 = time () quickSort_time(lista_qs) tiempo_qs.append(round(time()-t0,6)) #Imprimiendo tiempos parciales de ejecución print("Tiempos parciales de ejcución en INSERT SORT {} [s]\n".format(tiempo_is)) print("Tiempos parciales de ejcución en QUICK SORT {} [s]".format(tiempo_qs)) #Imprimiendo tiempos totales de ejecución #Para calcular el tiempo total se aplica la función sum() a las listas de tiempo print("Tiempo total de ejcución en INSERT SORT {} [s]\n".format(sum(tiempo_is))) print("Tiempo total de ejcución en QUICK SORT {} [s]\n".format(sum(tiempo_qs))) #Generandola gráfica fig, ax = subplots() ax.plot(datos, tiempo_is, label ="insert sort", marker="*", color="r") ax.plot(datos, tiempo_qs, label ="quick sort", marker="o", color="b") ax.set_xlabel('Datos') ax.set_ylabel('Tiempo') ax.grid(True) ax.legend(loc=2); plt.tittle('Tiempo de ejcución [s] (insert vs. quick)') plt.show()
new_data.plot.scatter(x='like' , y='Total Interaciones') # In[286]: dataset.plot.box() # In[ ]: for c in dataset.columns.sort_values(); plt.figure() dataset[c].hist() plt.tittle(t) # In[299]: grouped = dataset.groupby("Dia_Semana").mean() grouped["Total Interaciones"].plot() plt.show() # In[331]: dataset_col = dataset.Dia_Semana.value_counts() dataset_col.plot.bar()
from matplotlib import pyplot as plt from collections import Counter friends = [70, 65, 72, 63, 71, 64, 60, 64, 67] minutes = [175, 170, 205, 120, 220, 130, 105, 145, 190] lables = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] plt.scatter(friends, minutes) #각 포인트에 레이블을 달자,. for label, friend_count, minute_count in zip(lables, friends, minutes): plt.annotate( label, xy=(friend_count, minute_count), #레이블을 데이터 포인트 근처에 두되 xytext=(5, -5), # 약간 떨어져 있게 하자. textcoords='offset points') plt.tittle("Daily Minutes vs. Number of Friens") plt.xlabel("# of friends") plt.ylabel("daily minutes spent on the site") plt.show()
plt.legend() plt.show() #pie chart from matplotlib import pyplot as plt lab = 'python', 'c++', 'ruby', 'java' sizes = 215, 130, 245, 210 cols = ('c', 'm', 'r', 'b') plt.pie(sizes, labels=lab, explode=(0, 0, 0.2, 0), colors=cols, startangle=140, autopct='%0.2f%%', shadow=False) plt.tittle('pie') plt.show() help(plt.pie) #scatter from matplotlib import pyplot as plt x = [1, 2, 3, 1, 5, 5, 1, 8, 2, 3, 3, 3, 6] y = [7, 7, 5, 6, 1, 5, 2, 5, 3, 6, 4, 6, 6] x1 = [4, 5, 4, 5, 8, 6, 4, 8, 2, 5, 0] y1 = [6, 8, 9, 9, 8, 6, 5.2, 3.2, 5, 6, 0] plt.scatter(x, y, label='high', color='r') plt.scatter(x1, y1, label='low', color='g') plt.title('scatter Chart') #label-axis name plt.xlabel('Xaxis') plt.ylabel('Yaxis') plt.legend() #display label axis name in chart
import matplotlib.pyplot as plt import numpy as np fs=input('Enter value for fs:') f=input('Enter value for f:') x=np.arange(0,100,1) y1=np.sin(2*np.pi*f*x/fs) y2=np.cos(2*np.pi*f*x/fs) y=y1+y2 plt.subplot(1,3,1) plt.plot(x,y1) plt.tittle('sin wave') plt.xlabel('------------->t') plt.y1label('------------>voltage') plt.subplot(1,3,2) plt.plot(x,y2) plt.tittle('cos wave') plt.xlabel('------------->t') plt.y2label('------------>voltage') plt.subplot(1,3,3) plt.plot(x,y) plt.tittle('sin wave and cos wave') plt.xlabel('------------->t') plt.ylabel('------------>voltage') plt.show()
eating = [1, 2, 1, 3, 2] playing = [3, 4, 2, 6, 1] working = [4, 5, 8, 3, 9] plt.plot([], [], color='m', label='sleeping', linewidth=5) plt.plot([], [], color='c', label='eating', linewidth=5) plt.plot([], [], color='r', label='working', linewidth=5) plt.plot([], [], color='k', label='playing', linewidth=5) plt.stackplot(days, sleeping, eating, working, playing, colors=['m', 'c', 'r', 'k']) plt.tittle('stackplot') plt.xlabel('day') plt.ylabel('Activities') plt.legend() # to setting up labels plt.grid(True, color='k') # back group grid with black color plt.show() # generate graph # Pie plots from matplotlib import pyplot as plt slices = [7, 2, 2, 13] # percentage of the Pie. labs = ['IBM', 'TCS', 'WIPRO', 'INFY'] col = ['m', 'c', 'r', 'g'] plt.pie(slices, labels=labs, colors=col,