Untitled
unknown
plain_text
a year ago
10 kB
3
Indexable
Never
Мария Перебейнова, [21.05.2023 0:01] from sklearn.model_selection import train_test_split from sklearn.datasets import load_diabetes def load_split(): diabet = load_diabetes() X_train, X_test, y_train, y_test = train_test_split(diabet.data, diabet.target, random_state = 42, test_size=0.5) return X_train, y_train Мария Перебейнова, [21.05.2023 0:01] from sklearn.linear_model import LinearRegression def uniform(X_train,y_train): ans = LinearRegression().fit(X_train, y_train) return ans.coef_ def predict_model(model,X_test): return model.predict(X_test) Мария Перебейнова, [21.05.2023 0:01] from sklearn.linear_model import LinearRegression def uniform(X_train,y_train): ans = LinearRegression().fit(X_train, y_train) return ans.coef_ def predict_model(model,X_test): return model.predict(X_test) Мария Перебейнова, [21.05.2023 0:02] from sklearn import datasets from sklearn.model_selection import train_test_split def load_split(): iris = datasets.load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.8, random_state=42) return X_train[:15],y_train[:15] Мария Перебейнова, [21.05.2023 0:02] from sklearn.linear_model import LogisticRegression def train_model(X_train,y_train): ans = LogisticRegression(random_state = 42).fit(X_train, y_train) return ans.coef_ def model_predict(X_test, model): return model.predict(X_test)[:15] Мария Перебейнова, [21.05.2023 0:02] from sklearn.linear_model import LogisticRegression def train_model(X_train,y_train): ans = LogisticRegression(random_state = 42).fit(X_train, y_train) return ans.coef_ def model_predict(X_test, model): return model.predict(X_test)[:15] Мария Перебейнова, [21.05.2023 0:02] from sklearn.metrics import accuracy_score def accuracy(y_test,y_pred): return (y_test==y_pred).mean() Мария Перебейнова, [21.05.2023 0:02] from sklearn import preprocessing from sklearn.datasets import load_iris from sklearn.preprocessing import RobustScaler import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler import numpy as np def uniform(X, Y): scaler = StandardScaler() scaled_data = scaler.fit_transform(X) mean = np.mean(scaled_data) variance = np.var(scaled_data) mean_rounded = round(mean, 3) variance_rounded = round(variance, 3) return mean_rounded, variance_rounded Мария Перебейнова, [21.05.2023 0:02] from sklearn import preprocessing from sklearn.datasets import load_iris from sklearn.preprocessing import RobustScaler import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler import numpy as np def uniform(X, Y): scaler = MinMaxScaler() res = scaler.fit_transform(X,Y) mo = res.mean() ds = res.std() mean = np.round(mo, 3) variance = np.round(ds, 3) return mean, variance Мария Перебейнова, [21.05.2023 0:02] from sklearn import preprocessing from sklearn.datasets import load_iris import numpy as np import pandas as pd from sklearn.preprocessing import MaxAbsScaler import numpy as np def uniform(X, Y): scaler = MaxAbsScaler() res = scaler.fit_transform(X,Y) mo = res.mean() ds = res.std() mean = np.round(mo, 3) variance = np.round(ds, 3) return mean, variance Мария Перебейнова, [21.05.2023 0:03] from sklearn.datasets import load_iris import numpy as np import pandas as pd from sklearn import preprocessing from sklearn.preprocessing import RobustScaler def uniform(X, Y): scaler = RobustScaler() res = scaler.fit_transform(X,Y) mo = res.mean() ds = res.std() mean = np.round(mo, 3) variance = np.round(ds, 3) return mean, variance Мария Перебейнова, [21.05.2023 0:03] from sklearn.cluster import KMeans def predict_model(X, y): ans = KMeans(n_clusters = 3, random_state =42, n_init = "auto").fit(X,y) return ans.cluster_centers_ Мария Перебейнова, [21.05.2023 0:03] from sklearn import datasets def load_split(): res = datasets.load_wine() return res.data, res.target Мария Перебейнова, [21.05.2023 0:04] from sklearn.cluster import DBSCAN from sklearn import preprocessing from sklearn.datasets import load_iris def load_data(): res = load_iris() return preprocessing.StandardScaler().fit_transform(res.data) Мария Перебейнова, [21.05.2023 0:04] from sklearn.cluster import DBSCAN from sklearn import preprocessing from sklearn.datasets import load_iris def predict_model(data): dbscan = DBSCAN(eps=0.7, min_samples=2) dbscan.fit(data) labels = dbscan.labels_ return set(labels) Мария Перебейнова, [21.05.2023 0:04] from sklearn import datasets def load_split(): iris = datasets.load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.5, random_state=42) return X_train[:15], y_train[:15] Мария Перебейнова, [21.05.2023 0:04] from sklearn.naive_bayes import GaussianNB from sklearn.metrics import accuracy_score def uniform(X_train, X_test, y_train, y_test): gnb = GaussianNB() gnb.fit(X_train, y_train) y_pred = gnb.predict(X_test) accuracy = accuracy_score(y_test, y_pred) return accuracy Мария Перебейнова, [21.05.2023 0:05] это был 4 раздел все задания попорядку Мария Перебейнова, [21.05.2023 0:05] сейчас ещё визуализуцию Мария Перебейнова, [21.05.2023 0:06] def solve(): fig = plt.figure(figsize=(10,9)) gs = fig.add_gridspec(3,3) ax1 = fig.add_subplot(gs[:2, :1]) ax2 = fig.add_subplot(gs[:1, 1:]) ax3 = fig.add_subplot(gs[1:2, 1:2]) ax4 = fig.add_subplot(gs[1:2, 2:3]) ax5 = fig.add_subplot(gs[2:, :]) return fig Мария Перебейнова, [21.05.2023 0:06] def solve(ax, X, Y, color, title): minX, maxX = min(X), max(100, max(X)) minY, maxY = min(Y), max(1000, max(Y)) ax.plot(X, Y, color=color) ax.set_xlim([minX, maxX]) ax.set_ylim([minY, maxY]) ax.set_title(title) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_xticks(X) ax.set_yticks(Y) ax.grid() return ax Мария Перебейнова, [21.05.2023 0:06] Мария Перебейнова, [21.05.2023 0:06] def solve(ax, x, y_list, color_list, label_list, title): ax.set_title(title) ax.set_xlabel('X') ax.set_ylabel('Y') ax.plot(x, y_list[0], color=color_list[0], marker='s', markersize=5, label=label_list[0], linewidth=10) ax.plot(x, y_list[1], color=color_list[1], marker='x',linestyle = "dashdot", label=label_list[1], alpha=0.5) ax.plot(x, y_list[2], color=color_list[2], marker='D', linestyle='dashed', label=label_list[2], markeredgewidth=2, markeredgecolor='black') ax.legend() Мария Перебейнова, [21.05.2023 0:07] Мария Перебейнова, [21.05.2023 0:07] def solve(ax, df, colors): df.plot.line(ax=ax,color=colors, subplots = True) Мария Перебейнова, [21.05.2023 0:07] def solve(ax, x, y): # Начерчиваем площадную диаграмму ax.fill_between(x, y, color='green', alpha=0.5) # Выделяем границы площадной диаграммы линией ax.plot(x, y, color='black') # Устанавливаем заголовок графика ax.set_title('Площадная диаграмма') # Устанавливаем подпись оси x ax.set_xlabel('X') # Устанавливаем подпись оси y ax.set_ylabel('Y') # Возвращаем объект типа данных AxesSubplot return ax Мария Перебейнова, [21.05.2023 0:07] def solve(ax, x, y1, y2): ax.plot(x,y1) ax.plot(x,y2) ax.fill_between(x,y1,y2, where = (x>2)&(x<5)) Мария Перебейнова, [21.05.2023 0:07] Мария Перебейнова, [21.05.2023 0:07] def solve(ax, X, interval_length, colors): min_value = min(X) max_value = max(X) intervals = np.arange(min_value, max_value + interval_length, interval_length) heights = [] for i in range(len(intervals)): if i == len(intervals) - 1: count = len([x for x in X if intervals[i] <= x <= max_value]) else: count = len([x for x in X if intervals[i] <= x < intervals[i + 1]]) heights.append(count) mid_points = [] for i in range(len(intervals)): if i == len(intervals) - 1: mid_point = 9.0 else: mid_point = (intervals[i] + intervals[i + 1]) / 2 mid_points.append(mid_point) ax.bar(mid_points, heights, width=0.5, color=colors[:len(intervals)]) ax.set_xticks(mid_points) Мария Перебейнова, [21.05.2023 0:07] def solve(ax, x_centers, heights_left, heights_right): ax.bar(x_centers-0.2, heights_left, width=0.4, color='orange') ax.bar(x_centers+0.2, heights_right, width=0.4, color='blue') ax.set_xticks(x_centers) Мария Перебейнова, [21.05.2023 0:08] def solve(ax, y_centers, heights_left, heights_right): ax.barh(y_centers, heights_left, height=0.7,color = "orange") ax.barh(y_centers, heights_right, left=heights_left ,height = 0.7, color = "blue") ax.set_yticks(y_centers) Мария Перебейнова, [21.05.2023 0:08] def solve(ax, values, labels, colors): ax.pie(values, labels=labels, colors=colors, startangle=90, wedgeprops={'linewidth': 2, 'edgecolor': 'black'}) ax.set_title('Pie Chart') for text in ax.texts: text.set_color('orange')